Skip to content

Commit

Permalink
Tokenlist with mutableList
Browse files Browse the repository at this point in the history
  • Loading branch information
hannesa2 committed Aug 28, 2024
1 parent 4da301d commit ca3f1b4
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import android.content.*
import android.os.Build
import android.os.Bundle
import android.os.IBinder
import android.util.SparseArray
import info.mqtt.android.service.extension.parcelable
import info.mqtt.android.service.extension.serializable
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -51,7 +50,7 @@ class MqttAndroidClient @JvmOverloads constructor(
private val serviceConnection = MyServiceConnection()

// We hold the various tokens in a collection and pass identifiers for them to the service
private val tokenMap = SparseArray<IMqttToken>()
private val tokenList = mutableListOf<IMqttToken>()

//The acknowledgment that a message has been processed by the application
private val messageAck = ackType
Expand All @@ -61,7 +60,6 @@ class MqttAndroidClient @JvmOverloads constructor(

// An identifier for the underlying client connection, which we can pass to the service
private var clientHandle: String? = null
private var tokenNumber = 0
private var clientConnectOptions: MqttConnectOptions? = null
private var connectToken: IMqttToken? = null

Expand Down Expand Up @@ -258,9 +256,9 @@ class MqttAndroidClient @JvmOverloads constructor(
}
mqttService!!.isTraceEnabled = traceEnabled
mqttService!!.setTraceCallbackId(clientHandle)
val activityToken = storeToken(connectToken)
storeToken(connectToken)
try {
mqttService!!.connect(clientHandle!!, clientConnectOptions, activityToken)
mqttService!!.connect(clientHandle!!, clientConnectOptions, connectToken)
} catch (e: Exception) {
val listener = connectToken!!.actionCallback
listener?.onFailure(connectToken, e)
Expand All @@ -279,8 +277,8 @@ class MqttAndroidClient @JvmOverloads constructor(
*/
override fun disconnect(): IMqttToken {
val token: IMqttToken = MqttTokenAndroid(this, null, null)
val activityToken = storeToken(token)
mqttService!!.disconnect(clientHandle!!, null, activityToken)
storeToken(token)
mqttService!!.disconnect(clientHandle!!, null, token)
return token
}

Expand All @@ -299,8 +297,8 @@ class MqttAndroidClient @JvmOverloads constructor(
*/
override fun disconnect(quiesceTimeout: Long): IMqttToken {
val token: IMqttToken = MqttTokenAndroid(this, null, null)
val activityToken = storeToken(token)
mqttService!!.disconnect(clientHandle!!, quiesceTimeout, null, activityToken)
storeToken(token)
mqttService!!.disconnect(clientHandle!!, quiesceTimeout, null, token)
return token
}

Expand All @@ -317,8 +315,8 @@ class MqttAndroidClient @JvmOverloads constructor(
*/
override fun disconnect(userContext: Any?, callback: IMqttActionListener?): IMqttToken {
val token: IMqttToken = MqttTokenAndroid(this, userContext, callback)
val activityToken = storeToken(token)
mqttService!!.disconnect(clientHandle!!, null, activityToken)
storeToken(token)
mqttService!!.disconnect(clientHandle!!, null, token)
return token
}

Expand Down Expand Up @@ -351,8 +349,8 @@ class MqttAndroidClient @JvmOverloads constructor(
*/
override fun disconnect(quiesceTimeout: Long, userContext: Any?, callback: IMqttActionListener): IMqttToken {
val token: IMqttToken = MqttTokenAndroid(this, userContext, callback)
val activityToken = storeToken(token)
mqttService!!.disconnect(clientHandle!!, quiesceTimeout, null, activityToken)
storeToken(token)
mqttService!!.disconnect(clientHandle!!, quiesceTimeout, null, token)
return token
}

Expand Down Expand Up @@ -412,8 +410,8 @@ class MqttAndroidClient @JvmOverloads constructor(
message.qos = qos
message.isRetained = retained
val token = MqttDeliveryTokenAndroid(this, userContext, callback, message)
val activityToken = storeToken(token)
val internalToken = mqttService!!.publish(clientHandle!!, topic, payload, QoS.valueOf(qos), retained, null, activityToken)
storeToken(token)
val internalToken = mqttService!!.publish(clientHandle!!, topic, payload, QoS.valueOf(qos), retained, null, token)
token.setDelegate(internalToken)
return token
}
Expand Down Expand Up @@ -474,8 +472,8 @@ class MqttAndroidClient @JvmOverloads constructor(
*/
override fun publish(topic: String, message: MqttMessage, userContext: Any?, callback: IMqttActionListener?): IMqttDeliveryToken {
val token = MqttDeliveryTokenAndroid(this, userContext, callback, message)
val activityToken = storeToken(token)
val internalToken = mqttService!!.publish(clientHandle!!, topic, message, null, activityToken)
storeToken(token)
val internalToken = mqttService!!.publish(clientHandle!!, topic, message, null, token)
token.setDelegate(internalToken)
return token
}
Expand Down Expand Up @@ -526,8 +524,8 @@ class MqttAndroidClient @JvmOverloads constructor(
*/
override fun subscribe(topic: String, qos: Int, userContext: Any?, callback: IMqttActionListener?): IMqttToken {
val token: IMqttToken = MqttTokenAndroid(this, userContext, callback, arrayOf(topic))
val activityToken = storeToken(token)
mqttService!!.subscribe(clientHandle!!, topic, QoS.valueOf(qos), null, activityToken)
storeToken(token)
mqttService!!.subscribe(clientHandle!!, topic, QoS.valueOf(qos), null, token)
return token
}

Expand Down Expand Up @@ -630,8 +628,8 @@ class MqttAndroidClient @JvmOverloads constructor(
*/
override fun subscribe(topic: Array<String>, qos: IntArray, userContext: Any?, callback: IMqttActionListener?): IMqttToken {
val token: IMqttToken = MqttTokenAndroid(this, userContext, callback, topic)
val activityToken = storeToken(token)
mqttService!!.subscribe(clientHandle!!, topic, qos, null, activityToken)
storeToken(token)
mqttService!!.subscribe(clientHandle!!, topic, qos, null, token)
return token
}

Expand Down Expand Up @@ -709,8 +707,8 @@ class MqttAndroidClient @JvmOverloads constructor(
Array<IMqttMessageListener>
): IMqttToken {
val token: IMqttToken = MqttTokenAndroid(this, userContext, callback, topicFilters)
val activityToken = storeToken(token)
mqttService!!.subscribe(clientHandle!!, topicFilters, qos.map { QoS.valueOf(it) }.toTypedArray(), null, activityToken, messageListeners)
storeToken(token)
mqttService!!.subscribe(clientHandle!!, topicFilters, qos.map { QoS.valueOf(it) }.toTypedArray(), null, token, messageListeners)
return token
}

Expand Down Expand Up @@ -747,8 +745,8 @@ class MqttAndroidClient @JvmOverloads constructor(
*/
override fun unsubscribe(topic: String, userContext: Any?, callback: IMqttActionListener?): IMqttToken {
val token: IMqttToken = MqttTokenAndroid(this, userContext, callback)
val activityToken = storeToken(token)
mqttService!!.unsubscribe(clientHandle!!, topic, null, activityToken)
storeToken(token)
mqttService!!.unsubscribe(clientHandle!!, topic, null, token)
return token
}

Expand Down Expand Up @@ -778,8 +776,8 @@ class MqttAndroidClient @JvmOverloads constructor(
*/
override fun unsubscribe(topic: Array<String>, userContext: Any?, callback: IMqttActionListener?): IMqttToken {
val token: IMqttToken = MqttTokenAndroid(this, userContext, callback)
val activityToken = storeToken(token)
mqttService!!.unsubscribe(clientHandle!!, topic, null, activityToken)
storeToken(token)
mqttService!!.unsubscribe(clientHandle!!, topic, null, token)
return token
}

Expand Down Expand Up @@ -1106,16 +1104,15 @@ class MqttAndroidClient @JvmOverloads constructor(

/**
* @param token identifying an operation
* @return an identifier for the token which can be passed to the Android
* Service
*/
@Synchronized
private fun storeToken(token: IMqttToken?): String {
private fun storeToken(token: IMqttToken?) {
token?.let {
tokenMap.put(tokenNumber, token)
return (tokenNumber++).toString()
} ?: run {
return tokenNumber.toString()
val tokenFound = tokenList.find { it == token }
if (tokenFound == null) {
tokenList.add(token)
Timber.d("$token size=${tokenList.size}")
}
}
}

Expand All @@ -1127,10 +1124,10 @@ class MqttAndroidClient @JvmOverloads constructor(
@Synchronized
private fun removeMqttToken(data: Bundle): IMqttToken? {
val activityToken = data.getString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN)
if (activityToken != null) {
val tokenNumber = activityToken.toInt()
val token = tokenMap[tokenNumber]
tokenMap.delete(tokenNumber)
if (activityToken != null && activityToken != "null") {
val token = tokenList.find { it.toString() == activityToken }
tokenList.remove(token)
Timber.d("search=$activityToken ${tokenList.size}")
return token
}
return null
Expand All @@ -1144,7 +1141,8 @@ class MqttAndroidClient @JvmOverloads constructor(
@Synchronized
private fun getMqttToken(data: Bundle): IMqttToken? {
val activityToken = data.getString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN)
return tokenMap[activityToken!!.toInt()]
val token = tokenList.find { it.toString() == activityToken }
return token
}

/**
Expand Down
Loading

0 comments on commit ca3f1b4

Please sign in to comment.