Skip to main content

NetworkTopologyMapper

Struct NetworkTopologyMapper 

Source
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: String

Identifier 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

Source

pub fn new(local_id: String) -> Self

Create a new mapper with the given local node ID.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn neighbors(&self, node_id: &str) -> Vec<&str>

Return the outgoing neighbour IDs for node_id.

Source

pub fn in_degree(&self, node_id: &str) -> usize

Number of edges pointing into node_id.

Source

pub fn out_degree(&self, node_id: &str) -> usize

Number of outgoing edges from node_id.

Source

pub fn is_reachable(&self, from: &str, to: &str) -> bool

BFS reachability check.

Source

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.

Source

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.

Source

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.

Source

pub fn snapshot(&self) -> TopologySnapshot

Take a snapshot of the current topology.

Source

pub fn node_count(&self) -> usize

Total number of known nodes.

Source

pub fn edge_count(&self) -> usize

Total number of edges (including duplicates for the same pair).

Source

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.

Source

pub fn stats(&self) -> TopologyStats

Compute aggregate statistics for the current topology.

Trait Implementations§

Source§

impl Clone for NetworkTopologyMapper

Source§

fn clone(&self) -> NetworkTopologyMapper

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for NetworkTopologyMapper

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,

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<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more