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.modes import Modes, DigitalMode
from owrx.config import Config from owrx.config import Config
from owrx.waterfall import WaterfallOptions from owrx.waterfall import WaterfallOptions
from owrx.websocket import Handler
from queue import Queue, Full, Empty from queue import Queue, Full, Empty
from js8py import Js8Frame from js8py import Js8Frame
from abc import ABC, ABCMeta, abstractmethod from abc import ABC, ABCMeta, abstractmethod
@ -26,7 +27,7 @@ logger = logging.getLogger(__name__)
PoisonPill = object() PoisonPill = object()
class Client(ABC): class Client(Handler, metaclass=ABCMeta):
def __init__(self, conn): def __init__(self, conn):
self.conn = conn self.conn = conn
self.multithreadingQueue = Queue(100) self.multithreadingQueue = Queue(100)
@ -494,7 +495,7 @@ class MapConnection(OpenWebRxClient):
self.mp_send({"type": "update", "value": update}) self.mp_send({"type": "update", "value": update})
class WebSocketMessageHandler(object): class WebSocketMessageHandler(Handler):
def __init__(self): def __init__(self):
self.handshake = None self.handshake = None

View File

@ -5,6 +5,7 @@ import json
from multiprocessing import Pipe from multiprocessing import Pipe
import select import select
import threading import threading
from abc import ABC, abstractmethod
import logging import logging
@ -33,6 +34,20 @@ class WebSocketClosed(WebSocketException):
pass 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): class WebSocketConnection(object):
connections = [] connections = []
@ -44,9 +59,10 @@ class WebSocketConnection(object):
except: except:
logger.exception("exception while shutting down websocket connections") logger.exception("exception while shutting down websocket connections")
def __init__(self, handler, messageHandler): def __init__(self, handler, messageHandler: Handler):
self.handler = handler self.handler = handler
self.handler.connection.setblocking(0) self.handler.connection.setblocking(0)
self.messageHandler = None
self.setMessageHandler(messageHandler) self.setMessageHandler(messageHandler)
(self.interruptPipeRecv, self.interruptPipeSend) = Pipe(duplex=False) (self.interruptPipeRecv, self.interruptPipeSend) = Pipe(duplex=False)
self.open = True self.open = True
@ -72,7 +88,7 @@ class WebSocketConnection(object):
self.pingTimer = None self.pingTimer = None
self.resetPing() self.resetPing()
def setMessageHandler(self, messageHandler): def setMessageHandler(self, messageHandler: Handler):
self.messageHandler = messageHandler self.messageHandler = messageHandler
def get_header(self, size, opcode): def get_header(self, size, opcode):