improve handshake handling
This commit is contained in:
parent
3e7eb09f3e
commit
48dc75c728
@ -495,35 +495,35 @@ class MapConnection(OpenWebRxClient):
|
|||||||
self.mp_send({"type": "update", "value": update})
|
self.mp_send({"type": "update", "value": update})
|
||||||
|
|
||||||
|
|
||||||
class WebSocketMessageHandler(Handler):
|
class HandshakeMessageHandler(Handler):
|
||||||
def __init__(self):
|
"""
|
||||||
self.handshake = None
|
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):
|
def handleTextMessage(self, conn, message):
|
||||||
if message[:16] == "SERVER DE CLIENT":
|
if message[:16] == "SERVER DE CLIENT":
|
||||||
meta = message[17:].split(" ")
|
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")
|
logger.debug("client connection initialized")
|
||||||
|
|
||||||
if "type" in self.handshake:
|
client = None
|
||||||
if self.handshake["type"] == "receiver":
|
if "type" in handshake:
|
||||||
|
if handshake["type"] == "receiver":
|
||||||
client = OpenWebRxReceiverClient(conn)
|
client = OpenWebRxReceiverClient(conn)
|
||||||
if self.handshake["type"] == "map":
|
elif handshake["type"] == "map":
|
||||||
client = MapConnection(conn)
|
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:
|
else:
|
||||||
client = OpenWebRxReceiverClient(conn)
|
logger.warning('invalid handshake received')
|
||||||
|
else:
|
||||||
# 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")
|
logger.warning("not answering client request since handshake is not complete")
|
||||||
return
|
|
||||||
|
|
||||||
def handleBinaryMessage(self, conn, data):
|
def handleBinaryMessage(self, conn, data):
|
||||||
pass
|
pass
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
from . import Controller
|
from . import Controller
|
||||||
from owrx.websocket import WebSocketConnection
|
from owrx.websocket import WebSocketConnection
|
||||||
from owrx.connection import WebSocketMessageHandler
|
from owrx.connection import HandshakeMessageHandler
|
||||||
|
|
||||||
|
|
||||||
class WebSocketController(Controller):
|
class WebSocketController(Controller):
|
||||||
def indexAction(self):
|
def indexAction(self):
|
||||||
conn = WebSocketConnection(self.handler, WebSocketMessageHandler())
|
conn = WebSocketConnection(self.handler, HandshakeMessageHandler())
|
||||||
# enter read loop
|
# enter read loop
|
||||||
conn.handle()
|
conn.handle()
|
||||||
|
Loading…
Reference in New Issue
Block a user