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:
parent
7c7529ae39
commit
9d6979c8e8
@ -1,5 +1,6 @@
|
|||||||
package bin.srsc;
|
package bin.srsc;
|
||||||
|
|
||||||
|
import lib.TypeField;
|
||||||
import kernel.log.Log;
|
import kernel.log.Log;
|
||||||
import lib.KVStore;
|
import lib.KVStore;
|
||||||
import bin.srsc.PackageTypes;
|
import bin.srsc.PackageTypes;
|
||||||
@ -29,7 +30,7 @@ class SiteRessourceController implements Process {
|
|||||||
kernel.net.Net.instance.registerProto(SITE_CONTROLLER_RESSOURCE_MANAGER_PROTO, this.handleMsg);
|
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:
|
There are 3 types of messages:
|
||||||
- Register a new ressource name
|
- Register a new ressource name
|
||||||
@ -39,11 +40,11 @@ class SiteRessourceController implements Process {
|
|||||||
|
|
||||||
switch (pkg.data.type) {
|
switch (pkg.data.type) {
|
||||||
case "register":
|
case "register":
|
||||||
pkg.respond(handleRegister(pkg.data));
|
pkg.respond(handleRegister(cast pkg.data));
|
||||||
case "unregister":
|
case "unregister":
|
||||||
pkg.respond(handleUnregister(pkg.data));
|
pkg.respond(handleUnregister(cast pkg.data));
|
||||||
case "get":
|
case "get":
|
||||||
pkg.respond(handleGet(pkg.data));
|
pkg.respond(handleGet(cast pkg.data));
|
||||||
default:
|
default:
|
||||||
handle.writeLine("Unknown message type: " + pkg.data.type);
|
handle.writeLine("Unknown message type: " + pkg.data.type);
|
||||||
}
|
}
|
||||||
|
@ -124,7 +124,7 @@ class GPS {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@:allow(kernel.net.Net)
|
@: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) {
|
switch (pack.type) {
|
||||||
case GPSRequest:
|
case GPSRequest:
|
||||||
if (!shouldRespond) return;
|
if (!shouldRespond) return;
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package kernel.net;
|
package kernel.net;
|
||||||
|
|
||||||
|
import kernel.net.Package.GenericPackage;
|
||||||
using tink.CoreApi;
|
using tink.CoreApi;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -13,5 +14,5 @@ interface INetworkInterface {
|
|||||||
public function send(chan: Int,replyChan: Int,payload: Any):Void;
|
public function send(chan: Int,replyChan: Int,payload: Any):Void;
|
||||||
public function name():String;
|
public function name():String;
|
||||||
public function getBaseRoutingCost():Int;
|
public function getBaseRoutingCost():Int;
|
||||||
public var onMessage (default, null): Signal<{pack:Package,?dist:Float}>;
|
public var onMessage (default, null): Signal<{pack:GenericPackage,?dist:Float}>;
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package kernel.net;
|
package kernel.net;
|
||||||
|
|
||||||
|
import kernel.net.Package.GenericPackage;
|
||||||
import kernel.log.Log;
|
import kernel.log.Log;
|
||||||
using tink.CoreApi;
|
using tink.CoreApi;
|
||||||
|
|
||||||
@ -9,9 +10,9 @@ using tink.CoreApi;
|
|||||||
class Loopback implements INetworkInterface {
|
class Loopback implements INetworkInterface {
|
||||||
public static final instance:Loopback = new Loopback();
|
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 var openChans: Array<Int> = [];
|
||||||
|
|
||||||
private function new() {
|
private function new() {
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package kernel.net;
|
package kernel.net;
|
||||||
|
|
||||||
|
import kernel.net.Package.GenericPackage;
|
||||||
import kernel.gps.GPS;
|
import kernel.gps.GPS;
|
||||||
import haxe.ds.ReadOnlyArray;
|
import haxe.ds.ReadOnlyArray;
|
||||||
import kernel.net.Package.NetworkID;
|
import kernel.net.Package.NetworkID;
|
||||||
@ -25,8 +26,8 @@ class Net {
|
|||||||
public static inline final DEFAULT_TTL:Int = 10;
|
public static inline final DEFAULT_TTL:Int = 10;
|
||||||
|
|
||||||
public final networkID:NetworkID = OS.getComputerID();
|
public final networkID:NetworkID = OS.getComputerID();
|
||||||
private final responseBus:Map<Int, Callback<Outcome<Package,Error>>> = new Map();
|
private final responseBus:Map<Int, Callback<Outcome<GenericPackage,Error>>> = new Map();
|
||||||
private final protoHandlers:Map<String, Callback<Package>> = new Map();
|
private final protoHandlers:Map<String, Callback<GenericPackage>> = new Map();
|
||||||
private var interfaces:Array<INetworkInterface>;
|
private var interfaces:Array<INetworkInterface>;
|
||||||
|
|
||||||
@:allow(kernel.Init)
|
@:allow(kernel.Init)
|
||||||
@ -68,7 +69,7 @@ class Net {
|
|||||||
/**
|
/**
|
||||||
Called when a new package comes in.
|
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){
|
if (pack.toID == this.networkID || pack.toID == Net.BRODCAST_PORT){
|
||||||
switch pack.type {
|
switch pack.type {
|
||||||
case Data(_) | DataNoResponse(_):
|
case Data(_) | DataNoResponse(_):
|
||||||
@ -81,14 +82,14 @@ class Net {
|
|||||||
}
|
}
|
||||||
case RouteDiscover(_) | RouteDiscoverResponse(_) | RouteDiscoverUpdate(_):
|
case RouteDiscover(_) | RouteDiscoverResponse(_) | RouteDiscoverUpdate(_):
|
||||||
// Delegate to Routing
|
// Delegate to Routing
|
||||||
Routing.instance.handleRoutePackage(pack,interf);
|
Routing.instance.handleRoutePackage(cast pack,interf);
|
||||||
case GPSRequest | GPSResponse(_):
|
case GPSRequest | GPSResponse(_):
|
||||||
if (dist == null) {
|
if (dist == null) {
|
||||||
Log.silly("Got a GPS package but no distance was provided");
|
Log.silly("Got a GPS package but no distance was provided");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Delegate to GPS
|
// Delegate to GPS
|
||||||
GPS.instance.handlePackage(pack,dist,interf);
|
GPS.instance.handlePackage(cast pack,dist,interf);
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
// New message received but its not ment for us. Forward if possible.
|
// 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.
|
Send a message. Dont care if its reaches its destination nor it has a response.
|
||||||
**/
|
**/
|
||||||
public function sendAndForget(dest:Int, proto:String, data:Dynamic) {
|
public function sendAndForget(dest:Int, proto:String, data:Dynamic) {
|
||||||
var pack:Package = {
|
var pack:GenericPackage = {
|
||||||
toID: dest,
|
toID: dest,
|
||||||
fromID: networkID,
|
fromID: networkID,
|
||||||
msgID: generateMessageID(),
|
msgID: generateMessageID(),
|
||||||
@ -116,7 +117,7 @@ class Net {
|
|||||||
sendRaw(pack);
|
sendRaw(pack);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function forwardPackage(pack: Package) {
|
private function forwardPackage(pack: GenericPackage) {
|
||||||
if (pack.ttl == 0){
|
if (pack.ttl == 0){
|
||||||
|
|
||||||
if (pack.type.match(Data(_))) {
|
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(_))) {
|
if (pack.type.match(DataNoResponse(_))) {
|
||||||
Log.warn("Responed to a no response package. Ignoring");
|
Log.warn("Responed to a no response package. Ignoring");
|
||||||
return;
|
return;
|
||||||
@ -150,7 +151,7 @@ class Net {
|
|||||||
/**
|
/**
|
||||||
Send to package to the localy register handler based on the proto
|
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 {
|
var proto = switch pack.type {
|
||||||
case Data(proto):
|
case Data(proto):
|
||||||
proto;
|
proto;
|
||||||
@ -172,7 +173,7 @@ class Net {
|
|||||||
Just send the package to the right modem.
|
Just send the package to the right modem.
|
||||||
Returns true if message was send
|
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);
|
var route = Routing.instance.getRouteToID(pack.toID);
|
||||||
if (route == null){
|
if (route == null){
|
||||||
return false;
|
return false;
|
||||||
@ -185,9 +186,9 @@ class Net {
|
|||||||
/**
|
/**
|
||||||
Send a message and wait for a response.
|
Send a message and wait for a response.
|
||||||
**/
|
**/
|
||||||
public function sendAndAwait(dest:NetworkID, proto:String, data:Dynamic):Promise<Package> {
|
public function sendAndAwait<T>(dest:NetworkID, proto:String, data:T):Promise<GenericPackage> {
|
||||||
return new Promise<Package>((resolve, reject) -> {
|
return new Promise<GenericPackage>((resolve, reject) -> {
|
||||||
var pack:Package = {
|
var pack:GenericPackage = {
|
||||||
toID: dest,
|
toID: dest,
|
||||||
fromID: networkID,
|
fromID: networkID,
|
||||||
msgID: generateMessageID(),
|
msgID: generateMessageID(),
|
||||||
@ -198,7 +199,7 @@ class Net {
|
|||||||
|
|
||||||
var timeout:Timer = null;
|
var timeout:Timer = null;
|
||||||
|
|
||||||
responseBus[pack.msgID] = ((reponse:Outcome<Package,Error>) -> {
|
responseBus[pack.msgID] = ((reponse:Outcome<GenericPackage,Error>) -> {
|
||||||
|
|
||||||
switch reponse {
|
switch reponse {
|
||||||
case Success(pack):
|
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)) {
|
if (protoHandlers.exists(proto)) {
|
||||||
// Failed. Handler already exist.
|
// Failed. Handler already exist.
|
||||||
// TODO: return error
|
// TODO: return error
|
||||||
@ -270,7 +271,7 @@ class Net {
|
|||||||
|
|
||||||
@:allow(kernel.gps.GPS)
|
@:allow(kernel.gps.GPS)
|
||||||
private function brodcastGPSRequest() {
|
private function brodcastGPSRequest() {
|
||||||
var pack: Package = {
|
var pack: Package<Noise> = {
|
||||||
fromID: networkID,
|
fromID: networkID,
|
||||||
toID: Net.BRODCAST_PORT,
|
toID: Net.BRODCAST_PORT,
|
||||||
ttl: 0, // Prevent forwarding
|
ttl: 0, // Prevent forwarding
|
||||||
|
@ -3,6 +3,7 @@ package kernel.net;
|
|||||||
import lib.Pos3;
|
import lib.Pos3;
|
||||||
|
|
||||||
typedef NetworkID = Int;
|
typedef NetworkID = Int;
|
||||||
|
typedef GenericPackage = Package<Dynamic> ;
|
||||||
|
|
||||||
enum PackageTypes {
|
enum PackageTypes {
|
||||||
Data(proto:String);
|
Data(proto:String);
|
||||||
@ -18,15 +19,15 @@ enum PackageTypes {
|
|||||||
/**
|
/**
|
||||||
Representing a network package.
|
Representing a network package.
|
||||||
**/
|
**/
|
||||||
@:structInit class Package {
|
@:structInit class Package<T> {
|
||||||
public final fromID:NetworkID;
|
public final fromID:NetworkID;
|
||||||
public final toID:NetworkID;
|
public final toID:NetworkID;
|
||||||
public final msgID:Int;
|
public final msgID:Int;
|
||||||
public final type:PackageTypes;
|
public final type:PackageTypes;
|
||||||
public final data:Dynamic;
|
public final data:T;
|
||||||
public var ttl: Int;
|
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.fromID = fromID;
|
||||||
this.toID = toID;
|
this.toID = toID;
|
||||||
this.msgID = msgID;
|
this.msgID = msgID;
|
||||||
@ -38,7 +39,7 @@ enum PackageTypes {
|
|||||||
/**
|
/**
|
||||||
Create package that can be used as a response.
|
Create package that can be used as a response.
|
||||||
**/
|
**/
|
||||||
public function createResponse(newData:Dynamic):Package {
|
public function createResponse<T2>(newData:T2):Package<T2> {
|
||||||
return {
|
return {
|
||||||
toID: fromID,
|
toID: fromID,
|
||||||
fromID: toID,
|
fromID: toID,
|
||||||
|
@ -73,7 +73,7 @@ class Routing {
|
|||||||
Handle incomming packages that involve route discovery.
|
Handle incomming packages that involve route discovery.
|
||||||
**/
|
**/
|
||||||
@:allow(kernel.net.Net)
|
@: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);
|
addPossibleRoute(pack.fromID, interf, 0, pack.fromID);
|
||||||
|
|
||||||
var shouldRespond:Bool = switch pack.type {
|
var shouldRespond:Bool = switch pack.type {
|
||||||
@ -102,7 +102,7 @@ class Routing {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Respond to peer
|
// Respond to peer
|
||||||
var response:Package = {
|
var response:Package<Noise> = {
|
||||||
toID: pack.fromID,
|
toID: pack.fromID,
|
||||||
fromID: Net.instance.networkID,
|
fromID: Net.instance.networkID,
|
||||||
msgID: null,
|
msgID: null,
|
||||||
@ -128,8 +128,8 @@ class Routing {
|
|||||||
return routes;
|
return routes;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function newRoutDiscoverPackage():Package {
|
private function newRoutDiscoverPackage():Package<Noise> {
|
||||||
var pack:Package = {
|
var pack:Package<Noise> = {
|
||||||
type: RouteDiscover(genRouteList()),
|
type: RouteDiscover(genRouteList()),
|
||||||
toID: Net.BRODCAST_PORT,
|
toID: Net.BRODCAST_PORT,
|
||||||
msgID: null,
|
msgID: null,
|
||||||
|
@ -11,9 +11,9 @@ class Modem implements INetworkInterface implements IPeripheral {
|
|||||||
public static inline final TYPE_NAME:String = "modem";
|
public static inline final TYPE_NAME:String = "modem";
|
||||||
|
|
||||||
public final addr:String;
|
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;
|
private final native:cc.periphs.Modem.Modem;
|
||||||
|
|
||||||
@:allow(kernel.peripherals)
|
@:allow(kernel.peripherals)
|
||||||
@ -25,7 +25,7 @@ class Modem implements INetworkInterface implements IPeripheral {
|
|||||||
KernelEvents.instance.onModemMessage.handle(params ->{
|
KernelEvents.instance.onModemMessage.handle(params ->{
|
||||||
try{
|
try{
|
||||||
if (params.addr == this.addr){
|
if (params.addr == this.addr){
|
||||||
var pack:Package = {
|
var pack:GenericPackage = {
|
||||||
fromID: params.message.fromID,
|
fromID: params.message.fromID,
|
||||||
toID: params.message.toID,
|
toID: params.message.toID,
|
||||||
msgID: params.message.msgID,
|
msgID: params.message.msgID,
|
||||||
|
Loading…
Reference in New Issue
Block a user