From 9d6979c8e8bdd78ad9a5e5dc1567497ca97bb17e Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Wed, 7 Jun 2023 23:42:23 +0200 Subject: [PATCH] added a type to Package and added GenericPackage This forces you to use cast to if you want to force the package to have a specific type --- src/bin/srsc/SiteRessourceController.hx | 9 ++++--- src/kernel/gps/GPS.hx | 2 +- src/kernel/net/INetworkInterface.hx | 3 ++- src/kernel/net/Loopback.hx | 5 ++-- src/kernel/net/Net.hx | 33 +++++++++++++------------ src/kernel/net/Package.hx | 9 ++++--- src/kernel/net/Routing.hx | 8 +++--- src/kernel/peripherals/Modem.hx | 6 ++--- 8 files changed, 40 insertions(+), 35 deletions(-) diff --git a/src/bin/srsc/SiteRessourceController.hx b/src/bin/srsc/SiteRessourceController.hx index 747a2dc..6ad8ad5 100644 --- a/src/bin/srsc/SiteRessourceController.hx +++ b/src/bin/srsc/SiteRessourceController.hx @@ -1,5 +1,6 @@ package bin.srsc; +import lib.TypeField; import kernel.log.Log; import lib.KVStore; import bin.srsc.PackageTypes; @@ -29,7 +30,7 @@ class SiteRessourceController implements Process { kernel.net.Net.instance.registerProto(SITE_CONTROLLER_RESSOURCE_MANAGER_PROTO, this.handleMsg); } - private function handleMsg(pkg:Package) { + private function handleMsg(pkg:GenericPackage) { /* There are 3 types of messages: - Register a new ressource name @@ -39,11 +40,11 @@ class SiteRessourceController implements Process { switch (pkg.data.type) { case "register": - pkg.respond(handleRegister(pkg.data)); + pkg.respond(handleRegister(cast pkg.data)); case "unregister": - pkg.respond(handleUnregister(pkg.data)); + pkg.respond(handleUnregister(cast pkg.data)); case "get": - pkg.respond(handleGet(pkg.data)); + pkg.respond(handleGet(cast pkg.data)); default: handle.writeLine("Unknown message type: " + pkg.data.type); } diff --git a/src/kernel/gps/GPS.hx b/src/kernel/gps/GPS.hx index eb76421..16543da 100644 --- a/src/kernel/gps/GPS.hx +++ b/src/kernel/gps/GPS.hx @@ -124,7 +124,7 @@ class GPS { } @:allow(kernel.net.Net) - private function handlePackage(pack:Package, dist: Float,iface: INetworkInterface) { + private function handlePackage(pack:Package, dist: Float,iface: INetworkInterface) { switch (pack.type) { case GPSRequest: if (!shouldRespond) return; diff --git a/src/kernel/net/INetworkInterface.hx b/src/kernel/net/INetworkInterface.hx index 6a71108..c490ecf 100644 --- a/src/kernel/net/INetworkInterface.hx +++ b/src/kernel/net/INetworkInterface.hx @@ -1,5 +1,6 @@ package kernel.net; +import kernel.net.Package.GenericPackage; using tink.CoreApi; /** @@ -13,5 +14,5 @@ interface INetworkInterface { public function send(chan: Int,replyChan: Int,payload: Any):Void; public function name():String; public function getBaseRoutingCost():Int; - public var onMessage (default, null): Signal<{pack:Package,?dist:Float}>; + public var onMessage (default, null): Signal<{pack:GenericPackage,?dist:Float}>; } diff --git a/src/kernel/net/Loopback.hx b/src/kernel/net/Loopback.hx index 12e1308..ff474ec 100644 --- a/src/kernel/net/Loopback.hx +++ b/src/kernel/net/Loopback.hx @@ -1,5 +1,6 @@ package kernel.net; +import kernel.net.Package.GenericPackage; import kernel.log.Log; using tink.CoreApi; @@ -9,9 +10,9 @@ using tink.CoreApi; class Loopback implements INetworkInterface { public static final instance:Loopback = new Loopback(); - public var onMessage(default, null):Signal<{pack:Package,dist:Null}>; + public var onMessage(default, null):Signal<{pack:GenericPackage,dist:Null}>; - private final onMessageTrigger: SignalTrigger<{pack:Package,dist:Null}> = Signal.trigger(); + private final onMessageTrigger: SignalTrigger<{pack:GenericPackage,dist:Null}> = Signal.trigger(); private var openChans: Array = []; private function new() { diff --git a/src/kernel/net/Net.hx b/src/kernel/net/Net.hx index a3bde4b..6abbf8b 100644 --- a/src/kernel/net/Net.hx +++ b/src/kernel/net/Net.hx @@ -1,5 +1,6 @@ package kernel.net; +import kernel.net.Package.GenericPackage; import kernel.gps.GPS; import haxe.ds.ReadOnlyArray; import kernel.net.Package.NetworkID; @@ -25,8 +26,8 @@ class Net { public static inline final DEFAULT_TTL:Int = 10; public final networkID:NetworkID = OS.getComputerID(); - private final responseBus:Map>> = new Map(); - private final protoHandlers:Map> = new Map(); + private final responseBus:Map>> = new Map(); + private final protoHandlers:Map> = new Map(); private var interfaces:Array; @:allow(kernel.Init) @@ -68,7 +69,7 @@ class Net { /** Called when a new package comes in. **/ - private function handle(pack:Package,interf: INetworkInterface, ?dist: Float) { + private function handle(pack:GenericPackage,interf: INetworkInterface, ?dist: Float) { if (pack.toID == this.networkID || pack.toID == Net.BRODCAST_PORT){ switch pack.type { case Data(_) | DataNoResponse(_): @@ -81,14 +82,14 @@ class Net { } case RouteDiscover(_) | RouteDiscoverResponse(_) | RouteDiscoverUpdate(_): // Delegate to Routing - Routing.instance.handleRoutePackage(pack,interf); + Routing.instance.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.instance.handlePackage(pack,dist,interf); + GPS.instance.handlePackage(cast pack,dist,interf); } }else{ // New message received but its not ment for us. Forward if possible. @@ -104,7 +105,7 @@ class Net { Send a message. Dont care if its reaches its destination nor it has a response. **/ public function sendAndForget(dest:Int, proto:String, data:Dynamic) { - var pack:Package = { + var pack:GenericPackage = { toID: dest, fromID: networkID, msgID: generateMessageID(), @@ -116,7 +117,7 @@ class Net { sendRaw(pack); } - private function forwardPackage(pack: Package) { + private function forwardPackage(pack: GenericPackage) { if (pack.ttl == 0){ if (pack.type.match(Data(_))) { @@ -136,7 +137,7 @@ class Net { } } - public function respondTo(pack:Package, data:Dynamic) { + public function respondTo(pack:GenericPackage, data:Dynamic) { if (pack.type.match(DataNoResponse(_))) { Log.warn("Responed to a no response package. Ignoring"); return; @@ -150,7 +151,7 @@ class Net { /** Send to package to the localy register handler based on the proto **/ - private function routeToProto(pack:Package) { + private function routeToProto(pack:GenericPackage) { var proto = switch pack.type { case Data(proto): proto; @@ -172,7 +173,7 @@ class Net { Just send the package to the right modem. Returns true if message was send **/ - private function sendRaw(pack:Package): Bool { + private function sendRaw(pack:GenericPackage): Bool { var route = Routing.instance.getRouteToID(pack.toID); if (route == null){ return false; @@ -185,9 +186,9 @@ class Net { /** Send a message and wait for a response. **/ - public function sendAndAwait(dest:NetworkID, proto:String, data:Dynamic):Promise { - return new Promise((resolve, reject) -> { - var pack:Package = { + public function sendAndAwait(dest:NetworkID, proto:String, data:T):Promise { + return new Promise((resolve, reject) -> { + var pack:GenericPackage = { toID: dest, fromID: networkID, msgID: generateMessageID(), @@ -198,7 +199,7 @@ class Net { var timeout:Timer = null; - responseBus[pack.msgID] = ((reponse:Outcome) -> { + responseBus[pack.msgID] = ((reponse:Outcome) -> { switch reponse { case Success(pack): @@ -227,7 +228,7 @@ class Net { }); } - public function registerProto(proto:String, callback:Callback) { + public function registerProto(proto:String, callback:Callback) { if (protoHandlers.exists(proto)) { // Failed. Handler already exist. // TODO: return error @@ -270,7 +271,7 @@ class Net { @:allow(kernel.gps.GPS) private function brodcastGPSRequest() { - var pack: Package = { + var pack: Package = { fromID: networkID, toID: Net.BRODCAST_PORT, ttl: 0, // Prevent forwarding diff --git a/src/kernel/net/Package.hx b/src/kernel/net/Package.hx index 7d2dd33..b80707b 100644 --- a/src/kernel/net/Package.hx +++ b/src/kernel/net/Package.hx @@ -3,6 +3,7 @@ package kernel.net; import lib.Pos3; typedef NetworkID = Int; +typedef GenericPackage = Package ; enum PackageTypes { Data(proto:String); @@ -18,15 +19,15 @@ enum PackageTypes { /** Representing a network package. **/ -@:structInit class Package { +@:structInit class Package { public final fromID:NetworkID; public final toID:NetworkID; public final msgID:Int; public final type:PackageTypes; - public final data:Dynamic; + public final data:T; public var ttl: Int; - public function new(fromID:NetworkID, toID:NetworkID, msgID:Int, type:PackageTypes, data:Dynamic, ttl:Int) { + public function new(fromID:NetworkID, toID:NetworkID, msgID:Int, type:PackageTypes, data:T, ttl:Int) { this.fromID = fromID; this.toID = toID; this.msgID = msgID; @@ -38,7 +39,7 @@ enum PackageTypes { /** Create package that can be used as a response. **/ - public function createResponse(newData:Dynamic):Package { + public function createResponse(newData:T2):Package { return { toID: fromID, fromID: toID, diff --git a/src/kernel/net/Routing.hx b/src/kernel/net/Routing.hx index 22bb403..bc033a3 100644 --- a/src/kernel/net/Routing.hx +++ b/src/kernel/net/Routing.hx @@ -73,7 +73,7 @@ class Routing { Handle incomming packages that involve route discovery. **/ @:allow(kernel.net.Net) - private function handleRoutePackage(pack:Package, interf:INetworkInterface):Void { + private function handleRoutePackage(pack:Package, interf:INetworkInterface):Void { addPossibleRoute(pack.fromID, interf, 0, pack.fromID); var shouldRespond:Bool = switch pack.type { @@ -102,7 +102,7 @@ class Routing { } // Respond to peer - var response:Package = { + var response:Package = { toID: pack.fromID, fromID: Net.instance.networkID, msgID: null, @@ -128,8 +128,8 @@ class Routing { return routes; } - private function newRoutDiscoverPackage():Package { - var pack:Package = { + private function newRoutDiscoverPackage():Package { + var pack:Package = { type: RouteDiscover(genRouteList()), toID: Net.BRODCAST_PORT, msgID: null, diff --git a/src/kernel/peripherals/Modem.hx b/src/kernel/peripherals/Modem.hx index bd7b233..8d7d540 100644 --- a/src/kernel/peripherals/Modem.hx +++ b/src/kernel/peripherals/Modem.hx @@ -11,9 +11,9 @@ class Modem implements INetworkInterface implements IPeripheral { public static inline final TYPE_NAME:String = "modem"; public final addr:String; - public var onMessage(default, null):Signal<{pack:Package,dist:Null}>; + public var onMessage(default, null):Signal<{pack:GenericPackage,dist:Null}>; - private final onMessageTrigger:SignalTrigger<{pack:Package,dist:Null}> = Signal.trigger(); + private final onMessageTrigger:SignalTrigger<{pack:GenericPackage,dist:Null}> = Signal.trigger(); private final native:cc.periphs.Modem.Modem; @:allow(kernel.peripherals) @@ -25,7 +25,7 @@ class Modem implements INetworkInterface implements IPeripheral { KernelEvents.instance.onModemMessage.handle(params ->{ try{ if (params.addr == this.addr){ - var pack:Package = { + var pack:GenericPackage = { fromID: params.message.fromID, toID: params.message.toID, msgID: params.message.msgID,