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};
let mut router: Router<8> = Router::new(0x0A);
router.observe(0x0B, 0x0C, 3); // reach 0x0B via 0x0C, cost 3
assert_eq!(router.next_hop(0x0B), Some(0x0C));
assert_eq!(router.forward(0x0A), Forward::Deliver); // a packet for usImplementations§
Source§impl<const N: usize> Router<N>
impl<const N: usize> Router<N>
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 neighbour 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 neighbour 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.