package io.vertx.mqtt;
import io.vertx.codegen.annotations.DataObject;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.impl.Arguments;
import io.vertx.core.json.JsonObject;
import io.vertx.core.net.JksOptions;
import io.vertx.core.net.KeyCertOptions;
import io.vertx.core.net.NetClientOptions;
import io.vertx.core.net.PemKeyCertOptions;
import io.vertx.core.net.PemTrustOptions;
import io.vertx.core.net.PfxOptions;
import io.vertx.core.net.TrustOptions;
@DataObject(generateConverter = true)
public class MqttClientOptions extends NetClientOptions {
public static final int DEFAULT_PORT = 1883;
public static final int DEFAULT_TSL_PORT = 8883;
public static final String DEFAULT_HOST = "localhost";
public static final int DEFAULT_WILL_QOS = 0;
public static final int DEFAULT_KEEP_ALIVE_TIME_SECONDS = 30;
public static final int DEFAULT_MAX_INFLIGHT_QUEUE = 10;
public static final boolean DEFAULT_CLEAN_SESSION = true;
public static final boolean DEFAULT_WILL_FLAG = false;
public static final boolean DEFAULT_WILL_RETAIN = false;
public static final int DEFAULT_MAX_MESSAGE_SIZE = -1;
private String clientId;
private String username;
private String password;
private String willTopic;
private String willMessage;
private boolean cleanSession = DEFAULT_CLEAN_SESSION;
private boolean willFlag = DEFAULT_WILL_FLAG;
private int willQoS = DEFAULT_WILL_QOS;
private boolean willRetain = DEFAULT_WILL_RETAIN;
private int keepAliveTimeSeconds = DEFAULT_KEEP_ALIVE_TIME_SECONDS;
private boolean isAutoKeepAlive = true;
private boolean isAutoGeneratedClientId = true;
private int maxInflightQueue = DEFAULT_MAX_INFLIGHT_QUEUE;
private int maxMessageSize = DEFAULT_MAX_MESSAGE_SIZE;
public MqttClientOptions() {
super();
}
public MqttClientOptions(JsonObject json) {
super(json);
MqttClientOptionsConverter.fromJson(json, this);
}
public MqttClientOptions(MqttClientOptions other) {
super(other);
this.clientId = other.clientId;
this.username = other.username;
this.password = other.password;
this.willTopic = other.willTopic;
this.willMessage = other.willMessage;
this.cleanSession = other.cleanSession;
this.willFlag = other.willFlag;
this.willQoS = other.willQoS;
this.willRetain = other.willRetain;
this.keepAliveTimeSeconds = other.keepAliveTimeSeconds;
this.isAutoKeepAlive = other.isAutoKeepAlive;
this.isAutoGeneratedClientId = other.isAutoGeneratedClientId;
this.maxInflightQueue = other.maxInflightQueue;
this.maxMessageSize = other.maxMessageSize;
}
public boolean hasUsername() {
return username != null;
}
public boolean hasPassword() {
return password != null;
}
public boolean isCleanSession() {
return cleanSession;
}
public boolean isWillFlag() {
return willFlag;
}
public boolean isWillRetain() {
return willRetain;
}
public int getWillQoS() {
return willQoS;
}
public int getKeepAliveTimeSeconds() {
return keepAliveTimeSeconds;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public String getClientId() {
return clientId;
}
public String getWillTopic() {
return willTopic;
}
public String getWillMessage() {
return willMessage;
}
public MqttClientOptions setClientId(String clientId) {
this.clientId = clientId;
return this;
}
public MqttClientOptions setUsername(String username) {
this.username = username;
return this;
}
public MqttClientOptions setPassword(String password) {
this.password = password;
return this;
}
public MqttClientOptions setWillTopic(String willTopic) {
this.willTopic = willTopic;
return this;
}
public MqttClientOptions setWillMessage(String willMessage) {
this.willMessage = willMessage;
return this;
}
public MqttClientOptions setCleanSession(boolean cleanSession) {
this.cleanSession = cleanSession;
return this;
}
public MqttClientOptions setWillFlag(boolean willFlag) {
this.willFlag = willFlag;
return this;
}
public MqttClientOptions setWillQoS(int willQoS) {
this.willQoS = willQoS;
return this;
}
public MqttClientOptions setWillRetain(boolean willRetain) {
this.willRetain = willRetain;
return this;
}
public MqttClientOptions setKeepAliveTimeSeconds(int keepAliveTimeSeconds) {
this.keepAliveTimeSeconds = keepAliveTimeSeconds;
return this;
}
public int getMaxInflightQueue() {
return maxInflightQueue;
}
public MqttClientOptions setMaxInflightQueue(int maxInflightQueue) {
this.maxInflightQueue = maxInflightQueue;
return this;
}
public MqttClientOptions setAutoKeepAlive(boolean isAutoKeepAlive) {
this.isAutoKeepAlive = isAutoKeepAlive;
return this;
}
public MqttClientOptions setAutoGeneratedClientId(boolean isAutoGeneratedClientId) {
this.isAutoGeneratedClientId = isAutoGeneratedClientId;
return this;
}
public boolean isAutoKeepAlive() {
return this.isAutoKeepAlive;
}
public boolean isAutoGeneratedClientId() {
return isAutoGeneratedClientId;
}
public int getMaxMessageSize() {
return maxMessageSize;
}
@Override
public MqttClientOptions setReceiveBufferSize(int receiveBufferSize) {
if ((this.maxMessageSize > 0) && (receiveBufferSize > 0)) {
Arguments.require(receiveBufferSize >= this.maxMessageSize,
"Receiver buffer size can't be lower than max message size");
}
super.setReceiveBufferSize(receiveBufferSize);
return this;
}
public MqttClientOptions setMaxMessageSize(int maxMessageSize) {
Arguments.require(maxMessageSize > 0 || maxMessageSize == DEFAULT_MAX_MESSAGE_SIZE, "maxMessageSize must be > 0");
if ((maxMessageSize > 0) && (this.getReceiveBufferSize() > 0)) {
Arguments.require(this.getReceiveBufferSize() >= maxMessageSize,
"Receiver buffer size can't be lower than max message size");
}
this.maxMessageSize = maxMessageSize;
return this;
}
@Deprecated
@Override
public MqttClientOptions setIdleTimeout(int idleTimeout) {
return setKeepAliveTimeSeconds(idleTimeout);
}
@Override
public MqttClientOptions setSsl(boolean ssl) {
super.setSsl(ssl);
return this;
}
@Override
public MqttClientOptions setTrustStoreOptions(JksOptions options) {
super.setTrustStoreOptions(options);
return this;
}
@Override
public MqttClientOptions setTrustAll(boolean trustAll) {
super.setTrustAll(trustAll);
return this;
}
@Override
public MqttClientOptions setKeyCertOptions(KeyCertOptions options) {
super.setKeyCertOptions(options);
return this;
}
@Override
public MqttClientOptions setKeyStoreOptions(JksOptions options) {
super.setKeyStoreOptions(options);
return this;
}
@Override
public MqttClientOptions setPfxKeyCertOptions(PfxOptions options) {
super.setPfxKeyCertOptions(options);
return this;
}
@Override
public MqttClientOptions setPemKeyCertOptions(PemKeyCertOptions options) {
super.setPemKeyCertOptions(options);
return this;
}
@Override
public MqttClientOptions setTrustOptions(TrustOptions options) {
super.setTrustOptions(options);
return this;
}
@Override
public MqttClientOptions setPemTrustOptions(PemTrustOptions options) {
super.setPemTrustOptions(options);
return this;
}
@Override
public MqttClientOptions setPfxTrustOptions(PfxOptions options) {
super.setPfxTrustOptions(options);
return this;
}
@Override
public MqttClientOptions addEnabledCipherSuite(String suite) {
super.addEnabledCipherSuite(suite);
return this;
}
@Override
public MqttClientOptions addEnabledSecureTransportProtocol(String protocol) {
super.addEnabledSecureTransportProtocol(protocol);
return this;
}
@Override
public MqttClientOptions addCrlPath(String crlPath) throws NullPointerException {
super.addCrlPath(crlPath);
return this;
}
@Override
public MqttClientOptions addCrlValue(Buffer crlValue) throws NullPointerException {
super.addCrlValue(crlValue);
return this;
}
@Override
public String toString() {
return "Options {" +
"clientId='" + clientId + '\'' +
", username='" + username + '\'' +
", password='" + password + '\'' +
", willTopic='" + willTopic + '\'' +
", willMessage='" + willMessage + '\'' +
", cleanSession=" + cleanSession +
", willFlag=" + willFlag +
", willQoS=" + willQoS +
", willRetain=" + willRetain +
", keepAliveTimeSeconds=" + keepAliveTimeSeconds +
", isAutoKeepAlive=" + isAutoKeepAlive +
", isAutoGeneratedClientId=" + isAutoGeneratedClientId +
'}';
}
}