refresh peer list more often

This commit is contained in:
callebtc
2026-01-05 10:40:23 +07:00
parent 27c6dc5ed9
commit ee4b2610f9
4 changed files with 36 additions and 3 deletions
@@ -61,6 +61,7 @@ class BluetoothMeshService(private val context: Context) : MeshService, Transpor
field = value
if (::meshCore.isInitialized) {
meshCore.delegate = value
meshCore.refreshPeerList()
}
}
// Tracks whether this instance has been terminated via stopServices()
@@ -668,6 +668,10 @@ class MeshCore(
fun getActivePeerCount(): Int = try { peerManager.getActivePeerCount() } catch (_: Exception) { 0 }
fun refreshPeerList() {
try { peerManager.refreshPeerList() } catch (_: Exception) { }
}
fun getDeviceAddressForPeer(peerID: String): String? = transport.getDeviceAddressForPeer(peerID)
fun getDeviceAddressToPeerMapping(): Map<String, String> = transport.getDeviceAddressToPeerMapping()
@@ -107,10 +107,21 @@ class PeerManager {
isVerified: Boolean
): Boolean {
if (peerID == "unknown") return false
fun keysMatch(a: ByteArray?, b: ByteArray?): Boolean {
if (a == null && b == null) return true
if (a == null || b == null) return false
return a.contentEquals(b)
}
val now = System.currentTimeMillis()
val existingPeer = peers[peerID]
val isNewPeer = existingPeer == null
val wasVerified = existingPeer?.isVerifiedNickname == true
val nicknameChanged = existingPeer != null && existingPeer.nickname != nickname
val noiseKeyChanged = existingPeer != null && !keysMatch(existingPeer.noisePublicKey, noisePublicKey)
val signingKeyChanged = existingPeer != null && !keysMatch(existingPeer.signingPublicKey, signingPublicKey)
val connectedChanged = existingPeer != null && existingPeer.isConnected != true
// Update or create peer info
val peerInfo = PeerInfo(
@@ -130,18 +141,27 @@ class PeerManager {
// No legacy maps; peers map is the single source of truth
// Maintain announcedPeers for first-time announce semantics
val shouldNotify = when {
isNewPeer && isVerified -> true
wasVerified != isVerified -> true
nicknameChanged || noiseKeyChanged || signingKeyChanged || connectedChanged -> true
else -> false
}
if (isNewPeer && isVerified) {
announcedPeers.add(peerID)
notifyPeerListUpdate()
Log.d(TAG, "🆕 New verified peer: $nickname ($peerID)")
return true
} else if (isVerified) {
Log.d(TAG, "🔄 Updated verified peer: $nickname ($peerID)")
} else {
Log.d(TAG, "⚠️ Unverified peer announcement from: $nickname ($peerID)")
}
if (shouldNotify) {
notifyPeerListUpdate()
}
return false
return isNewPeer && isVerified
}
/**
@@ -408,6 +428,10 @@ class PeerManager {
val peerList = getActivePeerIDs()
delegate?.onPeerListUpdated(peerList)
}
fun refreshPeerList() {
notifyPeerListUpdate()
}
/**
* Start periodic cleanup of stale peers
@@ -81,6 +81,7 @@ class WifiAwareMeshService(private val context: Context) : MeshService, Transpor
field = value
if (::meshCore.isInitialized) {
meshCore.delegate = value
meshCore.refreshPeerList()
}
}
private val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
@@ -114,6 +115,9 @@ class WifiAwareMeshService(private val context: Context) : MeshService, Transpor
try { meshCore.gossipSyncManager.scheduleInitialSyncToPeer(pid, 1_000) } catch (_: Exception) { }
}
},
announcementNicknameProvider = {
try { com.bitchat.android.services.NicknameProvider.getNickname(context, myPeerID) } catch (_: Exception) { null }
},
leavePayloadProvider = {
(delegate?.getNickname() ?: myPeerID).toByteArray(Charsets.UTF_8)
}