2018-01-26 14:15:05 +00:00
|
|
|
import json
|
2017-11-17 14:47:22 +00:00
|
|
|
from tornado.websocket import WebSocketHandler
|
2017-11-27 17:16:35 +00:00
|
|
|
from util import parse_anchor_from_message
|
2017-11-17 14:47:22 +00:00
|
|
|
|
2018-01-29 16:24:17 +00:00
|
|
|
from event_handler_connector import EventHandlerConnector
|
2018-01-29 17:36:29 +00:00
|
|
|
from config.logs import logging
|
|
|
|
log = logging.getLogger(__name__)
|
2018-01-29 16:24:17 +00:00
|
|
|
|
2017-11-17 14:47:22 +00:00
|
|
|
|
|
|
|
class ZMQWebSocketHandler(WebSocketHandler):
|
2018-01-29 17:03:54 +00:00
|
|
|
_event_handler_connector = EventHandlerConnector() #<--------------------. TODO: how is __init__()
|
|
|
|
# fix: made it static | TODO: called more than once?
|
|
|
|
def __init__(self, application, request, **kwargs): #---------------------'
|
2017-11-17 14:47:22 +00:00
|
|
|
super().__init__(application, request, **kwargs)
|
|
|
|
|
|
|
|
def open(self, *args, **kwargs):
|
2018-01-29 17:36:29 +00:00
|
|
|
log.debug('WebSocket connection initiated')
|
2018-01-29 16:24:17 +00:00
|
|
|
self._event_handler_connector.register_callback(self.zmq_callback)
|
2017-12-20 10:34:21 +00:00
|
|
|
|
2018-01-17 13:58:01 +00:00
|
|
|
def zmq_callback(self, msg_parts):
|
|
|
|
anchor, data = msg_parts
|
2018-01-29 17:36:29 +00:00
|
|
|
log.debug('Received on pull socket: {}'.format(data.decode()))
|
2018-01-17 13:58:01 +00:00
|
|
|
self.write_message(data.decode())
|
2017-11-17 14:47:22 +00:00
|
|
|
|
|
|
|
def on_message(self, message):
|
2018-01-29 17:36:29 +00:00
|
|
|
log.debug('Received on WebSocket: {}'.format(message))
|
2018-01-29 13:09:40 +00:00
|
|
|
self.send_message(*self.make_response(message))
|
|
|
|
|
|
|
|
def make_response(self, message):
|
|
|
|
raise NotImplementedError
|
2017-11-17 14:47:22 +00:00
|
|
|
|
2017-11-27 17:16:35 +00:00
|
|
|
def send_message(self, message: str, anchor: str = None):
|
2018-01-29 16:24:17 +00:00
|
|
|
self._event_handler_connector.send_message(message, anchor)
|
2017-11-17 14:47:22 +00:00
|
|
|
|
|
|
|
def on_close(self):
|
2017-11-27 17:16:35 +00:00
|
|
|
pass
|
2017-11-17 14:47:22 +00:00
|
|
|
|
|
|
|
# much secure, very cors, wow
|
|
|
|
def check_origin(self, origin):
|
|
|
|
return True
|
|
|
|
|
2018-01-29 13:09:40 +00:00
|
|
|
|
|
|
|
class FSMManagingSocketHandler(ZMQWebSocketHandler):
|
|
|
|
def __init__(self, FSM, application, request, **kwargs):
|
|
|
|
super().__init__(application, request, **kwargs)
|
|
|
|
self.fsm = FSM()
|
2018-01-29 16:24:17 +00:00
|
|
|
self.fsm.subscribe_message_handler(self.handle_fsm_message)
|
|
|
|
|
|
|
|
def handle_fsm_message(self, message):
|
|
|
|
self._event_handler_connector.send_message(message)
|
2018-01-29 13:09:40 +00:00
|
|
|
|
|
|
|
def make_response(self, message):
|
|
|
|
self.fsm.trigger(parse_anchor_from_message(message), 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 json.dumps(response), anchor
|