Hide ZMQ serialization magic from ServerConnector clients

This commit is contained in:
Kristóf Tóth 2018-06-29 11:50:36 +02:00
parent 1b65bd4d3d
commit 934f8ec74c
4 changed files with 19 additions and 6 deletions

View File

@ -3,7 +3,6 @@
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from tfw.networking import deserialize_tfw_msg
from tfw.networking.event_handlers import ServerConnector from tfw.networking.event_handlers import ServerConnector
@ -20,14 +19,13 @@ class EventHandlerBase(ABC):
self.subscribe(self.key, 'reset') self.subscribe(self.key, 'reset')
self.server_connector.register_callback(self.event_handler_callback) 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. Callback that is invoked when receiving a message.
Dispatches messages to handler methods and sends Dispatches messages to handler methods and sends
a response back in case the handler returned something. a response back in case the handler returned something.
This is subscribed in __init__(). This is subscribed in __init__().
""" """
message = deserialize_tfw_msg(*msg_parts)
response = self.dispatch_handling(message) response = self.dispatch_handling(message)
if response: if response:
response['key'] = message['key'] response['key'] = message['key']

View File

@ -1,7 +1,7 @@
# Copyright (C) 2018 Avatao.com Innovative Learning Kft. # Copyright (C) 2018 Avatao.com Innovative Learning Kft.
# All Rights Reserved. See LICENSE file for details. # 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 .zmq_connector_base import ZMQConnectorBase
# from .controller_connector import ControllerConnector # TODO: readd once controller stuff is resolved # from .controller_connector import ControllerConnector # TODO: readd once controller stuff is resolved
from .message_sender import MessageSender from .message_sender import MessageSender

View File

@ -6,9 +6,12 @@ from functools import partial
import zmq import zmq
from zmq.eventloop.zmqstream import ZMQStream 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.networking import ZMQConnectorBase
from tfw.config import TFWENV from tfw.config import TFWENV
from tfw.config.logs import logging
LOG = logging.getLogger(__name__)
class ServerDownlinkConnector(ZMQConnectorBase): class ServerDownlinkConnector(ZMQConnectorBase):
@ -20,7 +23,10 @@ class ServerDownlinkConnector(ZMQConnectorBase):
self.subscribe = partial(self._zmq_sub_socket.setsockopt_string, zmq.SUBSCRIBE) self.subscribe = partial(self._zmq_sub_socket.setsockopt_string, zmq.SUBSCRIBE)
self.unsubscribe = partial(self._zmq_sub_socket.setsockopt_string, zmq.UNSUBSCRIBE) 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): class ServerUplinkConnector(ZMQConnectorBase):

View File

@ -22,6 +22,7 @@ The purpose of this module is abstracting away this low level behaviour.
""" """
import json import json
from functools import wraps
def validate_message(message): def validate_message(message):
@ -36,6 +37,14 @@ def serialize_tfw_msg(message):
return _serialize_all(message['key'], 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): def deserialize_tfw_msg(*args):
""" """
Return message from TFW multipart data Return message from TFW multipart data