Euroscope.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/usr/bin/env python
  2. import glob
  3. import os
  4. import sys
  5. import zmq
  6. from zmq.asyncio import Context
  7. import zmq.auth
  8. from zmq.auth.asyncio import AsyncioAuthenticator
  9. from aman.config import Server
  10. # @brief Receives and sends messages to EuroScope plugins
  11. class Euroscope:
  12. # @brief Initializes the ZMQ socket
  13. # @param[in] config The server configuration
  14. def __init__(self, config : Server.Server):
  15. self.context = Context.instance()
  16. # initialize the authentication module
  17. authLocation = (
  18. str(config.ClientKeyPath)
  19. )
  20. self.auth = AsyncioAuthenticator(context = self.context)
  21. self.auth.configure_curve(domain='*', location = authLocation)
  22. self.auth.allow('127.0.0.1')
  23. self.auth.start()
  24. # read the certificates
  25. keyPairPath = glob.glob(os.path.join(config.ServerKeyPath, '*.key_secret'))
  26. if 1 != len(keyPairPath):
  27. sys.stderr.write('No public-private keypair found for the server certificate')
  28. sys.exit(-1)
  29. keyPair = zmq.auth.load_certificate(keyPairPath[0])
  30. # initialize the receiver
  31. self.receiverSocket = self.context.socket(zmq.ROUTER)
  32. self.receiverSocket.setsockopt(zmq.CURVE_PUBLICKEY, keyPair[0])
  33. self.receiverSocket.setsockopt(zmq.CURVE_SECRETKEY, keyPair[1])
  34. self.receiverSocket.setsockopt(zmq.CURVE_SERVER, True)
  35. self.receiverSocket.bind('tcp://' + config.Address + ':' + str(config.PortReceiver))
  36. # initialize the notification
  37. self.notificationSocket = self.context.socket(zmq.DEALER)
  38. self.notificationSocket.setsockopt(zmq.CURVE_PUBLICKEY, keyPair[0])
  39. self.notificationSocket.setsockopt(zmq.CURVE_SECRETKEY, keyPair[1])
  40. self.notificationSocket.setsockopt(zmq.CURVE_SERVER, True)
  41. self.notificationSocket.bind('tcp://' + config.Address + ':' + str(config.PortNotification))
  42. def __exit__(self, *_exc):
  43. self.auth.stop()