mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-09-19 22:41:33 +00:00
39 lines
1.4 KiB
Python
39 lines
1.4 KiB
Python
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
|
|
# All Rights Reserved. See LICENSE file for details.
|
|
|
|
import zmq
|
|
from zmq.eventloop.zmqstream import ZMQStream
|
|
|
|
from tfw.networking import ZMQConnectorBase, serialize_tfw_msg
|
|
from tfw.config import TFWENV
|
|
from tfw.config.logs import logging
|
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
|
class EventHandlerDownlinkConnector(ZMQConnectorBase):
|
|
def __init__(self, zmq_context=None):
|
|
super(EventHandlerDownlinkConnector, self).__init__(zmq_context)
|
|
self._zmq_pull_socket = self._zmq_context.socket(zmq.PULL)
|
|
self._zmq_pull_stream = ZMQStream(self._zmq_pull_socket)
|
|
address = f'tcp://*:{TFWENV.RECEIVER_PORT}'
|
|
self._zmq_pull_socket.bind(address)
|
|
LOG.debug('Pull socket bound to %s', address)
|
|
|
|
|
|
class EventHandlerUplinkConnector(ZMQConnectorBase):
|
|
def __init__(self, zmq_context=None):
|
|
super(EventHandlerUplinkConnector, self).__init__(zmq_context)
|
|
self._zmq_pub_socket = self._zmq_context.socket(zmq.PUB)
|
|
address = f'tcp://*:{TFWENV.PUBLISHER_PORT}'
|
|
self._zmq_pub_socket.bind(address)
|
|
LOG.debug('Pub socket bound to %s', address)
|
|
|
|
|
|
class EventHandlerConnector(EventHandlerDownlinkConnector, EventHandlerUplinkConnector):
|
|
def register_callback(self, callback):
|
|
self._zmq_pull_stream.on_recv(callback)
|
|
|
|
def send_message(self, message: dict):
|
|
self._zmq_pub_socket.send_multipart(serialize_tfw_msg(message))
|