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; private final onMessageTrigger:SignalTrigger = 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, }; 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; } } }