From cbc7b73b1d1d9d40c6265651afbf23e22185e6f3 Mon Sep 17 00:00:00 2001 From: Jakob Ketterl Date: Sat, 28 Sep 2019 00:25:36 +0200 Subject: [PATCH] hand over message handling after initial handshake instead of delegating --- owrx/connection.py | 84 ++++++++++++++++++++++++---------------------- owrx/websocket.py | 5 ++- 2 files changed, 48 insertions(+), 41 deletions(-) diff --git a/owrx/connection.py b/owrx/connection.py index b667a53..b2df9f9 100644 --- a/owrx/connection.py +++ b/owrx/connection.py @@ -39,6 +39,12 @@ class Client(object): def mp_send(self, data): self.multiprocessingPipe.put(data, block=False) + def handleBinaryMessage(self, conn, data): + logger.error("unsupported binary message, discarding") + + def handleClose(self): + self.close() + class OpenWebRxReceiverClient(Client): config_keys = [ @@ -100,6 +106,35 @@ class OpenWebRxReceiverClient(Client): CpuUsageThread.getSharedInstance().add_client(self) + def handleTextMessage(self, conn, message): + try: + message = json.loads(message) + if "type" in message: + if message["type"] == "dspcontrol": + if "action" in message and message["action"] == "start": + self.startDsp() + + if "params" in message: + params = message["params"] + self.setDspProperties(params) + + if message["type"] == "config": + if "params" in message: + self.setParams(message["params"]) + if message["type"] == "setsdr": + if "params" in message: + self.setSdr(message["params"]["sdr"]) + if message["type"] == "selectprofile": + if "params" in message and "profile" in message["params"]: + profile = message["params"]["profile"].split("|") + self.setSdr(profile[0]) + self.sdr.activateProfile(profile[1]) + else: + logger.warning("received message without type: {0}".format(message)) + + except json.JSONDecodeError: + logger.warning("message is not json: {0}".format(message)) + def setSdr(self, id=None): next = SdrService.getSource(id) if next == self.sdr: @@ -229,6 +264,9 @@ class MapConnection(Client): Map.getSharedInstance().addClient(self) + def handleTextMessage(self, conn, message): + pass + def close(self): Map.getSharedInstance().removeClient(self) super().close() @@ -243,8 +281,6 @@ class MapConnection(Client): class WebSocketMessageHandler(object): def __init__(self): self.handshake = None - self.client = None - self.dsp = None def handleTextMessage(self, conn, message): if message[:16] == "SERVER DE CLIENT": @@ -256,50 +292,18 @@ class WebSocketMessageHandler(object): if "type" in self.handshake: if self.handshake["type"] == "receiver": - self.client = OpenWebRxReceiverClient(conn) + client = OpenWebRxReceiverClient(conn) if self.handshake["type"] == "map": - self.client = MapConnection(conn) + client = MapConnection(conn) # backwards compatibility else: - self.client = OpenWebRxReceiverClient(conn) + client = OpenWebRxReceiverClient(conn) + + # hand off all further communication to the correspondig connection + conn.setMessageHandler(client) return if not self.handshake: logger.warning("not answering client request since handshake is not complete") return - - try: - message = json.loads(message) - if "type" in message: - if message["type"] == "dspcontrol": - if "action" in message and message["action"] == "start": - self.client.startDsp() - - if "params" in message: - params = message["params"] - self.client.setDspProperties(params) - - if message["type"] == "config": - if "params" in message: - self.client.setParams(message["params"]) - if message["type"] == "setsdr": - if "params" in message: - self.client.setSdr(message["params"]["sdr"]) - if message["type"] == "selectprofile": - if "params" in message and "profile" in message["params"]: - profile = message["params"]["profile"].split("|") - self.client.setSdr(profile[0]) - self.client.sdr.activateProfile(profile[1]) - else: - logger.warning("received message without type: {0}".format(message)) - - except json.JSONDecodeError: - logger.warning("message is not json: {0}".format(message)) - - def handleBinaryMessage(self, conn, data): - logger.error("unsupported binary message, discarding") - - def handleClose(self): - if self.client: - self.client.close() diff --git a/owrx/websocket.py b/owrx/websocket.py index d0e4993..020cc23 100644 --- a/owrx/websocket.py +++ b/owrx/websocket.py @@ -38,7 +38,7 @@ class WebSocketConnection(object): def __init__(self, handler, messageHandler): self.handler = handler self.handler.connection.setblocking(0) - self.messageHandler = messageHandler + self.setMessageHandler(messageHandler) (self.interruptPipeRecv, self.interruptPipeSend) = Pipe(duplex=False) self.open = True self.sendLock = threading.Lock() @@ -64,6 +64,9 @@ class WebSocketConnection(object): self.pingTimer = None self.resetPing() + def setMessageHandler(self, messageHandler): + self.messageHandler = messageHandler + def get_header(self, size, opcode): ws_first_byte = 0b10000000 | (opcode & 0x0F) if size > 2 ** 16 - 1: