From d04479157d45b4df5af18a3ae90372918a69f0c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Thu, 12 Apr 2018 15:11:52 +0200 Subject: [PATCH 01/22] Refactor ServerUplinkConnector.send() signature --- lib/tfw/components/directory_monitor.py | 4 ++-- lib/tfw/event_handler_base.py | 3 ++- .../networking/event_handlers/server_connector.py | 12 ++++++------ lib/tfw/networking/message_sender.py | 4 ++-- lib/tfw/networking/server/tfw_server.py | 7 +++---- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/tfw/components/directory_monitor.py b/lib/tfw/components/directory_monitor.py index 9726eb9..630ee07 100644 --- a/lib/tfw/components/directory_monitor.py +++ b/lib/tfw/components/directory_monitor.py @@ -70,8 +70,8 @@ class WebideReloadWatchdogEventHandler(FileSystemWatchdogEventHandler): self.ignore = self.ignore - 1 return LOG.debug(event) - key = 'webide' - self.uplink.send(key, {'data': {'command': 'reload'}}) + self.uplink.send({'key': 'webide', + 'data': {'command': 'reload'}}) def with_monitor_paused(fun): diff --git a/lib/tfw/event_handler_base.py b/lib/tfw/event_handler_base.py index fdce167..479379d 100644 --- a/lib/tfw/event_handler_base.py +++ b/lib/tfw/event_handler_base.py @@ -19,9 +19,10 @@ class EventHandlerBase(ABC): def event_handler_callback(self, msg_parts): key, message = deserialize_all(*msg_parts) response = self.dispatch_handling(key, message) + response['key'] = key if response is None: return - self.server_connector.send(key, response) + self.server_connector.send(response) @abstractmethod def dispatch_handling(self, key, message): diff --git a/lib/tfw/networking/event_handlers/server_connector.py b/lib/tfw/networking/event_handlers/server_connector.py index 4479e75..74c2316 100644 --- a/lib/tfw/networking/event_handlers/server_connector.py +++ b/lib/tfw/networking/event_handlers/server_connector.py @@ -29,13 +29,13 @@ 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_to_eventhandler(self, key, message): - message['data']['key'] = key - self.send('mirror', message) + def send_to_eventhandler(self, message): + message['data']['key'] = message['key'] + message['key'] = 'mirror' + self.send(message) - def send(self, key, message): - message['key'] = key - self._zmq_push_socket.send_multipart(serialize_all(key, message)) + def send(self, message): + self._zmq_push_socket.send_multipart(serialize_all(message['key'], message)) class ServerConnector(ServerUplinkConnector, ServerDownlinkConnector): diff --git a/lib/tfw/networking/message_sender.py b/lib/tfw/networking/message_sender.py index ba08daf..5e73b08 100644 --- a/lib/tfw/networking/message_sender.py +++ b/lib/tfw/networking/message_sender.py @@ -17,5 +17,5 @@ class MessageSender: 'timestamp': datetime.now().isoformat(), 'message': message } - response = {'data': data} - self.server_connector.send(self.key, response) + self.server_connector.send({'key': self.key, + 'data': data}) diff --git a/lib/tfw/networking/server/tfw_server.py b/lib/tfw/networking/server/tfw_server.py index 63ddf72..b9766ae 100644 --- a/lib/tfw/networking/server/tfw_server.py +++ b/lib/tfw/networking/server/tfw_server.py @@ -100,12 +100,11 @@ class FSMUpdater: def update(self, kwargs_dict): # pylint: disable=unused-argument - self.uplink.send(*self.generate_fsm_update()) + self.uplink.send(self.generate_fsm_update()) def generate_fsm_update(self): - key = 'FSMUpdate' - response = {'data': self.get_fsm_state_and_transitions()} - return key, response + return {'key': 'FSMUpdate', + 'data': self.get_fsm_state_and_transitions()} def get_fsm_state_and_transitions(self): state = self.fsm.state From 18e9ee5f52a8ae9ba7ab1db42ead126a4fcb8b37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Fri, 13 Apr 2018 16:16:51 +0200 Subject: [PATCH 02/22] Rename make_response in networking to avoid confusion --- lib/tfw/networking/server/tfw_server.py | 4 ++-- lib/tfw/networking/server/zmq_websocket_handler.py | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/tfw/networking/server/tfw_server.py b/lib/tfw/networking/server/tfw_server.py index b9766ae..b573533 100644 --- a/lib/tfw/networking/server/tfw_server.py +++ b/lib/tfw/networking/server/tfw_server.py @@ -23,7 +23,7 @@ class TFWServer: self._event_handler_connector = EventHandlerConnector() self.application = Application( - [(r'/ws', ZMQWebSocketProxy, {'make_response': self.make_response, + [(r'/ws', ZMQWebSocketProxy, {'make_eventhandler_response': self.make_eventhandler_response, 'proxy_filter': self.proxy_filter, 'handle_trigger': self.handle_trigger, 'event_handler_connector': self._event_handler_connector})] @@ -38,7 +38,7 @@ class TFWServer: def fsm_manager(self): return self._fsm_manager - def make_response(self, message): + def make_eventhandler_response(self, message): self.trigger_fsm(message) message['FSMUpdate'] = self._fsm_updater.get_fsm_state_and_transitions() return message diff --git a/lib/tfw/networking/server/zmq_websocket_handler.py b/lib/tfw/networking/server/zmq_websocket_handler.py index 73a9754..e421733 100644 --- a/lib/tfw/networking/server/zmq_websocket_handler.py +++ b/lib/tfw/networking/server/zmq_websocket_handler.py @@ -49,10 +49,10 @@ class ZMQWebSocketHandler(WebSocketHandler, ABC): def on_message(self, message): LOG.debug('Received on WebSocket: %s', message) - self.send_message(self.make_response(message)) + self.send_message(self.make_eventhandler_response(message)) @abstractmethod - def make_response(self, message): + def make_eventhandler_response(self, message): raise NotImplementedError def send_message(self, message: dict, key: str = None): @@ -71,7 +71,7 @@ class ZMQWebSocketProxy(ZMQWebSocketHandler): # pylint: disable=abstract-method def initialize(self, **kwargs): # pylint: disable=arguments-differ super(ZMQWebSocketProxy, self).initialize(**kwargs) - self._make_response = kwargs['make_response'] + self._make_eventhandler_response = kwargs['make_eventhandler_response'] self._proxy_filter = kwargs['proxy_filter'] self._handle_trigger = kwargs['handle_trigger'] @@ -80,8 +80,8 @@ class ZMQWebSocketProxy(ZMQWebSocketHandler): if self._proxy_filter(message): super().on_message(message) - def make_response(self, message): - return self._make_response(message) + def make_eventhandler_response(self, message): + return self._make_eventhandler_response(message) def handle_trigger(self, message): self._handle_trigger(message) From 67a23166c6378d08e55a117258dabc43b020f05c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Fri, 13 Apr 2018 16:34:40 +0200 Subject: [PATCH 03/22] Introduce [de]serialize_tfw_msg() methods to abstract away enveloping --- lib/tfw/networking/__init__.py | 1 + lib/tfw/networking/serialization.py | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/lib/tfw/networking/__init__.py b/lib/tfw/networking/__init__.py index 161659b..b4abc25 100644 --- a/lib/tfw/networking/__init__.py +++ b/lib/tfw/networking/__init__.py @@ -2,6 +2,7 @@ # All Rights Reserved. See LICENSE file for details. from .serialization import decode_if_needed, encode_if_needed, serialize_all, deserialize_all +from .serialization import serialize_tfw_msg, deserialize_tfw_msg from .zmq_connector_base import ZMQConnectorBase from .controller_connector import ControllerConnector from .message_sender import MessageSender diff --git a/lib/tfw/networking/serialization.py b/lib/tfw/networking/serialization.py index d26257e..5c84a5c 100644 --- a/lib/tfw/networking/serialization.py +++ b/lib/tfw/networking/serialization.py @@ -4,6 +4,14 @@ import json +def serialize_tfw_msg(message): + return serialize_all(message['key'], message) + + +def deserialize_tfw_msg(*args): + return deserialize_all(*args)[1] + + def serialize_all(*args): return tuple(_serialize_single(arg) for arg in args) From cd36b6c22ca1e6447e01fa2e1000d9c79d5288d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Fri, 13 Apr 2018 16:35:30 +0200 Subject: [PATCH 04/22] Refactor ServerUplinkConnector to use new serializers --- lib/tfw/networking/event_handlers/server_connector.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/tfw/networking/event_handlers/server_connector.py b/lib/tfw/networking/event_handlers/server_connector.py index 74c2316..fa98f21 100644 --- a/lib/tfw/networking/event_handlers/server_connector.py +++ b/lib/tfw/networking/event_handlers/server_connector.py @@ -6,7 +6,7 @@ from functools import partial import zmq from zmq.eventloop.zmqstream import ZMQStream -from tfw.networking import serialize_all +from tfw.networking import serialize_tfw_msg from tfw.networking import ZMQConnectorBase from tfw.config import TFWENV @@ -35,7 +35,7 @@ class ServerUplinkConnector(ZMQConnectorBase): self.send(message) def send(self, message): - self._zmq_push_socket.send_multipart(serialize_all(message['key'], message)) + self._zmq_push_socket.send_multipart(serialize_tfw_msg(message)) class ServerConnector(ServerUplinkConnector, ServerDownlinkConnector): From aabaadc4c4f220ae04c4b7176cba28d4c548fc2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Fri, 13 Apr 2018 16:42:53 +0200 Subject: [PATCH 05/22] Refactor proxying to use new deserialization --- lib/tfw/networking/server/zmq_websocket_handler.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/tfw/networking/server/zmq_websocket_handler.py b/lib/tfw/networking/server/zmq_websocket_handler.py index e421733..b6da675 100644 --- a/lib/tfw/networking/server/zmq_websocket_handler.py +++ b/lib/tfw/networking/server/zmq_websocket_handler.py @@ -6,7 +6,7 @@ from abc import ABC, abstractmethod from tornado.websocket import WebSocketHandler -from tfw.networking import deserialize_all +from tfw.networking import deserialize_tfw_msg from tfw.config.logs import logging LOG = logging.getLogger(__name__) @@ -31,7 +31,8 @@ class ZMQWebSocketHandler(WebSocketHandler, ABC): def zmq_callback(self, msg_parts): keyhandlers = {'mirror': self.mirror} - key, message = deserialize_all(*msg_parts) + message = deserialize_tfw_msg(*msg_parts) + key = message['key'] LOG.debug('Received on pull socket: %s', message) self.handle_trigger(message) if key not in keyhandlers: From 9eb67179721b9ecb0bccb856466ca4c5c9e9fee9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Fri, 13 Apr 2018 19:57:04 +0200 Subject: [PATCH 06/22] Refactor 'mirror' message enveloping to use new serialization --- lib/tfw/event_handler_base.py | 5 +++-- lib/tfw/networking/event_handlers/server_connector.py | 3 ++- lib/tfw/networking/server/event_handler_connector.py | 6 ++---- lib/tfw/networking/server/zmq_websocket_handler.py | 8 ++++---- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/tfw/event_handler_base.py b/lib/tfw/event_handler_base.py index 479379d..ab455b0 100644 --- a/lib/tfw/event_handler_base.py +++ b/lib/tfw/event_handler_base.py @@ -3,7 +3,7 @@ from abc import ABC, abstractmethod -from tfw.networking import deserialize_all +from tfw.networking import deserialize_tfw_msg from tfw.networking.event_handlers import ServerConnector @@ -17,7 +17,8 @@ class EventHandlerBase(ABC): self.server_connector.register_callback(self.event_handler_callback) def event_handler_callback(self, msg_parts): - key, message = deserialize_all(*msg_parts) + message = deserialize_tfw_msg(*msg_parts) + key = message['key'] response = self.dispatch_handling(key, message) response['key'] = key if response is None: diff --git a/lib/tfw/networking/event_handlers/server_connector.py b/lib/tfw/networking/event_handlers/server_connector.py index fa98f21..5613bbb 100644 --- a/lib/tfw/networking/event_handlers/server_connector.py +++ b/lib/tfw/networking/event_handlers/server_connector.py @@ -30,8 +30,9 @@ class ServerUplinkConnector(ZMQConnectorBase): self._zmq_push_socket.connect('tcp://localhost:{}'.format(TFWENV.RECEIVER_PORT)) def send_to_eventhandler(self, message): - message['data']['key'] = message['key'] + nested_message = {'key': message['key'], 'data': message.pop('data')} message['key'] = 'mirror' + message['data'] = nested_message self.send(message) def send(self, message): diff --git a/lib/tfw/networking/server/event_handler_connector.py b/lib/tfw/networking/server/event_handler_connector.py index d8f3801..41b6b54 100644 --- a/lib/tfw/networking/server/event_handler_connector.py +++ b/lib/tfw/networking/server/event_handler_connector.py @@ -4,7 +4,7 @@ import zmq from zmq.eventloop.zmqstream import ZMQStream -from tfw.networking import ZMQConnectorBase, serialize_all +from tfw.networking import ZMQConnectorBase, serialize_tfw_msg from tfw.config import TFWENV from tfw.config.logs import logging @@ -35,6 +35,4 @@ class EventHandlerConnector(EventHandlerDownlinkConnector, EventHandlerUplinkCon self._zmq_pull_stream.on_recv(callback) def send_message(self, message: dict, key: str = None): - if not key: - key = message.get('key', '') - self._zmq_pub_socket.send_multipart(serialize_all(key, message)) + self._zmq_pub_socket.send_multipart(serialize_tfw_msg(message)) diff --git a/lib/tfw/networking/server/zmq_websocket_handler.py b/lib/tfw/networking/server/zmq_websocket_handler.py index b6da675..f0b865a 100644 --- a/lib/tfw/networking/server/zmq_websocket_handler.py +++ b/lib/tfw/networking/server/zmq_websocket_handler.py @@ -40,13 +40,13 @@ class ZMQWebSocketHandler(WebSocketHandler, ABC): instance.write_message(message) else: try: - keyhandlers[key](message['data']) + keyhandlers[key](message) except KeyError: LOG.error('Invalid mirror message format! Ignoring.') - def mirror(self, data): - key = data['key'] - self._event_handler_connector.send_message({'data': data}, key) + def mirror(self, message): + message = message['data'] + self._event_handler_connector.send_message(message, message['key']) def on_message(self, message): LOG.debug('Received on WebSocket: %s', message) From 499b575adf76a26f85e3e57673a4d1f3c737ff57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Fri, 13 Apr 2018 20:20:00 +0200 Subject: [PATCH 07/22] Add module docstring to serialization.py --- lib/tfw/networking/serialization.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/tfw/networking/serialization.py b/lib/tfw/networking/serialization.py index 5c84a5c..d95537c 100644 --- a/lib/tfw/networking/serialization.py +++ b/lib/tfw/networking/serialization.py @@ -1,5 +1,25 @@ # Copyright (C) 2018 Avatao.com Innovative Learning Kft. # All Rights Reserved. See LICENSE file for details. +""" +TFW JSON message format + +message: +{ + "key": string, # addressing + "data": {...}, # payload + "trigger": string # FSM trigger +} + +ZeroMQ's sub-pub sockets use enveloped messages +(http://zguide.zeromq.org/page:all#Pub-Sub-Message-Envelopes) +and TFW also uses them internally. This means that on ZMQ sockets +we always send the messages key separatley and then the actual +message (which contains the key as well) like so: + +socket.send_multipart([message['key'], message]) + +The purpose of this module is abstracting away this low level behaviour. +""" import json From 3c12b5503ea3b02ccea8e96fae4844df67237d1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Fri, 13 Apr 2018 20:29:02 +0200 Subject: [PATCH 08/22] Mark old serialization methods as module internal --- lib/tfw/networking/__init__.py | 3 +-- lib/tfw/networking/serialization.py | 16 ++++++++-------- lib/tfw/networking/server/__init__.py | 2 +- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/lib/tfw/networking/__init__.py b/lib/tfw/networking/__init__.py index b4abc25..3dac2c0 100644 --- a/lib/tfw/networking/__init__.py +++ b/lib/tfw/networking/__init__.py @@ -1,10 +1,9 @@ # Copyright (C) 2018 Avatao.com Innovative Learning Kft. # All Rights Reserved. See LICENSE file for details. -from .serialization import decode_if_needed, encode_if_needed, serialize_all, deserialize_all from .serialization import serialize_tfw_msg, deserialize_tfw_msg from .zmq_connector_base import ZMQConnectorBase -from .controller_connector import ControllerConnector +# from .controller_connector import ControllerConnector # TODO: readd once controller stuff is resolved from .message_sender import MessageSender from .event_handlers.server_connector import ServerUplinkConnector as TFWServerConnector from .server.tfw_server import TFWServer diff --git a/lib/tfw/networking/serialization.py b/lib/tfw/networking/serialization.py index d95537c..f24b38a 100644 --- a/lib/tfw/networking/serialization.py +++ b/lib/tfw/networking/serialization.py @@ -25,41 +25,41 @@ import json def serialize_tfw_msg(message): - return serialize_all(message['key'], message) + return _serialize_all(message['key'], message) def deserialize_tfw_msg(*args): - return deserialize_all(*args)[1] + return _deserialize_all(*args)[1] -def serialize_all(*args): +def _serialize_all(*args): return tuple(_serialize_single(arg) for arg in args) -def deserialize_all(*args): +def _deserialize_all(*args): return tuple(_deserialize_single(arg) for arg in args) def _serialize_single(data): if not isinstance(data, str): data = json.dumps(data) - return encode_if_needed(data) + return _encode_if_needed(data) def _deserialize_single(data): try: return json.loads(data) except ValueError: - return decode_if_needed(data) + return _decode_if_needed(data) -def encode_if_needed(value): +def _encode_if_needed(value): if isinstance(value, str): value = value.encode('utf-8') return value -def decode_if_needed(value): +def _decode_if_needed(value): if isinstance(value, (bytes, bytearray)): value = value.decode('utf-8') return value diff --git a/lib/tfw/networking/server/__init__.py b/lib/tfw/networking/server/__init__.py index e4c1b25..aea3e0c 100644 --- a/lib/tfw/networking/server/__init__.py +++ b/lib/tfw/networking/server/__init__.py @@ -4,4 +4,4 @@ from .event_handler_connector import EventHandlerConnector, EventHandlerUplinkConnector, EventHandlerDownlinkConnector from .tfw_server import TFWServer from .zmq_websocket_handler import ZMQWebSocketProxy -from .controller_responder import ControllerResponder +# from .controller_responder import ControllerResponder # TODO: readd once controller stuff is resolved From fe7bd4a678d22e090c0120310bb086ae076f3bc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Fri, 13 Apr 2018 20:33:35 +0200 Subject: [PATCH 09/22] Remove ambiguous key parameter from EventHanderConnector --- lib/tfw/networking/server/event_handler_connector.py | 2 +- lib/tfw/networking/server/zmq_websocket_handler.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/tfw/networking/server/event_handler_connector.py b/lib/tfw/networking/server/event_handler_connector.py index 41b6b54..a735673 100644 --- a/lib/tfw/networking/server/event_handler_connector.py +++ b/lib/tfw/networking/server/event_handler_connector.py @@ -34,5 +34,5 @@ class EventHandlerConnector(EventHandlerDownlinkConnector, EventHandlerUplinkCon def register_callback(self, callback): self._zmq_pull_stream.on_recv(callback) - def send_message(self, message: dict, key: str = None): + def send_message(self, message: dict): self._zmq_pub_socket.send_multipart(serialize_tfw_msg(message)) diff --git a/lib/tfw/networking/server/zmq_websocket_handler.py b/lib/tfw/networking/server/zmq_websocket_handler.py index f0b865a..efd7b00 100644 --- a/lib/tfw/networking/server/zmq_websocket_handler.py +++ b/lib/tfw/networking/server/zmq_websocket_handler.py @@ -46,7 +46,7 @@ class ZMQWebSocketHandler(WebSocketHandler, ABC): def mirror(self, message): message = message['data'] - self._event_handler_connector.send_message(message, message['key']) + self._event_handler_connector.send_message(message) def on_message(self, message): LOG.debug('Received on WebSocket: %s', message) @@ -57,7 +57,7 @@ class ZMQWebSocketHandler(WebSocketHandler, ABC): raise NotImplementedError def send_message(self, message: dict, key: str = None): - self._event_handler_connector.send_message(message, key) + self._event_handler_connector.send_message(message) @abstractmethod def handle_trigger(self, message): From 0cd11f9d64ea2d6c2288c3342f47f3a729cbae64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Fri, 13 Apr 2018 20:38:33 +0200 Subject: [PATCH 10/22] Remove ambiguous key parameter from proxy module --- lib/tfw/networking/server/zmq_websocket_handler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tfw/networking/server/zmq_websocket_handler.py b/lib/tfw/networking/server/zmq_websocket_handler.py index efd7b00..4d0d994 100644 --- a/lib/tfw/networking/server/zmq_websocket_handler.py +++ b/lib/tfw/networking/server/zmq_websocket_handler.py @@ -56,7 +56,7 @@ class ZMQWebSocketHandler(WebSocketHandler, ABC): def make_eventhandler_response(self, message): raise NotImplementedError - def send_message(self, message: dict, key: str = None): + def send_message(self, message: dict): self._event_handler_connector.send_message(message) @abstractmethod From ecec1cde59568f8a7bbb155dbbd8a61eb052cfc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Fri, 13 Apr 2018 20:45:34 +0200 Subject: [PATCH 11/22] Root out remaining ambiguous key arguments in EventHandlers --- .../directory_monitoring_event_handler.py | 2 +- .../process_managing_event_handler.py | 2 +- lib/tfw/components/terminado_event_handler.py | 2 +- lib/tfw/components/webide_event_handler.py | 2 +- lib/tfw/event_handler_base.py | 20 +++++++++---------- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/tfw/components/directory_monitoring_event_handler.py b/lib/tfw/components/directory_monitoring_event_handler.py index c9c39bc..d34cf00 100644 --- a/lib/tfw/components/directory_monitoring_event_handler.py +++ b/lib/tfw/components/directory_monitoring_event_handler.py @@ -44,7 +44,7 @@ class DirectoryMonitoringEventHandler(TriggerlessEventHandler): self._monitor = DirectoryMonitor(self._directory) self._monitor.watch() # This runs on a separate thread - def handle_event(self, key, message): + def handle_event(self, message): try: message['data'] = self.commands[message['data']['command']](message['data']) return message diff --git a/lib/tfw/components/process_managing_event_handler.py b/lib/tfw/components/process_managing_event_handler.py index f069c67..3f6c24a 100644 --- a/lib/tfw/components/process_managing_event_handler.py +++ b/lib/tfw/components/process_managing_event_handler.py @@ -30,7 +30,7 @@ class ProcessManagingEventHandler(TriggerlessEventHandler): self.processmanager = ProcessManager() @with_monitor_paused - def handle_event(self, key, message): + def handle_event(self, message): try: data = message['data'] self.processmanager(data['command'], data['process_name']) diff --git a/lib/tfw/components/terminado_event_handler.py b/lib/tfw/components/terminado_event_handler.py index 58ccd4b..65ab905 100644 --- a/lib/tfw/components/terminado_event_handler.py +++ b/lib/tfw/components/terminado_event_handler.py @@ -27,7 +27,7 @@ class TerminadoEventHandler(TriggerlessEventHandler): def historymonitor(self): return self._historymonitor - def handle_event(self, key, message): + def handle_event(self, message): LOG.debug('TerminadoEventHandler received event: %s', message) try: message['data'] = self.commands[message['data']['command']](message['data']) diff --git a/lib/tfw/components/webide_event_handler.py b/lib/tfw/components/webide_event_handler.py index 8c032d2..0a303ed 100644 --- a/lib/tfw/components/webide_event_handler.py +++ b/lib/tfw/components/webide_event_handler.py @@ -171,7 +171,7 @@ class WebideEventHandler(TriggerlessEventHandler): data['files'] = self.filemanager.files data['directory'] = self.filemanager.workdir - def handle_event(self, key, message): + def handle_event(self, message): try: data = message['data'] message['data'] = self.commands[data['command']](data) diff --git a/lib/tfw/event_handler_base.py b/lib/tfw/event_handler_base.py index ab455b0..f9b4a09 100644 --- a/lib/tfw/event_handler_base.py +++ b/lib/tfw/event_handler_base.py @@ -19,25 +19,25 @@ class EventHandlerBase(ABC): def event_handler_callback(self, msg_parts): message = deserialize_tfw_msg(*msg_parts) key = message['key'] - response = self.dispatch_handling(key, message) + response = self.dispatch_handling(message) response['key'] = key if response is None: return self.server_connector.send(response) @abstractmethod - def dispatch_handling(self, key, message): + def dispatch_handling(self, message): raise NotImplementedError - def _dispatch_handling(self, key, message): + def _dispatch_handling(self, message): # pylint: disable=no-else-return - if key != 'reset': - return self.handle_event(key, message) + if message['key'] != 'reset': + return self.handle_event(message) else: return self.handle_reset(message) @abstractmethod - def handle_event(self, key, message): + def handle_event(self, message): raise NotImplementedError def handle_reset(self, message): @@ -67,8 +67,8 @@ class EventHandlerBase(ABC): class TriggerlessEventHandler(EventHandlerBase, ABC): # pylint: disable=abstract-method - def dispatch_handling(self, key, message): - return self._dispatch_handling(key, message) + def dispatch_handling(self, message): + return self._dispatch_handling(message) class TriggeredEventHandler(EventHandlerBase, ABC): @@ -77,7 +77,7 @@ class TriggeredEventHandler(EventHandlerBase, ABC): super().__init__(key) self.trigger = trigger - def dispatch_handling(self, key, message): + def dispatch_handling(self, message): if message.get('trigger') == self.trigger: - return self._dispatch_handling(key, message) + return self._dispatch_handling(message) return None From b0822799168ed8ac42e5a3d2e46bb612a178133e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Fri, 13 Apr 2018 20:56:28 +0200 Subject: [PATCH 12/22] Implement message validation in TFW proxying --- lib/tfw/networking/__init__.py | 2 +- lib/tfw/networking/serialization.py | 6 ++++++ lib/tfw/networking/server/zmq_websocket_handler.py | 13 ++++++++----- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/tfw/networking/__init__.py b/lib/tfw/networking/__init__.py index 3dac2c0..6dd15f9 100644 --- a/lib/tfw/networking/__init__.py +++ b/lib/tfw/networking/__init__.py @@ -1,7 +1,7 @@ # Copyright (C) 2018 Avatao.com Innovative Learning Kft. # All Rights Reserved. See LICENSE file for details. -from .serialization import serialize_tfw_msg, deserialize_tfw_msg +from .serialization import serialize_tfw_msg, deserialize_tfw_msg, validate_message from .zmq_connector_base import ZMQConnectorBase # from .controller_connector import ControllerConnector # TODO: readd once controller stuff is resolved from .message_sender import MessageSender diff --git a/lib/tfw/networking/serialization.py b/lib/tfw/networking/serialization.py index f24b38a..0586309 100644 --- a/lib/tfw/networking/serialization.py +++ b/lib/tfw/networking/serialization.py @@ -24,6 +24,12 @@ The purpose of this module is abstracting away this low level behaviour. import json +def validate_message(message): + if 'key' not in message: + return False + return True + + def serialize_tfw_msg(message): return _serialize_all(message['key'], message) diff --git a/lib/tfw/networking/server/zmq_websocket_handler.py b/lib/tfw/networking/server/zmq_websocket_handler.py index 4d0d994..fee2ff1 100644 --- a/lib/tfw/networking/server/zmq_websocket_handler.py +++ b/lib/tfw/networking/server/zmq_websocket_handler.py @@ -6,7 +6,7 @@ from abc import ABC, abstractmethod from tornado.websocket import WebSocketHandler -from tfw.networking import deserialize_tfw_msg +from tfw.networking import deserialize_tfw_msg, validate_message from tfw.config.logs import logging LOG = logging.getLogger(__name__) @@ -32,15 +32,17 @@ class ZMQWebSocketHandler(WebSocketHandler, ABC): keyhandlers = {'mirror': self.mirror} message = deserialize_tfw_msg(*msg_parts) - key = message['key'] LOG.debug('Received on pull socket: %s', message) + if not validate_message(message): + return + self.handle_trigger(message) - if key not in keyhandlers: + if message['key'] not in keyhandlers: for instance in ZMQWebSocketHandler.instances: instance.write_message(message) else: try: - keyhandlers[key](message) + keyhandlers[message['key']](message) except KeyError: LOG.error('Invalid mirror message format! Ignoring.') @@ -50,7 +52,8 @@ class ZMQWebSocketHandler(WebSocketHandler, ABC): def on_message(self, message): LOG.debug('Received on WebSocket: %s', message) - self.send_message(self.make_eventhandler_response(message)) + if validate_message(message): + self.send_message(self.make_eventhandler_response(message)) @abstractmethod def make_eventhandler_response(self, message): From 586c435cc2ecb625d4a73b17d69958688a06109b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Sat, 14 Apr 2018 20:36:43 +0200 Subject: [PATCH 13/22] Refactor watchdog Observer handling to a mixin --- lib/tfw/components/directory_monitor.py | 13 +++---------- lib/tfw/components/history_monitor.py | 14 +++----------- lib/tfw/components/mixins/__init__.py | 1 + lib/tfw/components/mixins/observer_mixin.py | 16 ++++++++++++++++ 4 files changed, 23 insertions(+), 21 deletions(-) create mode 100644 lib/tfw/components/mixins/observer_mixin.py diff --git a/lib/tfw/components/directory_monitor.py b/lib/tfw/components/directory_monitor.py index 630ee07..cad2860 100644 --- a/lib/tfw/components/directory_monitor.py +++ b/lib/tfw/components/directory_monitor.py @@ -3,31 +3,24 @@ from functools import wraps -from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler as FileSystemWatchdogEventHandler from tfw.networking.event_handlers import ServerUplinkConnector from tfw.components.decorators import RateLimiter +from tfw.components.mixins import ObserverMixin from tfw.config.logs import logging LOG = logging.getLogger(__name__) -class DirectoryMonitor: +class DirectoryMonitor(ObserverMixin): def __init__(self, directory): - self.observer = Observer() + ObserverMixin.__init__(self) self.eventhandler = WebideReloadWatchdogEventHandler() self.observer.schedule(self.eventhandler, directory, recursive=True) self.pause, self.resume = self.eventhandler.pause, self.eventhandler.resume - def watch(self): - self.observer.start() - - def stop(self): - self.observer.stop() - self.observer.join() - @property def ignore(self): return self.eventhandler.ignore diff --git a/lib/tfw/components/history_monitor.py b/lib/tfw/components/history_monitor.py index 1665ad9..2540c3f 100644 --- a/lib/tfw/components/history_monitor.py +++ b/lib/tfw/components/history_monitor.py @@ -6,10 +6,9 @@ from re import findall from re import compile as compileregex from abc import ABC, abstractmethod -from watchdog.observers import Observer from watchdog.events import PatternMatchingEventHandler -from tfw.components.mixins import CallbackMixin +from tfw.components.mixins import CallbackMixin, ObserverMixin from tfw.components.decorators import RateLimiter @@ -24,13 +23,13 @@ class CallbackEventHandler(PatternMatchingEventHandler, ABC): callback() -class HistoryMonitor(CallbackMixin, ABC): +class HistoryMonitor(CallbackMixin, ObserverMixin, ABC): def __init__(self, histfile): CallbackMixin.__init__(self) + ObserverMixin.__init__(self) self.histfile = histfile self._history = [] self._last_length = len(self._history) - self.observer = Observer() self.observer.schedule(CallbackEventHandler([self.histfile], self._fetch_history, self._invoke_callbacks), @@ -60,13 +59,6 @@ class HistoryMonitor(CallbackMixin, ABC): if self._last_length < len(self._history): self._execute_callbacks(self.history) - def watch(self): - self.observer.start() - - def stop(self): - self.observer.stop() - self.observer.join() - class BashMonitor(HistoryMonitor): @property diff --git a/lib/tfw/components/mixins/__init__.py b/lib/tfw/components/mixins/__init__.py index 8003c33..0cf040e 100644 --- a/lib/tfw/components/mixins/__init__.py +++ b/lib/tfw/components/mixins/__init__.py @@ -3,3 +3,4 @@ from .supervisor_mixin import SupervisorMixin from .callback_mixin import CallbackMixin +from .observer_mixin import ObserverMixin diff --git a/lib/tfw/components/mixins/observer_mixin.py b/lib/tfw/components/mixins/observer_mixin.py new file mode 100644 index 0000000..4101d96 --- /dev/null +++ b/lib/tfw/components/mixins/observer_mixin.py @@ -0,0 +1,16 @@ +# Copyright (C) 2018 Avatao.com Innovative Learning Kft. +# All Rights Reserved. See LICENSE file for details. + +from watchdog.observers import Observer + + +class ObserverMixin: + def __init__(self): + self.observer = Observer() + + def watch(self): + self.observer.start() + + def stop(self): + self.observer.stop() + self.observer.join() From e2bb126e6f86a2213e47029565ca96a86482a56f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Sat, 14 Apr 2018 21:07:33 +0200 Subject: [PATCH 14/22] Make simple not if condition more pythonic --- lib/tfw/components/webide_event_handler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tfw/components/webide_event_handler.py b/lib/tfw/components/webide_event_handler.py index 0a303ed..abe9e21 100644 --- a/lib/tfw/components/webide_event_handler.py +++ b/lib/tfw/components/webide_event_handler.py @@ -58,7 +58,7 @@ class FileManager: # pylint: disable=too-many-instance-attributes @filename.setter def filename(self, filename): - if not filename in self.files: + if filename not in self.files: raise EnvironmentError('No such file in workdir!') self._filename = filename From 1c29b700c2a6f6367048e2d666eda4b92bd73340 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Sat, 14 Apr 2018 21:15:30 +0200 Subject: [PATCH 15/22] Move mixins and decorators to tfw module from tfw.components --- lib/tfw/components/directory_monitor.py | 4 ++-- lib/tfw/components/history_monitor.py | 4 ++-- lib/tfw/components/process_managing_event_handler.py | 2 +- lib/tfw/{components => }/decorators/__init__.py | 0 lib/tfw/{components => }/decorators/rate_limiter.py | 0 lib/tfw/fsm_base.py | 2 +- lib/tfw/{components => }/mixins/__init__.py | 0 lib/tfw/{components => }/mixins/callback_mixin.py | 0 lib/tfw/{components => }/mixins/observer_mixin.py | 0 lib/tfw/{components => }/mixins/supervisor_mixin.py | 0 10 files changed, 6 insertions(+), 6 deletions(-) rename lib/tfw/{components => }/decorators/__init__.py (100%) rename lib/tfw/{components => }/decorators/rate_limiter.py (100%) rename lib/tfw/{components => }/mixins/__init__.py (100%) rename lib/tfw/{components => }/mixins/callback_mixin.py (100%) rename lib/tfw/{components => }/mixins/observer_mixin.py (100%) rename lib/tfw/{components => }/mixins/supervisor_mixin.py (100%) diff --git a/lib/tfw/components/directory_monitor.py b/lib/tfw/components/directory_monitor.py index cad2860..9413fe5 100644 --- a/lib/tfw/components/directory_monitor.py +++ b/lib/tfw/components/directory_monitor.py @@ -6,8 +6,8 @@ from functools import wraps from watchdog.events import FileSystemEventHandler as FileSystemWatchdogEventHandler from tfw.networking.event_handlers import ServerUplinkConnector -from tfw.components.decorators import RateLimiter -from tfw.components.mixins import ObserverMixin +from tfw.decorators import RateLimiter +from tfw.mixins import ObserverMixin from tfw.config.logs import logging diff --git a/lib/tfw/components/history_monitor.py b/lib/tfw/components/history_monitor.py index 2540c3f..7f3965c 100644 --- a/lib/tfw/components/history_monitor.py +++ b/lib/tfw/components/history_monitor.py @@ -8,8 +8,8 @@ from abc import ABC, abstractmethod from watchdog.events import PatternMatchingEventHandler -from tfw.components.mixins import CallbackMixin, ObserverMixin -from tfw.components.decorators import RateLimiter +from tfw.mixins import CallbackMixin, ObserverMixin +from tfw.decorators import RateLimiter class CallbackEventHandler(PatternMatchingEventHandler, ABC): diff --git a/lib/tfw/components/process_managing_event_handler.py b/lib/tfw/components/process_managing_event_handler.py index 3f6c24a..3246174 100644 --- a/lib/tfw/components/process_managing_event_handler.py +++ b/lib/tfw/components/process_managing_event_handler.py @@ -4,7 +4,7 @@ from xmlrpc.client import Fault as SupervisorFault from tfw import TriggerlessEventHandler -from tfw.components.mixins import SupervisorMixin +from tfw.mixins import SupervisorMixin from tfw.config.logs import logging from .directory_monitor import with_monitor_paused diff --git a/lib/tfw/components/decorators/__init__.py b/lib/tfw/decorators/__init__.py similarity index 100% rename from lib/tfw/components/decorators/__init__.py rename to lib/tfw/decorators/__init__.py diff --git a/lib/tfw/components/decorators/rate_limiter.py b/lib/tfw/decorators/rate_limiter.py similarity index 100% rename from lib/tfw/components/decorators/rate_limiter.py rename to lib/tfw/decorators/rate_limiter.py diff --git a/lib/tfw/fsm_base.py b/lib/tfw/fsm_base.py index 545c0b6..4657b36 100644 --- a/lib/tfw/fsm_base.py +++ b/lib/tfw/fsm_base.py @@ -5,7 +5,7 @@ from typing import List from transitions import Machine -from tfw.components.mixins import CallbackMixin +from tfw.mixins import CallbackMixin class FSMBase(CallbackMixin): diff --git a/lib/tfw/components/mixins/__init__.py b/lib/tfw/mixins/__init__.py similarity index 100% rename from lib/tfw/components/mixins/__init__.py rename to lib/tfw/mixins/__init__.py diff --git a/lib/tfw/components/mixins/callback_mixin.py b/lib/tfw/mixins/callback_mixin.py similarity index 100% rename from lib/tfw/components/mixins/callback_mixin.py rename to lib/tfw/mixins/callback_mixin.py diff --git a/lib/tfw/components/mixins/observer_mixin.py b/lib/tfw/mixins/observer_mixin.py similarity index 100% rename from lib/tfw/components/mixins/observer_mixin.py rename to lib/tfw/mixins/observer_mixin.py diff --git a/lib/tfw/components/mixins/supervisor_mixin.py b/lib/tfw/mixins/supervisor_mixin.py similarity index 100% rename from lib/tfw/components/mixins/supervisor_mixin.py rename to lib/tfw/mixins/supervisor_mixin.py From a65d3127ba22c35d94e5461e250c7d62a3a2fe19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Sat, 14 Apr 2018 22:57:37 +0200 Subject: [PATCH 16/22] Refactor DirectoryMonitor managing code to a mixin --- .../directory_monitoring_event_handler.py | 20 +++----------- lib/tfw/components/webide_event_handler.py | 20 +++----------- lib/tfw/mixins/__init__.py | 1 + lib/tfw/mixins/monitor_manager_mixin.py | 27 +++++++++++++++++++ 4 files changed, 35 insertions(+), 33 deletions(-) create mode 100644 lib/tfw/mixins/monitor_manager_mixin.py diff --git a/lib/tfw/components/directory_monitoring_event_handler.py b/lib/tfw/components/directory_monitoring_event_handler.py index d34cf00..091b76e 100644 --- a/lib/tfw/components/directory_monitoring_event_handler.py +++ b/lib/tfw/components/directory_monitoring_event_handler.py @@ -5,17 +5,18 @@ from os.path import isdir, exists from tfw import TriggerlessEventHandler from tfw.config.logs import logging +from tfw.mixins import MonitorManagerMixin from .directory_monitor import DirectoryMonitor LOG = logging.getLogger(__name__) -class DirectoryMonitoringEventHandler(TriggerlessEventHandler): +class DirectoryMonitoringEventHandler(TriggerlessEventHandler, MonitorManagerMixin): def __init__(self, key, directory): super().__init__(key) self._directory = directory - self._monitor = None - self.reload_monitor() + MonitorManagerMixin.__init__(self, DirectoryMonitor, self._directory) + self.commands = {'pause': self.pause, 'resume': self.resume, 'ignore': self.ignore, @@ -31,19 +32,6 @@ class DirectoryMonitoringEventHandler(TriggerlessEventHandler): raise EnvironmentError('No such directory!') self._directory = directory - @property - def monitor(self): - return self._monitor - - def reload_monitor(self): - if self._monitor: - try: - self._monitor.stop() - except KeyError: - LOG.debug('Working directory was removed – ignoring...') - self._monitor = DirectoryMonitor(self._directory) - self._monitor.watch() # This runs on a separate thread - def handle_event(self, message): try: message['data'] = self.commands[message['data']['command']](message['data']) diff --git a/lib/tfw/components/webide_event_handler.py b/lib/tfw/components/webide_event_handler.py index abe9e21..9a007a2 100644 --- a/lib/tfw/components/webide_event_handler.py +++ b/lib/tfw/components/webide_event_handler.py @@ -7,6 +7,7 @@ from fnmatch import fnmatchcase from collections import Iterable from tfw import TriggerlessEventHandler +from tfw.mixins import MonitorManagerMixin from tfw.config.logs import logging from .directory_monitor import DirectoryMonitor @@ -90,12 +91,13 @@ class FileManager: # pylint: disable=too-many-instance-attributes return relpath(self._filepath(filename), start=self._workdir) -class WebideEventHandler(TriggerlessEventHandler): +class WebideEventHandler(TriggerlessEventHandler, MonitorManagerMixin): # pylint: disable=too-many-arguments def __init__(self, key, directory, allowed_directories, selected_file=None, exclude=None): super().__init__(key) self.filemanager = FileManager(allowed_directories=allowed_directories, working_directory=directory, selected_file=selected_file, exclude=exclude) + MonitorManagerMixin.__init__(self, DirectoryMonitor, self.filemanager.workdir) self.commands = {'read': self.read, 'write': self.write, @@ -103,22 +105,6 @@ class WebideEventHandler(TriggerlessEventHandler): 'selectdir': self.select_dir, 'exclude': self.exclude} - self._monitor = None - self.reload_monitor() - - @property - def monitor(self): - return self._monitor - - def reload_monitor(self): - if self._monitor: - try: - self._monitor.stop() - except KeyError: - LOG.debug('Working directory was removed – ignoring...') - self._monitor = DirectoryMonitor(self.filemanager.workdir) - self._monitor.watch() # This runs on a separate thread - def read(self, data): try: data['content'] = self.filemanager.file_contents diff --git a/lib/tfw/mixins/__init__.py b/lib/tfw/mixins/__init__.py index 0cf040e..60e67fc 100644 --- a/lib/tfw/mixins/__init__.py +++ b/lib/tfw/mixins/__init__.py @@ -4,3 +4,4 @@ from .supervisor_mixin import SupervisorMixin from .callback_mixin import CallbackMixin from .observer_mixin import ObserverMixin +from .monitor_manager_mixin import MonitorManagerMixin diff --git a/lib/tfw/mixins/monitor_manager_mixin.py b/lib/tfw/mixins/monitor_manager_mixin.py new file mode 100644 index 0000000..0c39725 --- /dev/null +++ b/lib/tfw/mixins/monitor_manager_mixin.py @@ -0,0 +1,27 @@ +# Copyright (C) 2018 Avatao.com Innovative Learning Kft. +# All Rights Reserved. See LICENSE file for details. + +from tfw.config.logs import logging + +LOG = logging.getLogger(__name__) + + +class MonitorManagerMixin: + def __init__(self, monitor_type, directory): + self._monitor_type = monitor_type + self._monitor = None + self._monitored_directory = directory + self.reload_monitor() + + @property + def monitor(self): + return self._monitor + + def reload_monitor(self): + if self._monitor: + try: + self._monitor.stop() + except KeyError: + LOG.debug('Working directory was removed – ignoring...') + self._monitor = self._monitor_type(self._monitored_directory) + self._monitor.watch() # This runs on a separate thread From efb8af4cd1d10a9b50642d6d2f757af1df5866e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Sat, 14 Apr 2018 23:09:02 +0200 Subject: [PATCH 17/22] Remove double trailing newline --- lib/tao/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/tao/__init__.py b/lib/tao/__init__.py index 1179607..db64b25 100644 --- a/lib/tao/__init__.py +++ b/lib/tao/__init__.py @@ -1,3 +1,2 @@ # Copyright (C) 2018 Avatao.com Innovative Learning Kft. # All Rights Reserved. See LICENSE file for details. - From 743fab366bf47e8d89bbe76e8f23ee7b061fb2d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Sat, 14 Apr 2018 23:15:44 +0200 Subject: [PATCH 18/22] Extract variable to silence retarded linter --- lib/tfw/components/terminado_event_handler.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/tfw/components/terminado_event_handler.py b/lib/tfw/components/terminado_event_handler.py index 65ab905..06347a1 100644 --- a/lib/tfw/components/terminado_event_handler.py +++ b/lib/tfw/components/terminado_event_handler.py @@ -30,7 +30,8 @@ class TerminadoEventHandler(TriggerlessEventHandler): def handle_event(self, message): LOG.debug('TerminadoEventHandler received event: %s', message) try: - message['data'] = self.commands[message['data']['command']](message['data']) + data = message['data'] + message['data'] = self.commands[data['command']](data) return message except KeyError: LOG.error('IGNORING MESSAGE: Invalid message received: %s', message) From 22cdbaac2cc73da1338e8c599cceba8d4927748c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Mon, 16 Apr 2018 12:50:08 +0200 Subject: [PATCH 19/22] Fix typo --- lib/tfw/networking/serialization.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tfw/networking/serialization.py b/lib/tfw/networking/serialization.py index 0586309..425abba 100644 --- a/lib/tfw/networking/serialization.py +++ b/lib/tfw/networking/serialization.py @@ -13,7 +13,7 @@ message: ZeroMQ's sub-pub sockets use enveloped messages (http://zguide.zeromq.org/page:all#Pub-Sub-Message-Envelopes) and TFW also uses them internally. This means that on ZMQ sockets -we always send the messages key separatley and then the actual +we always send the messages key separately and then the actual message (which contains the key as well) like so: socket.send_multipart([message['key'], message]) From 39243811dcd83b3dd5dbf355a61e83fda49ad787 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Mon, 16 Apr 2018 12:51:01 +0200 Subject: [PATCH 20/22] Remove unnecessary variable --- lib/tfw/event_handler_base.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/tfw/event_handler_base.py b/lib/tfw/event_handler_base.py index f9b4a09..46a42e8 100644 --- a/lib/tfw/event_handler_base.py +++ b/lib/tfw/event_handler_base.py @@ -18,9 +18,8 @@ class EventHandlerBase(ABC): def event_handler_callback(self, msg_parts): message = deserialize_tfw_msg(*msg_parts) - key = message['key'] response = self.dispatch_handling(message) - response['key'] = key + response['key'] = message['key'] if response is None: return self.server_connector.send(response) From 986a67f5db1dc7cf1b587e62b07572814afab24a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Mon, 16 Apr 2018 12:51:44 +0200 Subject: [PATCH 21/22] Simplify bool returning function --- lib/tfw/networking/serialization.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/tfw/networking/serialization.py b/lib/tfw/networking/serialization.py index 425abba..7738323 100644 --- a/lib/tfw/networking/serialization.py +++ b/lib/tfw/networking/serialization.py @@ -25,9 +25,7 @@ import json def validate_message(message): - if 'key' not in message: - return False - return True + return 'key' in message def serialize_tfw_msg(message): From d8988ea097d411afec3a3cf2deea66c6dd2c1036 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Mon, 16 Apr 2018 12:57:08 +0200 Subject: [PATCH 22/22] Rename make_eventhandler_response in TFWServer to increase readibility --- lib/tfw/networking/server/tfw_server.py | 4 ++-- lib/tfw/networking/server/zmq_websocket_handler.py | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/tfw/networking/server/tfw_server.py b/lib/tfw/networking/server/tfw_server.py index b573533..a39b33e 100644 --- a/lib/tfw/networking/server/tfw_server.py +++ b/lib/tfw/networking/server/tfw_server.py @@ -23,7 +23,7 @@ class TFWServer: self._event_handler_connector = EventHandlerConnector() self.application = Application( - [(r'/ws', ZMQWebSocketProxy, {'make_eventhandler_response': self.make_eventhandler_response, + [(r'/ws', ZMQWebSocketProxy, {'make_eventhandler_message': self.make_eventhandler_message, 'proxy_filter': self.proxy_filter, 'handle_trigger': self.handle_trigger, 'event_handler_connector': self._event_handler_connector})] @@ -38,7 +38,7 @@ class TFWServer: def fsm_manager(self): return self._fsm_manager - def make_eventhandler_response(self, message): + def make_eventhandler_message(self, message): self.trigger_fsm(message) message['FSMUpdate'] = self._fsm_updater.get_fsm_state_and_transitions() return message diff --git a/lib/tfw/networking/server/zmq_websocket_handler.py b/lib/tfw/networking/server/zmq_websocket_handler.py index fee2ff1..c842205 100644 --- a/lib/tfw/networking/server/zmq_websocket_handler.py +++ b/lib/tfw/networking/server/zmq_websocket_handler.py @@ -53,10 +53,10 @@ class ZMQWebSocketHandler(WebSocketHandler, ABC): def on_message(self, message): LOG.debug('Received on WebSocket: %s', message) if validate_message(message): - self.send_message(self.make_eventhandler_response(message)) + self.send_message(self.make_eventhandler_message(message)) @abstractmethod - def make_eventhandler_response(self, message): + def make_eventhandler_message(self, message): raise NotImplementedError def send_message(self, message: dict): @@ -75,7 +75,7 @@ class ZMQWebSocketProxy(ZMQWebSocketHandler): # pylint: disable=abstract-method def initialize(self, **kwargs): # pylint: disable=arguments-differ super(ZMQWebSocketProxy, self).initialize(**kwargs) - self._make_eventhandler_response = kwargs['make_eventhandler_response'] + self._make_eventhandler_message = kwargs['make_eventhandler_message'] self._proxy_filter = kwargs['proxy_filter'] self._handle_trigger = kwargs['handle_trigger'] @@ -84,8 +84,8 @@ class ZMQWebSocketProxy(ZMQWebSocketHandler): if self._proxy_filter(message): super().on_message(message) - def make_eventhandler_response(self, message): - return self._make_eventhandler_response(message) + def make_eventhandler_message(self, message): + return self._make_eventhandler_message(message) def handle_trigger(self, message): self._handle_trigger(message)