From 934f8ec74c73ef30b7f854b370b84a08dd5c6f03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Fri, 29 Jun 2018 11:50:36 +0200 Subject: [PATCH] Hide ZMQ serialization magic from ServerConnector clients --- lib/tfw/event_handler_base.py | 4 +--- lib/tfw/networking/__init__.py | 2 +- lib/tfw/networking/event_handlers/server_connector.py | 10 ++++++++-- lib/tfw/networking/serialization.py | 9 +++++++++ 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/lib/tfw/event_handler_base.py b/lib/tfw/event_handler_base.py index cca44b1..cfce6f7 100644 --- a/lib/tfw/event_handler_base.py +++ b/lib/tfw/event_handler_base.py @@ -3,7 +3,6 @@ from abc import ABC, abstractmethod -from tfw.networking import deserialize_tfw_msg from tfw.networking.event_handlers import ServerConnector @@ -20,14 +19,13 @@ class EventHandlerBase(ABC): self.subscribe(self.key, 'reset') self.server_connector.register_callback(self.event_handler_callback) - def event_handler_callback(self, msg_parts): + def event_handler_callback(self, message): """ Callback that is invoked when receiving a message. Dispatches messages to handler methods and sends a response back in case the handler returned something. This is subscribed in __init__(). """ - message = deserialize_tfw_msg(*msg_parts) response = self.dispatch_handling(message) if response: response['key'] = message['key'] diff --git a/lib/tfw/networking/__init__.py b/lib/tfw/networking/__init__.py index 6dd15f9..0958f0b 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, validate_message +from .serialization import serialize_tfw_msg, deserialize_tfw_msg, validate_message, with_deserialize_tfw_msg 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/event_handlers/server_connector.py b/lib/tfw/networking/event_handlers/server_connector.py index 6685ead..678bb3b 100644 --- a/lib/tfw/networking/event_handlers/server_connector.py +++ b/lib/tfw/networking/event_handlers/server_connector.py @@ -6,9 +6,12 @@ from functools import partial import zmq from zmq.eventloop.zmqstream import ZMQStream -from tfw.networking import serialize_tfw_msg +from tfw.networking import serialize_tfw_msg, with_deserialize_tfw_msg from tfw.networking import ZMQConnectorBase from tfw.config import TFWENV +from tfw.config.logs import logging + +LOG = logging.getLogger(__name__) class ServerDownlinkConnector(ZMQConnectorBase): @@ -20,7 +23,10 @@ class ServerDownlinkConnector(ZMQConnectorBase): self.subscribe = partial(self._zmq_sub_socket.setsockopt_string, zmq.SUBSCRIBE) self.unsubscribe = partial(self._zmq_sub_socket.setsockopt_string, zmq.UNSUBSCRIBE) - self.register_callback = self._zmq_sub_stream.on_recv + + def register_callback(self, callback): + callback = with_deserialize_tfw_msg(callback) + self._zmq_sub_stream.on_recv(callback) class ServerUplinkConnector(ZMQConnectorBase): diff --git a/lib/tfw/networking/serialization.py b/lib/tfw/networking/serialization.py index c21fc47..483b666 100644 --- a/lib/tfw/networking/serialization.py +++ b/lib/tfw/networking/serialization.py @@ -22,6 +22,7 @@ The purpose of this module is abstracting away this low level behaviour. """ import json +from functools import wraps def validate_message(message): @@ -36,6 +37,14 @@ def serialize_tfw_msg(message): return _serialize_all(message['key'], message) +def with_deserialize_tfw_msg(fun): + @wraps(fun) + def wrapper(message_parts): + message = deserialize_tfw_msg(*message_parts) + return fun(message) + return wrapper + + def deserialize_tfw_msg(*args): """ Return message from TFW multipart data