Refactor socket logic from state machine ¯\_(ツ)_/¯

This commit is contained in:
Kristóf Tóth 2018-01-29 17:24:17 +01:00
parent 9760db7c5c
commit 87a437cfb5
3 changed files with 16 additions and 10 deletions

View File

@ -41,6 +41,3 @@ class EventHandlerConnector(EventHandlerDownlinkConnector, EventHandlerUplinkCon
anchor = parse_anchor_from_message(message) anchor = parse_anchor_from_message(message)
encoded_message = [part.encode('utf-8') for part in (anchor, message)] encoded_message = [part.encode('utf-8') for part in (anchor, message)]
self._zmq_pub_socket.send_multipart(encoded_message) self._zmq_pub_socket.send_multipart(encoded_message)
ehc = EventHandlerConnector()

View File

@ -1,10 +1,8 @@
from transitions import Machine from transitions import Machine
from event_handler_connector import ehc
class FSMBase: class FSMBase:
states, transitions = [], [] states, transitions, message_handlers = [], [], []
def __init__(self, initial: str = None): def __init__(self, initial: str = None):
self.machine = Machine(model=self, self.machine = Machine(model=self,
@ -17,4 +15,9 @@ class FSMBase:
def forward_message(self, event_data): def forward_message(self, event_data):
message = event_data.kwargs.get('message') message = event_data.kwargs.get('message')
ehc.send_message(message) #ehc.send_message(message)
for msghandler in self.message_handlers:
msghandler(message)
def subscribe_message_handler(self, msghandler):
self.message_handlers.append(msghandler)

View File

@ -3,15 +3,17 @@ import json
from tornado.websocket import WebSocketHandler from tornado.websocket import WebSocketHandler
from util import parse_anchor_from_message from util import parse_anchor_from_message
from event_handler_connector import ehc from event_handler_connector import EventHandlerConnector
class ZMQWebSocketHandler(WebSocketHandler): class ZMQWebSocketHandler(WebSocketHandler):
def __init__(self, application, request, **kwargs): def __init__(self, application, request, **kwargs):
super().__init__(application, request, **kwargs) super().__init__(application, request, **kwargs)
self._event_handler_connector = EventHandlerConnector()
def open(self, *args, **kwargs): def open(self, *args, **kwargs):
logging.debug('WebSocket connection initiated') logging.debug('WebSocket connection initiated')
ehc.register_callback(self.zmq_callback) self._event_handler_connector.register_callback(self.zmq_callback)
def zmq_callback(self, msg_parts): def zmq_callback(self, msg_parts):
anchor, data = msg_parts anchor, data = msg_parts
@ -26,7 +28,7 @@ class ZMQWebSocketHandler(WebSocketHandler):
raise NotImplementedError raise NotImplementedError
def send_message(self, message: str, anchor: str = None): def send_message(self, message: str, anchor: str = None):
ehc.send_message(message, anchor) self._event_handler_connector.send_message(message, anchor)
def on_close(self): def on_close(self):
pass pass
@ -40,6 +42,10 @@ class FSMManagingSocketHandler(ZMQWebSocketHandler):
def __init__(self, FSM, application, request, **kwargs): def __init__(self, FSM, application, request, **kwargs):
super().__init__(application, request, **kwargs) super().__init__(application, request, **kwargs)
self.fsm = FSM() self.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): def make_response(self, message):
self.fsm.trigger(parse_anchor_from_message(message), message=message) self.fsm.trigger(parse_anchor_from_message(message), message=message)