From 2a0895018af1ebee0b7934a2c88967a831eba468 Mon Sep 17 00:00:00 2001 From: ingvar1995 Date: Mon, 1 Aug 2016 15:26:11 +0300 Subject: [PATCH] thread for incoming file transfers --- toxygen/callbacks.py | 44 ++++++++++++++++++++++++++++++++++++++++++- toxygen/main.py | 3 ++- toxygen/mainscreen.py | 1 + toxygen/profile.py | 9 +++++---- 4 files changed, 51 insertions(+), 6 deletions(-) diff --git a/toxygen/callbacks.py b/toxygen/callbacks.py index fc13b99..02a69ca 100644 --- a/toxygen/callbacks.py +++ b/toxygen/callbacks.py @@ -9,6 +9,13 @@ from toxcore_enums_and_consts import * from toxav_enums import * from tox import bin_to_string from plugin_support import PluginLoader +import queue +import threading + + +# ----------------------------------------------------------------------------------------------------------------- +# Threads +# ----------------------------------------------------------------------------------------------------------------- class InvokeEvent(QtCore.QEvent): @@ -33,6 +40,39 @@ _invoker = Invoker() def invoke_in_main_thread(fn, *args, **kwargs): QtCore.QCoreApplication.postEvent(_invoker, InvokeEvent(fn, *args, **kwargs)) + +class FileTransfersThread(threading.Thread): + + def __init__(self): + self._queue = queue.Queue() + self._timeout = 0.01 + self._continue = True + super().__init__() + + def execute(self, function, *args, **kwargs): + self._queue.put((function, args, kwargs)) + + def stop(self): + self._continue = False + + def run(self): + while self._continue: + try: + function, args, kwargs = self._queue.get(timeout=self._timeout) + function(*args, **kwargs) + except queue.Empty: + pass + except queue.Full: + print('Full') + +_thread = FileTransfersThread() +_thread.start() + + +def stop(): + _thread.stop() + _thread.join() + # ----------------------------------------------------------------------------------------------------------------- # Callbacks - current user # ----------------------------------------------------------------------------------------------------------------- @@ -204,7 +244,8 @@ def file_recv_chunk(tox, friend_number, file_number, position, chunk, length, us position, None) else: - Profile.get_instance().incoming_chunk(friend_number, file_number, position, chunk[:length]) + _thread.execute(Profile.get_instance().incoming_chunk, friend_number, file_number, position, chunk[:length]) + #Profile.get_instance().incoming_chunk(friend_number, file_number, position, chunk[:length]) def file_chunk_request(tox, friend_number, file_number, position, size, user_data): @@ -212,6 +253,7 @@ def file_chunk_request(tox, friend_number, file_number, position, size, user_dat Outgoing chunk """ if size: + #_thread.execute(Profile.get_instance().outgoing_chunk, friend_number, file_number, position, size) Profile.get_instance().outgoing_chunk(friend_number, file_number, position, size) else: invoke_in_main_thread(Profile.get_instance().outgoing_chunk, diff --git a/toxygen/main.py b/toxygen/main.py index 3d4d9be..42b22c1 100644 --- a/toxygen/main.py +++ b/toxygen/main.py @@ -8,7 +8,7 @@ except ImportError: from PyQt4 import QtCore, QtGui from bootstrap import node_generator from mainscreen import MainWindow -from callbacks import init_callbacks +from callbacks import init_callbacks, stop from util import curr_directory, program_version import styles.style import platform @@ -281,6 +281,7 @@ class Toxygen: self.mainloop.stop = True self.avloop.stop = True plugin_helper.stop() + stop() self.mainloop.wait() self.init.wait() self.avloop.wait() diff --git a/toxygen/mainscreen.py b/toxygen/mainscreen.py index cb7eba2..14c9f86 100644 --- a/toxygen/mainscreen.py +++ b/toxygen/mainscreen.py @@ -93,6 +93,7 @@ class MainWindow(QtGui.QMainWindow, Singleton): def event(self, event): if event.type() == QtCore.QEvent.WindowActivate: self.tray.setIcon(QtGui.QIcon(curr_directory() + '/images/icon.png')) + self.messages.repaint() return super(MainWindow, self).event(event) def retranslateUi(self): diff --git a/toxygen/profile.py b/toxygen/profile.py index ed04fac..3f6fe42 100644 --- a/toxygen/profile.py +++ b/toxygen/profile.py @@ -1072,10 +1072,11 @@ class Profile(contact.Contact, Singleton): os.path.basename(path), friend_number, st.get_file_number()) - item = self.create_file_transfer_item(tm) - st.set_state_changed_handler(item.update) - self._friends[self._active_friend].append_message(tm) - self._messages.scrollToBottom() + if friend_number == self.get_active_number(): + item = self.create_file_transfer_item(tm) + st.set_state_changed_handler(item.update) + self._friends[self._active_friend].append_message(tm) + self._messages.scrollToBottom() def incoming_chunk(self, friend_number, file_number, position, data): """