Skip to content

Commit

Permalink
Add event when operations are discarded
Browse files Browse the repository at this point in the history
  • Loading branch information
deepanshu42 committed Dec 6, 2023
1 parent cc56647 commit dba0bfd
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ internal object ConnectionInfoStore {
fun updateConnectionInfo(connectionInfo: ConnectionInfo) {
state = State.InitialisedState(connectionInfo)
}

fun removeConnectionInfo() {
state = State.UninitialisedState
}
}

private sealed class State(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.gojek.mqtt.client.model.ConnectionState
import com.gojek.mqtt.client.v3.IAndroidMqttClient
import com.gojek.mqtt.event.AdaptivePingEventHandler
import com.gojek.mqtt.event.EventHandler
import com.gojek.mqtt.event.MqttEvent.OperationDiscardedEvent
import com.gojek.mqtt.event.MqttEvent.OptimalKeepAliveFoundEvent
import com.gojek.mqtt.event.PingEventHandler
import com.gojek.mqtt.model.AdaptiveKeepAliveConfig
Expand Down Expand Up @@ -90,6 +91,7 @@ internal class MqttClientInternal(
fun disconnect() {
if (initialisationState == UNINITIALISED) {
mqttConfiguration.logger.d("MqttClient", "MqttClient is not initialised")
eventHandler.onEvent(OperationDiscardedEvent("Disconnect", "State uninitialised"))
return
}
androidMqttClient?.disconnect()
Expand All @@ -100,6 +102,7 @@ internal class MqttClientInternal(
fun destroy() {
if (initialisationState == UNINITIALISED) {
mqttConfiguration.logger.d("MqttClient", "MqttClient is not initialised")
eventHandler.onEvent(OperationDiscardedEvent("Destroy", "State uninitialised"))
return
}
androidMqttClient?.destroy()
Expand All @@ -111,6 +114,7 @@ internal class MqttClientInternal(
fun reconnect() {
if (initialisationState == UNINITIALISED) {
mqttConfiguration.logger.d("MqttClient", "MqttClient is not initialised")
eventHandler.onEvent(OperationDiscardedEvent("Reconnect", "State uninitialised"))
return
}
androidMqttClient?.reconnect()
Expand All @@ -121,6 +125,7 @@ internal class MqttClientInternal(
fun subscribe(vararg topics: Pair<String, QoS>) {
if (initialisationState == UNINITIALISED) {
mqttConfiguration.logger.d("MqttClient", "MqttClient is not initialised")
eventHandler.onEvent(OperationDiscardedEvent("Subscribe", "State uninitialised"))
return
}
androidMqttClient?.subscribe(mapOf(*topics))
Expand All @@ -130,6 +135,7 @@ internal class MqttClientInternal(
fun unsubscribe(vararg topics: String) {
if (initialisationState == UNINITIALISED) {
mqttConfiguration.logger.d("MqttClient", "MqttClient is not initialised")
eventHandler.onEvent(OperationDiscardedEvent("Unsubscribe", "State uninitialised"))
return
}
androidMqttClient?.unsubscribe(listOf(*topics))
Expand All @@ -139,6 +145,7 @@ internal class MqttClientInternal(
fun send(mqttPacket: MqttPacket, sendMessageCallback: SendMessageCallback): Boolean {
if (initialisationState == UNINITIALISED) {
mqttConfiguration.logger.d("MqttClient", "MqttClient is not initialised")
eventHandler.onEvent(OperationDiscardedEvent("SendMessage", "State uninitialised"))
return false
}
return androidMqttClient?.send(mqttPacket, sendMessageCallback) ?: false
Expand All @@ -148,6 +155,7 @@ internal class MqttClientInternal(
fun addMessageListener(topic: String, listener: MessageListener) {
if (initialisationState == UNINITIALISED) {
mqttConfiguration.logger.d("MqttClient", "MqttClient is not initialised")
eventHandler.onEvent(OperationDiscardedEvent("AddMessageListener", "State uninitialised"))
return
}
androidMqttClient?.addMessageListener(topic, listener)
Expand All @@ -157,6 +165,7 @@ internal class MqttClientInternal(
fun removeMessageListener(topic: String, listener: MessageListener) {
if (initialisationState == UNINITIALISED) {
mqttConfiguration.logger.d("MqttClient", "MqttClient is not initialised")
eventHandler.onEvent(OperationDiscardedEvent("RemoveMessageListener", "State uninitialised"))
return
}
androidMqttClient?.removeMessageListener(topic, listener)
Expand All @@ -166,6 +175,7 @@ internal class MqttClientInternal(
fun addGlobalMessageListener(listener: MessageListener) {
if (initialisationState == UNINITIALISED) {
mqttConfiguration.logger.d("MqttClient", "MqttClient is not initialised")
eventHandler.onEvent(OperationDiscardedEvent("AddGlobalMessageListener", "State uninitialised"))
return
}
androidMqttClient?.addGlobalMessageListener(listener)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ internal class AndroidMqttClient(
runnableScheduler.stopThread()
networkHandler.destroy()
eventHandler.onEvent(MqttClientDestroyedEvent())
ConnectionInfoStore.removeConnectionInfo()
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions mqtt-client/src/main/java/com/gojek/mqtt/event/MqttEvent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,10 @@ sealed class MqttEvent(open var connectionInfo: ConnectionInfo?) {
val timeTakenMillis: Long,
override var connectionInfo: ConnectionInfo? = null
) : MqttEvent(connectionInfo)

data class OperationDiscardedEvent(
val name: String,
val reason: String,
override var connectionInfo: ConnectionInfo? = null
) : MqttEvent(connectionInfo)
}

0 comments on commit dba0bfd

Please sign in to comment.