made Net a static class
This commit is contained in:
parent
505d318ffb
commit
97c906e013
@ -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<MyType> = cast pack; // Try not to use Dynamic
|
||||
|
||||
requestPack.respond("Hello Back");
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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");
|
||||
|
@ -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(){
|
||||
|
@ -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) {
|
||||
|
@ -35,7 +35,7 @@ class Init {
|
||||
}
|
||||
|
||||
Routing.init();
|
||||
Net.instance = new Net();
|
||||
Net.init();
|
||||
|
||||
GPS.instance = new GPS();
|
||||
INS.instance = new INS();
|
||||
|
@ -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
|
||||
|
||||
|
@ -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<Int, Callback<Outcome<GenericPackage,Error>>> = new Map();
|
||||
private final protoHandlers:Map<String, Callback<GenericPackage>> = new Map();
|
||||
private var interfaces:Array<INetworkInterface>;
|
||||
public static final networkID:NetworkID = OS.getComputerID();
|
||||
private static final responseBus:Map<Int, Callback<Outcome<GenericPackage,Error>>> = new Map();
|
||||
private static final protoHandlers:Map<String, Callback<GenericPackage>> = new Map();
|
||||
private static var interfaces:Array<INetworkInterface>;
|
||||
|
||||
@: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<T>(dest:NetworkID, proto:String, data:T):Promise<GenericPackage> {
|
||||
public static function sendAndAwait<T>(dest:NetworkID, proto:String, data:T):Promise<GenericPackage> {
|
||||
return new Promise<GenericPackage>((resolve, reject) -> {
|
||||
var pack:GenericPackage = {
|
||||
toID: dest,
|
||||
@ -230,7 +229,7 @@ class Net {
|
||||
});
|
||||
}
|
||||
|
||||
public function registerProto(proto:String, callback:Callback<GenericPackage>) {
|
||||
public static function registerProto(proto:String, callback:Callback<GenericPackage>) {
|
||||
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<Noise> {
|
||||
public static function ping(toID: NetworkID): Promise<Noise> {
|
||||
return new Promise<Noise>((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<String> {
|
||||
public static function getActiveProtocols(): ReadOnlyArray<String> {
|
||||
var arr = new Array<String>();
|
||||
|
||||
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<Noise> = {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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<Noise> = {
|
||||
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<Route> = routingTable[networkID];
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -15,7 +15,7 @@ class Import {
|
||||
}
|
||||
|
||||
private static function performRequest(netID: NetworkID, request: Request): Promise<Response> {
|
||||
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));
|
||||
|
@ -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}
|
||||
});
|
||||
};
|
||||
|
@ -14,7 +14,7 @@ abstract class RPCBase {
|
||||
}
|
||||
|
||||
private function _performRequest(func: String, args: Array<Dynamic>):Promise<Dynamic> {
|
||||
return Net.instance.sendAndAwait(id, this._proto, {
|
||||
return Net.sendAndAwait(id, this._proto, {
|
||||
func: func,
|
||||
// args: args
|
||||
}).map((res) -> {
|
||||
|
Loading…
Reference in New Issue
Block a user