change to request-response communication for an easier update of euroscope

This commit is contained in:
Sven Czarnian
2021-11-18 08:46:47 +01:00
parent 60d671ea9a
commit 74bf3e7439

View File

@@ -14,24 +14,31 @@ from aman.com import Communication_pb2
from aman.config.Server import Server from aman.config.Server import Server
from threading import Thread, _active from threading import Thread, _active
class ReceiverThread(Thread): class ComThread(Thread):
def __init__(self, socket, aman): def __init__(self, com, aman):
Thread.__init__(self) Thread.__init__(self)
self.Socket = socket self.Com = com
self.AMAN = aman self.AMAN = aman
def run(self): def run(self):
while True: while True:
try: try:
msg = self.Socket.recv(zmq.NOBLOCK) msg = self.Com.Socket.recv(zmq.NOBLOCK)
# parse the received message # parse the received message
report = Communication_pb2.AircraftUpdate() report = Communication_pb2.AircraftUpdate()
report.ParseFromString(msg) report.ParseFromString(msg)
# try to associate the received aircrafts to airports # try to associate the received aircrafts to airports
icao = []
for inbound in report.reports: for inbound in report.reports:
self.AMAN.updateAircraftReport(inbound) self.AMAN.updateAircraftReport(inbound)
icao = inbound.destination
# get the sequence of the airport
airport = self.AMAN.findAirport(icao)
if None != airport:
self.Com.sendSequence('', airport.inboundSequence(), airport.WeatherModel)
except zmq.ZMQError as error: except zmq.ZMQError as error:
if zmq.EAGAIN == error.errno: if zmq.EAGAIN == error.errno:
@@ -44,9 +51,8 @@ class ReceiverThread(Thread):
class Euroscope: class Euroscope:
def __init__(self, configPath : str, config : Server, aman): def __init__(self, configPath : str, config : Server, aman):
self.Context = None self.Context = None
self.ReceiverSocket = None self.Socket = None
self.ReceiverThread = None self.Thread = None
self.NotificationSocket = None
self.Context = zmq.Context() self.Context = zmq.Context()
# find the key directories # find the key directories
@@ -70,27 +76,19 @@ class Euroscope:
keyPair = zmq.auth.load_certificate(keyPairPath[0]) keyPair = zmq.auth.load_certificate(keyPairPath[0])
# initialize the receiver # initialize the receiver
self.ReceiverSocket = zmq.Socket(self.Context, zmq.SUB) self.Socket = zmq.Socket(self.Context, zmq.REP)
self.ReceiverSocket.setsockopt(zmq.CURVE_PUBLICKEY, keyPair[0]) self.Socket.setsockopt(zmq.CURVE_PUBLICKEY, keyPair[0])
self.ReceiverSocket.setsockopt(zmq.CURVE_SECRETKEY, keyPair[1]) self.Socket.setsockopt(zmq.CURVE_SECRETKEY, keyPair[1])
self.ReceiverSocket.setsockopt(zmq.CURVE_SERVER, True) self.Socket.setsockopt(zmq.CURVE_SERVER, True)
self.ReceiverSocket.bind('tcp://' + config.Address + ':' + str(config.PortReceiver)) self.Socket.bind('tcp://' + config.Address + ':' + str(config.PortReceiver))
self.ReceiverSocket.setsockopt(zmq.SUBSCRIBE, b'') #self.Socket.setsockopt(zmq.SUBSCRIBE, b'')
self.ReceiverThread = ReceiverThread(self.ReceiverSocket, aman) self.Thread = ComThread(self, aman)
self.ReceiverThread.setDaemon(True) self.Thread.setDaemon(True)
self.ReceiverThread.start() self.Thread.start()
print('Listening to tcp://' + config.Address + ':' + str(config.PortReceiver)) print('Listening to tcp://' + config.Address + ':' + str(config.PortReceiver))
# initialize the notification
self.NotificationSocket = zmq.Socket(self.Context, zmq.PUB)
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))
print('Publishing to tcp://' + config.Address + ':' + str(config.PortNotification))
def sendSequence(self, airport : str, inbounds, weather): def sendSequence(self, airport : str, inbounds, weather):
if None == self.NotificationSocket: if None == self.Socket:
return return
sequence = Communication_pb2.AircraftSequence() sequence = Communication_pb2.AircraftSequence()
@@ -133,4 +131,4 @@ class Euroscope:
wp.pta = pta[0:delimiter] wp.pta = pta[0:delimiter]
message = sequence.SerializeToString() message = sequence.SerializeToString()
self.NotificationSocket.send(message) self.Socket.send(message)