From 8199b33d53cc72c026dc2427de9fb46f0022b47d Mon Sep 17 00:00:00 2001 From: Sven Czarnian Date: Sun, 15 Aug 2021 09:00:04 +0200 Subject: [PATCH] use one module for the complete ES communication --- aman/com/Euroscope.py | 52 +++++++++++++++++++++++++++++++++++ aman/com/EuroscopeReceiver.py | 38 ------------------------- 2 files changed, 52 insertions(+), 38 deletions(-) create mode 100644 aman/com/Euroscope.py delete mode 100644 aman/com/EuroscopeReceiver.py diff --git a/aman/com/Euroscope.py b/aman/com/Euroscope.py new file mode 100644 index 0000000..0e814fa --- /dev/null +++ b/aman/com/Euroscope.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python + +import glob +import os +import sys + +import zmq +from zmq.asyncio import Context +import zmq.auth +from zmq.auth.asyncio import AsyncioAuthenticator + +from aman.config import Server + +# @brief Receives and sends messages to EuroScope plugins +class Euroscope: + # @brief Initializes the ZMQ socket + # @param[in] config The server configuration + def __init__(self, config : Server.Server): + self.context = Context.instance() + + # initialize the authentication module + authLocation = ( + str(config.ClientKeyPath) + ) + self.auth = AsyncioAuthenticator(context = self.context) + self.auth.configure_curve(domain='*', location = authLocation) + self.auth.allow('127.0.0.1') + self.auth.start() + + # read the certificates + keyPairPath = glob.glob(os.path.join(config.ServerKeyPath, '*.key_secret')) + if 1 != len(keyPairPath): + sys.stderr.write('No public-private keypair found for the server certificate') + sys.exit(-1) + keyPair = zmq.auth.load_certificate(keyPairPath[0]) + + # initialize the receiver + self.receiverSocket = self.context.socket(zmq.ROUTER) + self.receiverSocket.setsockopt(zmq.CURVE_PUBLICKEY, keyPair[0]) + self.receiverSocket.setsockopt(zmq.CURVE_SECRETKEY, keyPair[1]) + self.receiverSocket.setsockopt(zmq.CURVE_SERVER, True) + self.receiverSocket.bind('tcp://' + config.Address + ':' + str(config.PortReceiver)) + + # initialize the notification + self.notificationSocket = self.context.socket(zmq.DEALER) + self.notificationSocket.setsockopt(zmq.CURVE_PUBLICKEY, keyPair[0]) + self.notificationSocket.setsockopt(zmq.CURVE_SECRETKEY, keyPair[1]) + self.notificationSocket.setsockopt(zmq.CURVE_SERVER, True) + self.notificationSocket.bind('tcp://' + config.Address + ':' + str(config.PortNotification)) + + def __exit__(self, *_exc): + self.auth.stop() diff --git a/aman/com/EuroscopeReceiver.py b/aman/com/EuroscopeReceiver.py deleted file mode 100644 index 661f991..0000000 --- a/aman/com/EuroscopeReceiver.py +++ /dev/null @@ -1,38 +0,0 @@ -#!/usr/bin/env python - -import zmq -from zmq.asyncio import Context -import zmq.auth -from zmq.auth.asyncio import AsyncioAuthenticator - -from pathlib import Path -from typing import Union, Optional - -# @brief Receives the information of EuroScope -def EuroscopeReceiver(): - # @brief Initializes the ZMQ socket - # @param[in] address The server address - # @param[in] serverKeyPath Path to the server's keypair - # @param[in] clientKeys Path to the client's keypairs - def __init__(self, address: str, serverKeyPath: Union[str, Path], clientKeys: Union[str, Path]): - self.context = Context.instance() - - # initialize the authentication module - authLocation = ( - str(clientKeys) - ) - self.auth = AsyncioAuthenticator(context = self.context) - self.auth.configure_curve(domain='*', location = authLocation) - self.auth.allow('127.0.0.1') - self.auth.start() - - # initialize the socket - self.socket = self.context.socket(zmq.REP) - keys = zmq.auth.load_certificate(serverKeyPath) - self.socket.setsockopt(zmq.CURVE_PUBLICKEY, keys[0]) - self.socket.setsockopt(zmq.CURVE_SECRETKEY, keys[1]) - self.socket.setsockopt(zmq.CURVE_SERVER, True) - self.socket.bind(address) - - def __exit__(self, *_exc): - self.auth.stop()