improved networking

This commit is contained in:
Niklas Kapelle 2024-10-15 01:19:36 +02:00
parent df7991763d
commit b305594ea4
Signed by: niklas
GPG Key ID: 4EB651B36D841D16
2 changed files with 81 additions and 24 deletions

View File

@ -39,6 +39,35 @@ class Net {
setupInterf(interf); 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(); setupPingHandle();
Routing.setup(); Routing.setup();
@ -62,6 +91,9 @@ class Net {
}); });
} }
/**
Enables the interface to receive messages.
**/
private static function setupInterf(interf:INetworkInterface) { private static function setupInterf(interf:INetworkInterface) {
interf.onMessage.handle(e -> handle(e.pack, interf, e.dist)); interf.onMessage.handle(e -> handle(e.pack, interf, e.dist));
interf.listen(networkID); interf.listen(networkID);
@ -135,12 +167,13 @@ class Net {
if (!sendRaw(pack)) { if (!sendRaw(pack)) {
// Cant forward // Cant forward
Log.silly('Received a message that could not be forwarded to ${pack.toID}');
} }
} }
public static function respondTo(pack:GenericPackage, data:Dynamic) { public static function respondTo(pack:GenericPackage, data:Dynamic) {
if (pack.type.match(DataNoResponse(_))) { if (!pack.type.match(Data(_))) {
Log.warn("Responed to a no response package. Ignoring"); Log.warn("Responding to a package that does require responding. Ignoring.");
return; return;
} }
@ -212,6 +245,8 @@ class Net {
if (timeout != null) { if (timeout != null) {
timeout.cancle(); timeout.cancle();
} }
responseBus.remove(pack.msgID);
}); });
timeout = new Timer(MESSAGE_TIMEOUT, () -> { timeout = new Timer(MESSAGE_TIMEOUT, () -> {

View File

@ -74,42 +74,37 @@ class Routing {
private static function handleRoutePackage(pack:Package<Noise>, interf:INetworkInterface):Void { private static function handleRoutePackage(pack:Package<Noise>, interf:INetworkInterface):Void {
addPossibleRoute(pack.fromID, interf, 0, pack.fromID); addPossibleRoute(pack.fromID, interf, 0, pack.fromID);
var shouldRespond:Bool = switch pack.type { switch pack.type {
case RouteDiscoverResponse(routes): case RouteDiscoverResponse(routes):
// A request for routes has been answerd
for (route in routes) { for (route in routes) {
addPossibleRoute(route.id, interf, route.cost, pack.fromID); addPossibleRoute(route.id, interf, route.cost, pack.fromID);
} }
false;
case RouteDiscover(routes): case RouteDiscover(routes):
// Received a request for available routes
for (route in routes) { for (route in routes) {
addPossibleRoute(route.id, interf, route.cost, pack.fromID); addPossibleRoute(route.id, interf, route.cost, pack.fromID);
} }
true;
// Respond to peer
var response:Package<Noise> = {
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): case RouteDiscoverUpdate(routes):
// Received an update of routes
for (route in routes) { for (route in routes) {
addPossibleRoute(route.id, interf, route.cost, pack.fromID); addPossibleRoute(route.id, interf, route.cost, pack.fromID);
} }
false;
default: default:
Log.error("Expected package to be a Route discover package"); Log.silly("Expected package to be a Route discover package");
false;
}; };
if (!shouldRespond) {
return;
}
// Respond to peer
var response:Package<Noise> = {
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; return routes;
} }
/**
Create a new packate to brodcast our routes.
**/
private static function newRoutDiscoverPackage():Package<Noise> { private static function newRoutDiscoverPackage():Package<Noise> {
var pack:Package<Noise> = { var pack:Package<Noise> = {
type: RouteDiscover(genRouteList()), type: RouteDiscover(genRouteList()),
@ -179,4 +177,28 @@ class Routing {
public static function getRouteTable():Map<NetworkID, Route> { public static function getRouteTable():Map<NetworkID, Route> {
return routingTable; 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);
}
} }