Skip to content

Commit cf2e9f1

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

File tree

6 files changed

+45
-1
lines changed

6 files changed

+45
-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/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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import com.gojek.mqtt.exception.handler.v3.MqttExceptionHandler
1818
import com.gojek.mqtt.exception.handler.v3.impl.MqttExceptionHandlerImpl
1919
import com.gojek.mqtt.logging.PahoLogger
2020
import com.gojek.mqtt.model.ServerUri
21+
import com.gojek.mqtt.model.Will
2122
import com.gojek.mqtt.network.NetworkHandler
2223
import com.gojek.mqtt.persistence.impl.PahoPersistence
2324
import com.gojek.mqtt.pingsender.MqttPingSender
@@ -184,6 +185,16 @@ internal class MqttConnection(
184185
connectionSpec = mqttConnectOptions.connectionSpec
185186
alpnProtocolList = mqttConnectOptions.protocols
186187
}
188+
189+
mqttConnectOptions.will?.apply{
190+
options?.setWill(
191+
topic,
192+
message.toByteArray(),
193+
qos.value,
194+
retained
195+
)
196+
}
197+
187198
// Setting some connection options which we need to reset on every connect
188199

189200
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)