thread for incoming file transfers

This commit is contained in:
ingvar1995 2016-08-01 15:26:11 +03:00
parent d1437b3445
commit 2a0895018a
4 changed files with 51 additions and 6 deletions

View File

@ -9,6 +9,13 @@ from toxcore_enums_and_consts import *
from toxav_enums import * from toxav_enums import *
from tox import bin_to_string from tox import bin_to_string
from plugin_support import PluginLoader from plugin_support import PluginLoader
import queue
import threading
# -----------------------------------------------------------------------------------------------------------------
# Threads
# -----------------------------------------------------------------------------------------------------------------
class InvokeEvent(QtCore.QEvent): class InvokeEvent(QtCore.QEvent):
@ -33,6 +40,39 @@ _invoker = Invoker()
def invoke_in_main_thread(fn, *args, **kwargs): def invoke_in_main_thread(fn, *args, **kwargs):
QtCore.QCoreApplication.postEvent(_invoker, InvokeEvent(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 # Callbacks - current user
# ----------------------------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------------------------
@ -204,7 +244,8 @@ def file_recv_chunk(tox, friend_number, file_number, position, chunk, length, us
position, position,
None) None)
else: 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): 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 Outgoing chunk
""" """
if size: 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) Profile.get_instance().outgoing_chunk(friend_number, file_number, position, size)
else: else:
invoke_in_main_thread(Profile.get_instance().outgoing_chunk, invoke_in_main_thread(Profile.get_instance().outgoing_chunk,

View File

@ -8,7 +8,7 @@ except ImportError:
from PyQt4 import QtCore, QtGui from PyQt4 import QtCore, QtGui
from bootstrap import node_generator from bootstrap import node_generator
from mainscreen import MainWindow from mainscreen import MainWindow
from callbacks import init_callbacks from callbacks import init_callbacks, stop
from util import curr_directory, program_version from util import curr_directory, program_version
import styles.style import styles.style
import platform import platform
@ -281,6 +281,7 @@ class Toxygen:
self.mainloop.stop = True self.mainloop.stop = True
self.avloop.stop = True self.avloop.stop = True
plugin_helper.stop() plugin_helper.stop()
stop()
self.mainloop.wait() self.mainloop.wait()
self.init.wait() self.init.wait()
self.avloop.wait() self.avloop.wait()

View File

@ -93,6 +93,7 @@ class MainWindow(QtGui.QMainWindow, Singleton):
def event(self, event): def event(self, event):
if event.type() == QtCore.QEvent.WindowActivate: if event.type() == QtCore.QEvent.WindowActivate:
self.tray.setIcon(QtGui.QIcon(curr_directory() + '/images/icon.png')) self.tray.setIcon(QtGui.QIcon(curr_directory() + '/images/icon.png'))
self.messages.repaint()
return super(MainWindow, self).event(event) return super(MainWindow, self).event(event)
def retranslateUi(self): def retranslateUi(self):

View File

@ -1072,6 +1072,7 @@ class Profile(contact.Contact, Singleton):
os.path.basename(path), os.path.basename(path),
friend_number, friend_number,
st.get_file_number()) st.get_file_number())
if friend_number == self.get_active_number():
item = self.create_file_transfer_item(tm) item = self.create_file_transfer_item(tm)
st.set_state_changed_handler(item.update) st.set_state_changed_handler(item.update)
self._friends[self._active_friend].append_message(tm) self._friends[self._active_friend].append_message(tm)