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

View File

@ -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(_):

View File

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