Skip to content

Commit

Permalink
Add support for the last will
Browse files Browse the repository at this point in the history
  • Loading branch information
sumitjat3 committed Oct 18, 2024
1 parent bdcbff5 commit cf2e9f1
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 1 deletion.
10 changes: 10 additions & 0 deletions app/src/main/java/com/gojek/courier/app/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/deps.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ internal class AndroidMqttClient(
.keepAlive(keepAliveProvider.getKeepAlive(connectOptions))
.clientId(connectOptions.clientId + ":adaptive")
.cleanSession(true)
.clearWill()
.build()
} else {
connectOptions.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class MqttConnectOptions private constructor(

val protocols: List<Protocol> = builder.protocols

val will: Will? = builder.will

init {
if (connectionSpec.isTls.not()) {
this.sslSocketFactory = null
Expand Down Expand Up @@ -77,6 +79,7 @@ class MqttConnectOptions private constructor(
internal var x509TrustManagerOrNull: X509TrustManager? = null
internal var connectionSpec: ConnectionSpec = DEFAULT_CONNECTION_SPECS
internal var protocols: List<Protocol> = emptyList()
internal var will: Will? = null

internal constructor(mqttConnectOptions: MqttConnectOptions) : this() {
this.serverUris = mqttConnectOptions.serverUris
Expand All @@ -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<ServerUri>) = apply {
Expand Down Expand Up @@ -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)
}

Expand Down
10 changes: 10 additions & 0 deletions mqtt-client/src/main/java/com/gojek/mqtt/model/Will.kt
Original file line number Diff line number Diff line change
@@ -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
)

0 comments on commit cf2e9f1

Please sign in to comment.