pub struct NetworkTopologyMapper {
pub local_id: String,
pub nodes: HashMap<String, TopoNode>,
pub edges: Vec<TopoEdge>,
pub adjacency: HashMap<String, Vec<String>>,
}Expand description
Discovers and maps the P2P network topology.
§Example
use ipfrs_network::topology_mapper::{NetworkTopologyMapper, TopoNode, TopoEdge};
let mut mapper = NetworkTopologyMapper::new("local".to_string());
mapper.add_node(TopoNode {
node_id: "peer-1".to_string(),
address: "/ip4/127.0.0.1/tcp/4001".to_string(),
discovered_at: 0,
hop_distance: 1,
});
mapper.add_edge(TopoEdge {
from: "local".to_string(),
to: "peer-1".to_string(),
latency_ms: 5.0,
bandwidth_bps: 10_000_000,
discovered_at: 0,
});
let path = mapper.shortest_path("local", "peer-1");
assert!(path.is_some());Fields§
§local_id: StringIdentifier of the local node (the observer).
nodes: HashMap<String, TopoNode>All known nodes keyed by node_id.
edges: Vec<TopoEdge>All directed edges (flat list; may contain duplicate from→to pairs
if the caller adds them; the last-added is preferred for path finding).
adjacency: HashMap<String, Vec<String>>Adjacency list: node_id → [neighbour_ids] (outgoing only).
Implementations§
Source§impl NetworkTopologyMapper
impl NetworkTopologyMapper
Sourcepub fn add_node(&mut self, node: TopoNode)
pub fn add_node(&mut self, node: TopoNode)
Insert or update a node. When a node is updated its hop_distance and
address are refreshed; discovered_at is kept if the new value is 0.
Sourcepub fn remove_node(&mut self, node_id: &str) -> bool
pub fn remove_node(&mut self, node_id: &str) -> bool
Remove a node and all edges that reference it (as source or destination).
Returns true if the node existed, false otherwise.
Sourcepub fn add_edge(&mut self, edge: TopoEdge)
pub fn add_edge(&mut self, edge: TopoEdge)
Add a directed edge and update the adjacency list for from.
Duplicate from→to edges are allowed; path queries always use the edge
with the lowest latency between a pair.
Sourcepub fn remove_edge(&mut self, from: &str, to: &str) -> bool
pub fn remove_edge(&mut self, from: &str, to: &str) -> bool
Remove all edges from from to to.
Returns true if at least one edge was removed.
Sourcepub fn shortest_path(&self, from: &str, to: &str) -> Option<PathResult>
pub fn shortest_path(&self, from: &str, to: &str) -> Option<PathResult>
Dijkstra shortest path weighted by latency_ms.
Returns None if either node is unknown or no path exists.
Sourcepub fn hop_shortest_path(&self, from: &str, to: &str) -> Option<PathResult>
pub fn hop_shortest_path(&self, from: &str, to: &str) -> Option<PathResult>
BFS shortest path (unweighted; minimises hop count).
Returns None if either node is unknown or no path exists.
Sourcepub fn neighbors(&self, node_id: &str) -> Vec<&str>
pub fn neighbors(&self, node_id: &str) -> Vec<&str>
Return the outgoing neighbour IDs for node_id.
Sourcepub fn out_degree(&self, node_id: &str) -> usize
pub fn out_degree(&self, node_id: &str) -> usize
Number of outgoing edges from node_id.
Sourcepub fn is_reachable(&self, from: &str, to: &str) -> bool
pub fn is_reachable(&self, from: &str, to: &str) -> bool
BFS reachability check.
Sourcepub fn connected_components(&self) -> Vec<Vec<String>>
pub fn connected_components(&self) -> Vec<Vec<String>>
Compute weakly connected components (treat directed edges as undirected).
Each component is a sorted list of node IDs. The returned list of components is sorted by the first element of each component.
Sourcepub fn diameter(&self) -> Option<f64>
pub fn diameter(&self) -> Option<f64>
Graph diameter: the maximum shortest-path latency over all reachable pairs.
Returns None when there are fewer than two nodes.
This is O(V × Dijkstra) and is expensive for large graphs.
Sourcepub fn average_clustering_coefficient(&self) -> f64
pub fn average_clustering_coefficient(&self) -> f64
Mean clustering coefficient (undirected interpretation).
For each node whose combined (in+out) neighbour set has size ≥ 2, the local clustering coefficient is:
C(v) = (actual triangles through v) / (k * (k-1) / 2)
where k is the size of the undirected neighbourhood, and a “triangle”
exists when two neighbours are themselves connected (in either direction).
Returns 0.0 if no node has k ≥ 2.
Sourcepub fn snapshot(&self) -> TopologySnapshot
pub fn snapshot(&self) -> TopologySnapshot
Take a snapshot of the current topology.
Sourcepub fn node_count(&self) -> usize
pub fn node_count(&self) -> usize
Total number of known nodes.
Sourcepub fn edge_count(&self) -> usize
pub fn edge_count(&self) -> usize
Total number of edges (including duplicates for the same pair).
Sourcepub fn evict_stale(&mut self, max_age_ms: u64, now: u64) -> usize
pub fn evict_stale(&mut self, max_age_ms: u64, now: u64) -> usize
Remove nodes whose discovered_at is older than max_age_ms relative
to now. All edges referencing removed nodes are also cleaned up.
Returns the number of nodes removed.
Sourcepub fn stats(&self) -> TopologyStats
pub fn stats(&self) -> TopologyStats
Compute aggregate statistics for the current topology.
Trait Implementations§
Source§impl Clone for NetworkTopologyMapper
impl Clone for NetworkTopologyMapper
Source§fn clone(&self) -> NetworkTopologyMapper
fn clone(&self) -> NetworkTopologyMapper
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for NetworkTopologyMapper
impl RefUnwindSafe for NetworkTopologyMapper
impl Send for NetworkTopologyMapper
impl Sync for NetworkTopologyMapper
impl Unpin for NetworkTopologyMapper
impl UnsafeUnpin for NetworkTopologyMapper
impl UnwindSafe for NetworkTopologyMapper
Blanket Implementations§
impl<T> Allocation for T
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more