From cf2e9f1121bbd2e3998c778824fe9bbc372bf0bd Mon Sep 17 00:00:00 2001 From: sumitjat Date: Fri, 18 Oct 2024 10:10:50 +0530 Subject: [PATCH] Add support for the last will --- .../java/com/gojek/courier/app/ui/MainActivity.kt | 10 ++++++++++ buildSrc/src/main/kotlin/deps.kt | 2 +- .../gojek/mqtt/client/v3/impl/AndroidMqttClient.kt | 1 + .../java/com/gojek/mqtt/connection/MqttConnection.kt | 11 +++++++++++ .../java/com/gojek/mqtt/model/MqttConnectOptions.kt | 12 ++++++++++++ .../src/main/java/com/gojek/mqtt/model/Will.kt | 10 ++++++++++ 6 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 mqtt-client/src/main/java/com/gojek/mqtt/model/Will.kt diff --git a/app/src/main/java/com/gojek/courier/app/ui/MainActivity.kt b/app/src/main/java/com/gojek/courier/app/ui/MainActivity.kt index 2c4e524c..09ee7a9b 100644 --- a/app/src/main/java/com/gojek/courier/app/ui/MainActivity.kt +++ b/app/src/main/java/com/gojek/courier/app/ui/MainActivity.kt @@ -29,6 +29,7 @@ import com.gojek.mqtt.model.AdaptiveKeepAliveConfig import com.gojek.mqtt.model.KeepAlive import com.gojek.mqtt.model.MqttConnectOptions import com.gojek.mqtt.model.ServerUri +import com.gojek.mqtt.model.Will import com.gojek.workmanager.pingsender.WorkManagerPingSenderConfig import com.gojek.workmanager.pingsender.WorkPingSenderFactory import kotlinx.android.synthetic.main.activity_main.brokerIP @@ -122,12 +123,21 @@ class MainActivity : AppCompatActivity() { } private fun connectMqtt(clientId: String, username: String, password: String, ip: String, port: Int) { + + val will = Will( + topic = "last/will/topic", + message = "Client disconnected unexpectedly", + qos = QoS.ZERO, + retained = false + ) + val connectOptions = MqttConnectOptions.Builder() .serverUris(listOf(ServerUri(ip, port, if (port == 443) "ssl" else "tcp"))) .clientId(clientId) .userName(username) .password(password) .cleanSession(false) + .will(will) .keepAlive(KeepAlive(timeSeconds = 30)) .build() diff --git a/buildSrc/src/main/kotlin/deps.kt b/buildSrc/src/main/kotlin/deps.kt index 6f2a86bd..9df35a12 100644 --- a/buildSrc/src/main/kotlin/deps.kt +++ b/buildSrc/src/main/kotlin/deps.kt @@ -7,7 +7,7 @@ object versions { const val kotlin = "1.6.21" const val agp = "7.4.2" const val jetifierProcessor = "1.0.0-beta10" - const val jfrogBuildInfoExtractor = "4.11.0" + const val jfrogBuildInfoExtractor = "4.23.4" const val navigation = "2.1.0-rc01" const val coroutines = "1.3.2" const val broadcast = "1.0.0" diff --git a/mqtt-client/src/main/java/com/gojek/mqtt/client/v3/impl/AndroidMqttClient.kt b/mqtt-client/src/main/java/com/gojek/mqtt/client/v3/impl/AndroidMqttClient.kt index 0807e68a..d22ec73e 100644 --- a/mqtt-client/src/main/java/com/gojek/mqtt/client/v3/impl/AndroidMqttClient.kt +++ b/mqtt-client/src/main/java/com/gojek/mqtt/client/v3/impl/AndroidMqttClient.kt @@ -522,6 +522,7 @@ internal class AndroidMqttClient( .keepAlive(keepAliveProvider.getKeepAlive(connectOptions)) .clientId(connectOptions.clientId + ":adaptive") .cleanSession(true) + .clearWill() .build() } else { connectOptions.newBuilder() diff --git a/mqtt-client/src/main/java/com/gojek/mqtt/connection/MqttConnection.kt b/mqtt-client/src/main/java/com/gojek/mqtt/connection/MqttConnection.kt index f436c559..cffde68a 100644 --- a/mqtt-client/src/main/java/com/gojek/mqtt/connection/MqttConnection.kt +++ b/mqtt-client/src/main/java/com/gojek/mqtt/connection/MqttConnection.kt @@ -18,6 +18,7 @@ import com.gojek.mqtt.exception.handler.v3.MqttExceptionHandler import com.gojek.mqtt.exception.handler.v3.impl.MqttExceptionHandlerImpl import com.gojek.mqtt.logging.PahoLogger import com.gojek.mqtt.model.ServerUri +import com.gojek.mqtt.model.Will import com.gojek.mqtt.network.NetworkHandler import com.gojek.mqtt.persistence.impl.PahoPersistence import com.gojek.mqtt.pingsender.MqttPingSender @@ -184,6 +185,16 @@ internal class MqttConnection( connectionSpec = mqttConnectOptions.connectionSpec alpnProtocolList = mqttConnectOptions.protocols } + + mqttConnectOptions.will?.apply{ + options?.setWill( + topic, + message.toByteArray(), + qos.value, + retained + ) + } + // Setting some connection options which we need to reset on every connect logger.d(TAG, "MQTT connecting on : " + mqtt!!.serverURI) diff --git a/mqtt-client/src/main/java/com/gojek/mqtt/model/MqttConnectOptions.kt b/mqtt-client/src/main/java/com/gojek/mqtt/model/MqttConnectOptions.kt index d90d7c12..c22c8cea 100644 --- a/mqtt-client/src/main/java/com/gojek/mqtt/model/MqttConnectOptions.kt +++ b/mqtt-client/src/main/java/com/gojek/mqtt/model/MqttConnectOptions.kt @@ -41,6 +41,8 @@ class MqttConnectOptions private constructor( val protocols: List = builder.protocols + val will: Will? = builder.will + init { if (connectionSpec.isTls.not()) { this.sslSocketFactory = null @@ -77,6 +79,7 @@ class MqttConnectOptions private constructor( internal var x509TrustManagerOrNull: X509TrustManager? = null internal var connectionSpec: ConnectionSpec = DEFAULT_CONNECTION_SPECS internal var protocols: List = emptyList() + internal var will: Will? = null internal constructor(mqttConnectOptions: MqttConnectOptions) : this() { this.serverUris = mqttConnectOptions.serverUris @@ -93,6 +96,7 @@ class MqttConnectOptions private constructor( this.x509TrustManagerOrNull = mqttConnectOptions.x509TrustManager this.connectionSpec = mqttConnectOptions.connectionSpec this.protocols = mqttConnectOptions.protocols + this.will = mqttConnectOptions.will } fun serverUris(serverUris: List) = apply { @@ -204,6 +208,14 @@ class MqttConnectOptions private constructor( this.protocols = protocols } + fun will(will: Will) = apply { + this.will = will + } + + fun clearWill() = apply { + this.will = null + } + fun build(): MqttConnectOptions = MqttConnectOptions(this) } diff --git a/mqtt-client/src/main/java/com/gojek/mqtt/model/Will.kt b/mqtt-client/src/main/java/com/gojek/mqtt/model/Will.kt new file mode 100644 index 00000000..64941247 --- /dev/null +++ b/mqtt-client/src/main/java/com/gojek/mqtt/model/Will.kt @@ -0,0 +1,10 @@ +package com.gojek.mqtt.model + +import com.gojek.courier.QoS + +data class Will( + val topic: String, + val message: String, + val qos: QoS, + val retained: Boolean +)