From 3fca5552519db222ec82e5639ac672ad884631eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Wed, 24 Jul 2019 17:22:08 +0200 Subject: [PATCH] Move TFWRouter to separate file --- tfw/internals/server/tfw_router.py | 22 +++++++++++++++++++ tfw/internals/server/zmq_websocket_router.py | 23 +------------------- 2 files changed, 23 insertions(+), 22 deletions(-) create mode 100644 tfw/internals/server/tfw_router.py diff --git a/tfw/internals/server/tfw_router.py b/tfw/internals/server/tfw_router.py new file mode 100644 index 0000000..c2a18ab --- /dev/null +++ b/tfw/internals/server/tfw_router.py @@ -0,0 +1,22 @@ +from tfw.internals.networking import Scope + + +class TFWRouter: + def __init__(self, send_to_zmq, send_to_websockets): + self.send_to_zmq = send_to_zmq + self.send_to_websockets = send_to_websockets + + def route(self, message): + scope = Scope(message.pop('scope', 'zmq')) + + routing_table = { + Scope.ZMQ: self.send_to_zmq, + Scope.WEBSOCKET: self.send_to_websockets, + Scope.BROADCAST: self.broadcast + } + action = routing_table[scope] + action(message) + + def broadcast(self, message): + self.send_to_zmq(message) + self.send_to_websockets(message) diff --git a/tfw/internals/server/zmq_websocket_router.py b/tfw/internals/server/zmq_websocket_router.py index cdafc2f..3f5e7fc 100644 --- a/tfw/internals/server/zmq_websocket_router.py +++ b/tfw/internals/server/zmq_websocket_router.py @@ -3,7 +3,7 @@ import logging from tornado.websocket import WebSocketHandler -from tfw.internals.networking import Scope +from .tfw_router import TFWRouter LOG = logging.getLogger(__name__) @@ -46,24 +46,3 @@ class ZMQWebSocketRouter(WebSocketHandler): # much secure, very cors, wow def check_origin(self, origin): return True - - -class TFWRouter: - def __init__(self, send_to_zmq, send_to_websockets): - self.send_to_zmq = send_to_zmq - self.send_to_websockets = send_to_websockets - - def route(self, message): - scope = Scope(message.pop('scope', 'zmq')) - - routing_table = { - Scope.ZMQ: self.send_to_zmq, - Scope.WEBSOCKET: self.send_to_websockets, - Scope.BROADCAST: self.broadcast - } - action = routing_table[scope] - action(message) - - def broadcast(self, message): - self.send_to_zmq(message) - self.send_to_websockets(message)