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
This commit is contained in:
Djeeberjr 2023-06-07 23:42:23 +02:00
parent 7c7529ae39
commit 9d6979c8e8
8 changed files with 40 additions and 35 deletions

View File

@ -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);
}

View File

@ -124,7 +124,7 @@ class GPS {
}
@:allow(kernel.net.Net)
private function handlePackage(pack:Package, dist: Float,iface: INetworkInterface) {
private function handlePackage(pack:Package<Noise>, dist: Float,iface: INetworkInterface) {
switch (pack.type) {
case GPSRequest:
if (!shouldRespond) return;

View File

@ -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}>;
}

View File

@ -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<Float>}>;
public var onMessage(default, null):Signal<{pack:GenericPackage,dist:Null<Float>}>;
private final onMessageTrigger: SignalTrigger<{pack:Package,dist:Null<Float>}> = Signal.trigger();
private final onMessageTrigger: SignalTrigger<{pack:GenericPackage,dist:Null<Float>}> = Signal.trigger();
private var openChans: Array<Int> = [];
private function new() {

View File

@ -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<Int, Callback<Outcome<Package,Error>>> = new Map();
private final protoHandlers:Map<String, Callback<Package>> = new Map();
private final responseBus:Map<Int, Callback<Outcome<GenericPackage,Error>>> = new Map();
private final protoHandlers:Map<String, Callback<GenericPackage>> = new Map();
private var interfaces:Array<INetworkInterface>;
@: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<Package> {
return new Promise<Package>((resolve, reject) -> {
var pack:Package = {
public function sendAndAwait<T>(dest:NetworkID, proto:String, data:T):Promise<GenericPackage> {
return new Promise<GenericPackage>((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<Package,Error>) -> {
responseBus[pack.msgID] = ((reponse:Outcome<GenericPackage,Error>) -> {
switch reponse {
case Success(pack):
@ -227,7 +228,7 @@ class Net {
});
}
public function registerProto(proto:String, callback:Callback<Package>) {
public function registerProto(proto:String, callback:Callback<GenericPackage>) {
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<Noise> = {
fromID: networkID,
toID: Net.BRODCAST_PORT,
ttl: 0, // Prevent forwarding

View File

@ -3,6 +3,7 @@ package kernel.net;
import lib.Pos3;
typedef NetworkID = Int;
typedef GenericPackage = Package<Dynamic> ;
enum PackageTypes {
Data(proto:String);
@ -18,15 +19,15 @@ enum PackageTypes {
/**
Representing a network package.
**/
@:structInit class Package {
@:structInit class Package<T> {
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<T2>(newData:T2):Package<T2> {
return {
toID: fromID,
fromID: toID,

View File

@ -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<Noise>, 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<Noise> = {
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<Noise> {
var pack:Package<Noise> = {
type: RouteDiscover(genRouteList()),
toID: Net.BRODCAST_PORT,
msgID: null,

View File

@ -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<Float>}>;
public var onMessage(default, null):Signal<{pack:GenericPackage,dist:Null<Float>}>;
private final onMessageTrigger:SignalTrigger<{pack:Package,dist:Null<Float>}> = Signal.trigger();
private final onMessageTrigger:SignalTrigger<{pack:GenericPackage,dist:Null<Float>}> = 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,