introduce a websocket handler interface

This commit is contained in:
Jakob Ketterl 2021-05-18 15:42:30 +02:00
parent 9baebf444d
commit 3e7eb09f3e
2 changed files with 21 additions and 4 deletions

View File

@ -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

View File

@ -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):