cc-haxe/src/kernel/peripherals/Modem.hx
2022-03-01 16:59:53 +01:00

73 lines
1.6 KiB
Haxe

package kernel.peripherals;
using tink.CoreApi;
import kernel.net.Package;
import kernel.net.INetworkInterface;
class Modem implements INetworkInterface implements IPeripheral {
public final addr:String;
public var onMessage(default, null):Signal<Package>;
private final onMessageTrigger:SignalTrigger<Package> = Signal.trigger();
private final native:cc.periphs.Modem.Modem;
@:allow(kernel.peripherals)
private function new(nativePeripherals:cc.periphs.Modem.Modem, addr:String) {
this.onMessage = onMessageTrigger.asSignal();
this.native = nativePeripherals;
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,
};
this.onMessageTrigger.trigger(pack);
}
});
}
public function listen(chan:Int) {
native.open(chan);
}
public function isListening(chan:Int):Bool {
return native.isOpen(chan);
}
public function closeAll() {
native.closeAll();
}
public function send(chan:Int, replyChan:Int, payload:Any) {
native.transmit(chan, replyChan, payload);
}
public function name():String {
return addr;
}
public function close(chan:Int) {
native.close(chan);
}
public function getAddr():String {
return this.addr;
}
public function getBaseRoutingCost():Int {
if (this.native.isWireless()){
return 2; // Prefere messages over cable
}else{
return 1;
}
}
}