2018-04-03 12:49:14 +00:00
|
|
|
# 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
|
2018-03-25 13:43:59 +00:00
|
|
|
|
|
|
|
import zmq
|
2018-01-24 17:19:49 +00:00
|
|
|
from zmq.eventloop.zmqstream import ZMQStream
|
|
|
|
|
2018-04-13 14:35:30 +00:00
|
|
|
from tfw.networking import serialize_tfw_msg
|
2018-04-06 13:21:45 +00:00
|
|
|
from tfw.networking import ZMQConnectorBase
|
2018-03-25 14:36:31 +00:00
|
|
|
from tfw.config import TFWENV
|
2018-01-31 14:50:52 +00:00
|
|
|
|
2018-01-24 17:19:49 +00:00
|
|
|
|
2018-01-30 13:31:00 +00:00
|
|
|
class ServerDownlinkConnector(ZMQConnectorBase):
|
2018-01-29 15:07:36 +00:00
|
|
|
def __init__(self, zmq_context=None):
|
|
|
|
super(ServerDownlinkConnector, self).__init__(zmq_context)
|
2018-01-24 17:19:49 +00:00
|
|
|
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}')
|
2018-01-24 17:19:49 +00:00
|
|
|
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)
|
|
|
|
self.register_callback = self._zmq_sub_stream.on_recv
|
2018-01-26 14:16:34 +00:00
|
|
|
|
2018-01-29 15:07:36 +00:00
|
|
|
|
2018-01-30 13:31:00 +00:00
|
|
|
class ServerUplinkConnector(ZMQConnectorBase):
|
2018-04-18 17:44:26 +00:00
|
|
|
"""
|
|
|
|
Class capable of sending messages to the TFW server and event handlers.
|
|
|
|
"""
|
2018-01-29 15:07:36 +00:00
|
|
|
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}')
|
2018-01-29 15:07:36 +00:00
|
|
|
|
2018-04-12 13:11:52 +00:00
|
|
|
def send_to_eventhandler(self, message):
|
2018-04-18 17:44:26 +00:00
|
|
|
"""
|
|
|
|
Send a message to an event handler.
|
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
|
|
|
|
:param message['key']: key of event handler you want to address
|
|
|
|
"""
|
2018-04-13 17:57:04 +00:00
|
|
|
nested_message = {'key': message['key'], 'data': message.pop('data')}
|
2018-04-12 13:11:52 +00:00
|
|
|
message['key'] = 'mirror'
|
2018-04-13 17:57:04 +00:00
|
|
|
message['data'] = nested_message
|
2018-04-12 13:11:52 +00:00
|
|
|
self.send(message)
|
2018-03-14 13:09:30 +00:00
|
|
|
|
2018-04-12 13:11:52 +00:00
|
|
|
def send(self, message):
|
2018-04-18 17:44:26 +00:00
|
|
|
"""
|
|
|
|
Send a message to 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
|
|
|
|
"""
|
2018-04-13 14:35:30 +00:00
|
|
|
self._zmq_push_socket.send_multipart(serialize_tfw_msg(message))
|
2018-01-29 15:07:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ServerConnector(ServerUplinkConnector, ServerDownlinkConnector):
|
2018-03-25 14:29:59 +00:00
|
|
|
pass
|