Euroscope.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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.com import Communication_pb2
  11. from aman.config.Server import Server
  12. from threading import Thread, _active
  13. class ReceiverThread(Thread):
  14. def __init__(self, socket, aman):
  15. Thread.__init__(self)
  16. self.Socket = socket
  17. self.AMAN = aman
  18. def run(self):
  19. while True:
  20. try:
  21. msg = self.Socket.recv(zmq.NOBLOCK)
  22. # parse the received message
  23. report = AircraftReport_pb2.AircraftReport()
  24. report.ParseFromString(msg)
  25. # try to associate the received aircraft to an airport
  26. self.AMAN.updateAircraftReport(report)
  27. except zmq.ZMQError as error:
  28. if zmq.EAGAIN == error.errno:
  29. time.sleep(0.5)
  30. continue
  31. else:
  32. return
  33. def threadId(self):
  34. if hasattr(self, '_thread_id'):
  35. return self._thread_id
  36. for id, thread in _active.items():
  37. if thread is self:
  38. return id
  39. def stopThread(self):
  40. id = self.threadId()
  41. res = ctypes.pythonapi.PyThreadState_SetAsyncExc(id, ctypes.py_object(SystemExit))
  42. if 1 < res:
  43. ctypes.pythonapi.PyThreadState_SetAsyncExc(id, 0)
  44. # @brief Receives and sends messages to EuroScope plugins
  45. class Euroscope:
  46. def __init__(self):
  47. self.Context = None
  48. self.ReceiverSocket = None
  49. self.ReceiverThread = None
  50. self.NotificationSocket = None
  51. def __del__(self):
  52. self.release()
  53. # @brief Initializes the ZMQ socket
  54. # @param[in] config The server configuration
  55. def acquire(self, configPath : str, config : Server, aman):
  56. self.Context = zmq.Context()
  57. # find the key directories
  58. serverKeyPath = os.path.join(os.path.join(configPath, 'keys'), 'server')
  59. if False == os.path.isdir(serverKeyPath):
  60. sys.stderr.write('No directory for the server key found')
  61. sys.exit(-1)
  62. print('Path to the server key: ' + serverKeyPath)
  63. clientKeyPath = os.path.join(os.path.join(configPath, 'keys'), 'clients')
  64. if False == os.path.isdir(clientKeyPath):
  65. sys.stderr.write('No directory for the client keys found')
  66. sys.exit(-1)
  67. print('Path to the client keys: ' + clientKeyPath)
  68. # read the certificates
  69. keyPairPath = glob.glob(os.path.join(serverKeyPath, '*.key_secret'))
  70. if 1 != len(keyPairPath):
  71. sys.stderr.write('No public-private keypair found for the server certificate')
  72. sys.exit(-1)
  73. keyPair = zmq.auth.load_certificate(keyPairPath[0])
  74. # initialize the receiver
  75. self.ReceiverSocket = zmq.Socket(self.Context, zmq.SUB)
  76. self.ReceiverSocket.setsockopt(zmq.CURVE_PUBLICKEY, keyPair[0])
  77. self.ReceiverSocket.setsockopt(zmq.CURVE_SECRETKEY, keyPair[1])
  78. self.ReceiverSocket.setsockopt(zmq.CURVE_SERVER, True)
  79. self.ReceiverSocket.bind('tcp://' + config.Address + ':' + str(config.PortReceiver))
  80. self.ReceiverSocket.setsockopt(zmq.SUBSCRIBE, b'')
  81. self.ReceiverThread = ReceiverThread(self.ReceiverSocket, aman)
  82. self.ReceiverThread.start()
  83. print('Listening to tcp://' + config.Address + ':' + str(config.PortReceiver))
  84. # initialize the notification
  85. self.NotificationSocket = zmq.Socket(self.Context, zmq.PUB)
  86. self.NotificationSocket.setsockopt(zmq.CURVE_PUBLICKEY, keyPair[0])
  87. self.NotificationSocket.setsockopt(zmq.CURVE_SECRETKEY, keyPair[1])
  88. self.NotificationSocket.setsockopt(zmq.CURVE_SERVER, True)
  89. self.NotificationSocket.bind('tcp://' + config.Address + ':' + str(config.PortNotification))
  90. print('Publishing to tcp://' + config.Address + ':' + str(config.PortNotification))
  91. def release(self):
  92. if None != self.ReceiverThread:
  93. self.ReceiverThread.stopThread()
  94. self.ReceiverThread.join()
  95. self.ReceiverThread = None
  96. if None != self.ReceiverSocket:
  97. self.ReceiverSocket.close()
  98. self.ReceiverSocket = None
  99. if None != self.NotificationSocket:
  100. self.NotificationSocket.close()
  101. self.NotificationSocket = None
  102. self.Context = None
  103. def sendSequence(self, airport : str, inbounds):
  104. if None == self.NotificationSocket:
  105. return
  106. sequence = Communication_pb2.AircraftSequence()
  107. sequence.airport = airport
  108. for inbound in inbounds:
  109. entry = sequence.sequence.add()
  110. entry.callsign = inbound.Callsign
  111. entry.arrivalRoute = inbound.PlannedStar.Name
  112. entry.arrivalRunway = inbound.PlannedRunway.Name
  113. for waypoint in inbound.PlannedArrivalRoute:
  114. wp = entry.waypoints.add()
  115. wp.name = waypoint.Waypoint.Name
  116. pta = str(waypoint.PTA)
  117. delimiter = pta.find('.')
  118. if -1 == delimiter:
  119. delimiter = pta.find('+')
  120. wp.pta = pta[0:delimiter]
  121. message = sequence.SerializeToString()
  122. self.NotificationSocket.send(message)