diff --git a/GroundSystem.py b/GroundSystem.py index 318a142..130757e 100644 --- a/GroundSystem.py +++ b/GroundSystem.py @@ -54,7 +54,7 @@ def __init__(self, parent=None): def closeEvent(self, evnt): if self.RoutingService: self.RoutingService.stop() - print "Stopped routing service" + print ("Stopped routing service") super(GroundSystem, self).closeEvent(evnt) @@ -70,7 +70,7 @@ def getSelectedSpacecraftName(self): # Display popup with error # def DisplayErrorMessage(self, message): - print message + print (message) alert = QtGui.QMessageBox() alert.setText(message) alert.setIcon(QtGui.QMessageBox.Warning) diff --git a/RoutingService.py b/RoutingService.py index 206ad9e..425dccd 100644 --- a/RoutingService.py +++ b/RoutingService.py @@ -60,44 +60,55 @@ def run(self): self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.sock.bind(('', udpRecvPort)) - # Wait for UDP messages - while True: - try: - # Receive message - datagram, host = self.sock.recvfrom(4096) # buffer size is 1024 bytes - - # Ignore datagram if it is not long enough (doesnt contain tlm header?) - if len(datagram) < 6: - continue - - # Read host address - hostIpAddress = host[0] - - # - # Add Host to the list if not already in list - # - if not any(hostIpAddress in s for s in self.ipAddressesList): - hostName = "Spacecraft" + str(len(self.spacecraftNames)) - print "Detected " + hostName + " at " + hostIpAddress - self.ipAddressesList.append(hostIpAddress); - self.spacecraftNames.append(hostName) - self.emit(self.signalUpdateIpList, hostIpAddress, hostName) - - # Forward the message using zeroMQ - name = self.spacecraftNames[self.ipAddressesList.index(hostIpAddress)] - self.forwardMessage(datagram, name) - - # Handle errors - except socket.error, v: - print 'Ignored socket error.' - sleep(1) + print ('Attempting to wait for UDP messages') + + socketErrorCount = 0 + while socketErrorCount < 5: + + # Wait for UDP messages + while True: + try: + # Receive message + datagram, host = self.sock.recvfrom(4096) # buffer size is 1024 bytes + + print ('length datagram: %d' % len(datagram)) + + # Ignore datagram if it is not long enough (doesnt contain tlm header?) + if len(datagram) < 6: + continue + + # Read host address + hostIpAddress = host[0] + + # + # Add Host to the list if not already in list + # + if not any(hostIpAddress in s for s in self.ipAddressesList): + hostName = 'Spacecraft' + str(len(self.spacecraftNames)) + my_hostName_as_bytes = str.encode(hostName) + print ("Detected " + hostName + " at " + hostIpAddress) + self.ipAddressesList.append(hostIpAddress); + self.spacecraftNames.append(my_hostName_as_bytes) + self.emit(self.signalUpdateIpList, hostIpAddress, my_hostName_as_bytes) + + # Forward the message using zeroMQ + name = self.spacecraftNames[self.ipAddressesList.index(hostIpAddress)] + self.forwardMessage(datagram, name) + + # Handle errors + except socket.error as v: + print ('Ignored socket error for attempt %s' % socketErrorCount) + socketErrorCount = socketErrorCount + 1 + sleep(1) # Apply header using hostname and packet id and send msg using zeroMQ def forwardMessage(self, datagram, hostName): # Forward message to channel GroundSystem.. pktId = self.getPktId(datagram) - header = "GroundSystem." + hostName + ".TelemetryPackets." + pktId - self.publisher.send_multipart([header, datagram]) + my_decoded_hostName = hostName.decode() + header = "GroundSystem." + my_decoded_hostName + ".TelemetryPackets." + pktId + my_header_as_bytes = str.encode(header) + self.publisher.send_multipart([my_header_as_bytes, datagram]) #print header