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)