From 33731778ea9be5b5206e710dfd12c153a5581c94 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Tue, 22 Feb 2022 02:28:53 +0100 Subject: [PATCH] improved net --- src/kernel/net/Net.hx | 49 ++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/src/kernel/net/Net.hx b/src/kernel/net/Net.hx index bf781b2..79e800e 100644 --- a/src/kernel/net/Net.hx +++ b/src/kernel/net/Net.hx @@ -8,7 +8,6 @@ import kernel.KernelEvents; import haxe.Exception; import util.Promise; import kernel.Timer; -import util.EventBus; import cc.OS; using Lambda; @@ -20,8 +19,16 @@ using util.Extender.LambdaExtender; **/ class Net { public static var instance:Net; + public static inline final BRODCAST_PORT:Int = 65533; + public static inline final MESSAGE_TIMEOUT:Int = 3; public final onNewNeigbor: Signal; + + private final networkID:Int = OS.getComputerID(); + private var responseBus:Map> = new Map(); + private var protoHandlers:Map> = new Map(); + private var allModems:Array; + private var routingTable:Map = new Map(); private final onNewNeigborTrigger: SignalTrigger = Signal.trigger(); @:allow(kernel.Init) @@ -40,19 +47,11 @@ class Net { }); allModems = Peripheral.instance.getModems(); + open(); discoverNeighbors(); } - public static inline final BRODCAST_PORT:Int = 65533; - public static inline final MESSAGE_TIMEOUT:Int = 3; - - private var networkID:Int = OS.getComputerID(); - private var responseBus:util.EventBus = new EventBus(); - private var protoHandlers:MapVoid> = new Map(); - private var allModems:Array; - private var routingTable:Map = new Map(); - private function handelIncomming(pack:Package, ?addr:String) { switch pack.type { case Data(_): @@ -60,7 +59,9 @@ class Net { case DataNoResponse(_): routeTo(pack); case Response | RouteDiscoverResponse: - responseBus.emit(Std.string(pack.msgID), pack); + if (responseBus.exists(pack.msgID)){ + responseBus[pack.msgID].invoke(pack); + } case RouteDiscover: handleRoute(pack, addr); } @@ -83,12 +84,13 @@ class Net { var pack = newRoutPackage(); var timeout:Timer = null; - var responeListner = responseBus.on(Std.string(pack.msgID), pack -> { - addRoute(pack.fromID, modem.addr); + + responseBus[pack.msgID] = ((response:Package)->{ + addRoute(response.fromID, modem.addr); }); timeout = new Timer(MESSAGE_TIMEOUT, () -> { - responseBus.removeListner(responeListner); + responseBus.remove(pack.msgID); }); modem.transmit(BRODCAST_PORT, OS.getComputerID(), pack); @@ -114,8 +116,8 @@ class Net { private function addRoute(toID:Int, addr:String) { Log.debug("Added new route to " + toID + " via " + addr); - this.onNewNeigborTrigger.trigger(toID); routingTable.set(toID, allModems.find(item -> item.addr == addr)); + this.onNewNeigborTrigger.trigger(toID); } public function getAllNeighbors():Array { @@ -176,7 +178,7 @@ class Net { return; } - protoHandlers[proto](pack); + protoHandlers[proto].invoke(pack); } private function sendRaw(pack:Package) { @@ -204,15 +206,16 @@ class Net { } var timeout:Timer = null; - var responeListner = responseBus.once(Std.string(pack.msgID), p -> { - resolve(p); + + responseBus[pack.msgID] = ((reponse: Package) ->{ + resolve(reponse); if (timeout != null) { timeout.cancle(); } }); timeout = new Timer(MESSAGE_TIMEOUT, () -> { - responseBus.removeListner(responeListner); + responseBus.remove(pack.msgID); reject(new Exception("Timeout")); }); @@ -220,17 +223,21 @@ class Net { }); } - public function registerProto(proto:String, cb:Package->Void) { + public function registerProto(proto:String, callback:Callback) { if (protoHandlers.exists(proto)) { // Failed. Handler already exist. // TODO: return error return; } - protoHandlers[proto] = cb; + protoHandlers[proto] = callback; } public function removeProto(proto:String) { protoHandlers.remove(proto); } + + public function getNeigbors():Array { + return this.routingTable.mapi((index, item) -> {return index;}); + } }