pub struct Router<const N: usize> { /* private fields */ }Expand description
A fixed-size routing table for one node.
The table holds up to N routes, learned from the traffic the node hears. It keeps the
cheapest route it knows to each destination, and when full it gives up the most
expensive route to make room for a cheaper one, so its limited memory holds the routes
most worth keeping.
§Examples
use pamoja_routing::{Forward, Router};
// A soil probe, node 10, reaches the gateway, node 11, through a relay, node 12,
// three hops out.
let (probe, gateway, relay) = (10, 11, 12);
let mut router: Router<8> = Router::new(probe);
router.observe(gateway, relay, 3);
assert_eq!(router.next_hop(gateway), Some(relay));
// A packet addressed to the probe itself is delivered, not forwarded.
assert_eq!(router.forward(probe), Forward::Deliver);Implementations§
Source§impl<const N: usize> Router<N>
impl<const N: usize> Router<N>
Sourcepub fn routes(&self) -> impl Iterator<Item = Route> + '_
pub fn routes(&self) -> impl Iterator<Item = Route> + '_
Lists the routes the table holds.
§Returns
Each route once, in the order the table holds them. A new route takes the first free slot and one that displaces another takes that slot, so the order is the table’s own, not sorted by destination or cost.
§Examples
use pamoja_routing::Router;
// A weather mast, node 1, has heard a rain gauge and a wind vane.
let (mast, rain_gauge, wind_vane, relay) = (1, 20, 21, 5);
let mut router: Router<4> = Router::new(mast);
router.observe(rain_gauge, relay, 2);
router.observe(wind_vane, wind_vane, 1);
let held: Vec<(u32, u32, u16)> = router
.routes()
.map(|route| (route.dst(), route.next_hop(), route.cost()))
.collect();
assert_eq!(held, [(rain_gauge, relay, 2), (wind_vane, wind_vane, 1)]);Sourcepub fn observe(&mut self, origin: u32, via: u32, cost: u16) -> bool
pub fn observe(&mut self, origin: u32, via: u32, cost: u16) -> bool
Learns the way to a node from a packet heard from it.
A packet that originated at origin and reached this node via the neighbor via
proves via is a way back to origin at the reported cost. The router adopts the
route if it is cheaper than what it knows, or if it refreshes the cost of the route
it is already using, and ignores a route to itself.
§Arguments
origin- the node the packet came from, the destination this route reaches.via- the neighbor the packet arrived through, the next hop for this route.cost- the cost the packet reports for reachingoriginthroughvia.
§Returns
true if the table changed (a route was added, redirected, or recosted), false
if the observation taught it nothing new.
Sourcepub fn forward(&self, dst: u32) -> Forward
pub fn forward(&self, dst: u32) -> Forward
Decides what to do with a packet bound for a destination.
§Arguments
dst- the packet’s destination.
§Returns
Forward::Deliver if the packet is for this node, Forward::Relay with the
next hop if a route is known, or Forward::Flood otherwise.
Sourcepub fn forget(&mut self, dst: u32)
pub fn forget(&mut self, dst: u32)
Forgets the route to a destination, if one is held.
§Arguments
dst- the destination whose route to drop.