diff --git a/doc/Concepts.md b/doc/Concepts.md index 4bdaf1c..97a47f8 100644 --- a/doc/Concepts.md +++ b/doc/Concepts.md @@ -26,7 +26,7 @@ import net.Net; var data = {"foo": "bar"}; -Net.instance.sendAndAwait(netID,"protoname",data).map((response)->{ +Net.sendAndAwait(netID,"protoname",data).map((response)->{ switch (response){ case Success(data): trace(data); @@ -35,7 +35,7 @@ Net.instance.sendAndAwait(netID,"protoname",data).map((response)->{ } }); -Net.instance.registerProto("res",(pack: GenericPackage)->{ +Net.registerProto("res",(pack: GenericPackage)->{ var requestPack: Package = cast pack; // Try not to use Dynamic requestPack.respond("Hello Back"); diff --git a/src/bin/ID.hx b/src/bin/ID.hx index c81df48..380e9f9 100644 --- a/src/bin/ID.hx +++ b/src/bin/ID.hx @@ -7,7 +7,7 @@ class ID implements Process { public function new() {} public function run(handle:ProcessHandle) { - handle.writeLine("ID: " + kernel.net.Net.instance.networkID); + handle.writeLine("ID: " + kernel.net.Net.networkID); handle.close(); } } diff --git a/src/bin/Net.hx b/src/bin/Net.hx index 49dbd87..a5616bf 100644 --- a/src/bin/Net.hx +++ b/src/bin/Net.hx @@ -29,7 +29,7 @@ class Net extends CLIAppBase { }); registerSyncSubcommand("proto",(args)->{ - var protos = kernel.net.Net.instance.getActiveProtocols(); + var protos = kernel.net.Net.getActiveProtocols(); for (proto in protos) { handle.writeLine(proto); @@ -50,7 +50,7 @@ class Net extends CLIAppBase { return Future.sync(false); } - return kernel.net.Net.instance.ping(toID).map(result -> { + return kernel.net.Net.ping(toID).map(result -> { switch (result){ case Success(_): handle.write("Ping succeeded"); diff --git a/src/bin/exporter/ResManager.hx b/src/bin/exporter/ResManager.hx index 1855bf1..e73b8c7 100644 --- a/src/bin/exporter/ResManager.hx +++ b/src/bin/exporter/ResManager.hx @@ -21,7 +21,7 @@ class ResManager implements Process { public function run(handle:ProcessHandle) { this.handle = handle; - Net.instance.registerProto("res",handlePackage); + Net.registerProto("res",handlePackage); load(); } @@ -53,7 +53,7 @@ class ResManager implements Process { } private function registerName(id: String){ - return RessourceNames.register(id, Net.instance.networkID); + return RessourceNames.register(id, Net.networkID); } private function persist(){ diff --git a/src/bin/srsc/SiteRessourceController.hx b/src/bin/srsc/SiteRessourceController.hx index c42eb97..d233f64 100644 --- a/src/bin/srsc/SiteRessourceController.hx +++ b/src/bin/srsc/SiteRessourceController.hx @@ -25,7 +25,7 @@ class SiteRessourceController implements Process { load(); // Register proto - kernel.net.Net.instance.registerProto(SITE_CONTROLLER_RESSOURCE_MANAGER_PROTO, this.handleMsg); + kernel.net.Net.registerProto(SITE_CONTROLLER_RESSOURCE_MANAGER_PROTO, this.handleMsg); } private function handleMsg(pkg:GenericPackage) { diff --git a/src/kernel/Init.hx b/src/kernel/Init.hx index 2c3f138..39d3c28 100644 --- a/src/kernel/Init.hx +++ b/src/kernel/Init.hx @@ -35,7 +35,7 @@ class Init { } Routing.init(); - Net.instance = new Net(); + Net.init(); GPS.instance = new GPS(); INS.instance = new INS(); diff --git a/src/kernel/gps/GPS.hx b/src/kernel/gps/GPS.hx index 16543da..492ca1c 100644 --- a/src/kernel/gps/GPS.hx +++ b/src/kernel/gps/GPS.hx @@ -120,7 +120,7 @@ class GPS { } private function sendPositionRequest() { - Net.instance.brodcastGPSRequest(); + Net.brodcastGPSRequest(); } @:allow(kernel.net.Net) @@ -131,8 +131,8 @@ class GPS { if (posAccuracy < 2) return; if (cachedPosition == null) return; - var response = new Package(Net.instance.networkID,pack.fromID, pack.msgID, GPSResponse(cachedPosition),null,0); - iface.send(pack.fromID,Net.instance.networkID,response); + var response = new Package(Net.networkID,pack.fromID, pack.msgID, GPSResponse(cachedPosition),null,0); + iface.send(pack.fromID,Net.networkID,response); case GPSResponse(pos): if (lastPositionResponse.contains({pos:pos,dist:dist})) return; // Ignore duplicate responses diff --git a/src/kernel/net/Net.hx b/src/kernel/net/Net.hx index 3297040..7f33714 100644 --- a/src/kernel/net/Net.hx +++ b/src/kernel/net/Net.hx @@ -20,20 +20,19 @@ class Net { /** Depends on: KernelEvents **/ - public static var instance:Net; public static inline final BRODCAST_PORT:Int = 65533; public static inline final MESSAGE_TIMEOUT:Int = 3; public static inline final DEFAULT_TTL:Int = 10; - public final networkID:NetworkID = OS.getComputerID(); - private final responseBus:Map>> = new Map(); - private final protoHandlers:Map> = new Map(); - private var interfaces:Array; + public static final networkID:NetworkID = OS.getComputerID(); + private static final responseBus:Map>> = new Map(); + private static final protoHandlers:Map> = new Map(); + private static var interfaces:Array; @:allow(kernel.Init) - private function new() { - this.interfaces = [for (e in Peripheral.getAllModems()) e ]; // TODO: is this the way to do it? - this.interfaces.push(Loopback.instance); + private static function init() { + interfaces = [for (e in Peripheral.getAllModems()) e ]; // TODO: is this the way to do it? + interfaces.push(Loopback.instance); for (interf in interfaces){ setupInterf(interf); @@ -44,11 +43,11 @@ class Net { Routing.setup(); } - private function setupPingHandle() { - this.registerProto("icmp", pack -> { + private static function setupPingHandle() { + registerProto("icmp", pack -> { switch pack.data.type { case "ping": - this.respondTo(pack, "pong"); + respondTo(pack, "pong"); case "died": // If we get a "died" message from a node when one of our packages ttl hits 0 // the `data.msgId` prop is the message id @@ -62,7 +61,7 @@ class Net { }); } - private function setupInterf(interf: INetworkInterface) { + private static function setupInterf(interf: INetworkInterface) { interf.onMessage.handle(e -> handle(e.pack,interf,e.dist)); interf.listen(networkID); interf.listen(BRODCAST_PORT); @@ -71,8 +70,8 @@ class Net { /** Called when a new package comes in. **/ - private function handle(pack:GenericPackage,interf: INetworkInterface, ?dist: Float) { - if (pack.toID == this.networkID || pack.toID == Net.BRODCAST_PORT){ + private static function handle(pack:GenericPackage,interf: INetworkInterface, ?dist: Float) { + if (pack.toID == networkID || pack.toID == Net.BRODCAST_PORT){ switch pack.type { case Data(_) | DataNoResponse(_): // Let a local proccess handle it @@ -99,14 +98,14 @@ class Net { } } - private function generateMessageID():Int { + private static function generateMessageID():Int { return Std.random(2147483647); // TODO: better uniqe number } /** Send a message. Dont care if its reaches its destination nor it has a response. **/ - public function sendAndForget(dest:Int, proto:String, data:Dynamic) { + public static function sendAndForget(dest:Int, proto:String, data:Dynamic) { var pack:GenericPackage = { toID: dest, fromID: networkID, @@ -119,7 +118,7 @@ class Net { sendRaw(pack); } - private function forwardPackage(pack: GenericPackage) { + private static function forwardPackage(pack: GenericPackage) { if (pack.ttl == 0){ if (pack.type.match(Data(_))) { @@ -139,7 +138,7 @@ class Net { } } - public function respondTo(pack:GenericPackage, data:Dynamic) { + public static function respondTo(pack:GenericPackage, data:Dynamic) { if (pack.type.match(DataNoResponse(_))) { Log.warn("Responed to a no response package. Ignoring"); return; @@ -153,7 +152,7 @@ class Net { /** Send to package to the localy register handler based on the proto **/ - private function routeToProto(pack:GenericPackage) { + private static function routeToProto(pack:GenericPackage) { var proto = switch pack.type { case Data(proto): proto; @@ -175,20 +174,20 @@ class Net { Just send the package to the right modem. Returns true if message was send **/ - private function sendRaw(pack:GenericPackage): Bool { + private static function sendRaw(pack:GenericPackage): Bool { var route = Routing.getRouteToID(pack.toID); if (route == null){ return false; } - route.interf.send(route.rep,this.networkID,pack); + route.interf.send(route.rep,networkID,pack); return true; } /** Send a message and wait for a response. **/ - public function sendAndAwait(dest:NetworkID, proto:String, data:T):Promise { + public static function sendAndAwait(dest:NetworkID, proto:String, data:T):Promise { return new Promise((resolve, reject) -> { var pack:GenericPackage = { toID: dest, @@ -230,7 +229,7 @@ class Net { }); } - public function registerProto(proto:String, callback:Callback) { + public static function registerProto(proto:String, callback:Callback) { if (protoHandlers.exists(proto)) { // Failed. Handler already exist. // TODO: return error @@ -240,16 +239,16 @@ class Net { protoHandlers[proto] = callback; } - public function removeProto(proto:String) { + public static function removeProto(proto:String) { protoHandlers.remove(proto); } /** Sends a ping package to the given id. Returns true if there was a response. **/ - public function ping(toID: NetworkID): Promise { + public static function ping(toID: NetworkID): Promise { return new Promise((resolve,reject)->{ - this.sendAndAwait(toID,"icmp",{type:"ping"}).handle(pack -> { + sendAndAwait(toID,"icmp",{type:"ping"}).handle(pack -> { switch pack { case Success(_): resolve(Noise); @@ -261,7 +260,7 @@ class Net { }); } - public function getActiveProtocols(): ReadOnlyArray { + public static function getActiveProtocols(): ReadOnlyArray { var arr = new Array(); for (proto in protoHandlers.keys()) { @@ -272,7 +271,7 @@ class Net { } @:allow(kernel.gps.GPS) - private function brodcastGPSRequest() { + private static function brodcastGPSRequest() { var pack: Package = { fromID: networkID, toID: Net.BRODCAST_PORT, @@ -284,7 +283,7 @@ class Net { for (modem in Peripheral.getAllModems()) { if (!modem.isWireless()) continue; - modem.send(Net.BRODCAST_PORT, Net.instance.networkID, pack); + modem.send(Net.BRODCAST_PORT, networkID, pack); } } } diff --git a/src/kernel/net/Package.hx b/src/kernel/net/Package.hx index b80707b..6f299fc 100644 --- a/src/kernel/net/Package.hx +++ b/src/kernel/net/Package.hx @@ -51,9 +51,9 @@ enum PackageTypes { } /** - Wrapper for `Net.instance.respondTo`. + Wrapper for `Net.respondTo`. **/ public function respond(data:Dynamic) { - Net.instance.respondTo(this, data); + Net.respondTo(this, data); } } diff --git a/src/kernel/net/Routing.hx b/src/kernel/net/Routing.hx index d386ea6..f8c4015 100644 --- a/src/kernel/net/Routing.hx +++ b/src/kernel/net/Routing.hx @@ -36,7 +36,7 @@ class Routing { @:allow(kernel.net.Net) private static function setup() { - routingTable.set(Net.instance.networkID, {interf: Loopback.instance, cost: 0, rep: Net.instance.networkID}); + routingTable.set(Net.networkID, {interf: Loopback.instance, cost: 0, rep: Net.networkID}); brodcastRoutingTable(); } @@ -64,7 +64,7 @@ class Routing { var pack = newRoutDiscoverPackage(); for (modem in Peripheral.getAllModems()) { - modem.send(Net.BRODCAST_PORT, Net.instance.networkID, pack); + modem.send(Net.BRODCAST_PORT, Net.networkID, pack); } } @@ -103,14 +103,14 @@ class Routing { // Respond to peer var response:Package = { toID: pack.fromID, - fromID: Net.instance.networkID, + fromID: Net.networkID, msgID: null, type: RouteDiscoverResponse(genRouteList()), data: null, ttl: 0, // Prevent forwarding } - interf.send(response.toID, Net.instance.networkID, response); + interf.send(response.toID, Net.networkID, response); } /** @@ -132,7 +132,7 @@ class Routing { type: RouteDiscover(genRouteList()), toID: Net.BRODCAST_PORT, msgID: null, - fromID: Net.instance.networkID, + fromID: Net.networkID, data: null, ttl: 0, // Prevent forwarding } @@ -145,7 +145,7 @@ class Routing { Its possible to be called multiple times with the same id but different addr. **/ private static function addPossibleRoute(toID:Int, interf:INetworkInterface, cost:Int, rep:NetworkID) { - if (toID == Net.instance.networkID) { + if (toID == Net.networkID) { return; } @@ -164,8 +164,8 @@ class Routing { } public static function getRouteToID(networkID:NetworkID):{interf:INetworkInterface, rep:NetworkID} { - if (networkID == Net.instance.networkID) { - return {interf: Loopback.instance, rep: Net.instance.networkID}; + if (networkID == Net.networkID) { + return {interf: Loopback.instance, rep: Net.networkID}; } var route:Null = routingTable[networkID]; diff --git a/src/lib/Debug.hx b/src/lib/Debug.hx index aa3c39f..6d8068b 100644 --- a/src/lib/Debug.hx +++ b/src/lib/Debug.hx @@ -45,7 +45,7 @@ class Debug { #if webconsole public static function printWeb(msg:String) { - HTTP.request("http://127.0.0.1:8080/"+Net.instance.networkID,msg); + HTTP.request("http://127.0.0.1:8080/"+Net.networkID,msg); } #end } diff --git a/src/lib/RessourceNames.hx b/src/lib/RessourceNames.hx index 74128d4..7a3be90 100644 --- a/src/lib/RessourceNames.hx +++ b/src/lib/RessourceNames.hx @@ -17,7 +17,7 @@ class RessourceNames { var payload: GetRequest = {name: name, type: "get"}; - return Net.instance.sendAndAwait( + return Net.sendAndAwait( controllerID, SiteRessourceController.SITE_CONTROLLER_RESSOURCE_MANAGER_PROTO, payload @@ -36,7 +36,7 @@ class RessourceNames { var payload: RegisterRequest = {name: name, netID: netID, type: "register"}; - return Net.instance.sendAndAwait( + return Net.sendAndAwait( controllerID, SiteRessourceController.SITE_CONTROLLER_RESSOURCE_MANAGER_PROTO, payload @@ -56,7 +56,7 @@ class RessourceNames { var payload: UnregisterRequest = {name: name, type: "unregister"}; - return Net.instance.sendAndAwait( + return Net.sendAndAwait( controllerID, SiteRessourceController.SITE_CONTROLLER_RESSOURCE_MANAGER_PROTO, payload @@ -68,7 +68,7 @@ class RessourceNames { var payload: ListRequest = {type: "list"}; - return Net.instance.sendAndAwait( + return Net.sendAndAwait( controllerID, SiteRessourceController.SITE_CONTROLLER_RESSOURCE_MANAGER_PROTO, payload diff --git a/src/lib/exporter/Import.hx b/src/lib/exporter/Import.hx index 4fb6bf1..dbf7910 100644 --- a/src/lib/exporter/Import.hx +++ b/src/lib/exporter/Import.hx @@ -15,7 +15,7 @@ class Import { } private static function performRequest(netID: NetworkID, request: Request): Promise { - return Net.instance.sendAndAwait(netID,"res",request).map((response)->{ + return Net.sendAndAwait(netID,"res",request).map((response)->{ switch (response){ case Success(data): return Success(cast (data.data, Response)); diff --git a/src/macros/rpc/RPC.hx b/src/macros/rpc/RPC.hx index 7d4430c..7ca366e 100644 --- a/src/macros/rpc/RPC.hx +++ b/src/macros/rpc/RPC.hx @@ -71,7 +71,7 @@ class RPC { } return macro { - kernel.net.Net.instance.registerProto($v{proto},(pack)->{ + kernel.net.Net.registerProto($v{proto},(pack)->{ $a{exprs} }); }; diff --git a/src/macros/rpc/RPCBase.hx b/src/macros/rpc/RPCBase.hx index c4d4a14..b9a3301 100644 --- a/src/macros/rpc/RPCBase.hx +++ b/src/macros/rpc/RPCBase.hx @@ -14,7 +14,7 @@ abstract class RPCBase { } private function _performRequest(func: String, args: Array):Promise { - return Net.instance.sendAndAwait(id, this._proto, { + return Net.sendAndAwait(id, this._proto, { func: func, // args: args }).map((res) -> {