Euroscope.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/env python
  2. import ctypes
  3. import glob
  4. import os
  5. import sys
  6. import time
  7. import zmq
  8. import zmq.auth
  9. from aman.com import AircraftReport_pb2
  10. from aman.config.Server import Server
  11. from threading import Thread, _active
  12. class ReceiverThread(Thread):
  13. def __init__(self, socket, aman):
  14. Thread.__init__(self)
  15. self.socket = socket
  16. self.aman = aman
  17. def run(self):
  18. while True:
  19. try:
  20. msg = self.socket.recv(zmq.NOBLOCK)
  21. # parse the received message
  22. report = AircraftReport_pb2.AircraftReport()
  23. report.ParseFromString(msg)
  24. # try to associate the received aircraft to an airport
  25. self.aman.updateAircraftReport(report)
  26. except zmq.ZMQError as error:
  27. if zmq.EAGAIN == error.errno:
  28. time.sleep(0.5)
  29. continue
  30. else:
  31. return
  32. def threadId(self):
  33. if hasattr(self, '_thread_id'):
  34. return self._thread_id
  35. for id, thread in _active.items():
  36. if thread is self:
  37. return id
  38. def stopThread(self):
  39. id = self.threadId()
  40. res = ctypes.pythonapi.PyThreadState_SetAsyncExc(id, ctypes.py_object(SystemExit))
  41. if 1 < res:
  42. ctypes.pythonapi.PyThreadState_SetAsyncExc(id, 0)
  43. # @brief Receives and sends messages to EuroScope plugins
  44. class Euroscope:
  45. def __init__(self):
  46. self.context = None
  47. self.receiverSocket = None
  48. self.receiverThread = None
  49. self.notificationSocket = None
  50. def __del__(self):
  51. self.release()
  52. # @brief Initializes the ZMQ socket
  53. # @param[in] config The server configuration
  54. def acquire(self, configPath : str, config : Server, aman):
  55. self.context = zmq.Context()
  56. # find the key directories
  57. serverKeyPath = os.path.join(os.path.join(configPath, 'keys'), 'server')
  58. if False == os.path.isdir(serverKeyPath):
  59. sys.stderr.write('No directory for the server key found')
  60. sys.exit(-1)
  61. print('Path to the server key: ' + serverKeyPath)
  62. clientKeyPath = os.path.join(os.path.join(configPath, 'keys'), 'clients')
  63. if False == os.path.isdir(clientKeyPath):
  64. sys.stderr.write('No directory for the client keys found')
  65. sys.exit(-1)
  66. print('Path to the client keys: ' + clientKeyPath)
  67. # read the certificates
  68. keyPairPath = glob.glob(os.path.join(serverKeyPath, '*.key_secret'))
  69. if 1 != len(keyPairPath):
  70. sys.stderr.write('No public-private keypair found for the server certificate')
  71. sys.exit(-1)
  72. keyPair = zmq.auth.load_certificate(keyPairPath[0])
  73. # initialize the receiver
  74. self.receiverSocket = zmq.Socket(self.context, zmq.SUB)
  75. self.receiverSocket.setsockopt(zmq.CURVE_PUBLICKEY, keyPair[0])
  76. self.receiverSocket.setsockopt(zmq.CURVE_SECRETKEY, keyPair[1])
  77. self.receiverSocket.setsockopt(zmq.CURVE_SERVER, True)
  78. self.receiverSocket.bind('tcp://' + config.Address + ':' + str(config.PortReceiver))
  79. self.receiverSocket.setsockopt(zmq.SUBSCRIBE, b'')
  80. self.receiverThread = ReceiverThread(self.receiverSocket, aman)
  81. self.receiverThread.start()
  82. print('Listening to tcp://' + config.Address + ':' + str(config.PortReceiver))
  83. # initialize the notification
  84. self.notificationSocket = zmq.Socket(self.context, zmq.PUB)
  85. self.notificationSocket.setsockopt(zmq.CURVE_PUBLICKEY, keyPair[0])
  86. self.notificationSocket.setsockopt(zmq.CURVE_SECRETKEY, keyPair[1])
  87. self.notificationSocket.setsockopt(zmq.CURVE_SERVER, True)
  88. self.notificationSocket.bind('tcp://' + config.Address + ':' + str(config.PortNotification))
  89. print('Publishing to tcp://' + config.Address + ':' + str(config.PortNotification))
  90. def release(self):
  91. if None != self.receiverThread:
  92. self.receiverThread.stopThread()
  93. self.receiverThread.join()
  94. self.receiverThread = None
  95. if None != self.receiverSocket:
  96. self.receiverSocket.close()
  97. self.receiverSocket = None
  98. if None != self.notificationSocket:
  99. self.notificationSocket.close()
  100. self.notificationSocket = None