From 018286f3971764af937cbc118d92db820631bd0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Fri, 28 Jun 2019 16:50:36 +0200 Subject: [PATCH] Improve EventHandler response sending pattern --- .../directory_snapshotting_event_handler.py | 2 +- lib/tfw/builtins/frontend_event_handler.py | 2 +- lib/tfw/builtins/fsm_aware_event_handler.py | 5 +-- .../builtins/fsm_managing_event_handler.py | 2 +- lib/tfw/builtins/ide_event_handler.py | 2 +- .../process_managing_event_handler.py | 2 +- lib/tfw/builtins/terminal_event_handler.py | 2 +- lib/tfw/event_handlers/event_handler_base.py | 45 +++++++------------ 8 files changed, 24 insertions(+), 38 deletions(-) diff --git a/lib/tfw/builtins/directory_snapshotting_event_handler.py b/lib/tfw/builtins/directory_snapshotting_event_handler.py index 80b6b34..81683c5 100644 --- a/lib/tfw/builtins/directory_snapshotting_event_handler.py +++ b/lib/tfw/builtins/directory_snapshotting_event_handler.py @@ -53,7 +53,7 @@ class DirectorySnapshottingEventHandler(EventHandler): try: data = message['data'] message['data'] = self.command_handlers[data['command']](data) - return message + self.send_message(message) except KeyError: LOG.error('IGNORING MESSAGE: Invalid message received: %s', message) diff --git a/lib/tfw/builtins/frontend_event_handler.py b/lib/tfw/builtins/frontend_event_handler.py index 9e46869..1099564 100644 --- a/lib/tfw/builtins/frontend_event_handler.py +++ b/lib/tfw/builtins/frontend_event_handler.py @@ -17,7 +17,7 @@ class FrontendEventHandler(EventHandler): self._frontend_message_storage.save_message(message) if message['key'] == 'recover': self.recover_frontend() - return message + self.send_message(message) def recover_frontend(self): for message in self._frontend_message_storage.messages: diff --git a/lib/tfw/builtins/fsm_aware_event_handler.py b/lib/tfw/builtins/fsm_aware_event_handler.py index 6eb7565..ac467ed 100644 --- a/lib/tfw/builtins/fsm_aware_event_handler.py +++ b/lib/tfw/builtins/fsm_aware_event_handler.py @@ -21,6 +21,5 @@ class FSMAwareEventHandler(EventHandler, FSMAware, ABC): self.subscribe('fsm_update') def dispatch_handling(self, message): - if self.refresh_on_fsm_update(message): - return None - return super().dispatch_handling(message) + if not self.refresh_on_fsm_update(message): + super().dispatch_handling(message) diff --git a/lib/tfw/builtins/fsm_managing_event_handler.py b/lib/tfw/builtins/fsm_managing_event_handler.py index a4814f9..8b5f4d6 100644 --- a/lib/tfw/builtins/fsm_managing_event_handler.py +++ b/lib/tfw/builtins/fsm_managing_event_handler.py @@ -46,7 +46,7 @@ class FSMManagingEventHandler(EventHandler): sign_message(self.auth_key, message) sign_message(self.auth_key, fsm_update_message) self.server_connector.send_message(fsm_update_message, Scope.BROADCAST) - return message + self.send_message(message) except KeyError: LOG.error('IGNORING MESSAGE: Invalid message received: %s', message) diff --git a/lib/tfw/builtins/ide_event_handler.py b/lib/tfw/builtins/ide_event_handler.py index dd257d3..e03fc17 100644 --- a/lib/tfw/builtins/ide_event_handler.py +++ b/lib/tfw/builtins/ide_event_handler.py @@ -187,7 +187,7 @@ class IdeEventHandler(EventHandler): data = message['data'] message['data'] = self.commands[data['command']](data) self.attach_fileinfo(data) - return message + self.send_message(message) except KeyError: LOG.error('IGNORING MESSAGE: Invalid message received: %s', message) diff --git a/lib/tfw/builtins/process_managing_event_handler.py b/lib/tfw/builtins/process_managing_event_handler.py index 0109e7a..5f687cd 100644 --- a/lib/tfw/builtins/process_managing_event_handler.py +++ b/lib/tfw/builtins/process_managing_event_handler.py @@ -58,6 +58,6 @@ class ProcessManagingEventHandler(EventHandler): data['process_name'], self.log_tail ) - return message + self.send_message(message) except KeyError: LOG.error('IGNORING MESSAGE: Invalid message received: %s', message) diff --git a/lib/tfw/builtins/terminal_event_handler.py b/lib/tfw/builtins/terminal_event_handler.py index 68a6b78..9b0df08 100644 --- a/lib/tfw/builtins/terminal_event_handler.py +++ b/lib/tfw/builtins/terminal_event_handler.py @@ -56,7 +56,7 @@ class TerminalEventHandler(EventHandler): try: data = message['data'] message['data'] = self.commands[data['command']](data) - return message + self.send_message(message) except KeyError: LOG.error('IGNORING MESSAGE: Invalid message received: %s', message) diff --git a/lib/tfw/event_handlers/event_handler_base.py b/lib/tfw/event_handlers/event_handler_base.py index cc0be96..281c92f 100644 --- a/lib/tfw/event_handlers/event_handler_base.py +++ b/lib/tfw/event_handlers/event_handler_base.py @@ -5,8 +5,6 @@ import logging from inspect import currentframe from typing import Iterable -from tfw.networking import Scope - LOG = logging.getLogger(__name__) @@ -17,7 +15,7 @@ class EventHandlerBase: Derived classes must implement the handle_event() method """ - def __init__(self, key, server_connector, scope=Scope.ZMQ): + def __init__(self, key, server_connector, scope): self.server_connector = server_connector self.scope = scope self.keys = [] @@ -29,12 +27,18 @@ class EventHandlerBase: self.subscribe(*self.keys) self.server_connector.register_callback(self.event_handler_callback) - @property - def key(self): + def subscribe(self, *keys): """ - Returns the oldest key this EventHandler was subscribed to. + Subscribe this EventHandler to receive events for given keys. + Note that you can subscribe to the same key several times in which + case you will need to unsubscribe multiple times in order to stop + receiving events. + + :param keys: list of keys to subscribe to """ - return self.keys[0] + for key in keys: + self.server_connector.subscribe(key) + self.keys.append(key) def event_handler_callback(self, message): """ @@ -43,15 +47,8 @@ class EventHandlerBase: a response back in case the handler returned something. This is subscribed in __init__(). """ - if not self.check_key(message): - return - - response = self.dispatch_handling(message) - if response: - self.send_message(response) - - def send_message(self, message): - self.server_connector.send_message(message, self.scope) + if self.check_key(message): + self.dispatch_handling(message) def check_key(self, message): """ @@ -74,7 +71,7 @@ class EventHandlerBase: :param message: the message received :returns: the message to send back """ - return self.handle_event(message) + self.handle_event(message) def handle_event(self, message): """ @@ -85,18 +82,8 @@ class EventHandlerBase: """ raise NotImplementedError - def subscribe(self, *keys): - """ - Subscribe this EventHandler to receive events for given keys. - Note that you can subscribe to the same key several times in which - case you will need to unsubscribe multiple times in order to stop - receiving events. - - :param keys: list of keys to subscribe to - """ - for key in keys: - self.server_connector.subscribe(key) - self.keys.append(key) + def send_message(self, message): + self.server_connector.send_message(message, self.scope) def unsubscribe(self, *keys): """