mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-23 02:41:31 +00:00
Overhaul WS->ZMQ proxying
This commit is contained in:
parent
26538a4ad9
commit
ecbb61a862
@ -7,7 +7,7 @@ class FSMBase:
|
|||||||
states, transitions = [], []
|
states, transitions = [], []
|
||||||
|
|
||||||
def __init__(self, initial: str = None, accepted_states: List[str] = None):
|
def __init__(self, initial: str = None, accepted_states: List[str] = None):
|
||||||
self.message_handlers = []
|
self.callbacks = []
|
||||||
self.accepted_states = accepted_states or [self.states[-1]]
|
self.accepted_states = accepted_states or [self.states[-1]]
|
||||||
self.machine = Machine(model=self,
|
self.machine = Machine(model=self,
|
||||||
states=self.states,
|
states=self.states,
|
||||||
@ -15,18 +15,17 @@ class FSMBase:
|
|||||||
initial=initial or self.states[0],
|
initial=initial or self.states[0],
|
||||||
send_event=True,
|
send_event=True,
|
||||||
ignore_invalid_triggers=True,
|
ignore_invalid_triggers=True,
|
||||||
after_state_change='forward_message')
|
after_state_change='execute_callbacks')
|
||||||
|
|
||||||
def forward_message(self, event_data):
|
def execute_callbacks(self, event_data):
|
||||||
message = event_data.kwargs.get('message')
|
for callback in self.callbacks:
|
||||||
for msghandler in self.message_handlers:
|
callback(event_data.kwargs)
|
||||||
msghandler(message)
|
|
||||||
|
|
||||||
def subscribe_message_handler(self, msghandler):
|
def subscribe(self, callback):
|
||||||
self.message_handlers.append(msghandler)
|
self.callbacks.append(callback)
|
||||||
|
|
||||||
def unsubscribe_message_handler(self, msghandler):
|
def unsubscribe(self, callback):
|
||||||
self.message_handlers.remove(msghandler)
|
self.callbacks.remove(callback)
|
||||||
|
|
||||||
def is_solved(self):
|
def is_solved(self):
|
||||||
return self.state in self.accepted_states
|
return self.state in self.accepted_states
|
||||||
|
@ -1,14 +1,20 @@
|
|||||||
from tornado.web import Application
|
from tornado.web import Application
|
||||||
|
|
||||||
from tfw.networking.server.controller_responder import ControllerResponder
|
from tfw.networking.server.controller_responder import ControllerResponder
|
||||||
from tfw.networking.server.zmq_websocket_handler import FSMManagingSocketHandler
|
from tfw.networking.server.zmq_websocket_handler import ZMQWebSocketProxy
|
||||||
|
from tfw.networking.event_handlers.server_connector import ServerUplinkConnector
|
||||||
|
from tfw.config.logs import logging
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class TFWServer:
|
class TFWServer:
|
||||||
def __init__(self, fsm_type):
|
def __init__(self, fsm_type):
|
||||||
self._fsm = fsm_type()
|
self._fsm = fsm_type()
|
||||||
|
self.uplink = ServerUplinkConnector()
|
||||||
|
self._fsm.subscribe(self.fsm_callback)
|
||||||
self.application = Application(
|
self.application = Application(
|
||||||
[(r'/ws', FSMManagingSocketHandler, {'fsm': self.fsm})]
|
[(r'/ws', ZMQWebSocketProxy, {'make_response': self.make_response,
|
||||||
|
'proxy_filter': self.proxy_filter})]
|
||||||
)
|
)
|
||||||
self.controller_responder = ControllerResponder(self.fsm)
|
self.controller_responder = ControllerResponder(self.fsm)
|
||||||
|
|
||||||
@ -16,5 +22,24 @@ class TFWServer:
|
|||||||
def fsm(self):
|
def fsm(self):
|
||||||
return self._fsm
|
return self._fsm
|
||||||
|
|
||||||
|
def make_response(self, message):
|
||||||
|
return message
|
||||||
|
|
||||||
|
def proxy_filter(self, message):
|
||||||
|
try: return self.fsm.trigger(message['key'], message=message)
|
||||||
|
except AttributeError: return False
|
||||||
|
|
||||||
|
def fsm_callback(self, kwargs_dict):
|
||||||
|
response, key = self.generate_fsm_update() # TODO: unify (key,response) order
|
||||||
|
self.uplink.send(key, response)
|
||||||
|
|
||||||
|
def generate_fsm_update(self):
|
||||||
|
key = 'FSMUpdate'
|
||||||
|
response = {'key': key,
|
||||||
|
'data': {'current_state': self.fsm.state,
|
||||||
|
'valid_transitions':
|
||||||
|
[{'trigger': trigger} for trigger in self.fsm.machine.get_triggers()]}}
|
||||||
|
return response, key
|
||||||
|
|
||||||
def listen(self, port):
|
def listen(self, port):
|
||||||
self.application.listen(port)
|
self.application.listen(port)
|
||||||
|
@ -30,7 +30,7 @@ class ZMQWebSocketHandler(WebSocketHandler):
|
|||||||
|
|
||||||
def on_message(self, message):
|
def on_message(self, message):
|
||||||
log.debug('Received on WebSocket: {}'.format(message))
|
log.debug('Received on WebSocket: {}'.format(message))
|
||||||
self.send_message(*self.make_response(json.loads(message)))
|
self.send_message(self.make_response(message))
|
||||||
|
|
||||||
def make_response(self, message):
|
def make_response(self, message):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
@ -43,23 +43,15 @@ class ZMQWebSocketHandler(WebSocketHandler):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
class FSMManagingSocketHandler(ZMQWebSocketHandler):
|
class ZMQWebSocketProxy(ZMQWebSocketHandler):
|
||||||
def initialize(self, fsm):
|
def initialize(self, make_response, proxy_filter):
|
||||||
self.fsm = fsm
|
self._make_response = make_response
|
||||||
self.fsm.subscribe_message_handler(self.handle_fsm_message)
|
self._proxy_filter = proxy_filter
|
||||||
|
|
||||||
def on_close(self):
|
def on_message(self, message):
|
||||||
super().on_close()
|
message = json.loads(message)
|
||||||
self.fsm.unsubscribe_message_handler(self.handle_fsm_message)
|
if self._proxy_filter(message):
|
||||||
|
super().on_message(message)
|
||||||
def handle_fsm_message(self, message):
|
|
||||||
self._event_handler_connector.send_message(message)
|
|
||||||
|
|
||||||
def make_response(self, message):
|
def make_response(self, message):
|
||||||
self.fsm.trigger(message['key'], message=message)
|
return self._make_response(message)
|
||||||
key = 'FSMUpdate'
|
|
||||||
response = {'key': key,
|
|
||||||
'data': {'current_state': self.fsm.state,
|
|
||||||
'valid_transitions':
|
|
||||||
[{'trigger': trigger} for trigger in self.fsm.machine.get_triggers()]}}
|
|
||||||
return response, key
|
|
||||||
|
Loading…
Reference in New Issue
Block a user