PeerManager

Struct PeerManager 

Source
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

Source

pub fn new(node_id: NodeId, config: PeerManagerConfig) -> Self

Create a new peer manager

Source

pub fn node_id(&self) -> NodeId

Get our node ID

Source

pub fn mesh_id(&self) -> &str

Get the mesh ID

Source

pub fn matches_mesh(&self, device_mesh_id: Option<&str>) -> bool

Check if a device mesh ID matches our mesh

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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).

Source

pub fn record_sync(&self, node_id: NodeId, now_ms: u64)

Record that we synced with a peer

Source

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.

Source

pub fn get_peers(&self) -> Vec<HivePeer>

Get all known peers

Source

pub fn get_connected_peers(&self) -> Vec<HivePeer>

Get connected peers only

Source

pub fn get_peer(&self, node_id: NodeId) -> Option<HivePeer>

Get a specific peer by NodeId

Source

pub fn get_peer_by_identifier(&self, identifier: &str) -> Option<HivePeer>

Get a peer by platform identifier

Source

pub fn get_node_id(&self, identifier: &str) -> Option<NodeId>

Get NodeId for a platform identifier

Source

pub fn peer_count(&self) -> usize

Get peer count

Source

pub fn connected_count(&self) -> usize

Get connected peer count

Source

pub fn peers_needing_sync(&self, now_ms: u64) -> Vec<HivePeer>

Get peers that need sync (connected and past cooldown)

Source

pub fn generate_state_event(&self) -> HiveEvent

Generate events for current mesh state

Useful for notifying observers of the current state after initialization.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.