improved Routing

This commit is contained in:
Djeeberjr 2022-12-19 21:25:39 +01:00
parent 45a8851e2a
commit f988c79e2f

View File

@ -21,7 +21,7 @@ class Routing {
**/
public static var instance:Routing;
public static inline final UPDATE_WAIT_TIME:Float = 3;
public static inline final UPDATE_WAIT_TIME:Float = 1;
public final onNewNeigbor:Signal<Int>;
@ -37,11 +37,13 @@ class Routing {
@:allow(kernel.Init)
private function init() {
routingTable.set(Net.instance.networkID, {interf: Loopback.instance, cost: 0, rep: Net.instance.networkID});
for (modem in Peripheral.instance.getModems()) {
modem.send(Net.BRODCAST_PORT, Net.instance.networkID, newRoutDiscoverPackage());
}
brodcastRoutingTable();
}
/**
Prepares an brodcast of the current routing table. If a brodcast is already in queue it will be ignored.
After UPDATE_WAIT_TIME seconds the routing table will be brodcasted. This is done to prevent spamming the network.
**/
private function prepareRouteUpdate() {
if (this.routeUpdateInQueue) {
return;
@ -50,23 +52,18 @@ class Routing {
this.routeUpdateInQueue = true;
new Timer(UPDATE_WAIT_TIME, () -> {
brodcastUpdatedRoutes();
brodcastRoutingTable();
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
};
/**
Brodcast the current routing table to all peers.
**/
private function brodcastRoutingTable() {
var pack = newRoutDiscoverPackage();
for (modem in Peripheral.instance.getModems()) {
modem.send(Net.BRODCAST_PORT, Net.instance.networkID, pack);
}
}
@ -116,6 +113,9 @@ class Routing {
interf.send(response.toID, Net.instance.networkID, response);
}
/**
Generate a list of routes to send to a peer based on the current routing table.
**/
private function genRouteList():Array<{id:NetworkID, cost:Int}> {
var routes:Array<{id:NetworkID, cost:Int}> = [];
for (k => v in this.routingTable) {
@ -168,7 +168,7 @@ class Routing {
return {interf: Loopback.instance, rep: Net.instance.networkID};
}
var route = this.routingTable[networkID];
var route:Null<Route> = this.routingTable[networkID];
if (route == null) {
return null;