improved net

This commit is contained in:
Djeeberjr 2022-02-22 02:28:53 +01:00
parent 418952bd25
commit 33731778ea

View File

@ -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<Int>;
private final networkID:Int = OS.getComputerID();
private var responseBus:Map<Int,Callback<Package>> = new Map();
private var protoHandlers:Map<String, Callback<Package>> = new Map();
private var allModems:Array<kernel.peripherals.Modem>;
private var routingTable:Map<Int, kernel.peripherals.Modem> = new Map();
private final onNewNeigborTrigger: SignalTrigger<Int> = 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<Package> = new EventBus();
private var protoHandlers:Map<String, Package->Void> = new Map();
private var allModems:Array<kernel.peripherals.Modem>;
private var routingTable:Map<Int, kernel.peripherals.Modem> = 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<Int> {
@ -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<Package>) {
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<Int> {
return this.routingTable.mapi((index, item) -> {return index;});
}
}