added distance to network message

This commit is contained in:
Djeeberjr 2023-03-28 00:54:18 +02:00
parent 409c4fb411
commit f7c320c123
5 changed files with 25 additions and 20 deletions

View File

@ -34,7 +34,7 @@ class KernelEvents {
channel:Int, channel:Int,
replyChannel:Int, replyChannel:Int,
message:Dynamic, message:Dynamic,
distance:Int distance:Null<Float>
}>; }>;
public final onMonitorResize:Signal<String>; public final onMonitorResize:Signal<String>;
public final onMonitorTouch:Signal<{addr:String, pos:Pos}>; public final onMonitorTouch:Signal<{addr:String, pos:Pos}>;
@ -71,7 +71,7 @@ class KernelEvents {
channel:Int, channel:Int,
replyChannel:Int, replyChannel:Int,
message:Dynamic, message:Dynamic,
distance:Int distance:Null<Float>
}> = Signal.trigger(); }> = Signal.trigger();
private final onMonitorResizeTrigger:SignalTrigger<String> = Signal.trigger(); private final onMonitorResizeTrigger:SignalTrigger<String> = Signal.trigger();
private final onMonitorTouchTrigger:SignalTrigger<{addr:String, pos:Pos}> = Signal.trigger(); private final onMonitorTouchTrigger:SignalTrigger<{addr:String, pos:Pos}> = Signal.trigger();

View File

@ -13,5 +13,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<Package>; public var onMessage (default, null): Signal<{pack:Package,?dist:Float}>;
} }

View File

@ -9,9 +9,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<Package>; public var onMessage(default, null):Signal<{pack:Package,dist:Null<Float>}>;
private final onMessageTrigger: SignalTrigger<Package> = Signal.trigger(); private final onMessageTrigger: SignalTrigger<{pack:Package,dist:Null<Float>}> = Signal.trigger();
private var openChans: Array<Int> = []; private var openChans: Array<Int> = [];
private function new() { private function new() {
@ -38,7 +38,7 @@ class Loopback implements INetworkInterface {
public function send(chan:Int, replyChan:Int, payload:Any) { public function send(chan:Int, replyChan:Int, payload:Any) {
if (this.openChans.contains(chan)){ if (this.openChans.contains(chan)){
this.onMessageTrigger.trigger(payload); this.onMessageTrigger.trigger({pack:payload,dist:null});
}else{ }else{
Log.silly("Loopback got package on non open channel"); Log.silly("Loopback got package on non open channel");
} }

View File

@ -60,7 +60,7 @@ class Net {
} }
private function setupInterf(interf: INetworkInterface) { 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(networkID);
interf.listen(BRODCAST_PORT); interf.listen(BRODCAST_PORT);
} }
@ -68,7 +68,7 @@ class Net {
/** /**
Called when a new package comes in. 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){ if (pack.toID == this.networkID || pack.toID == Net.BRODCAST_PORT){
switch pack.type { switch pack.type {
case Data(_) | DataNoResponse(_): case Data(_) | DataNoResponse(_):

View File

@ -1,5 +1,6 @@
package kernel.peripherals; package kernel.peripherals;
import kernel.log.Log;
import kernel.net.Package; import kernel.net.Package;
import kernel.net.INetworkInterface; import kernel.net.INetworkInterface;
@ -7,9 +8,9 @@ using tink.CoreApi;
class Modem implements INetworkInterface implements IPeripheral { class Modem implements INetworkInterface implements IPeripheral {
public final addr:String; public final addr:String;
public var onMessage(default, null):Signal<Package>; public var onMessage(default, null):Signal<{pack:Package,dist:Null<Float>}>;
private final onMessageTrigger:SignalTrigger<Package> = Signal.trigger(); private final onMessageTrigger:SignalTrigger<{pack:Package,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)
@ -19,17 +20,21 @@ class Modem implements INetworkInterface implements IPeripheral {
this.addr = addr; this.addr = addr;
KernelEvents.instance.onModemMessage.handle(params ->{ KernelEvents.instance.onModemMessage.handle(params ->{
if (params.addr == this.addr){ try{
var pack:Package = { if (params.addr == this.addr){
fromID: params.message.fromID, var pack:Package = {
toID: params.message.toID, fromID: params.message.fromID,
msgID: params.message.msgID, toID: params.message.toID,
type: params.message.type, msgID: params.message.msgID,
data: params.message.data, type: params.message.type,
ttl: params.message.ttl, 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");
} }
}); });
} }