diff --git a/owrx/connection.py b/owrx/connection.py index 5fb77bb..f4146b2 100644 --- a/owrx/connection.py +++ b/owrx/connection.py @@ -495,35 +495,35 @@ class MapConnection(OpenWebRxClient): self.mp_send({"type": "update", "value": update}) -class WebSocketMessageHandler(Handler): - def __init__(self): - self.handshake = None - +class HandshakeMessageHandler(Handler): + """ + This handler receives text messages, but will only respond to the second handshake string. + As soon as a valid handshake is received, the handler replaces itself with the corresponding handler type. + """ def handleTextMessage(self, conn, message): if message[:16] == "SERVER DE CLIENT": meta = message[17:].split(" ") - self.handshake = {v[0]: "=".join(v[1:]) for v in map(lambda x: x.split("="), meta)} + handshake = {v[0]: "=".join(v[1:]) for v in map(lambda x: x.split("="), meta)} - conn.send("CLIENT DE SERVER server=openwebrx version={version}".format(version=openwebrx_version)) logger.debug("client connection initialized") - if "type" in self.handshake: - if self.handshake["type"] == "receiver": + client = None + if "type" in handshake: + if handshake["type"] == "receiver": client = OpenWebRxReceiverClient(conn) - if self.handshake["type"] == "map": + elif handshake["type"] == "map": client = MapConnection(conn) - # backwards compatibility + else: + logger.warning("invalid connection type: %s", handshake["type"]) + + if client is not None: + # hand off all further communication to the correspondig connection + conn.send("CLIENT DE SERVER server=openwebrx version={version}".format(version=openwebrx_version)) + conn.setMessageHandler(client) else: - client = OpenWebRxReceiverClient(conn) - - # hand off all further communication to the correspondig connection - conn.setMessageHandler(client) - - return - - if not self.handshake: + logger.warning('invalid handshake received') + else: logger.warning("not answering client request since handshake is not complete") - return def handleBinaryMessage(self, conn, data): pass diff --git a/owrx/controllers/websocket.py b/owrx/controllers/websocket.py index f242f2c..3363abf 100644 --- a/owrx/controllers/websocket.py +++ b/owrx/controllers/websocket.py @@ -1,10 +1,10 @@ from . import Controller from owrx.websocket import WebSocketConnection -from owrx.connection import WebSocketMessageHandler +from owrx.connection import HandshakeMessageHandler class WebSocketController(Controller): def indexAction(self): - conn = WebSocketConnection(self.handler, WebSocketMessageHandler()) + conn = WebSocketConnection(self.handler, HandshakeMessageHandler()) # enter read loop conn.handle()