Forwarding and TTL

This commit is contained in:
2022-03-01 16:59:53 +01:00
parent d65b06a9b4
commit eb43c51e50
4 changed files with 75 additions and 33 deletions

View File

@@ -22,6 +22,7 @@ class Net {
public static var instance:Net;
public static inline final BRODCAST_PORT:Int = 65533;
public static inline final MESSAGE_TIMEOUT:Int = 3;
public static inline final DEFAULT_TTL:Int = 10;
public final networkID:NetworkID = OS.getComputerID();
private final responseBus:Map<Int, Callback<Package>> = new Map();
@@ -48,18 +49,23 @@ class Net {
Called when a new package comes in.
**/
private function handle(pack:Package,interf: INetworkInterface) {
switch pack.type {
case Data(_) | DataNoResponse(_):
// Let a local proccess handle it
routeToProto(pack);
case Response:
// Got a response to a send message. Invoke the callback
if (responseBus.exists(pack.msgID)) {
responseBus[pack.msgID].invoke(pack);
}
case RouteDiscover(_) | RouteDiscoverResponse(_):
// Delegate to Routing
Routing.instance.handleRoutePackage(pack,interf);
if (pack.toID == this.networkID || pack.toID == Net.BRODCAST_PORT){
switch pack.type {
case Data(_) | DataNoResponse(_):
// Let a local proccess handle it
routeToProto(pack);
case Response:
// Got a response to a send message. Invoke the callback
if (responseBus.exists(pack.msgID)) {
responseBus[pack.msgID].invoke(pack);
}
case RouteDiscover(_) | RouteDiscoverResponse(_):
// Delegate to Routing
Routing.instance.handleRoutePackage(pack,interf);
}
}else{
// New message received but its not ment for us. Forward if possible.
forwardPackage(pack);
}
}
@@ -76,12 +82,26 @@ class Net {
fromID: networkID,
msgID: generateMessageID(),
type: DataNoResponse(proto),
data: data
data: data,
ttl: Net.DEFAULT_TTL,
}
sendRaw(pack);
}
private function forwardPackage(pack: Package) {
if (pack.ttl == 0){
// Drop package
return;
}
pack.ttl--;
if (sendRaw(pack)){
// Cant forward
}
}
public function respondTo(pack:Package, data:Dynamic) {
if (pack.type.match(DataNoResponse(_))) {
Log.warn("Responed to a no response package. Ignoring");
@@ -97,16 +117,17 @@ class Net {
Send to package to the localy register handler based on the proto
**/
private function routeToProto(pack:Package) {
var proto:String = switch pack.type {
var proto = switch pack.type {
case Data(proto):
proto;
case DataNoResponse(proto):
proto;
case _:
default:
return;
}
if (!protoHandlers.exists(proto) && protoHandlers[proto] != null) {
if (!protoHandlers.exists(proto)) {
Log.warn("Trying to route package to proto: \"" + proto + "\" but nothing was register");
return;
}
@@ -115,24 +136,30 @@ class Net {
/**
Just send the package to the right modem.
Returns true if message was send
**/
private function sendRaw(pack:Package) {
private function sendRaw(pack:Package): Bool {
var route = Routing.instance.getRouteToID(pack.toID);
if (route == null){
return false;
}
Routing.instance.getIterfaceToId(pack.toID).send(pack.toID,pack.fromID,pack);
route.interf.send(route.rep,this.networkID,pack);
return true;
}
/**
Send a message and wait for a response.
**/
public function sendAndAwait(dest:NetworkID, proto:String, data:Dynamic):Promise<Package> {
return new Promise<Package>((resolve, reject) -> {
var pack:Package = {
toID: dest,
fromID: networkID,
msgID: generateMessageID(),
type: Data(proto),
data: data
data: data,
ttl: Net.DEFAULT_TTL,
}
var timeout:Timer = null;
@@ -149,7 +176,9 @@ class Net {
reject(new Error(InternalError,"Message timeout"));
});
sendRaw(pack);
if (!sendRaw(pack)){
reject(new Error("ID unreachable"));
}
// No callback link for you
return null;