Move TFWRouter to separate file

This commit is contained in:
Kristóf Tóth 2019-07-24 17:22:08 +02:00
parent 42f959878f
commit 3fca555251
2 changed files with 23 additions and 22 deletions

View File

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

View File

@ -3,7 +3,7 @@ import logging
from tornado.websocket import WebSocketHandler from tornado.websocket import WebSocketHandler
from tfw.internals.networking import Scope from .tfw_router import TFWRouter
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
@ -46,24 +46,3 @@ class ZMQWebSocketRouter(WebSocketHandler):
# much secure, very cors, wow # much secure, very cors, wow
def check_origin(self, origin): def check_origin(self, origin):
return True 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)