@pamoja/routing
v0.1.18
Published
Reverse-path routing that learns the cheapest route from overheard traffic.
Readme
@pamoja/routing
Reverse-path routing that learns the cheapest route from overheard traffic. One capability of pamoja, one memory-safe Rust core with bindings for TypeScript, Python, and C#.
Install
npm install @pamoja/routingThis pulls in @pamoja/native, the compiled engine. npm install pamoja is the whole framework in one package.
Example
The test that runs in CI, spliced here as it ran.
From bindings/node/guides/routing.ts:
import { ForwardAction, Router } from '@pamoja/routing'
// The nodes on this mesh. An address is just a number; naming them is what makes the
// table below read as a map of the site rather than a list of numbers.
const GATEWAY = 1
const PUMP = 9
const TANK = 10
const NORTH_RELAY = 5
const EAST_RELAY = 7
const SOUTH_RELAY = 3
const SILO = 32
// A node learns the way to another from traffic it already hears: a packet from the pump
// that arrived through the north relay proves that relay is a way back, at the cost the
// packet reports.
const router = new Router(GATEWAY, 4)
router.observe(PUMP, NORTH_RELAY, 2)
// The table keeps only the cheapest way it knows to each node, so a cost-1 report through
// the east relay takes over and the later cost-4 report changes nothing.
router.observe(PUMP, EAST_RELAY, 1)
router.observe(PUMP, SOUTH_RELAY, 4)
router.observe(TANK, NORTH_RELAY, 3)
const route = router.route(PUMP)
console.log(`to the pump via ${route?.nextHop} at cost ${route?.cost}`)
console.log(`routes held ${router.size}`)
// Every packet gets one of three answers: deliver it here, relay it to the neighbor on
// the way, or flood it because no route is known yet.
for (const [name, address] of [
['gateway', GATEWAY],
['pump', PUMP],
['silo', SILO],
] as const) {
const decision = router.forward(address)
if (decision.action === ForwardAction.Deliver) {
console.log(`for the ${name.padEnd(8)} deliver here`)
} else if (decision.action === ForwardAction.Relay) {
console.log(`for the ${name.padEnd(8)} relay via ${decision.nextHop}`)
} else {
console.log(`for the ${name.padEnd(8)} flood, no route known`)
}
}
// Forgetting a node that has gone quiet returns its traffic to flooding, so routing is an
// optimization over flooding rather than a second thing that can fail.
router.forget(PUMP)
const after = router.forward(PUMP)
console.log(`pump forgotten, so it floods again: ${after.action === ForwardAction.Flood}`)The same capability in every language
| Language | Package | Reference |
| --- | --- | --- |
| Rust | pamoja-routing | reference, docs.rs, install |
| TypeScript | @pamoja/routing | reference, install |
| Python | pamoja-routing | reference, install |
| C# | Pamoja.Routing | reference, install |
Documentation
@pamoja/routingreference, every class, function, and type this package exports.- The Routing guide, with the same example in Rust, Python, and C#.
- Every capability, and the install page.
License
MIT
