mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-12 19:47:17 +00:00
64 lines
2.1 KiB
Python
64 lines
2.1 KiB
Python
import json
|
|
|
|
from tornado.websocket import WebSocketHandler
|
|
|
|
from .serialization import deserialize_all
|
|
from .event_handler_connector import EventHandlerConnector
|
|
from ..config.logs import logging
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
class ZMQWebSocketHandler(WebSocketHandler):
|
|
_event_handler_connector = EventHandlerConnector()
|
|
|
|
def __init__(self, application, request, **kwargs):
|
|
super().__init__(application, request, **kwargs)
|
|
|
|
def open(self, *args, **kwargs):
|
|
log.debug('WebSocket connection initiated')
|
|
self._event_handler_connector.register_callback(self.zmq_callback)
|
|
|
|
def zmq_callback(self, msg_parts):
|
|
anchor, data = deserialize_all(*msg_parts)
|
|
log.debug('Received on pull socket: {}'.format(data))
|
|
self.write_message(data)
|
|
|
|
def on_message(self, message):
|
|
log.debug('Received on WebSocket: {}'.format(message))
|
|
self.send_message(*self.make_response(json.loads(message)))
|
|
|
|
def make_response(self, message):
|
|
raise NotImplementedError
|
|
|
|
def send_message(self, message: dict, anchor: str = None):
|
|
self._event_handler_connector.send_message(message, anchor)
|
|
|
|
def on_close(self):
|
|
pass
|
|
|
|
# much secure, very cors, wow
|
|
def check_origin(self, origin):
|
|
return True
|
|
|
|
|
|
class FSMManagingSocketHandler(ZMQWebSocketHandler):
|
|
fsm = None
|
|
|
|
def __init__(self, FSM, application, request, **kwargs):
|
|
super().__init__(application, request, **kwargs)
|
|
if self.fsm is None:
|
|
FSMManagingSocketHandler.fsm = FSM()
|
|
self.fsm.subscribe_message_handler(self.handle_fsm_message)
|
|
|
|
def handle_fsm_message(self, message):
|
|
self._event_handler_connector.send_message(message)
|
|
|
|
def make_response(self, message):
|
|
self.fsm.trigger(message['anchor'], message=message)
|
|
anchor = 'FSMUpdate'
|
|
response = {'anchor': anchor,
|
|
'data': {'current_state': self.fsm.state,
|
|
'valid_transitions':
|
|
[{'trigger': trigger} for trigger in self.fsm.machine.get_triggers()]}}
|
|
return response, anchor
|