From e8ade014aeb3c9925c979c62c4c6153816009709 Mon Sep 17 00:00:00 2001 From: 0x330a <92654767+0x330a@users.noreply.github.com> Date: Wed, 13 Sep 2023 18:39:16 +1000 Subject: [PATCH] fix: implement hashcode and equals to make pollers unique in concurrent hash map --- .../pollers/ClosedGroupPoller.kt | 10 +++++---- .../session/libsignal/utilities/SessionId.kt | 21 +++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/pollers/ClosedGroupPoller.kt b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/pollers/ClosedGroupPoller.kt index 45459c24e5..70d24e5504 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/pollers/ClosedGroupPoller.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/pollers/ClosedGroupPoller.kt @@ -4,6 +4,7 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job import kotlinx.coroutines.delay +import kotlinx.coroutines.isActive import kotlinx.coroutines.launch import network.loki.messenger.libsession_util.GroupInfoConfig import network.loki.messenger.libsession_util.GroupKeysConfig @@ -62,7 +63,7 @@ class ClosedGroupPoller(private val executor: CoroutineScope, job = executor.launch(Dispatchers.IO) { val closedGroups = configFactoryProtocol.userGroups?: return@launch isRunning = true - while (isRunning) { + while (isActive && isRunning) { val group = closedGroups.getClosedGroup(closedGroupSessionId.hexString()) ?: break val nextPoll = poll(group) if (nextPoll != null) { @@ -132,6 +133,7 @@ class ClosedGroupPoller(private val executor: CoroutineScope, // TODO: add the extend duration TTLs for known hashes here // if poll result body is null here we don't have any things ig + Log.d("ClosedGroupPoller", "Poll results @${SnodeAPI.nowWithOffset}:") (pollResult["results"] as List).forEachIndexed { index, response -> when (index) { keysIndex -> handleKeyPoll(response, keys, info, members) @@ -177,7 +179,7 @@ class ClosedGroupPoller(private val executor: CoroutineScope, // get all the data to hash objects and process them parseMessages(response).forEach { (message, hash, timestamp) -> keysConfig.loadKey(message, hash, timestamp, infoConfig, membersConfig) - Log.d("ClosedGroupPoller", "Merged $hash for keys") + Log.d("ClosedGroupPoller", "Merged $hash for keys on ${closedGroupSessionId.hexString()}") } } @@ -185,7 +187,7 @@ class ClosedGroupPoller(private val executor: CoroutineScope, infoConfig: GroupInfoConfig) { parseMessages(response).forEach { (message, hash, _) -> infoConfig.merge(hash to message) - Log.d("ClosedGroupPoller", "Merged $hash for info") + Log.d("ClosedGroupPoller", "Merged $hash for info on ${closedGroupSessionId.hexString()}") } } @@ -193,7 +195,7 @@ class ClosedGroupPoller(private val executor: CoroutineScope, membersConfig: GroupMembersConfig) { parseMessages(response).forEach { (message, hash, _) -> membersConfig.merge(hash to message) - Log.d("ClosedGroupPoller", "Merged $hash for members") + Log.d("ClosedGroupPoller", "Merged $hash for members on ${closedGroupSessionId.hexString()}") } } diff --git a/libsignal/src/main/java/org/session/libsignal/utilities/SessionId.kt b/libsignal/src/main/java/org/session/libsignal/utilities/SessionId.kt index ede1f246b4..b98bce3f3b 100644 --- a/libsignal/src/main/java/org/session/libsignal/utilities/SessionId.kt +++ b/libsignal/src/main/java/org/session/libsignal/utilities/SessionId.kt @@ -22,4 +22,25 @@ class SessionId { } fun hexString() = prefix?.value + publicKey + + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + + other as SessionId + + if (prefix != other.prefix) return false + if (publicKey != other.publicKey) return false + + return true + } + + override fun hashCode(): Int { + var result = prefix?.hashCode() ?: 0 + result = 31 * result + publicKey.hashCode() + return result + } + + } \ No newline at end of file