mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-14 22:07:16 +00:00
56 lines
1.9 KiB
Python
56 lines
1.9 KiB
Python
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
|
|
# All Rights Reserved. See LICENSE file for details.
|
|
|
|
import logging
|
|
from functools import partial
|
|
|
|
import zmq
|
|
from zmq.eventloop.zmqstream import ZMQStream
|
|
|
|
from .scope import Scope
|
|
from .serialization import serialize_tfw_msg, with_deserialize_tfw_msg
|
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
|
class ServerDownlinkConnector():
|
|
def __init__(self, connect_addr):
|
|
self._zmq_sub_socket = zmq.Context.instance().socket(zmq.SUB)
|
|
self._zmq_sub_socket.connect(connect_addr)
|
|
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)
|
|
|
|
def close(self):
|
|
self._zmq_sub_stream.close()
|
|
|
|
|
|
class ServerUplinkConnector():
|
|
def __init__(self, connect_addr):
|
|
self._zmq_push_socket = zmq.Context.instance().socket(zmq.PUSH)
|
|
self._zmq_push_socket.connect(connect_addr)
|
|
self._zmq_push_socket.setsockopt(zmq.SNDHWM, 0)
|
|
|
|
def send_message(self, message, scope=Scope.ZMQ):
|
|
message['scope'] = scope.value
|
|
self._zmq_push_socket.send_multipart(serialize_tfw_msg(message))
|
|
|
|
def close(self):
|
|
self._zmq_push_socket.close()
|
|
|
|
|
|
class ServerConnector(ServerDownlinkConnector, ServerUplinkConnector):
|
|
def __init__(self, downlink_connect_addr, uplink_connect_addr):
|
|
ServerDownlinkConnector.__init__(self, downlink_connect_addr)
|
|
ServerUplinkConnector.__init__(self, uplink_connect_addr)
|
|
|
|
def close(self):
|
|
ServerDownlinkConnector.close(self)
|
|
ServerUplinkConnector.close(self)
|