From 8318cc5c210452737713a4e8341e078712c92474 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Tue, 1 Mar 2022 17:28:15 +0100 Subject: [PATCH] implemented distance vector routing --- src/kernel/net/Net.hx | 2 +- src/kernel/net/Package.hx | 1 + src/kernel/net/Routing.hx | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/kernel/net/Net.hx b/src/kernel/net/Net.hx index 1e2e72d..13ee49b 100644 --- a/src/kernel/net/Net.hx +++ b/src/kernel/net/Net.hx @@ -59,7 +59,7 @@ class Net { if (responseBus.exists(pack.msgID)) { responseBus[pack.msgID].invoke(pack); } - case RouteDiscover(_) | RouteDiscoverResponse(_): + case RouteDiscover(_) | RouteDiscoverResponse(_) | RouteDiscoverUpdate(_): // Delegate to Routing Routing.instance.handleRoutePackage(pack,interf); } diff --git a/src/kernel/net/Package.hx b/src/kernel/net/Package.hx index 25cba5e..9509cc6 100644 --- a/src/kernel/net/Package.hx +++ b/src/kernel/net/Package.hx @@ -8,6 +8,7 @@ enum PackageTypes { Response; RouteDiscover(reachableIDs: Array<{id:NetworkID,cost:Int}>); RouteDiscoverResponse(reachableIDs: Array<{id:NetworkID,cost:Int}>); + RouteDiscoverUpdate(reachableIDs: Array<{id:NetworkID,cost:Int}>); } /** diff --git a/src/kernel/net/Routing.hx b/src/kernel/net/Routing.hx index 660bcb3..01ad4d9 100644 --- a/src/kernel/net/Routing.hx +++ b/src/kernel/net/Routing.hx @@ -22,10 +22,13 @@ class Routing { **/ public static var instance:Routing; + public static inline final UPDATE_WAIT_TIME:Float = 3; + public final onNewNeigbor:Signal; private final onNewNeigborTrigger:SignalTrigger = Signal.trigger(); private final routingTable:Map = new Map(); + private var routeUpdateInQueue: Bool = false; @:allow(kernel.Init) private function new() { @@ -40,6 +43,35 @@ class Routing { } } + private function prepareRouteUpdate() { + if (this.routeUpdateInQueue){ + return; + } + + this.routeUpdateInQueue = true; + + new Timer(UPDATE_WAIT_TIME,()->{ + brodcastUpdatedRoutes(); + this.routeUpdateInQueue = false; + }); + } + + private function brodcastUpdatedRoutes() { + var pack: Package = { + type: RouteDiscoverUpdate(genRouteList()), + toID: Net.BRODCAST_PORT, + msgID: null, + fromID: Net.instance.networkID, + data: null, + ttl: 0, // Prevent forwarding + }; + + for (modem in Peripheral.instance.getModems()) { + + modem.send(Net.BRODCAST_PORT, Net.instance.networkID,pack); + } + } + /** Handle incomming packages that involve route discovery. **/ @@ -57,6 +89,11 @@ class Routing { addPossibleRoute(route.id,interf,route.cost,pack.fromID); } true; + case RouteDiscoverUpdate(routes): + for (route in routes) { + addPossibleRoute(route.id,interf,route.cost,pack.fromID); + } + false; default: Log.error("Expected RouteDiscover or RouteDiscoverResponse type"); false; @@ -118,11 +155,13 @@ class Routing { if (this.routingTable[toID].cost > fullCost){ Log.info("Better route: " + toID + " -> " + interf.name() + ":$"+fullCost); this.routingTable[toID] = {interf:interf,cost:cost + interf.getBaseRoutingCost(),rep: rep}; + this.prepareRouteUpdate(); } }else{ this.routingTable[toID] = {interf:interf,cost:cost + interf.getBaseRoutingCost(),rep: rep}; Log.info("New route: " + toID + " -> " + interf.name() + ":$"+fullCost); this.onNewNeigborTrigger.trigger(toID); + this.prepareRouteUpdate(); } }