implemented distance vector routing

This commit is contained in:
Djeeberjr 2022-03-01 17:28:15 +01:00
parent eb43c51e50
commit 8318cc5c21
3 changed files with 41 additions and 1 deletions

View File

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

View File

@ -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}>);
}
/**

View File

@ -22,10 +22,13 @@ class Routing {
**/
public static var instance:Routing;
public static inline final UPDATE_WAIT_TIME:Float = 3;
public final onNewNeigbor:Signal<Int>;
private final onNewNeigborTrigger:SignalTrigger<NetworkID> = Signal.trigger();
private final routingTable:Map<NetworkID, Route> = 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();
}
}