From b305594ea40d1eb8a2868d98b8a70b93f28dc082 Mon Sep 17 00:00:00 2001 From: Niklas Kapelle Date: Tue, 15 Oct 2024 01:19:36 +0200 Subject: [PATCH] improved networking --- src/kernel/net/Net.hx | 39 +++++++++++++++++++++-- src/kernel/net/Routing.hx | 66 ++++++++++++++++++++++++++------------- 2 files changed, 81 insertions(+), 24 deletions(-) diff --git a/src/kernel/net/Net.hx b/src/kernel/net/Net.hx index 18e7fed..ccd888c 100644 --- a/src/kernel/net/Net.hx +++ b/src/kernel/net/Net.hx @@ -39,6 +39,35 @@ class Net { setupInterf(interf); } + // Handle when a modem has been removed + KernelEvents.onPeripheralDetach.handle((addr) -> { + var idx = interfaces.findIndex((i) -> { + return i.name() == addr; + }); + if (idx == -1) { + return; + } + + Log.debug('Removed modem on addr: $addr'); + + interfaces.splice(idx, 1); + Routing.removeInterface(addr); + }); + + // Handle when a new modem is connected + KernelEvents.onPeripheral.handle((addr) -> { + if (!Peripheral.getTypes(addr).contains("modem")) { + return; + } + + Log.debug('Added modem on addr: $addr'); + + var modem = Peripheral.getModem(addr); + interfaces.push(modem); + setupInterf(modem); + Routing.addInterface(modem); + }); + setupPingHandle(); Routing.setup(); @@ -62,6 +91,9 @@ class Net { }); } + /** + Enables the interface to receive messages. + **/ private static function setupInterf(interf:INetworkInterface) { interf.onMessage.handle(e -> handle(e.pack, interf, e.dist)); interf.listen(networkID); @@ -135,12 +167,13 @@ class Net { if (!sendRaw(pack)) { // Cant forward + Log.silly('Received a message that could not be forwarded to ${pack.toID}'); } } public static function respondTo(pack:GenericPackage, data:Dynamic) { - if (pack.type.match(DataNoResponse(_))) { - Log.warn("Responed to a no response package. Ignoring"); + if (!pack.type.match(Data(_))) { + Log.warn("Responding to a package that does require responding. Ignoring."); return; } @@ -212,6 +245,8 @@ class Net { if (timeout != null) { timeout.cancle(); } + + responseBus.remove(pack.msgID); }); timeout = new Timer(MESSAGE_TIMEOUT, () -> { diff --git a/src/kernel/net/Routing.hx b/src/kernel/net/Routing.hx index be0cfc3..4939543 100644 --- a/src/kernel/net/Routing.hx +++ b/src/kernel/net/Routing.hx @@ -74,42 +74,37 @@ class Routing { private static function handleRoutePackage(pack:Package, interf:INetworkInterface):Void { addPossibleRoute(pack.fromID, interf, 0, pack.fromID); - var shouldRespond:Bool = switch pack.type { + switch pack.type { case RouteDiscoverResponse(routes): + // A request for routes has been answerd for (route in routes) { addPossibleRoute(route.id, interf, route.cost, pack.fromID); } - false; case RouteDiscover(routes): + // Received a request for available routes for (route in routes) { addPossibleRoute(route.id, interf, route.cost, pack.fromID); } - true; + + // Respond to peer + var response:Package = { + toID: pack.fromID, + fromID: Net.networkID, + msgID: null, + type: RouteDiscoverResponse(genRouteList()), + data: null, + ttl: 0, // Prevent forwarding + } + + interf.send(response.toID, Net.networkID, response); case RouteDiscoverUpdate(routes): + // Received an update of routes for (route in routes) { addPossibleRoute(route.id, interf, route.cost, pack.fromID); } - false; default: - Log.error("Expected package to be a Route discover package"); - false; + Log.silly("Expected package to be a Route discover package"); }; - - if (!shouldRespond) { - return; - } - - // Respond to peer - var response:Package = { - toID: pack.fromID, - fromID: Net.networkID, - msgID: null, - type: RouteDiscoverResponse(genRouteList()), - data: null, - ttl: 0, // Prevent forwarding - } - - interf.send(response.toID, Net.networkID, response); } /** @@ -126,6 +121,9 @@ class Routing { return routes; } + /** + Create a new packate to brodcast our routes. + **/ private static function newRoutDiscoverPackage():Package { var pack:Package = { type: RouteDiscover(genRouteList()), @@ -179,4 +177,28 @@ class Routing { public static function getRouteTable():Map { return routingTable; } + + /** + Call when an interface is no longer available. + Will rebrodcast routing requests. + **/ + @:allow(kernel.net.Net) + private static function removeInterface(addr:String) { + // Remove the interface from the routing table + for (to => modem in routingTable) { + if (modem.interf.name() == addr) { + routingTable.remove(to); + } + } + + // Since some routes to some host could be lost + // we rebrodcast on all interfaces again + brodcastRoutingTable(); + } + + @:allow(kernel.net.Net) + private static function addInterface(iface:INetworkInterface) { + var pack = newRoutDiscoverPackage(); + iface.send(Net.BRODCAST_PORT, Net.networkID, pack); + } }