implemented distance vector routing
This commit is contained in:
parent
eb43c51e50
commit
8318cc5c21
@ -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);
|
||||
}
|
||||
|
@ -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}>);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user