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(); } }