diff --git a/owrx/connection.py b/owrx/connection.py index 798c994..5fb77bb 100644 --- a/owrx/connection.py +++ b/owrx/connection.py @@ -13,6 +13,7 @@ from owrx.property import PropertyStack, PropertyDeleted from owrx.modes import Modes, DigitalMode from owrx.config import Config from owrx.waterfall import WaterfallOptions +from owrx.websocket import Handler from queue import Queue, Full, Empty from js8py import Js8Frame from abc import ABC, ABCMeta, abstractmethod @@ -26,7 +27,7 @@ logger = logging.getLogger(__name__) PoisonPill = object() -class Client(ABC): +class Client(Handler, metaclass=ABCMeta): def __init__(self, conn): self.conn = conn self.multithreadingQueue = Queue(100) @@ -494,7 +495,7 @@ class MapConnection(OpenWebRxClient): self.mp_send({"type": "update", "value": update}) -class WebSocketMessageHandler(object): +class WebSocketMessageHandler(Handler): def __init__(self): self.handshake = None diff --git a/owrx/websocket.py b/owrx/websocket.py index 953cc71..b841bb6 100644 --- a/owrx/websocket.py +++ b/owrx/websocket.py @@ -5,6 +5,7 @@ import json from multiprocessing import Pipe import select import threading +from abc import ABC, abstractmethod import logging @@ -33,6 +34,20 @@ class WebSocketClosed(WebSocketException): pass +class Handler(ABC): + @abstractmethod + def handleTextMessage(self, connection, message: str): + pass + + @abstractmethod + def handleBinaryMessage(self, connection, data: bytes): + pass + + @abstractmethod + def handleClose(self): + pass + + class WebSocketConnection(object): connections = [] @@ -44,9 +59,10 @@ class WebSocketConnection(object): except: logger.exception("exception while shutting down websocket connections") - def __init__(self, handler, messageHandler): + def __init__(self, handler, messageHandler: Handler): self.handler = handler self.handler.connection.setblocking(0) + self.messageHandler = None self.setMessageHandler(messageHandler) (self.interruptPipeRecv, self.interruptPipeSend) = Pipe(duplex=False) self.open = True @@ -72,7 +88,7 @@ class WebSocketConnection(object): self.pingTimer = None self.resetPing() - def setMessageHandler(self, messageHandler): + def setMessageHandler(self, messageHandler: Handler): self.messageHandler = messageHandler def get_header(self, size, opcode):