Refactor 'mirror' message enveloping to use new serialization

This commit is contained in:
Kristóf Tóth 2018-04-13 19:57:04 +02:00
parent aabaadc4c4
commit 9eb6717972
4 changed files with 11 additions and 11 deletions

View File

@ -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:

View File

@ -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):

View File

@ -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))

View File

@ -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)