added more icmp stuff

This commit is contained in:
Djeeberjr 2022-12-19 22:33:17 +01:00
parent f988c79e2f
commit 0b965d32b6

View File

@ -25,7 +25,7 @@ class Net {
public static inline final DEFAULT_TTL:Int = 10; public static inline final DEFAULT_TTL:Int = 10;
public final networkID:NetworkID = OS.getComputerID(); public final networkID:NetworkID = OS.getComputerID();
private final responseBus:Map<Int, Callback<Package>> = new Map(); private final responseBus:Map<Int, Callback<Outcome<Package,Error>>> = new Map();
private final protoHandlers:Map<String, Callback<Package>> = new Map(); private final protoHandlers:Map<String, Callback<Package>> = new Map();
private var interfaces:Array<INetworkInterface>; private var interfaces:Array<INetworkInterface>;
@ -42,8 +42,20 @@ class Net {
} }
private function setupPingHandle() { private function setupPingHandle() {
this.registerProto('ping', pack -> { this.registerProto("icmp", pack -> {
this.respondTo(pack, pack.data); switch pack.data.type {
case "ping":
this.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
var msgID:Int = pack.data.msgID;
if (responseBus.exists(msgID)) {
responseBus[msgID].invoke(Outcome.Failure(new Error("TTL reached 0")));
}
default:
Log.silly('Unknown icmp message: ${pack.data}');
}
}); });
} }
@ -65,7 +77,7 @@ class Net {
case Response: case Response:
// Got a response to a send message. Invoke the callback // Got a response to a send message. Invoke the callback
if (responseBus.exists(pack.msgID)) { if (responseBus.exists(pack.msgID)) {
responseBus[pack.msgID].invoke(pack); responseBus[pack.msgID].invoke(Outcome.Success(pack));
} }
case RouteDiscover(_) | RouteDiscoverResponse(_) | RouteDiscoverUpdate(_): case RouteDiscover(_) | RouteDiscoverResponse(_) | RouteDiscoverUpdate(_):
// Delegate to Routing // Delegate to Routing
@ -99,6 +111,13 @@ class Net {
private function forwardPackage(pack: Package) { private function forwardPackage(pack: Package) {
if (pack.ttl == 0){ if (pack.ttl == 0){
if (pack.type.match(Data(_))) {
// If the package is a data package and the ttl hits 0
// we send a "died" message to the sender
sendAndForget(pack.fromID, "icmp", {type:"died", msgID: pack.msgID});
}
// Drop package // Drop package
return; return;
} }
@ -172,8 +191,16 @@ class Net {
var timeout:Timer = null; var timeout:Timer = null;
responseBus[pack.msgID] = ((reponse:Package) -> { responseBus[pack.msgID] = ((reponse:Outcome<Package,Error>) -> {
resolve(reponse);
switch reponse {
case Success(pack):
resolve(pack);
case Failure(err):
reject(err);
}
// Always remove the timeout
if (timeout != null) { if (timeout != null) {
timeout.cancle(); timeout.cancle();
} }
@ -210,14 +237,14 @@ class Net {
/** /**
Sends a ping package to the given id. Returns true if there was a response. Sends a ping package to the given id. Returns true if there was a response.
**/ **/
public function ping(toID: NetworkID): Promise<Bool> { public function ping(toID: NetworkID): Promise<Noise> {
return new Promise<Bool>((resolve,reject)->{ return new Promise<Noise>((resolve,reject)->{
this.sendAndAwait(toID,"ping",null).handle(pack -> { this.sendAndAwait(toID,"icmp",{type:"ping"}).handle(pack -> {
switch pack { switch pack {
case Success(_): case Success(_):
resolve(true); resolve(Noise);
case Failure(err): case Failure(err):
resolve(false); reject(err);
} }
}); });
return null; return null;