pub struct PeerManager { /* private fields */ }Expand description
Centralized peer manager for HIVE mesh
Tracks discovered peers, their connection state, and sync history. Thread-safe and designed for use from platform BLE callbacks.
Implementations§
Source§impl PeerManager
impl PeerManager
Sourcepub fn new(node_id: NodeId, config: PeerManagerConfig) -> Self
pub fn new(node_id: NodeId, config: PeerManagerConfig) -> Self
Create a new peer manager
Sourcepub fn matches_mesh(&self, device_mesh_id: Option<&str>) -> bool
pub fn matches_mesh(&self, device_mesh_id: Option<&str>) -> bool
Check if a device mesh ID matches our mesh
Sourcepub fn on_discovered(
&self,
identifier: &str,
name: Option<&str>,
rssi: i8,
mesh_id: Option<&str>,
now_ms: u64,
) -> Option<(NodeId, bool)>
pub fn on_discovered( &self, identifier: &str, name: Option<&str>, rssi: i8, mesh_id: Option<&str>, now_ms: u64, ) -> Option<(NodeId, bool)>
Handle a discovered BLE device
Called by the platform BLE adapter when a device is discovered during scanning. Parses the device name to extract NodeId and mesh ID.
Returns Some((node_id, is_new)) if this is a HIVE device on our mesh,
where is_new indicates if this is a newly discovered peer.
Returns None if the device should be ignored.
Sourcepub fn on_connected(&self, identifier: &str, now_ms: u64) -> Option<NodeId>
pub fn on_connected(&self, identifier: &str, now_ms: u64) -> Option<NodeId>
Handle a peer connection
Called by the platform BLE adapter when a connection is established. Returns the NodeId if found, or None if this identifier is unknown.
Sourcepub fn on_disconnected(
&self,
identifier: &str,
reason: DisconnectReason,
) -> Option<(NodeId, DisconnectReason)>
pub fn on_disconnected( &self, identifier: &str, reason: DisconnectReason, ) -> Option<(NodeId, DisconnectReason)>
Handle a peer disconnection
Called by the platform BLE adapter when a connection is lost. Returns the NodeId and disconnect reason if found.
Sourcepub fn on_disconnected_by_node_id(
&self,
node_id: NodeId,
_reason: DisconnectReason,
) -> bool
pub fn on_disconnected_by_node_id( &self, node_id: NodeId, _reason: DisconnectReason, ) -> bool
Handle a peer disconnection by NodeId
Alternative to on_disconnected() when only NodeId is known (e.g., ESP32). Returns true if the peer was found and marked disconnected.
Sourcepub fn on_incoming_connection(
&self,
identifier: &str,
node_id: NodeId,
now_ms: u64,
) -> bool
pub fn on_incoming_connection( &self, identifier: &str, node_id: NodeId, now_ms: u64, ) -> bool
Register a peer from an incoming BLE connection
Called when a remote device connects to us as a peripheral. Creates a peer entry if one doesn’t exist for this identifier.
Sourcepub fn should_sync_with(&self, node_id: NodeId, now_ms: u64) -> bool
pub fn should_sync_with(&self, node_id: NodeId, now_ms: u64) -> bool
Check if we should sync with a peer
Returns true if enough time has passed since the last sync (cooldown).
Sourcepub fn record_sync(&self, node_id: NodeId, now_ms: u64)
pub fn record_sync(&self, node_id: NodeId, now_ms: u64)
Record that we synced with a peer
Sourcepub fn cleanup_stale(&self, now_ms: u64) -> Vec<NodeId>
pub fn cleanup_stale(&self, now_ms: u64) -> Vec<NodeId>
Clean up stale peers
Removes peers that haven’t been seen within the timeout period. Returns list of removed NodeIds for generating PeerLost events.
Sourcepub fn get_connected_peers(&self) -> Vec<HivePeer>
pub fn get_connected_peers(&self) -> Vec<HivePeer>
Get connected peers only
Sourcepub fn get_peer_by_identifier(&self, identifier: &str) -> Option<HivePeer>
pub fn get_peer_by_identifier(&self, identifier: &str) -> Option<HivePeer>
Get a peer by platform identifier
Sourcepub fn get_node_id(&self, identifier: &str) -> Option<NodeId>
pub fn get_node_id(&self, identifier: &str) -> Option<NodeId>
Get NodeId for a platform identifier
Sourcepub fn peer_count(&self) -> usize
pub fn peer_count(&self) -> usize
Get peer count
Sourcepub fn connected_count(&self) -> usize
pub fn connected_count(&self) -> usize
Get connected peer count
Sourcepub fn peers_needing_sync(&self, now_ms: u64) -> Vec<HivePeer>
pub fn peers_needing_sync(&self, now_ms: u64) -> Vec<HivePeer>
Get peers that need sync (connected and past cooldown)
Sourcepub fn generate_state_event(&self) -> HiveEvent
pub fn generate_state_event(&self) -> HiveEvent
Generate events for current mesh state
Useful for notifying observers of the current state after initialization.