mirror of
https://github.com/permissionlesstech/bitchat-android.git
synced 2026-07-25 06:25:21 +00:00
refresh peer list more often
This commit is contained in:
@@ -61,6 +61,7 @@ class BluetoothMeshService(private val context: Context) : MeshService, Transpor
|
|||||||
field = value
|
field = value
|
||||||
if (::meshCore.isInitialized) {
|
if (::meshCore.isInitialized) {
|
||||||
meshCore.delegate = value
|
meshCore.delegate = value
|
||||||
|
meshCore.refreshPeerList()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Tracks whether this instance has been terminated via stopServices()
|
// 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 getActivePeerCount(): Int = try { peerManager.getActivePeerCount() } catch (_: Exception) { 0 }
|
||||||
|
|
||||||
|
fun refreshPeerList() {
|
||||||
|
try { peerManager.refreshPeerList() } catch (_: Exception) { }
|
||||||
|
}
|
||||||
|
|
||||||
fun getDeviceAddressForPeer(peerID: String): String? = transport.getDeviceAddressForPeer(peerID)
|
fun getDeviceAddressForPeer(peerID: String): String? = transport.getDeviceAddressForPeer(peerID)
|
||||||
|
|
||||||
fun getDeviceAddressToPeerMapping(): Map<String, String> = transport.getDeviceAddressToPeerMapping()
|
fun getDeviceAddressToPeerMapping(): Map<String, String> = transport.getDeviceAddressToPeerMapping()
|
||||||
|
|||||||
@@ -108,9 +108,20 @@ class PeerManager {
|
|||||||
): Boolean {
|
): Boolean {
|
||||||
if (peerID == "unknown") return false
|
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 now = System.currentTimeMillis()
|
||||||
val existingPeer = peers[peerID]
|
val existingPeer = peers[peerID]
|
||||||
val isNewPeer = existingPeer == null
|
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
|
// Update or create peer info
|
||||||
val peerInfo = PeerInfo(
|
val peerInfo = PeerInfo(
|
||||||
@@ -130,18 +141,27 @@ class PeerManager {
|
|||||||
// No legacy maps; peers map is the single source of truth
|
// No legacy maps; peers map is the single source of truth
|
||||||
// Maintain announcedPeers for first-time announce semantics
|
// 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) {
|
if (isNewPeer && isVerified) {
|
||||||
announcedPeers.add(peerID)
|
announcedPeers.add(peerID)
|
||||||
notifyPeerListUpdate()
|
|
||||||
Log.d(TAG, "🆕 New verified peer: $nickname ($peerID)")
|
Log.d(TAG, "🆕 New verified peer: $nickname ($peerID)")
|
||||||
return true
|
|
||||||
} else if (isVerified) {
|
} else if (isVerified) {
|
||||||
Log.d(TAG, "🔄 Updated verified peer: $nickname ($peerID)")
|
Log.d(TAG, "🔄 Updated verified peer: $nickname ($peerID)")
|
||||||
} else {
|
} else {
|
||||||
Log.d(TAG, "⚠️ Unverified peer announcement from: $nickname ($peerID)")
|
Log.d(TAG, "⚠️ Unverified peer announcement from: $nickname ($peerID)")
|
||||||
}
|
}
|
||||||
|
|
||||||
return false
|
if (shouldNotify) {
|
||||||
|
notifyPeerListUpdate()
|
||||||
|
}
|
||||||
|
|
||||||
|
return isNewPeer && isVerified
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -409,6 +429,10 @@ class PeerManager {
|
|||||||
delegate?.onPeerListUpdated(peerList)
|
delegate?.onPeerListUpdated(peerList)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun refreshPeerList() {
|
||||||
|
notifyPeerListUpdate()
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start periodic cleanup of stale peers
|
* Start periodic cleanup of stale peers
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -81,6 +81,7 @@ class WifiAwareMeshService(private val context: Context) : MeshService, Transpor
|
|||||||
field = value
|
field = value
|
||||||
if (::meshCore.isInitialized) {
|
if (::meshCore.isInitialized) {
|
||||||
meshCore.delegate = value
|
meshCore.delegate = value
|
||||||
|
meshCore.refreshPeerList()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
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) { }
|
try { meshCore.gossipSyncManager.scheduleInitialSyncToPeer(pid, 1_000) } catch (_: Exception) { }
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
announcementNicknameProvider = {
|
||||||
|
try { com.bitchat.android.services.NicknameProvider.getNickname(context, myPeerID) } catch (_: Exception) { null }
|
||||||
|
},
|
||||||
leavePayloadProvider = {
|
leavePayloadProvider = {
|
||||||
(delegate?.getNickname() ?: myPeerID).toByteArray(Charsets.UTF_8)
|
(delegate?.getNickname() ?: myPeerID).toByteArray(Charsets.UTF_8)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user