diff --git a/src/bin/Net.hx b/src/bin/Net.hx index 0925dd7..49dbd87 100644 --- a/src/bin/Net.hx +++ b/src/bin/Net.hx @@ -9,7 +9,7 @@ using tink.CoreApi; class Net extends CLIAppBase { public function new() { registerSyncSubcommand("route", (args)->{ - var routes = Routing.instance.getRouteTable(); + var routes = Routing.getRouteTable(); for(k => v in routes) { handle.writeLine('${k} => ${v.interf.name()}(${v.cost})'); diff --git a/src/kernel/Init.hx b/src/kernel/Init.hx index daf654a..2c3f138 100644 --- a/src/kernel/Init.hx +++ b/src/kernel/Init.hx @@ -34,7 +34,7 @@ class Init { Turtle.instance = new Turtle(); } - Routing.instance = new Routing(); + Routing.init(); Net.instance = new Net(); GPS.instance = new GPS(); @@ -47,8 +47,6 @@ class Init { Debug.printBuildInfo(); - Routing.instance.init(); - if (!FS.exists("/var/ns")) { FS.makeDir("/var/ns"); } diff --git a/src/kernel/net/Net.hx b/src/kernel/net/Net.hx index f723a47..3297040 100644 --- a/src/kernel/net/Net.hx +++ b/src/kernel/net/Net.hx @@ -40,6 +40,8 @@ class Net { } setupPingHandle(); + + Routing.setup(); } private function setupPingHandle() { @@ -82,7 +84,7 @@ class Net { } case RouteDiscover(_) | RouteDiscoverResponse(_) | RouteDiscoverUpdate(_): // Delegate to Routing - Routing.instance.handleRoutePackage(cast pack,interf); + Routing.handleRoutePackage(cast pack,interf); case GPSRequest | GPSResponse(_): if (dist == null) { Log.silly("Got a GPS package but no distance was provided"); @@ -174,7 +176,7 @@ class Net { Returns true if message was send **/ private function sendRaw(pack:GenericPackage): Bool { - var route = Routing.instance.getRouteToID(pack.toID); + var route = Routing.getRouteToID(pack.toID); if (route == null){ return false; } diff --git a/src/kernel/net/Routing.hx b/src/kernel/net/Routing.hx index 72042fa..d386ea6 100644 --- a/src/kernel/net/Routing.hx +++ b/src/kernel/net/Routing.hx @@ -20,23 +20,22 @@ class Routing { /** Depends on: Peripheral **/ - public static var instance:Routing; public static inline final UPDATE_WAIT_TIME:Float = 1; - public final onNewNeigbor:Signal; + public static var onNewNeigbor(default, null):Signal; - private final onNewNeigborTrigger:SignalTrigger = Signal.trigger(); - private final routingTable:Map = new Map(); - private var routeUpdateInQueue:Bool = false; + private static final onNewNeigborTrigger:SignalTrigger = Signal.trigger(); + private static final routingTable:Map = new Map(); + private static var routeUpdateInQueue:Bool = false; @:allow(kernel.Init) - private function new() { - this.onNewNeigbor = this.onNewNeigborTrigger.asSignal(); + private static function init() { + onNewNeigbor = onNewNeigborTrigger.asSignal(); } - @:allow(kernel.Init) - private function init() { + @:allow(kernel.net.Net) + private static function setup() { routingTable.set(Net.instance.networkID, {interf: Loopback.instance, cost: 0, rep: Net.instance.networkID}); brodcastRoutingTable(); } @@ -45,23 +44,23 @@ class Routing { 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) { + private static function prepareRouteUpdate() { + if (routeUpdateInQueue) { return; } - this.routeUpdateInQueue = true; + routeUpdateInQueue = true; new Timer(UPDATE_WAIT_TIME, () -> { brodcastRoutingTable(); - this.routeUpdateInQueue = false; + routeUpdateInQueue = false; }); } /** Brodcast the current routing table to all peers. **/ - private function brodcastRoutingTable() { + private static function brodcastRoutingTable() { var pack = newRoutDiscoverPackage(); for (modem in Peripheral.getAllModems()) { @@ -73,7 +72,7 @@ class Routing { Handle incomming packages that involve route discovery. **/ @:allow(kernel.net.Net) - private function handleRoutePackage(pack:Package, interf:INetworkInterface):Void { + private static function handleRoutePackage(pack:Package, interf:INetworkInterface):Void { addPossibleRoute(pack.fromID, interf, 0, pack.fromID); var shouldRespond:Bool = switch pack.type { @@ -117,9 +116,9 @@ class Routing { /** 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 static function genRouteList():Array<{id:NetworkID, cost:Int}> { var routes:Array<{id:NetworkID, cost:Int}> = []; - for (k => v in this.routingTable) { + for (k => v in routingTable) { routes.push({ id: k, cost: v.cost @@ -128,7 +127,7 @@ class Routing { return routes; } - private function newRoutDiscoverPackage():Package { + private static function newRoutDiscoverPackage():Package { var pack:Package = { type: RouteDiscover(genRouteList()), toID: Net.BRODCAST_PORT, @@ -145,31 +144,31 @@ class Routing { Called when a route to a client has been disoverd. Its possible to be called multiple times with the same id but different addr. **/ - private function addPossibleRoute(toID:Int, interf:INetworkInterface, cost:Int, rep:NetworkID) { + private static function addPossibleRoute(toID:Int, interf:INetworkInterface, cost:Int, rep:NetworkID) { if (toID == Net.instance.networkID) { return; } var fullCost = cost + interf.getBaseRoutingCost(); - if (this.routingTable.exists(toID)) { - if (this.routingTable[toID].cost > fullCost) { - this.routingTable[toID] = {interf: interf, cost: cost + interf.getBaseRoutingCost(), rep: rep}; - this.prepareRouteUpdate(); + if (routingTable.exists(toID)) { + if (routingTable[toID].cost > fullCost) { + routingTable[toID] = {interf: interf, cost: cost + interf.getBaseRoutingCost(), rep: rep}; + prepareRouteUpdate(); } } else { - this.routingTable[toID] = {interf: interf, cost: cost + interf.getBaseRoutingCost(), rep: rep}; - this.onNewNeigborTrigger.trigger(toID); - this.prepareRouteUpdate(); + routingTable[toID] = {interf: interf, cost: cost + interf.getBaseRoutingCost(), rep: rep}; + onNewNeigborTrigger.trigger(toID); + prepareRouteUpdate(); } } - public function getRouteToID(networkID:NetworkID):{interf:INetworkInterface, rep:NetworkID} { + public static function getRouteToID(networkID:NetworkID):{interf:INetworkInterface, rep:NetworkID} { if (networkID == Net.instance.networkID) { return {interf: Loopback.instance, rep: Net.instance.networkID}; } - var route:Null = this.routingTable[networkID]; + var route:Null = routingTable[networkID]; if (route == null) { return null; @@ -178,7 +177,7 @@ class Routing { } } - public function getRouteTable():Map { - return this.routingTable; + public static function getRouteTable():Map { + return routingTable; } }