Skip to content

Commit

Permalink
Refactor: Changed queue manager to use message.obj to store message d…
Browse files Browse the repository at this point in the history
…ata instead of storing in a separate cache table
  • Loading branch information
shannon-rodricks committed Mar 5, 2023
1 parent 1cd4ffb commit e59d0d0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 48 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
buildscript {
ext {
kotlin_version = '1.8.0'
versionName = '0.0.24'
versionName = '0.0.27'
}

repositories {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ class ConnectionManagerImpl(
) : ConnectionManager, ServiceConnection, MADAssistantClientAIDL.Stub() {

companion object {
const val TAG = "MADAssist:ConnectionManagerImpl"

const val REPO_SERVICE_PACKAGE = "com.evdayapps.madassistant.repository"
const val REPO_SERVICE_CLASS = "$REPO_SERVICE_PACKAGE.service.MADAssistantService"
const val DEFAULT_REPO_SIGNATURE = "1B:C0:79:26:82:9E:FB:96:5C:6A:51:6C:96:7C:52:88:42:" +
"7E:73:8C:05:7D:60:D8:13:9D:C4:3C:18:3B:E3:63"
}

private val TAG = "MADAssist:ConnectionManagerImpl"

private var currentState: ConnectionManager.State = ConnectionManager.State.None

private var callback: ConnectionManager.Callback? = null
Expand Down Expand Up @@ -237,12 +237,8 @@ class ConnectionManagerImpl(
}

when (errorMessage) {
null -> {
setConnectionState(ConnectionManager.State.Connected)
}
else -> {
disconnect(code = 401, message = "AuthToken Failed")
}
null -> setConnectionState(ConnectionManager.State.Connected)
else -> disconnect(code = 401, message = "AuthToken Failed")
}
}
// endregion Handshake
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ class QueueManagerImpl(

private var callback: QueueManager.Callback? = null

private val cacheTable: Hashtable<Int, MessageData> = Hashtable()

/**
* Add a new message to the queue
* @param type The type of the log. One of [MADAssistantTransmissionType]
Expand All @@ -51,19 +49,18 @@ class QueueManagerImpl(
) {
if (connectionManager.isConnectedOrConnecting()) {
try {
val data = MessageData(
timestamp = timestamp,
threadName = Thread.currentThread().name,
sessionId = sessionId,
first = first,
second = second,
third = third,
fourth = fourth
queueMessage(
type = type,
data = MessageData(
timestamp = timestamp,
threadName = Thread.currentThread().name,
sessionId = sessionId,
first = first,
second = second,
third = third,
fourth = fourth
),
)
val key = "$type:$timestamp".hashCode()
cacheTable[key] = data

queueMessage(type = type, key = key)
} catch (ex: Exception) {
logger?.e(ex)
}
Expand All @@ -74,17 +71,11 @@ class QueueManagerImpl(
* Since the system is not yet ready to send the message (and not disconnecting/disconnected),
* Queue the message again so its sent when the system is ready
*/
internal fun queueMessage(type: Int, key: Int) {
/*logger?.v(
TAG,
"queueMessage: state: ${connectionManager.currentState} type: $type data: ${
key.toString().take(256)
}"
)*/

internal fun queueMessage(type: Int, data: MessageData) {
try {

_clientHandler.sendMessage(
_clientHandler.obtainMessage(type).apply { arg1 = key }
_clientHandler.obtainMessage(type, 0, 0, data)
)
} catch (ex: Exception) {
logger?.e(ex)
Expand All @@ -100,23 +91,22 @@ class QueueManagerImpl(
* - Drops the message if the state is Disconnected
*/
override fun handleMessage(message: Message): Boolean {
/*logger?.v(
TAG,
"handleMessage: state: ${connectionManager.currentState} message: $message"
)*/

when {
connectionManager.isConnected() || connectionManager.isDisconnecting() -> {
val data = cacheTable[message.arg1]
callback?.processQueuedMessage(type = message.what, data = data as MessageData)

cacheTable.remove(message.arg1)
try {
when {
connectionManager.isConnected() || connectionManager.isDisconnecting() -> {
callback?.processQueuedMessage(
type = message.what,
data = message.obj as MessageData
)
}

connectionManager.isConnecting() -> queueMessage(
type = message.what,
data = message.obj as MessageData
)
}

connectionManager.isConnecting() -> queueMessage(
type = message.what,
key = message.arg1
)
} catch (ex: Exception) {
logger?.e(ex)
}

return true
Expand Down

0 comments on commit e59d0d0

Please sign in to comment.