EuroscopeReceiver.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/usr/bin/env python
  2. import zmq
  3. from zmq.asyncio import Context
  4. import zmq.auth
  5. from zmq.auth.asyncio import AsyncioAuthenticator
  6. from pathlib import Path
  7. from typing import Union, Optional
  8. # @brief Receives the information of EuroScope
  9. def EuroscopeReceiver():
  10. # @brief Initializes the ZMQ socket
  11. # @param[in] address The server address
  12. # @param[in] serverKeyPath Path to the server's keypair
  13. # @param[in] clientKeys Path to the client's keypairs
  14. def __init__(self, address: str, serverKeyPath: Union[str, Path], clientKeys: Union[str, Path]):
  15. self.context = Context.instance()
  16. # initialize the authentication module
  17. authLocation = (
  18. str(clientKeys)
  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. # initialize the socket
  25. self.socket = self.context.socket(zmq.REP)
  26. keys = zmq.auth.load_certificate(serverKeyPath)
  27. self.socket.setsockopt(zmq.CURVE_PUBLICKEY, keys[0])
  28. self.socket.setsockopt(zmq.CURVE_SECRETKEY, keys[1])
  29. self.socket.setsockopt(zmq.CURVE_SERVER, True)
  30. self.socket.bind(address)
  31. def __exit__(self, *_exc):
  32. self.auth.stop()