Skip to content

Commit

Permalink
Add support for the last will (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
sumitjat3 authored Nov 5, 2024
1 parent ef84e24 commit 5644838
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 0 deletions.
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
20 changes: 20 additions & 0 deletions mqtt-client/api/mqtt-client.api
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,7 @@ public final class com/gojek/mqtt/model/MqttConnectOptions {
public final fun getUserPropertiesMap ()Ljava/util/Map;
public final fun getUsername ()Ljava/lang/String;
public final fun getVersion ()Lcom/gojek/mqtt/model/MqttVersion;
public final fun getWill ()Lcom/gojek/mqtt/model/Will;
public final fun getX509TrustManager ()Ljavax/net/ssl/X509TrustManager;
public final fun isCleanSession ()Z
public final fun newBuilder ()Lcom/gojek/mqtt/model/MqttConnectOptions$Builder;
Expand All @@ -1064,6 +1065,7 @@ public final class com/gojek/mqtt/model/MqttConnectOptions$Builder {
public final fun alpnProtocols (Ljava/util/List;)Lcom/gojek/mqtt/model/MqttConnectOptions$Builder;
public final fun build ()Lcom/gojek/mqtt/model/MqttConnectOptions;
public final fun cleanSession (Z)Lcom/gojek/mqtt/model/MqttConnectOptions$Builder;
public final fun clearWill ()Lcom/gojek/mqtt/model/MqttConnectOptions$Builder;
public final fun clientId (Ljava/lang/String;)Lcom/gojek/mqtt/model/MqttConnectOptions$Builder;
public final fun connectionSpec (Lorg/eclipse/paho/client/mqttv3/ConnectionSpec;)Lcom/gojek/mqtt/model/MqttConnectOptions$Builder;
public final fun keepAlive (Lcom/gojek/mqtt/model/KeepAlive;)Lcom/gojek/mqtt/model/MqttConnectOptions$Builder;
Expand All @@ -1075,6 +1077,7 @@ public final class com/gojek/mqtt/model/MqttConnectOptions$Builder {
public final fun sslSocketFactory (Ljavax/net/ssl/SSLSocketFactory;Ljavax/net/ssl/X509TrustManager;)Lcom/gojek/mqtt/model/MqttConnectOptions$Builder;
public final fun userName (Ljava/lang/String;)Lcom/gojek/mqtt/model/MqttConnectOptions$Builder;
public final fun userProperties (Ljava/util/Map;)Lcom/gojek/mqtt/model/MqttConnectOptions$Builder;
public final fun will (Lcom/gojek/mqtt/model/Will;)Lcom/gojek/mqtt/model/MqttConnectOptions$Builder;
}

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

public final class com/gojek/mqtt/model/Will {
public fun <init> (Ljava/lang/String;Ljava/lang/String;Lcom/gojek/courier/QoS;Z)V
public final fun component1 ()Ljava/lang/String;
public final fun component2 ()Ljava/lang/String;
public final fun component3 ()Lcom/gojek/courier/QoS;
public final fun component4 ()Z
public final fun copy (Ljava/lang/String;Ljava/lang/String;Lcom/gojek/courier/QoS;Z)Lcom/gojek/mqtt/model/Will;
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;
public fun equals (Ljava/lang/Object;)Z
public final fun getMessage ()Ljava/lang/String;
public final fun getQos ()Lcom/gojek/courier/QoS;
public final fun getRetained ()Z
public final fun getTopic ()Ljava/lang/String;
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}

public final class com/gojek/mqtt/network/ActiveNetInfo {
public fun <init> (ZZS)V
public final fun component1 ()Z
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 @@ -184,6 +184,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 5644838

Please sign in to comment.