improved networking
This commit is contained in:
parent
df7991763d
commit
b305594ea4
@ -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, () -> {
|
||||
|
@ -74,30 +74,17 @@ class Routing {
|
||||
private static function handleRoutePackage(pack:Package<Noise>, 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;
|
||||
case RouteDiscoverUpdate(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;
|
||||
};
|
||||
|
||||
if (!shouldRespond) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Respond to peer
|
||||
var response:Package<Noise> = {
|
||||
@ -110,6 +97,14 @@ class Routing {
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
default:
|
||||
Log.silly("Expected package to be a Route discover package");
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@ -126,6 +121,9 @@ class Routing {
|
||||
return routes;
|
||||
}
|
||||
|
||||
/**
|
||||
Create a new packate to brodcast our routes.
|
||||
**/
|
||||
private static function newRoutDiscoverPackage():Package<Noise> {
|
||||
var pack:Package<Noise> = {
|
||||
type: RouteDiscover(genRouteList()),
|
||||
@ -179,4 +177,28 @@ class Routing {
|
||||
public static function getRouteTable():Map<NetworkID, Route> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user