From 31ba6cb623edd58c8b11de0a9d368e419844934b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Wed, 21 Feb 2018 15:28:16 +0100 Subject: [PATCH 1/8] =?UTF-8?q?Rename=20anchor=20->=20key=20globally=20?= =?UTF-8?q?=C2=AF\=5F(=E3=83=84)=5F/=C2=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/tfw/components/directory_monitor.py | 6 ++-- .../components/source_code_event_handler.py | 6 ++-- lib/tfw/components/terminado_event_handler.py | 8 ++--- lib/tfw/event_handler_base.py | 36 +++++++++---------- lib/tfw/message_sender.py | 8 ++--- .../event_handlers/server_connector.py | 4 +-- .../server/event_handler_connector.py | 8 ++--- .../server/zmq_websocket_handler.py | 14 ++++---- src/demo/event_handler_main.py | 4 +-- src/demo/sql_injection_fsm.py | 14 ++++---- 10 files changed, 54 insertions(+), 54 deletions(-) diff --git a/lib/tfw/components/directory_monitor.py b/lib/tfw/components/directory_monitor.py index 5c7da14..5234367 100644 --- a/lib/tfw/components/directory_monitor.py +++ b/lib/tfw/components/directory_monitor.py @@ -24,9 +24,9 @@ class WebideReloadEventHandler(FileSystemEventHandler): def on_modified(self, event): if self._paused: return log.debug(event) - anchor = 'anchor_webide' - self.uplink.send(anchor, {'anchor': anchor, - 'data': {'command': 'reload'}}) + key = 'webide' + self.uplink.send(key, {'key': key, + 'data': {'command': 'reload'}}) class DirectoryMonitor: diff --git a/lib/tfw/components/source_code_event_handler.py b/lib/tfw/components/source_code_event_handler.py index 88782eb..9885e49 100644 --- a/lib/tfw/components/source_code_event_handler.py +++ b/lib/tfw/components/source_code_event_handler.py @@ -44,8 +44,8 @@ class FileManager: class SourceCodeEventHandler(EventHandlerBase, SupervisorMixin): - def __init__(self, anchor, directory, process_name, selected_file=None): - super().__init__(anchor) + def __init__(self, key, directory, process_name, selected_file=None): + super().__init__(key) self.filemanager = FileManager(directory, selected_file=selected_file) self.process_name = process_name @@ -81,7 +81,7 @@ class SourceCodeEventHandler(EventHandlerBase, SupervisorMixin): data['filename'] = self.filemanager.filename data['files'] = self.filemanager.files - def handle_event(self, anchor, data_json): + def handle_event(self, key, data_json): data = data_json['data'] data_json['data'] = self.commands[data['command']](data) self.attach_fileinfo(data) diff --git a/lib/tfw/components/terminado_event_handler.py b/lib/tfw/components/terminado_event_handler.py index bdfebb2..1b9782c 100644 --- a/lib/tfw/components/terminado_event_handler.py +++ b/lib/tfw/components/terminado_event_handler.py @@ -6,12 +6,12 @@ log = logging.getLogger(__name__) class TerminadoEventHandler(EventHandlerBase, SupervisorMixin): - def __init__(self, anchor, process_name): - super().__init__(anchor) + def __init__(self, key, process_name): + super().__init__(key) self.working_directory = tfwenv.TERMINADO_DIR self.process_name = process_name self.start_process() - def handle_event(self, anchor, data_json): - log.debug('TerminadoEventHandler received event for anchor {}'.format(anchor)) + def handle_event(self, key, data_json): + log.debug('TerminadoEventHandler received event for key {}'.format(key)) # TODO: wat do? diff --git a/lib/tfw/event_handler_base.py b/lib/tfw/event_handler_base.py index 22ff707..a6c1b38 100644 --- a/lib/tfw/event_handler_base.py +++ b/lib/tfw/event_handler_base.py @@ -14,25 +14,25 @@ def cenator(): class EventHandlerBase: - def __init__(self, anchor): + def __init__(self, key): self.server_connector = ServerConnector() - self.anchor = anchor + self.key = key self.subscriptions = set() - self.subscribe(self.anchor) + self.subscribe(self.key) self.subscribe('reset') self.server_connector.register_callback(self.event_handler_callback) self.cenerator = cycle(cenator()) def event_handler_callback(self, msg_parts): - anchor, message = deserialize_all(*msg_parts) + key, message = deserialize_all(*msg_parts) from .message_sender import MessageSender ms = MessageSender() ms.send('JOHN CENA', next(self.cenerator)) - response = self.handle_event(anchor, message) if anchor != 'reset' else self.handle_reset(message) + response = self.handle_event(key, message) if key != 'reset' else self.handle_reset(message) if response is None: return - self.server_connector.send(anchor, response) + self.server_connector.send(key, response) - def handle_event(self, anchor, data_json): + def handle_event(self, key, data_json): raise NotImplementedError def handle_reset(self, data_json): @@ -41,26 +41,26 @@ class EventHandlerBase: def cleanup(self): pass - def message_other(self, anchor, data): + def message_other(self, key, data): message = { - 'anchor': anchor, + 'key': key, 'data': data } - self.server_connector.send(anchor, message) + self.server_connector.send(key, message) - def subscribe(self, anchor): - if anchor not in self.subscriptions: - self.subscriptions.add(anchor) - self.server_connector.subscribe(anchor) + def subscribe(self, key): + if key not in self.subscriptions: + self.subscriptions.add(key) + self.server_connector.subscribe(key) - def unsubscribe(self, anchor): + def unsubscribe(self, key): try: - self.subscriptions.remove(anchor) - self.server_connector.unsubscribe(anchor) + self.subscriptions.remove(key) + self.server_connector.unsubscribe(key) except KeyError: pass def unsubscribe_all(self): for sub in self.subscriptions: - self.server_connector.unsubscribe(anchor=sub) + self.server_connector.unsubscribe(key=sub) self.subscriptions.clear() diff --git a/lib/tfw/message_sender.py b/lib/tfw/message_sender.py index df21265..6d57aa6 100644 --- a/lib/tfw/message_sender.py +++ b/lib/tfw/message_sender.py @@ -4,9 +4,9 @@ from tfw.networking.event_handlers.server_connector import ServerUplinkConnector class MessageSender: - def __init__(self, custom_anchor: str = None): + def __init__(self, custom_key: str = None): self.server_connector = ServerUplinkConnector() - self.anchor = custom_anchor or 'message' + self.key = custom_key or 'message' def send(self, originator, message): data = { @@ -15,7 +15,7 @@ class MessageSender: 'message': message } response = { - 'anchor': self.anchor, + 'key': self.key, 'data': data } - self.server_connector.send(self.anchor, response) + self.server_connector.send(self.key, response) diff --git a/lib/tfw/networking/event_handlers/server_connector.py b/lib/tfw/networking/event_handlers/server_connector.py index d15d467..99fe058 100644 --- a/lib/tfw/networking/event_handlers/server_connector.py +++ b/lib/tfw/networking/event_handlers/server_connector.py @@ -25,8 +25,8 @@ class ServerUplinkConnector(ZMQConnectorBase): self._zmq_push_socket = self._zmq_context.socket(zmq.PUSH) self._zmq_push_socket.connect('tcp://localhost:{}'.format(tfwenv.RECEIVER_PORT)) - def send(self, anchor, response): - self._zmq_push_socket.send_multipart(serialize_all(anchor, response)) + def send(self, key, response): + self._zmq_push_socket.send_multipart(serialize_all(key, response)) class ServerConnector(ServerUplinkConnector, ServerDownlinkConnector): diff --git a/lib/tfw/networking/server/event_handler_connector.py b/lib/tfw/networking/server/event_handler_connector.py index a5149f3..a40425f 100644 --- a/lib/tfw/networking/server/event_handler_connector.py +++ b/lib/tfw/networking/server/event_handler_connector.py @@ -34,7 +34,7 @@ class EventHandlerConnector(EventHandlerDownlinkConnector, EventHandlerUplinkCon def register_callback(self, callback): self._zmq_pull_stream.on_recv(callback) - def send_message(self, message: dict, anchor: str = None): - if not anchor: - anchor = message['anchor'] - self._zmq_pub_socket.send_multipart(serialize_all(anchor, message)) + def send_message(self, message: dict, key: str = None): + if not key: + key = message['key'] + self._zmq_pub_socket.send_multipart(serialize_all(key, message)) diff --git a/lib/tfw/networking/server/zmq_websocket_handler.py b/lib/tfw/networking/server/zmq_websocket_handler.py index 04e7745..b16796c 100644 --- a/lib/tfw/networking/server/zmq_websocket_handler.py +++ b/lib/tfw/networking/server/zmq_websocket_handler.py @@ -23,7 +23,7 @@ class ZMQWebSocketHandler(WebSocketHandler): @staticmethod def zmq_callback(msg_parts): - anchor, data = deserialize_all(*msg_parts) + key, data = deserialize_all(*msg_parts) log.debug('Received on pull socket: {}'.format(data)) for instance in ZMQWebSocketHandler.instances: instance.write_message(data) @@ -32,8 +32,8 @@ class ZMQWebSocketHandler(WebSocketHandler): log.debug('Received on WebSocket: {}'.format(message)) self.send_message(*self.make_response(json.loads(message))) - def send_message(self, message: dict, anchor: str = None): - self._event_handler_connector.send_message(message, anchor) + def send_message(self, message: dict, key: str = None): + self._event_handler_connector.send_message(message, key) # much secure, very cors, wow def check_origin(self, origin): @@ -53,10 +53,10 @@ class FSMManagingSocketHandler(ZMQWebSocketHandler): self._event_handler_connector.send_message(message) def make_response(self, message): - self.fsm.trigger(message['anchor'], message=message) - anchor = 'FSMUpdate' - response = {'anchor': anchor, + self.fsm.trigger(message['key'], message=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, anchor + return response, key diff --git a/src/demo/event_handler_main.py b/src/demo/event_handler_main.py index dda5c36..dab8927 100644 --- a/src/demo/event_handler_main.py +++ b/src/demo/event_handler_main.py @@ -6,8 +6,8 @@ from tfw.config import tfwenv if __name__ == '__main__': - eventhandlers = {SourceCodeEventHandler('anchor_webide', tfwenv.WEBIDE_WD, 'login'), - TerminadoEventHandler('anchor_terminado', 'terminado')} + eventhandlers = {SourceCodeEventHandler('webide', tfwenv.WEBIDE_WD, 'login'), + TerminadoEventHandler('terminado', 'terminado')} try: IOLoop.instance().start() finally: diff --git a/src/demo/sql_injection_fsm.py b/src/demo/sql_injection_fsm.py index 933ab46..550fc00 100644 --- a/src/demo/sql_injection_fsm.py +++ b/src/demo/sql_injection_fsm.py @@ -12,13 +12,13 @@ class SQLInjectionFSM(FSMBase): 'end', ] transitions = [ - {'trigger': 'anchor_webide', 'source': '*', 'dest': 'stripped_code'}, # TODO: delet this - {'trigger': 'anchor_webide', 'source': 'start', 'dest': 'stripped_code'}, - {'trigger': 'anchor_login', 'source': 'stripped_code', 'dest': 'sql'}, - {'trigger': 'anchor_logger', 'source': 'sql', 'dest': 'commented_code'}, - {'trigger': 'anchor_webide', 'source': 'commented_code', 'dest': 'sql_with_substitutions'}, - {'trigger': 'anchor_logger', 'source': 'sql_with_substitutions', 'dest': 'sql_output'}, - {'trigger': 'anchor_logger', 'source': 'sql_output', 'dest': 'end'}, + {'trigger': 'webide', 'source': '*', 'dest': 'stripped_code'}, # TODO: delet this + {'trigger': 'webide', 'source': 'start', 'dest': 'stripped_code'}, + {'trigger': 'login', 'source': 'stripped_code', 'dest': 'sql'}, + {'trigger': 'logger', 'source': 'sql', 'dest': 'commented_code'}, + {'trigger': 'webide', 'source': 'commented_code', 'dest': 'sql_with_substitutions'}, + {'trigger': 'logger', 'source': 'sql_with_substitutions', 'dest': 'sql_output'}, + {'trigger': 'logger', 'source': 'sql_output', 'dest': 'end'}, {'trigger': 'reset', 'source': 'end', 'dest': 'start'}, ] From 26538a4ad9c683c03c838b8f6e7db941b0c44824 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Wed, 21 Feb 2018 16:46:25 +0100 Subject: [PATCH 2/8] Make ZMQWebSocketHandler "abstract" --- lib/tfw/networking/server/zmq_websocket_handler.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/tfw/networking/server/zmq_websocket_handler.py b/lib/tfw/networking/server/zmq_websocket_handler.py index b16796c..cf79129 100644 --- a/lib/tfw/networking/server/zmq_websocket_handler.py +++ b/lib/tfw/networking/server/zmq_websocket_handler.py @@ -32,6 +32,9 @@ class ZMQWebSocketHandler(WebSocketHandler): 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, key: str = None): self._event_handler_connector.send_message(message, key) From ecbb61a862863dc2ff5e44d3f6a56de09969341f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Fri, 23 Feb 2018 12:07:30 +0100 Subject: [PATCH 3/8] Overhaul WS->ZMQ proxying --- lib/tfw/fsm_base.py | 19 ++++++------ lib/tfw/networking/server/tfw_server.py | 29 +++++++++++++++++-- .../server/zmq_websocket_handler.py | 28 +++++++----------- 3 files changed, 46 insertions(+), 30 deletions(-) diff --git a/lib/tfw/fsm_base.py b/lib/tfw/fsm_base.py index fc1300f..04494e6 100644 --- a/lib/tfw/fsm_base.py +++ b/lib/tfw/fsm_base.py @@ -7,7 +7,7 @@ class FSMBase: states, transitions = [], [] 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.machine = Machine(model=self, states=self.states, @@ -15,18 +15,17 @@ class FSMBase: initial=initial or self.states[0], send_event=True, ignore_invalid_triggers=True, - after_state_change='forward_message') + after_state_change='execute_callbacks') - def forward_message(self, event_data): - message = event_data.kwargs.get('message') - for msghandler in self.message_handlers: - msghandler(message) + def execute_callbacks(self, event_data): + for callback in self.callbacks: + callback(event_data.kwargs) - def subscribe_message_handler(self, msghandler): - self.message_handlers.append(msghandler) + def subscribe(self, callback): + self.callbacks.append(callback) - def unsubscribe_message_handler(self, msghandler): - self.message_handlers.remove(msghandler) + def unsubscribe(self, callback): + self.callbacks.remove(callback) def is_solved(self): return self.state in self.accepted_states diff --git a/lib/tfw/networking/server/tfw_server.py b/lib/tfw/networking/server/tfw_server.py index 0129686..f025e00 100644 --- a/lib/tfw/networking/server/tfw_server.py +++ b/lib/tfw/networking/server/tfw_server.py @@ -1,14 +1,20 @@ from tornado.web import Application 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: def __init__(self, fsm_type): self._fsm = fsm_type() + self.uplink = ServerUplinkConnector() + self._fsm.subscribe(self.fsm_callback) 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) @@ -16,5 +22,24 @@ class TFWServer: def fsm(self): 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): self.application.listen(port) diff --git a/lib/tfw/networking/server/zmq_websocket_handler.py b/lib/tfw/networking/server/zmq_websocket_handler.py index cf79129..de2932e 100644 --- a/lib/tfw/networking/server/zmq_websocket_handler.py +++ b/lib/tfw/networking/server/zmq_websocket_handler.py @@ -30,7 +30,7 @@ class ZMQWebSocketHandler(WebSocketHandler): def on_message(self, 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): raise NotImplementedError @@ -43,23 +43,15 @@ class ZMQWebSocketHandler(WebSocketHandler): return True -class FSMManagingSocketHandler(ZMQWebSocketHandler): - def initialize(self, fsm): - self.fsm = fsm - self.fsm.subscribe_message_handler(self.handle_fsm_message) +class ZMQWebSocketProxy(ZMQWebSocketHandler): + def initialize(self, make_response, proxy_filter): + self._make_response = make_response + self._proxy_filter = proxy_filter - def on_close(self): - super().on_close() - self.fsm.unsubscribe_message_handler(self.handle_fsm_message) - - def handle_fsm_message(self, message): - self._event_handler_connector.send_message(message) + def on_message(self, message): + message = json.loads(message) + if self._proxy_filter(message): + super().on_message(message) def make_response(self, message): - self.fsm.trigger(message['key'], message=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 + return self._make_response(message) From 38ca56fa9dc2d7ba6b8fce4338f31846697d201c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Fri, 23 Feb 2018 12:08:14 +0100 Subject: [PATCH 4/8] Fix generate_fsm_update() not listing valid transitions properly --- lib/tfw/networking/server/tfw_server.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tfw/networking/server/tfw_server.py b/lib/tfw/networking/server/tfw_server.py index f025e00..91f46ce 100644 --- a/lib/tfw/networking/server/tfw_server.py +++ b/lib/tfw/networking/server/tfw_server.py @@ -38,7 +38,7 @@ class TFWServer: response = {'key': key, 'data': {'current_state': self.fsm.state, 'valid_transitions': - [{'trigger': trigger} for trigger in self.fsm.machine.get_triggers()]}} + [{'trigger': trigger} for trigger in self.fsm.machine.get_triggers(self.fsm.state)]}} return response, key def listen(self, port): From fd1cb100ad292b32086acb6c635075b8f2d9d691 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Fri, 23 Feb 2018 12:13:19 +0100 Subject: [PATCH 5/8] Unify (key, response) ordering --- lib/tfw/networking/server/tfw_server.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/tfw/networking/server/tfw_server.py b/lib/tfw/networking/server/tfw_server.py index 91f46ce..211e95f 100644 --- a/lib/tfw/networking/server/tfw_server.py +++ b/lib/tfw/networking/server/tfw_server.py @@ -30,8 +30,7 @@ class TFWServer: 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) + self.uplink.send(*self.generate_fsm_update()) def generate_fsm_update(self): key = 'FSMUpdate' @@ -39,7 +38,7 @@ class TFWServer: 'data': {'current_state': self.fsm.state, 'valid_transitions': [{'trigger': trigger} for trigger in self.fsm.machine.get_triggers(self.fsm.state)]}} - return response, key + return key, response def listen(self, port): self.application.listen(port) From 940e74d6daa4b756c228bcac80885f79b069fa42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Fri, 23 Feb 2018 14:07:41 +0100 Subject: [PATCH 6/8] Implement triggered and triggerless EventHandler base classes --- .../components/source_code_event_handler.py | 4 ++-- lib/tfw/components/terminado_event_handler.py | 4 ++-- lib/tfw/event_handler_base.py | 24 ++++++++++++++++++- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/lib/tfw/components/source_code_event_handler.py b/lib/tfw/components/source_code_event_handler.py index 9885e49..263b754 100644 --- a/lib/tfw/components/source_code_event_handler.py +++ b/lib/tfw/components/source_code_event_handler.py @@ -2,7 +2,7 @@ from os.path import isfile, join, relpath from glob import glob from tfw.components.mixins import SupervisorMixin -from tfw.event_handler_base import EventHandlerBase +from tfw.event_handler_base import TriggerlessEventHandler from tfw.components.directory_monitor import DirectoryMonitor from tfw.config.logs import logging @@ -43,7 +43,7 @@ class FileManager: return relpath(self._filepath(filename), start=self._workdir) -class SourceCodeEventHandler(EventHandlerBase, SupervisorMixin): +class SourceCodeEventHandler(TriggerlessEventHandler, SupervisorMixin): def __init__(self, key, directory, process_name, selected_file=None): super().__init__(key) self.filemanager = FileManager(directory, selected_file=selected_file) diff --git a/lib/tfw/components/terminado_event_handler.py b/lib/tfw/components/terminado_event_handler.py index 1b9782c..42b09d2 100644 --- a/lib/tfw/components/terminado_event_handler.py +++ b/lib/tfw/components/terminado_event_handler.py @@ -1,11 +1,11 @@ -from tfw.event_handler_base import EventHandlerBase +from tfw.event_handler_base import TriggerlessEventHandler from tfw.components.mixins import SupervisorMixin from tfw.config import tfwenv from tfw.config.logs import logging log = logging.getLogger(__name__) -class TerminadoEventHandler(EventHandlerBase, SupervisorMixin): +class TerminadoEventHandler(TriggerlessEventHandler, SupervisorMixin): def __init__(self, key, process_name): super().__init__(key) self.working_directory = tfwenv.TERMINADO_DIR diff --git a/lib/tfw/event_handler_base.py b/lib/tfw/event_handler_base.py index a6c1b38..1a4d1c5 100644 --- a/lib/tfw/event_handler_base.py +++ b/lib/tfw/event_handler_base.py @@ -28,10 +28,17 @@ class EventHandlerBase: from .message_sender import MessageSender ms = MessageSender() ms.send('JOHN CENA', next(self.cenerator)) - response = self.handle_event(key, message) if key != 'reset' else self.handle_reset(message) + response = self.dispatch_handling(key, message) if response is None: return self.server_connector.send(key, response) + def dispatch_handling(self, key, message): + raise NotImplementedError + + def _dispatch_handling(self, key, message): + if key != 'reset': return self.handle_event(key, message) + else: return self.handle_reset(message) + def handle_event(self, key, data_json): raise NotImplementedError @@ -64,3 +71,18 @@ class EventHandlerBase: for sub in self.subscriptions: self.server_connector.unsubscribe(key=sub) self.subscriptions.clear() + + +class TriggerlessEventHandler(EventHandlerBase): + def dispatch_handling(self, key, message): + return self._dispatch_handling(key, message) + + +class TriggeredEventHandler(EventHandlerBase): + def __init__(self, key, trigger): + super().__init__(key) + self.trigger = trigger + + def dispatch_handling(self, key, message): + if message.get('trigger') == self.trigger: + return self._dispatch_handling(key, message) From cf4a5bc09d174a6455d5384a65b15e3a84611562 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Fri, 23 Feb 2018 14:55:58 +0100 Subject: [PATCH 7/8] Refactor FSMUpdate message sending logic to class FSMUpdater --- lib/tfw/networking/server/tfw_server.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/tfw/networking/server/tfw_server.py b/lib/tfw/networking/server/tfw_server.py index 211e95f..60499db 100644 --- a/lib/tfw/networking/server/tfw_server.py +++ b/lib/tfw/networking/server/tfw_server.py @@ -10,8 +10,9 @@ log = logging.getLogger(__name__) class TFWServer: def __init__(self, fsm_type): self._fsm = fsm_type() - self.uplink = ServerUplinkConnector() - self._fsm.subscribe(self.fsm_callback) + self.fsm_updater = FSMUpdater(self._fsm) + self._fsm.subscribe(self.fsm_updater.update) + self.application = Application( [(r'/ws', ZMQWebSocketProxy, {'make_response': self.make_response, 'proxy_filter': self.proxy_filter})] @@ -29,7 +30,16 @@ class TFWServer: try: return self.fsm.trigger(message['key'], message=message) except AttributeError: return False - def fsm_callback(self, kwargs_dict): + def listen(self, port): + self.application.listen(port) + + +class FSMUpdater: + def __init__(self, fsm): + self.fsm = fsm + self.uplink = ServerUplinkConnector() + + def update(self, kwargs_dict): self.uplink.send(*self.generate_fsm_update()) def generate_fsm_update(self): @@ -39,6 +49,3 @@ class TFWServer: 'valid_transitions': [{'trigger': trigger} for trigger in self.fsm.machine.get_triggers(self.fsm.state)]}} return key, response - - def listen(self, port): - self.application.listen(port) From 76456b36deec52658ab42c68d283310dc2b8083a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Fri, 23 Feb 2018 15:14:23 +0100 Subject: [PATCH 8/8] Enable proxying of Triggerless messages in TFWServer --- lib/tfw/networking/server/tfw_server.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/tfw/networking/server/tfw_server.py b/lib/tfw/networking/server/tfw_server.py index 60499db..8411bd4 100644 --- a/lib/tfw/networking/server/tfw_server.py +++ b/lib/tfw/networking/server/tfw_server.py @@ -24,11 +24,13 @@ class TFWServer: return self._fsm def make_response(self, message): + trigger = message.get('trigger', '') + try: self.fsm.trigger(trigger, message=message) + except AttributeError: log.debug('FSM failed to execute nonexistent trigger: "{}"'.format(trigger)) return message def proxy_filter(self, message): - try: return self.fsm.trigger(message['key'], message=message) - except AttributeError: return False + return True def listen(self, port): self.application.listen(port)