Skip to content

Commit 09fd7c9

Browse files
committed
Add support for the last will
1 parent bdcbff5 commit 09fd7c9

File tree

7 files changed

+64
-1
lines changed

7 files changed

+64
-1
lines changed

app/src/main/java/com/gojek/courier/app/ui/MainActivity.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import com.gojek.mqtt.model.AdaptiveKeepAliveConfig
2929
import com.gojek.mqtt.model.KeepAlive
3030
import com.gojek.mqtt.model.MqttConnectOptions
3131
import com.gojek.mqtt.model.ServerUri
32+
import com.gojek.mqtt.model.Will
3233
import com.gojek.workmanager.pingsender.WorkManagerPingSenderConfig
3334
import com.gojek.workmanager.pingsender.WorkPingSenderFactory
3435
import kotlinx.android.synthetic.main.activity_main.brokerIP
@@ -122,12 +123,21 @@ class MainActivity : AppCompatActivity() {
122123
}
123124

124125
private fun connectMqtt(clientId: String, username: String, password: String, ip: String, port: Int) {
126+
127+
val will = Will(
128+
topic = "last/will/topic",
129+
message = "Client disconnected unexpectedly",
130+
qos = QoS.ZERO,
131+
retained = false
132+
)
133+
125134
val connectOptions = MqttConnectOptions.Builder()
126135
.serverUris(listOf(ServerUri(ip, port, if (port == 443) "ssl" else "tcp")))
127136
.clientId(clientId)
128137
.userName(username)
129138
.password(password)
130139
.cleanSession(false)
140+
.will(will)
131141
.keepAlive(KeepAlive(timeSeconds = 30))
132142
.build()
133143

buildSrc/src/main/kotlin/deps.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ object versions {
77
const val kotlin = "1.6.21"
88
const val agp = "7.4.2"
99
const val jetifierProcessor = "1.0.0-beta10"
10-
const val jfrogBuildInfoExtractor = "4.11.0"
10+
const val jfrogBuildInfoExtractor = "4.23.4"
1111
const val navigation = "2.1.0-rc01"
1212
const val coroutines = "1.3.2"
1313
const val broadcast = "1.0.0"

mqtt-client/api/mqtt-client.api

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,7 @@ public final class com/gojek/mqtt/model/MqttConnectOptions {
10541054
public final fun getUserPropertiesMap ()Ljava/util/Map;
10551055
public final fun getUsername ()Ljava/lang/String;
10561056
public final fun getVersion ()Lcom/gojek/mqtt/model/MqttVersion;
1057+
public final fun getWill ()Lcom/gojek/mqtt/model/Will;
10571058
public final fun getX509TrustManager ()Ljavax/net/ssl/X509TrustManager;
10581059
public final fun isCleanSession ()Z
10591060
public final fun newBuilder ()Lcom/gojek/mqtt/model/MqttConnectOptions$Builder;
@@ -1064,6 +1065,7 @@ public final class com/gojek/mqtt/model/MqttConnectOptions$Builder {
10641065
public final fun alpnProtocols (Ljava/util/List;)Lcom/gojek/mqtt/model/MqttConnectOptions$Builder;
10651066
public final fun build ()Lcom/gojek/mqtt/model/MqttConnectOptions;
10661067
public final fun cleanSession (Z)Lcom/gojek/mqtt/model/MqttConnectOptions$Builder;
1068+
public final fun clearWill ()Lcom/gojek/mqtt/model/MqttConnectOptions$Builder;
10671069
public final fun clientId (Ljava/lang/String;)Lcom/gojek/mqtt/model/MqttConnectOptions$Builder;
10681070
public final fun connectionSpec (Lorg/eclipse/paho/client/mqttv3/ConnectionSpec;)Lcom/gojek/mqtt/model/MqttConnectOptions$Builder;
10691071
public final fun keepAlive (Lcom/gojek/mqtt/model/KeepAlive;)Lcom/gojek/mqtt/model/MqttConnectOptions$Builder;
@@ -1075,6 +1077,7 @@ public final class com/gojek/mqtt/model/MqttConnectOptions$Builder {
10751077
public final fun sslSocketFactory (Ljavax/net/ssl/SSLSocketFactory;Ljavax/net/ssl/X509TrustManager;)Lcom/gojek/mqtt/model/MqttConnectOptions$Builder;
10761078
public final fun userName (Ljava/lang/String;)Lcom/gojek/mqtt/model/MqttConnectOptions$Builder;
10771079
public final fun userProperties (Ljava/util/Map;)Lcom/gojek/mqtt/model/MqttConnectOptions$Builder;
1080+
public final fun will (Lcom/gojek/mqtt/model/Will;)Lcom/gojek/mqtt/model/MqttConnectOptions$Builder;
10781081
}
10791082

10801083
public final class com/gojek/mqtt/model/MqttConnectOptions$Companion {
@@ -1103,6 +1106,23 @@ public final class com/gojek/mqtt/model/ServerUri {
11031106
public fun toString ()Ljava/lang/String;
11041107
}
11051108

1109+
public final class com/gojek/mqtt/model/Will {
1110+
public fun <init> (Ljava/lang/String;Ljava/lang/String;Lcom/gojek/courier/QoS;Z)V
1111+
public final fun component1 ()Ljava/lang/String;
1112+
public final fun component2 ()Ljava/lang/String;
1113+
public final fun component3 ()Lcom/gojek/courier/QoS;
1114+
public final fun component4 ()Z
1115+
public final fun copy (Ljava/lang/String;Ljava/lang/String;Lcom/gojek/courier/QoS;Z)Lcom/gojek/mqtt/model/Will;
1116+
public static synthetic fun copy$default (Lcom/gojek/mqtt/model/Will;Ljava/lang/String;Ljava/lang/String;Lcom/gojek/courier/QoS;ZILjava/lang/Object;)Lcom/gojek/mqtt/model/Will;
1117+
public fun equals (Ljava/lang/Object;)Z
1118+
public final fun getMessage ()Ljava/lang/String;
1119+
public final fun getQos ()Lcom/gojek/courier/QoS;
1120+
public final fun getRetained ()Z
1121+
public final fun getTopic ()Ljava/lang/String;
1122+
public fun hashCode ()I
1123+
public fun toString ()Ljava/lang/String;
1124+
}
1125+
11061126
public final class com/gojek/mqtt/network/ActiveNetInfo {
11071127
public fun <init> (ZZS)V
11081128
public final fun component1 ()Z

mqtt-client/src/main/java/com/gojek/mqtt/client/v3/impl/AndroidMqttClient.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ internal class AndroidMqttClient(
522522
.keepAlive(keepAliveProvider.getKeepAlive(connectOptions))
523523
.clientId(connectOptions.clientId + ":adaptive")
524524
.cleanSession(true)
525+
.clearWill()
525526
.build()
526527
} else {
527528
connectOptions.newBuilder()

mqtt-client/src/main/java/com/gojek/mqtt/connection/MqttConnection.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,16 @@ internal class MqttConnection(
184184
connectionSpec = mqttConnectOptions.connectionSpec
185185
alpnProtocolList = mqttConnectOptions.protocols
186186
}
187+
188+
mqttConnectOptions.will?.apply {
189+
options!!.setWill(
190+
topic,
191+
message.toByteArray(),
192+
qos.value,
193+
retained
194+
)
195+
}
196+
187197
// Setting some connection options which we need to reset on every connect
188198

189199
logger.d(TAG, "MQTT connecting on : " + mqtt!!.serverURI)

mqtt-client/src/main/java/com/gojek/mqtt/model/MqttConnectOptions.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class MqttConnectOptions private constructor(
4141

4242
val protocols: List<Protocol> = builder.protocols
4343

44+
val will: Will? = builder.will
45+
4446
init {
4547
if (connectionSpec.isTls.not()) {
4648
this.sslSocketFactory = null
@@ -77,6 +79,7 @@ class MqttConnectOptions private constructor(
7779
internal var x509TrustManagerOrNull: X509TrustManager? = null
7880
internal var connectionSpec: ConnectionSpec = DEFAULT_CONNECTION_SPECS
7981
internal var protocols: List<Protocol> = emptyList()
82+
internal var will: Will? = null
8083

8184
internal constructor(mqttConnectOptions: MqttConnectOptions) : this() {
8285
this.serverUris = mqttConnectOptions.serverUris
@@ -93,6 +96,7 @@ class MqttConnectOptions private constructor(
9396
this.x509TrustManagerOrNull = mqttConnectOptions.x509TrustManager
9497
this.connectionSpec = mqttConnectOptions.connectionSpec
9598
this.protocols = mqttConnectOptions.protocols
99+
this.will = mqttConnectOptions.will
96100
}
97101

98102
fun serverUris(serverUris: List<ServerUri>) = apply {
@@ -204,6 +208,14 @@ class MqttConnectOptions private constructor(
204208
this.protocols = protocols
205209
}
206210

211+
fun will(will: Will) = apply {
212+
this.will = will
213+
}
214+
215+
fun clearWill() = apply {
216+
this.will = null
217+
}
218+
207219
fun build(): MqttConnectOptions = MqttConnectOptions(this)
208220
}
209221

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.gojek.mqtt.model
2+
3+
import com.gojek.courier.QoS
4+
5+
data class Will(
6+
val topic: String,
7+
val message: String,
8+
val qos: QoS,
9+
val retained: Boolean
10+
)

0 commit comments

Comments
 (0)