baseimage-tutorial-framework/lib/tfw/networking/event_handlers/server_connector.py

90 lines
2.9 KiB
Python
Raw Normal View History

# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
# All Rights Reserved. See LICENSE file for details.
2018-01-31 14:50:52 +00:00
from functools import partial
import zmq
from zmq.eventloop.zmqstream import ZMQStream
from tfw.networking.zmq_connector_base import ZMQConnectorBase
from tfw.networking.serialization import serialize_tfw_msg, with_deserialize_tfw_msg
from tfw.config import TFWENV
from tfw.config.logs import logging
LOG = logging.getLogger(__name__)
2018-01-31 14:50:52 +00:00
class ServerDownlinkConnector(ZMQConnectorBase):
def __init__(self, zmq_context=None):
super(ServerDownlinkConnector, self).__init__(zmq_context)
self._zmq_sub_socket = self._zmq_context.socket(zmq.SUB)
2018-04-19 07:21:41 +00:00
self._zmq_sub_socket.connect(f'tcp://localhost:{TFWENV.PUBLISHER_PORT}')
2019-05-20 09:06:57 +00:00
self._zmq_sub_socket.setsockopt(zmq.RCVHWM,0)
self._zmq_sub_stream = ZMQStream(self._zmq_sub_socket)
self.subscribe = partial(self._zmq_sub_socket.setsockopt_string, zmq.SUBSCRIBE)
self.unsubscribe = partial(self._zmq_sub_socket.setsockopt_string, zmq.UNSUBSCRIBE)
def register_callback(self, callback):
callback = with_deserialize_tfw_msg(callback)
self._zmq_sub_stream.on_recv(callback)
2018-01-26 14:16:34 +00:00
2019-05-20 09:06:57 +00:00
def close(self):
self._zmq_sub_stream.close()
class ServerUplinkConnector(ZMQConnectorBase):
2018-04-18 17:44:26 +00:00
"""
Class capable of sending messages to the TFW server and event handlers.
"""
def __init__(self, zmq_context=None):
super(ServerUplinkConnector, self).__init__(zmq_context)
self._zmq_push_socket = self._zmq_context.socket(zmq.PUSH)
2018-04-19 07:21:41 +00:00
self._zmq_push_socket.connect(f'tcp://localhost:{TFWENV.RECEIVER_PORT}')
2019-05-20 09:06:57 +00:00
self._zmq_push_socket.setsockopt(zmq.SNDHWM,0)
def send_to_eventhandler(self, message):
2018-04-18 17:44:26 +00:00
"""
Send a message to an event handler through the TFW server.
2018-05-11 12:07:59 +00:00
This envelopes the desired message in the 'data' field of the message to
TFWServer, which will mirror it to event handlers.
2018-04-18 17:44:26 +00:00
:param message: JSON message you want to send
"""
self.send({
'key': 'mirror',
'data': message
})
def send(self, message):
2018-04-18 17:44:26 +00:00
"""
Send a message to the frontend through the TFW server.
2018-05-11 12:07:59 +00:00
2018-04-18 17:44:26 +00:00
:param message: JSON message you want to send
"""
self._zmq_push_socket.send_multipart(serialize_tfw_msg(message))
def broadcast(self, message):
"""
Broadast a message through the TFW server.
This envelopes the desired message in the 'data' field of the message to
TFWServer, which will broadast it.
:param message: JSON message you want to send
"""
self.send({
'key': 'broadcast',
'data': message
})
2019-05-20 09:06:57 +00:00
def close(self):
self._zmq_push_socket.close()
class ServerConnector(ServerUplinkConnector, ServerDownlinkConnector):
2019-05-20 09:06:57 +00:00
def close(self):
ServerUplinkConnector.close(self)
ServerDownlinkConnector.close(self)