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 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>; public final onNewNeigbor:Signal<Int>;
@ -37,11 +37,13 @@ class Routing {
@:allow(kernel.Init) @:allow(kernel.Init)
private function init() { private function init() {
routingTable.set(Net.instance.networkID, {interf: Loopback.instance, cost: 0, rep: Net.instance.networkID}); routingTable.set(Net.instance.networkID, {interf: Loopback.instance, cost: 0, rep: Net.instance.networkID});
for (modem in Peripheral.instance.getModems()) { brodcastRoutingTable();
modem.send(Net.BRODCAST_PORT, Net.instance.networkID, newRoutDiscoverPackage());
}
} }
/**
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() { private function prepareRouteUpdate() {
if (this.routeUpdateInQueue) { if (this.routeUpdateInQueue) {
return; return;
@ -50,23 +52,18 @@ class Routing {
this.routeUpdateInQueue = true; this.routeUpdateInQueue = true;
new Timer(UPDATE_WAIT_TIME, () -> { new Timer(UPDATE_WAIT_TIME, () -> {
brodcastUpdatedRoutes(); brodcastRoutingTable();
this.routeUpdateInQueue = false; this.routeUpdateInQueue = false;
}); });
} }
private function brodcastUpdatedRoutes() { /**
var pack: Package = { Brodcast the current routing table to all peers.
type: RouteDiscoverUpdate(genRouteList()), **/
toID: Net.BRODCAST_PORT, private function brodcastRoutingTable() {
msgID: null, var pack = newRoutDiscoverPackage();
fromID: Net.instance.networkID,
data: null,
ttl: 0, // Prevent forwarding
};
for (modem in Peripheral.instance.getModems()) { for (modem in Peripheral.instance.getModems()) {
modem.send(Net.BRODCAST_PORT, Net.instance.networkID, pack); modem.send(Net.BRODCAST_PORT, Net.instance.networkID, pack);
} }
} }
@ -116,6 +113,9 @@ class Routing {
interf.send(response.toID, Net.instance.networkID, response); 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}> { private function genRouteList():Array<{id:NetworkID, cost:Int}> {
var routes:Array<{id:NetworkID, cost:Int}> = []; var routes:Array<{id:NetworkID, cost:Int}> = [];
for (k => v in this.routingTable) { for (k => v in this.routingTable) {
@ -168,7 +168,7 @@ class Routing {
return {interf: Loopback.instance, rep: Net.instance.networkID}; return {interf: Loopback.instance, rep: Net.instance.networkID};
} }
var route = this.routingTable[networkID]; var route:Null<Route> = this.routingTable[networkID];
if (route == null) { if (route == null) {
return null; return null;