BIG FORMATING COMMIT

This commit is contained in:
2023-07-30 15:55:22 +02:00
parent 088fce0aaa
commit 91972107eb
103 changed files with 1610 additions and 1585 deletions

View File

@@ -21,20 +21,21 @@ class Net {
Depends on: KernelEvents
**/
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 static final networkID:NetworkID = OS.getComputerID();
private static final responseBus:Map<Int, Callback<Outcome<GenericPackage,Error>>> = new Map();
private static final responseBus:Map<Int, Callback<Outcome<GenericPackage, Error>>> = new Map();
private static final protoHandlers:Map<String, Callback<GenericPackage>> = new Map();
private static var interfaces:Array<INetworkInterface>;
@:allow(kernel.Init)
private static function init() {
interfaces = [for (e in Peripheral.getAllModems()) e ]; // TODO: is this the way to do it?
interfaces = [for (e in Peripheral.getAllModems()) e]; // TODO: is this the way to do it?
interfaces.push(Loopback.instance);
for (interf in interfaces){
for (interf in interfaces) {
setupInterf(interf);
}
@@ -61,8 +62,8 @@ class Net {
});
}
private static function setupInterf(interf: INetworkInterface) {
interf.onMessage.handle(e -> handle(e.pack,interf,e.dist));
private static function setupInterf(interf:INetworkInterface) {
interf.onMessage.handle(e -> handle(e.pack, interf, e.dist));
interf.listen(networkID);
interf.listen(BRODCAST_PORT);
}
@@ -70,8 +71,8 @@ class Net {
/**
Called when a new package comes in.
**/
private static function handle(pack:GenericPackage,interf: INetworkInterface, ?dist: Float) {
if (pack.toID == networkID || pack.toID == Net.BRODCAST_PORT){
private static function handle(pack:GenericPackage, interf:INetworkInterface, ?dist:Float) {
if (pack.toID == networkID || pack.toID == Net.BRODCAST_PORT) {
switch pack.type {
case Data(_) | DataNoResponse(_):
// Let a local proccess handle it
@@ -83,16 +84,16 @@ class Net {
}
case RouteDiscover(_) | RouteDiscoverResponse(_) | RouteDiscoverUpdate(_):
// Delegate to Routing
Routing.handleRoutePackage(cast pack,interf);
Routing.handleRoutePackage(cast pack, interf);
case GPSRequest | GPSResponse(_):
if (dist == null) {
Log.silly("Got a GPS package but no distance was provided");
return;
}
// Delegate to GPS
GPS.handlePackage(cast pack,dist,interf);
GPS.handlePackage(cast pack, dist, interf);
}
}else{
} else {
// New message received but its not ment for us. Forward if possible.
forwardPackage(pack);
}
@@ -118,13 +119,12 @@ class Net {
sendRaw(pack);
}
private static function forwardPackage(pack: GenericPackage) {
if (pack.ttl == 0){
private static function forwardPackage(pack:GenericPackage) {
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});
sendAndForget(pack.fromID, "icmp", {type: "died", msgID: pack.msgID});
}
// Drop package
@@ -133,7 +133,7 @@ class Net {
pack.ttl--;
if (!sendRaw(pack)){
if (!sendRaw(pack)) {
// Cant forward
}
}
@@ -174,13 +174,13 @@ class Net {
Just send the package to the right modem.
Returns true if message was send
**/
private static function sendRaw(pack:GenericPackage): Bool {
private static function sendRaw(pack:GenericPackage):Bool {
var route = Routing.getRouteToID(pack.toID);
if (route == null){
if (route == null) {
return false;
}
route.interf.send(route.rep,networkID,pack);
route.interf.send(route.rep, networkID, pack);
return true;
}
@@ -200,8 +200,7 @@ class Net {
var timeout:Timer = null;
responseBus[pack.msgID] = ((reponse:Outcome<GenericPackage,Error>) -> {
responseBus[pack.msgID] = ((reponse:Outcome<GenericPackage, Error>) -> {
switch reponse {
case Success(pack):
resolve(pack);
@@ -217,10 +216,10 @@ class Net {
timeout = new Timer(MESSAGE_TIMEOUT, () -> {
responseBus.remove(pack.msgID);
reject(new Error(InternalError,"Message timeout"));
reject(new Error(InternalError, "Message timeout"));
});
if (!sendRaw(pack)){
if (!sendRaw(pack)) {
reject(new Error("ID unreachable"));
}
@@ -246,9 +245,9 @@ class Net {
/**
Sends a ping package to the given id. Returns true if there was a response.
**/
public static function ping(toID: NetworkID): Promise<Noise> {
return new Promise<Noise>((resolve,reject)->{
sendAndAwait(toID,"icmp",{type:"ping"}).handle(pack -> {
public static function ping(toID:NetworkID):Promise<Noise> {
return new Promise<Noise>((resolve, reject) -> {
sendAndAwait(toID, "icmp", {type: "ping"}).handle(pack -> {
switch pack {
case Success(_):
resolve(Noise);
@@ -260,7 +259,7 @@ class Net {
});
}
public static function getActiveProtocols(): ReadOnlyArray<String> {
public static function getActiveProtocols():ReadOnlyArray<String> {
var arr = new Array<String>();
for (proto in protoHandlers.keys()) {
@@ -272,7 +271,7 @@ class Net {
@:allow(kernel.gps.GPS)
private static function brodcastGPSRequest() {
var pack: Package<Noise> = {
var pack:Package<Noise> = {
fromID: networkID,
toID: Net.BRODCAST_PORT,
ttl: 0, // Prevent forwarding
@@ -282,7 +281,8 @@ class Net {
};
for (modem in Peripheral.getAllModems()) {
if (!modem.isWireless()) continue;
if (!modem.isWireless())
continue;
modem.send(Net.BRODCAST_PORT, networkID, pack);
}
}