def __init__(self, address, protocol, vehicle): self.address = address self.vehicle = vehicle self.protocol = protocol # Queue is filled with incoming messages (receive_thread) and emptied by receive worker (receive_task_thread) self.msg_queue = Queue.Queue() # Loads the initial vehicle's values according to class Params self.vehicle_params = Params(network=self, vehicle=vehicle) # For collision avoidance purposes self.drones = [] self.context = {'mode': None, 'mission': None, 'next_wp': None} self.priority = None # First entry in drones list will be our own vehicle self.drones.append(self.vehicle_params) # Normally it is kept as is until the first receive_thread # self.populate_drones('two_dummies') self.sock_send = None self.sock_receive = None # Create transceiver and worker threads self.t_send = SendThread(self, self.address) self.t_receive = ReceiveThread(self, self.msg_queue) self.t_task = ReceiveTaskThread(self, self.msg_queue) self.receive_count = self.t_receive.count self.task_count = self.t_task.count
def __init__(self, parent, account, package): QtGui.QDialog.__init__(self, parent) self.ui = Ui_SendDialog() self.ui.setupUi(self) self.send_thread = SendThread(self, account, package) self.history = [] # slots/signals self.connect(self.send_thread, QtCore.SIGNAL("transfer_progress"), self.transfer_progress) self.connect(self.send_thread, QtCore.SIGNAL("fatal_error"), self.fatal_error) self.connect(self.send_thread, QtCore.SIGNAL("canceled"), self.canceled) self.connect(self.send_thread, QtCore.SIGNAL("status"), self.status) self.connect(self.send_thread, QtCore.SIGNAL("done"), self.finished) self.send_thread.start()
class KurirSendDialog(QtGui.QDialog): def __init__(self, parent, account, package): QtGui.QDialog.__init__(self, parent) self.ui = Ui_SendDialog() self.ui.setupUi(self) self.send_thread = SendThread(self, account, package) self.history = [] # slots/signals self.connect(self.send_thread, QtCore.SIGNAL("transfer_progress"), self.transfer_progress) self.connect(self.send_thread, QtCore.SIGNAL("fatal_error"), self.fatal_error) self.connect(self.send_thread, QtCore.SIGNAL("canceled"), self.canceled) self.connect(self.send_thread, QtCore.SIGNAL("status"), self.status) self.connect(self.send_thread, QtCore.SIGNAL("done"), self.finished) self.send_thread.start() def finished(self): self.send_thread.quit() self.send_thread.wait() self.ui.labelOverall.setText("Done!") self.ui.btnCancelFinish.setText("Close") def status(self, msg): self.ui.labelOverall.setText(msg) def canceled(self): self.send_thread.quit() self.send_thread.wait() self.ui.labelOverall.setText("Canceled") self.ui.btnCancelFinish.setEnabled(True) self.ui.btnCancelFinish.setText("Close") def fatal_error(self, msg): QtGui.QMessageBox.critical(self, "Error", msg) self.ui.labelOverall.setText("Sending failed...") self.send_thread.quit() self.send_thread.wait() self.ui.btnCancelFinish.setText("Close") # def overall_progress(self, filename, progress, total): # print "file progress", progress, total # # self.history = [] # # if progress % 2: # norm_progress = (progress + 1)/2 # else: # norm_progress = progress / 2 # # self.ui.labelOverall.setText("Sending %s [%s of %s]" % (os.path.split(filename)[-1], norm_progress, total/2)) # self.ui.progressOverall.setMaximum(total) # self.ui.progressOverall.setValue(progress) # # if progress == total: # self.send_thread.quit() # self.send_thread.wait() # self.ui.btnCancelFinish.setText('Close') def transfer_progress(self, timestamp, progress, total): print "send progress", timestamp, progress, total self.history.append((timestamp, progress)) # calculate speed history = self.history[-500:] time_diff = history[-1][0] - history[0][0] progress_diff = history[-1][1] - history[0][1] if progress == total: self.history = [] print "timediff", time_diff, progress_diff if time_diff: speed = progress_diff / time_diff else: speed = progress_diff # self.ui.labelTransfer.setText("Transferred %s of %s (%s/s)" % (convert_bytes(progress), convert_bytes(total), convert_bytes(speed))) self.ui.progressTransfer.setMaximum(total) self.ui.progressTransfer.setValue(progress) self.setWindowTitle("%s/s" % (convert_bytes(speed))) @QtCore.pyqtSlot() def on_btnCancelFinish_clicked(self): if self.ui.btnCancelFinish.text() == "Close": self.accept() else: btn = QtGui.QMessageBox.question( self, "Question", "Are you sure you want to cancel this transfer?", QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, ) if btn == QtGui.QMessageBox.Yes: self.emit(QtCore.SIGNAL("cancel")) self.status("Cancelling...") self.ui.btnCancelFinish.setEnabled(False)
class Networking: MAX_STAY = 5 # seconds until entry is removed from structure MAX_ZONE = 500 # metres Consider as the maximum communication range NEAR_ZONE = 50 # metres FORMATION_NEAR_ZONE = 10 # metres CRITICAL_ZONE = 10 # metres POLL_RATE = 0.1 # how often to broadcast/receive messages def __init__(self, address, protocol, vehicle): self.address = address self.vehicle = vehicle self.protocol = protocol # Queue is filled with incoming messages (receive_thread) and emptied by receive worker (receive_task_thread) self.msg_queue = Queue.Queue() # Loads the initial vehicle's values according to class Params self.vehicle_params = Params(network=self, vehicle=vehicle) # For collision avoidance purposes self.drones = [] self.context = {'mode': None, 'mission': None, 'next_wp': None} self.priority = None # First entry in drones list will be our own vehicle self.drones.append(self.vehicle_params) # Normally it is kept as is until the first receive_thread # self.populate_drones('two_dummies') self.sock_send = None self.sock_receive = None # Create transceiver and worker threads self.t_send = SendThread(self, self.address) self.t_receive = ReceiveThread(self, self.msg_queue) self.t_task = ReceiveTaskThread(self, self.msg_queue) self.receive_count = self.t_receive.count self.task_count = self.t_task.count def run(self): """ 1.Opens the network sockets depending the explicitly stated protocol 2.Starts the threads responsible for sending (t_send) and receiving messages (t_receive, t_task) """ # Start networking protocol if self.protocol == "UDP_BROADCAST": if self.create_udp_broadcast(self.address): logger.info("Network interface started succesfully") else: logger.error("Network interface not started, exit...") # status -> critical # run failsafe else: # You can use whatever protocol you want here logger.warning("Unsupported protocol") # raise signal # Start threads self.t_receive.start() self.t_send.start() self.t_task.start() def stop(self): """ Closes the sockets """ self.sock_receive.close() # Release the resource self.sock_send.close() # Release the resource def create_udp_broadcast(self, address): """ If drone_network class is initialized with the UDP_Broadcast protocol it opens a UDP socket in the broadcast address If anything goes wrong an error appears which is handled out of the function """ try: # Setting up a UDP socket for sending broadcast messages self.sock_send = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) self.sock_send.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) # Setting up a UDP socket for receiving broadcast messages # Set to non-blocking because we have select() for that reason self.sock_receive = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) self.sock_receive.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) self.sock_receive.setblocking(0) self.sock_receive.bind(self.address) exit_status = 1 except socket.error, e: logger.debug("Error in socket operation: %s", e) exit_status = 0 finally:
class KurirSendDialog(QtGui.QDialog): def __init__(self, parent, account, package): QtGui.QDialog.__init__(self, parent) self.ui = Ui_SendDialog() self.ui.setupUi(self) self.send_thread = SendThread(self, account, package) self.history = [] # slots/signals self.connect(self.send_thread, QtCore.SIGNAL("transfer_progress"), self.transfer_progress) self.connect(self.send_thread, QtCore.SIGNAL("fatal_error"), self.fatal_error) self.connect(self.send_thread, QtCore.SIGNAL("canceled"), self.canceled) self.connect(self.send_thread, QtCore.SIGNAL("status"), self.status) self.connect(self.send_thread, QtCore.SIGNAL("done"), self.finished) self.send_thread.start() def finished(self): self.send_thread.quit() self.send_thread.wait() self.ui.labelOverall.setText("Done!") self.ui.btnCancelFinish.setText('Close') def status(self, msg): self.ui.labelOverall.setText(msg) def canceled(self): self.send_thread.quit() self.send_thread.wait() self.ui.labelOverall.setText("Canceled") self.ui.btnCancelFinish.setEnabled(True) self.ui.btnCancelFinish.setText("Close") def fatal_error(self, msg): QtGui.QMessageBox.critical(self, "Error", msg) self.ui.labelOverall.setText("Sending failed...") self.send_thread.quit() self.send_thread.wait() self.ui.btnCancelFinish.setText("Close") # def overall_progress(self, filename, progress, total): # print "file progress", progress, total # # self.history = [] # # if progress % 2: # norm_progress = (progress + 1)/2 # else: # norm_progress = progress / 2 # # self.ui.labelOverall.setText("Sending %s [%s of %s]" % (os.path.split(filename)[-1], norm_progress, total/2)) # self.ui.progressOverall.setMaximum(total) # self.ui.progressOverall.setValue(progress) # # if progress == total: # self.send_thread.quit() # self.send_thread.wait() # self.ui.btnCancelFinish.setText('Close') def transfer_progress(self, timestamp, progress, total): print "send progress", timestamp, progress, total self.history.append((timestamp, progress)) # calculate speed history = self.history[-500:] time_diff = history[-1][0] - history[0][0] progress_diff = history[-1][1] - history[0][1] if progress == total: self.history = [] print "timediff", time_diff, progress_diff if time_diff: speed = progress_diff / time_diff else: speed = progress_diff # self.ui.labelTransfer.setText("Transferred %s of %s (%s/s)" % (convert_bytes(progress), convert_bytes(total), convert_bytes(speed))) self.ui.progressTransfer.setMaximum(total) self.ui.progressTransfer.setValue(progress) self.setWindowTitle("%s/s" % (convert_bytes(speed))) @QtCore.pyqtSlot() def on_btnCancelFinish_clicked(self): if self.ui.btnCancelFinish.text() == 'Close': self.accept() else: btn = QtGui.QMessageBox.question( self, "Question", "Are you sure you want to cancel this transfer?", QtGui.QMessageBox.Yes | QtGui.QMessageBox.No) if btn == QtGui.QMessageBox.Yes: self.emit(QtCore.SIGNAL("cancel")) self.status("Cancelling...") self.ui.btnCancelFinish.setEnabled(False)