baseimage-tutorial-framework/tfw/internals/server/tfw_router.py

23 lines
641 B
Python
Raw Permalink Normal View History

2019-07-24 15:22:08 +00:00
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)