From 0b965d32b6b834f09456fc7e80299bffc98e3d1c Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Mon, 19 Dec 2022 22:33:17 +0100 Subject: [PATCH] added more icmp stuff --- src/kernel/net/Net.hx | 49 +++++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/src/kernel/net/Net.hx b/src/kernel/net/Net.hx index 23aacbe..51ab922 100644 --- a/src/kernel/net/Net.hx +++ b/src/kernel/net/Net.hx @@ -25,7 +25,7 @@ class Net { public static inline final DEFAULT_TTL:Int = 10; public final networkID:NetworkID = OS.getComputerID(); - private final responseBus:Map> = new Map(); + private final responseBus:Map>> = new Map(); private final protoHandlers:Map> = new Map(); private var interfaces:Array; @@ -42,8 +42,20 @@ class Net { } private function setupPingHandle() { - this.registerProto('ping', pack -> { - this.respondTo(pack, pack.data); + this.registerProto("icmp", pack -> { + 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: // Got a response to a send message. Invoke the callback if (responseBus.exists(pack.msgID)) { - responseBus[pack.msgID].invoke(pack); + responseBus[pack.msgID].invoke(Outcome.Success(pack)); } case RouteDiscover(_) | RouteDiscoverResponse(_) | RouteDiscoverUpdate(_): // Delegate to Routing @@ -99,6 +111,13 @@ class Net { private function forwardPackage(pack: Package) { 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 return; } @@ -172,8 +191,16 @@ class Net { var timeout:Timer = null; - responseBus[pack.msgID] = ((reponse:Package) -> { - resolve(reponse); + responseBus[pack.msgID] = ((reponse:Outcome) -> { + + switch reponse { + case Success(pack): + resolve(pack); + case Failure(err): + reject(err); + } + + // Always remove the timeout if (timeout != null) { timeout.cancle(); } @@ -210,14 +237,14 @@ class Net { /** Sends a ping package to the given id. Returns true if there was a response. **/ - public function ping(toID: NetworkID): Promise { - return new Promise((resolve,reject)->{ - this.sendAndAwait(toID,"ping",null).handle(pack -> { + public function ping(toID: NetworkID): Promise { + return new Promise((resolve,reject)->{ + this.sendAndAwait(toID,"icmp",{type:"ping"}).handle(pack -> { switch pack { case Success(_): - resolve(true); + resolve(Noise); case Failure(err): - resolve(false); + reject(err); } }); return null;