2021-12-20 00:55:30 +00:00
|
|
|
package kernel.peripherals;
|
|
|
|
|
2022-02-24 18:47:27 +00:00
|
|
|
using tink.CoreApi;
|
2021-12-20 00:55:30 +00:00
|
|
|
|
2022-02-24 18:47:27 +00:00
|
|
|
import kernel.net.Package;
|
|
|
|
import kernel.net.INetworkInterface;
|
2021-12-20 00:55:30 +00:00
|
|
|
|
2022-02-24 18:47:27 +00:00
|
|
|
class Modem implements INetworkInterface implements IPeripheral {
|
2021-12-20 00:55:30 +00:00
|
|
|
public final addr:String;
|
2022-02-24 18:47:27 +00:00
|
|
|
public var onMessage(default, null):Signal<Package>;
|
|
|
|
|
|
|
|
private final onMessageTrigger:SignalTrigger<Package> = Signal.trigger();
|
|
|
|
private final native:cc.periphs.Modem.Modem;
|
2021-12-20 00:55:30 +00:00
|
|
|
|
|
|
|
@:allow(kernel.peripherals)
|
2022-02-21 14:35:37 +00:00
|
|
|
private function new(nativePeripherals:cc.periphs.Modem.Modem, addr:String) {
|
2022-02-24 18:47:27 +00:00
|
|
|
this.onMessage = onMessageTrigger.asSignal();
|
|
|
|
this.native = nativePeripherals;
|
2021-12-20 00:55:30 +00:00
|
|
|
this.addr = addr;
|
|
|
|
|
2022-02-24 18:47:27 +00:00
|
|
|
KernelEvents.instance.onModemMessage.handle(params ->{
|
|
|
|
if (params.addr == this.addr){
|
|
|
|
var pack:Package = {
|
2022-03-01 11:59:23 +00:00
|
|
|
fromID: params.message.fromID,
|
|
|
|
toID: params.message.toID,
|
2022-02-24 18:47:27 +00:00
|
|
|
msgID: params.message.msgID,
|
|
|
|
type: params.message.type,
|
|
|
|
data: params.message.data,
|
2022-03-01 15:59:53 +00:00
|
|
|
ttl: params.message.ttl,
|
2022-02-24 18:47:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.onMessageTrigger.trigger(pack);
|
|
|
|
}
|
|
|
|
});
|
2021-12-20 00:55:30 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 18:47:27 +00:00
|
|
|
public function listen(chan:Int) {
|
|
|
|
native.open(chan);
|
2021-12-20 00:55:30 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 18:47:27 +00:00
|
|
|
public function isListening(chan:Int):Bool {
|
|
|
|
return native.isOpen(chan);
|
2021-12-20 00:55:30 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 18:47:27 +00:00
|
|
|
public function closeAll() {
|
|
|
|
native.closeAll();
|
2021-12-20 00:55:30 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 18:47:27 +00:00
|
|
|
public function send(chan:Int, replyChan:Int, payload:Any) {
|
|
|
|
native.transmit(chan, replyChan, payload);
|
2021-12-20 00:55:30 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 18:47:27 +00:00
|
|
|
public function name():String {
|
|
|
|
return addr;
|
2021-12-20 00:55:30 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 18:47:27 +00:00
|
|
|
public function close(chan:Int) {
|
|
|
|
native.close(chan);
|
2021-12-20 00:55:30 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 18:47:27 +00:00
|
|
|
public function getAddr():String {
|
|
|
|
return this.addr;
|
2021-12-20 00:55:30 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 18:47:27 +00:00
|
|
|
public function getBaseRoutingCost():Int {
|
|
|
|
if (this.native.isWireless()){
|
|
|
|
return 2; // Prefere messages over cable
|
|
|
|
}else{
|
|
|
|
return 1;
|
2021-12-20 00:55:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|