made Routing a static class

This commit is contained in:
Djeeberjr 2023-07-30 15:35:43 +02:00
parent 89f209130e
commit 505d318ffb
4 changed files with 35 additions and 36 deletions

View File

@ -9,7 +9,7 @@ using tink.CoreApi;
class Net extends CLIAppBase { class Net extends CLIAppBase {
public function new() { public function new() {
registerSyncSubcommand("route", (args)->{ registerSyncSubcommand("route", (args)->{
var routes = Routing.instance.getRouteTable(); var routes = Routing.getRouteTable();
for(k => v in routes) { for(k => v in routes) {
handle.writeLine('${k} => ${v.interf.name()}(${v.cost})'); handle.writeLine('${k} => ${v.interf.name()}(${v.cost})');

View File

@ -34,7 +34,7 @@ class Init {
Turtle.instance = new Turtle(); Turtle.instance = new Turtle();
} }
Routing.instance = new Routing(); Routing.init();
Net.instance = new Net(); Net.instance = new Net();
GPS.instance = new GPS(); GPS.instance = new GPS();
@ -47,8 +47,6 @@ class Init {
Debug.printBuildInfo(); Debug.printBuildInfo();
Routing.instance.init();
if (!FS.exists("/var/ns")) { if (!FS.exists("/var/ns")) {
FS.makeDir("/var/ns"); FS.makeDir("/var/ns");
} }

View File

@ -40,6 +40,8 @@ class Net {
} }
setupPingHandle(); setupPingHandle();
Routing.setup();
} }
private function setupPingHandle() { private function setupPingHandle() {
@ -82,7 +84,7 @@ class Net {
} }
case RouteDiscover(_) | RouteDiscoverResponse(_) | RouteDiscoverUpdate(_): case RouteDiscover(_) | RouteDiscoverResponse(_) | RouteDiscoverUpdate(_):
// Delegate to Routing // Delegate to Routing
Routing.instance.handleRoutePackage(cast pack,interf); Routing.handleRoutePackage(cast pack,interf);
case GPSRequest | GPSResponse(_): case GPSRequest | GPSResponse(_):
if (dist == null) { if (dist == null) {
Log.silly("Got a GPS package but no distance was provided"); Log.silly("Got a GPS package but no distance was provided");
@ -174,7 +176,7 @@ class Net {
Returns true if message was send Returns true if message was send
**/ **/
private function sendRaw(pack:GenericPackage): Bool { private function sendRaw(pack:GenericPackage): Bool {
var route = Routing.instance.getRouteToID(pack.toID); var route = Routing.getRouteToID(pack.toID);
if (route == null){ if (route == null){
return false; return false;
} }

View File

@ -20,23 +20,22 @@ class Routing {
/** /**
Depends on: Peripheral Depends on: Peripheral
**/ **/
public static var instance:Routing;
public static inline final UPDATE_WAIT_TIME:Float = 1; public static inline final UPDATE_WAIT_TIME:Float = 1;
public final onNewNeigbor:Signal<Int>; public static var onNewNeigbor(default, null):Signal<Int>;
private final onNewNeigborTrigger:SignalTrigger<NetworkID> = Signal.trigger(); private static final onNewNeigborTrigger:SignalTrigger<NetworkID> = Signal.trigger();
private final routingTable:Map<NetworkID, Route> = new Map(); private static final routingTable:Map<NetworkID, Route> = new Map();
private var routeUpdateInQueue:Bool = false; private static var routeUpdateInQueue:Bool = false;
@:allow(kernel.Init) @:allow(kernel.Init)
private function new() { private static function init() {
this.onNewNeigbor = this.onNewNeigborTrigger.asSignal(); onNewNeigbor = onNewNeigborTrigger.asSignal();
} }
@:allow(kernel.Init) @:allow(kernel.net.Net)
private function init() { private static function setup() {
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});
brodcastRoutingTable(); 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. 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. After UPDATE_WAIT_TIME seconds the routing table will be brodcasted. This is done to prevent spamming the network.
**/ **/
private function prepareRouteUpdate() { private static function prepareRouteUpdate() {
if (this.routeUpdateInQueue) { if (routeUpdateInQueue) {
return; return;
} }
this.routeUpdateInQueue = true; routeUpdateInQueue = true;
new Timer(UPDATE_WAIT_TIME, () -> { new Timer(UPDATE_WAIT_TIME, () -> {
brodcastRoutingTable(); brodcastRoutingTable();
this.routeUpdateInQueue = false; routeUpdateInQueue = false;
}); });
} }
/** /**
Brodcast the current routing table to all peers. Brodcast the current routing table to all peers.
**/ **/
private function brodcastRoutingTable() { private static function brodcastRoutingTable() {
var pack = newRoutDiscoverPackage(); var pack = newRoutDiscoverPackage();
for (modem in Peripheral.getAllModems()) { for (modem in Peripheral.getAllModems()) {
@ -73,7 +72,7 @@ class Routing {
Handle incomming packages that involve route discovery. Handle incomming packages that involve route discovery.
**/ **/
@:allow(kernel.net.Net) @:allow(kernel.net.Net)
private function handleRoutePackage(pack:Package<Noise>, interf:INetworkInterface):Void { private static function handleRoutePackage(pack:Package<Noise>, interf:INetworkInterface):Void {
addPossibleRoute(pack.fromID, interf, 0, pack.fromID); addPossibleRoute(pack.fromID, interf, 0, pack.fromID);
var shouldRespond:Bool = switch pack.type { 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. 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}> = []; var routes:Array<{id:NetworkID, cost:Int}> = [];
for (k => v in this.routingTable) { for (k => v in routingTable) {
routes.push({ routes.push({
id: k, id: k,
cost: v.cost cost: v.cost
@ -128,7 +127,7 @@ class Routing {
return routes; return routes;
} }
private function newRoutDiscoverPackage():Package<Noise> { private static function newRoutDiscoverPackage():Package<Noise> {
var pack:Package<Noise> = { var pack:Package<Noise> = {
type: RouteDiscover(genRouteList()), type: RouteDiscover(genRouteList()),
toID: Net.BRODCAST_PORT, toID: Net.BRODCAST_PORT,
@ -145,31 +144,31 @@ class Routing {
Called when a route to a client has been disoverd. Called when a route to a client has been disoverd.
Its possible to be called multiple times with the same id but different addr. 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) { if (toID == Net.instance.networkID) {
return; return;
} }
var fullCost = cost + interf.getBaseRoutingCost(); var fullCost = cost + interf.getBaseRoutingCost();
if (this.routingTable.exists(toID)) { if (routingTable.exists(toID)) {
if (this.routingTable[toID].cost > fullCost) { if (routingTable[toID].cost > fullCost) {
this.routingTable[toID] = {interf: interf, cost: cost + interf.getBaseRoutingCost(), rep: rep}; routingTable[toID] = {interf: interf, cost: cost + interf.getBaseRoutingCost(), rep: rep};
this.prepareRouteUpdate(); prepareRouteUpdate();
} }
} else { } else {
this.routingTable[toID] = {interf: interf, cost: cost + interf.getBaseRoutingCost(), rep: rep}; routingTable[toID] = {interf: interf, cost: cost + interf.getBaseRoutingCost(), rep: rep};
this.onNewNeigborTrigger.trigger(toID); onNewNeigborTrigger.trigger(toID);
this.prepareRouteUpdate(); 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) { if (networkID == Net.instance.networkID) {
return {interf: Loopback.instance, rep: Net.instance.networkID}; return {interf: Loopback.instance, rep: Net.instance.networkID};
} }
var route:Null<Route> = this.routingTable[networkID]; var route:Null<Route> = routingTable[networkID];
if (route == null) { if (route == null) {
return null; return null;
@ -178,7 +177,7 @@ class Routing {
} }
} }
public function getRouteTable():Map<NetworkID, Route> { public static function getRouteTable():Map<NetworkID, Route> {
return this.routingTable; return routingTable;
} }
} }