diff --git a/src/kernel/KernelEvents.hx b/src/kernel/KernelEvents.hx index 9c54528..3a16297 100644 --- a/src/kernel/KernelEvents.hx +++ b/src/kernel/KernelEvents.hx @@ -34,7 +34,7 @@ class KernelEvents { channel:Int, replyChannel:Int, message:Dynamic, - distance:Int + distance:Null }>; public final onMonitorResize:Signal; public final onMonitorTouch:Signal<{addr:String, pos:Pos}>; @@ -71,7 +71,7 @@ class KernelEvents { channel:Int, replyChannel:Int, message:Dynamic, - distance:Int + distance:Null }> = Signal.trigger(); private final onMonitorResizeTrigger:SignalTrigger = Signal.trigger(); private final onMonitorTouchTrigger:SignalTrigger<{addr:String, pos:Pos}> = Signal.trigger(); diff --git a/src/kernel/net/INetworkInterface.hx b/src/kernel/net/INetworkInterface.hx index f3dd8eb..6a71108 100644 --- a/src/kernel/net/INetworkInterface.hx +++ b/src/kernel/net/INetworkInterface.hx @@ -13,5 +13,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; + public var onMessage (default, null): Signal<{pack:Package,?dist:Float}>; } diff --git a/src/kernel/net/Loopback.hx b/src/kernel/net/Loopback.hx index 07e29e8..12e1308 100644 --- a/src/kernel/net/Loopback.hx +++ b/src/kernel/net/Loopback.hx @@ -9,9 +9,9 @@ using tink.CoreApi; class Loopback implements INetworkInterface { public static final instance:Loopback = new Loopback(); - public var onMessage(default, null):Signal; + public var onMessage(default, null):Signal<{pack:Package,dist:Null}>; - private final onMessageTrigger: SignalTrigger = Signal.trigger(); + private final onMessageTrigger: SignalTrigger<{pack:Package,dist:Null}> = Signal.trigger(); private var openChans: Array = []; private function new() { @@ -38,7 +38,7 @@ class Loopback implements INetworkInterface { public function send(chan:Int, replyChan:Int, payload:Any) { if (this.openChans.contains(chan)){ - this.onMessageTrigger.trigger(payload); + this.onMessageTrigger.trigger({pack:payload,dist:null}); }else{ Log.silly("Loopback got package on non open channel"); } diff --git a/src/kernel/net/Net.hx b/src/kernel/net/Net.hx index fd96a14..a6489ca 100644 --- a/src/kernel/net/Net.hx +++ b/src/kernel/net/Net.hx @@ -60,7 +60,7 @@ class Net { } private function setupInterf(interf: INetworkInterface) { - interf.onMessage.handle(pack -> handle(pack,interf)); + interf.onMessage.handle(e -> handle(e.pack,interf,e.dist)); interf.listen(networkID); interf.listen(BRODCAST_PORT); } @@ -68,7 +68,7 @@ class Net { /** Called when a new package comes in. **/ - private function handle(pack:Package,interf: INetworkInterface) { + private function handle(pack:Package,interf: INetworkInterface, ?dist: Float) { if (pack.toID == this.networkID || pack.toID == Net.BRODCAST_PORT){ switch pack.type { case Data(_) | DataNoResponse(_): diff --git a/src/kernel/peripherals/Modem.hx b/src/kernel/peripherals/Modem.hx index 5a0f7a5..ef9e5e2 100644 --- a/src/kernel/peripherals/Modem.hx +++ b/src/kernel/peripherals/Modem.hx @@ -1,5 +1,6 @@ package kernel.peripherals; +import kernel.log.Log; import kernel.net.Package; import kernel.net.INetworkInterface; @@ -7,9 +8,9 @@ using tink.CoreApi; class Modem implements INetworkInterface implements IPeripheral { public final addr:String; - public var onMessage(default, null):Signal; + public var onMessage(default, null):Signal<{pack:Package,dist:Null}>; - private final onMessageTrigger:SignalTrigger = Signal.trigger(); + private final onMessageTrigger:SignalTrigger<{pack:Package,dist:Null}> = Signal.trigger(); private final native:cc.periphs.Modem.Modem; @:allow(kernel.peripherals) @@ -19,17 +20,21 @@ class Modem implements INetworkInterface implements IPeripheral { this.addr = addr; KernelEvents.instance.onModemMessage.handle(params ->{ - if (params.addr == this.addr){ - var pack:Package = { - fromID: params.message.fromID, - toID: params.message.toID, - msgID: params.message.msgID, - type: params.message.type, - data: params.message.data, - ttl: params.message.ttl, - }; + try{ + if (params.addr == this.addr){ + var pack:Package = { + fromID: params.message.fromID, + toID: params.message.toID, + msgID: params.message.msgID, + type: params.message.type, + data: params.message.data, + ttl: params.message.ttl, + }; - this.onMessageTrigger.trigger(pack); + this.onMessageTrigger.trigger({pack: pack, dist: params.distance}); + } + }catch(e:Dynamic){ + Log.error("Error while parsing modem message"); } }); }