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)