コード例 #1
0
    def __init__(self,session):
        self.session = session
        self.sesslock = session.sesslock
        self.sessconfig = session.sessconfig

        # Notifier for callbacks to API user
        self.threadpool = ThreadPool(2)
        self.notifier = Notifier.getInstance(self.threadpool)
コード例 #2
0
    def __init__(self,session):
        self.session = session
        self.sesslock = session.sesslock
        self.sessconfig = session.sessconfig

        # Notifier for callbacks to API user
        self.threadpool = ThreadPool(2)
        self.notifier = Notifier.getInstance(self.threadpool)
コード例 #3
0
class UserCallbackHandler:
    
    def __init__(self,session):
        self.session = session
        self.sesslock = session.sesslock
        self.sessconfig = session.sessconfig

        # Notifier for callbacks to API user
        self.threadpool = ThreadPool(2)
        self.notifier = Notifier.getInstance(self.threadpool)

    def shutdown(self):
        # stop threadpool
        self.threadpool.joinAll()

    def perform_vod_usercallback(self,d,usercallback,event,params):
        """ Called by network thread """
        if DEBUG:
            print >>sys.stderr,time.asctime(),'-', "Session: perform_vod_usercallback()",`d.get_def().get_name_as_unicode()`
        def session_vod_usercallback_target():
            try:
                usercallback(d,event,params)
            except:
                print_exc()
        self.perform_usercallback(session_vod_usercallback_target)

    def perform_getstate_usercallback(self,usercallback,data,returncallback):
        """ Called by network thread """
        if DEBUG:
            print >>sys.stderr,time.asctime(),'-', "Session: perform_getstate_usercallback()"
        def session_getstate_usercallback_target():
            try:
                (when,getpeerlist) = usercallback(data)
                returncallback(usercallback,when,getpeerlist)
            except:
                print_exc()
        self.perform_usercallback(session_getstate_usercallback_target)


    def perform_removestate_callback(self,infohash,contentdest,removecontent):
        """ Called by network thread """
        if DEBUG:
            print >>sys.stderr,time.asctime(),'-', "Session: perform_removestate_callback()"
        def session_removestate_callback_target():
            if DEBUG:
                print >>sys.stderr,time.asctime(),'-', "Session: session_removestate_callback_target called",currentThread().getName()
            try:
                self.sesscb_removestate(infohash,contentdest,removecontent)
            except:
                print_exc()
        self.perform_usercallback(session_removestate_callback_target)
        
    def perform_usercallback(self,target):
        self.sesslock.acquire()
        try:
            # TODO: thread pool, etc.
            self.threadpool.queueTask(target)
            
        finally:
            self.sesslock.release()

    def sesscb_removestate(self,infohash,contentdest,removecontent):
        """  See DownloadImpl.setup().
        Called by SessionCallbackThread """
        if DEBUG:
            print >>sys.stderr,time.asctime(),'-', "Session: sesscb_removestate called",`infohash`,`contentdest`,removecontent
        self.sesslock.acquire()
        try:
            if self.session.lm.download_exists(infohash):
                print >>sys.stderr,time.asctime(),'-', "Session: sesscb_removestate: Download is back, restarted? Canceling removal!",`infohash`
                return
            
            dlpstatedir = os.path.join(self.sessconfig['state_dir'],STATEDIR_DLPSTATE_DIR)
        finally:
            self.sesslock.release()

        # See if torrent uses internal tracker
        try:
            self.session.remove_from_internal_tracker_by_infohash(infohash)
        except:
            # Show must go on
            print_exc()

        # Remove checkpoint
        hexinfohash = binascii.hexlify(infohash)
        try:
            basename = hexinfohash+'.pickle'
            filename = os.path.join(dlpstatedir,basename)
            if DEBUG:
                print >>sys.stderr,time.asctime(),'-', "Session: sesscb_removestate: removing dlcheckpoint entry",filename
            if os.access(filename,os.F_OK):
                os.remove(filename)
        except:
            # Show must go on
            print_exc()

        # Remove downloaded content from disk
        if removecontent:
            if DEBUG:
                print >>sys.stderr,time.asctime(),'-', "Session: sesscb_removestate: removing saved content",contentdest
            if not os.path.isdir(contentdest):
                # single-file torrent
                os.remove(contentdest)
            else:
                # multi-file torrent
                shutil.rmtree(contentdest,True) # ignore errors


    def notify(self, subject, changeType, obj_id, *args):
        """
        Notify all interested observers about an event with threads from the pool
        """
        if DEBUG:
            print >>sys.stderr,time.asctime(),'-', "ucb: notify called:",subject,changeType,`obj_id`, args
        self.notifier.notify(subject,changeType,obj_id,*args)
コード例 #4
0
ファイル: test_threadpool.py プロジェクト: Swizec/IJS-stuff
 def setUp(self):
     """ unittest test setup code """
     self.tp = ThreadPool(10)
     self.exp = []
     self.gotlock = RLock()
     self.got = []
コード例 #5
0
ファイル: test_threadpool.py プロジェクト: Swizec/IJS-stuff
class TestThreadPool(unittest.TestCase):
    """ 
    Parent class for testing internal thread pool of Tribler
    """
    
    def setUp(self):
        """ unittest test setup code """
        self.tp = ThreadPool(10)
        self.exp = []
        self.gotlock = RLock()
        self.got = []

    def tearDown(self):
        """ unittest test tear down code """
        time.sleep(2)
        self.got.sort()
        self.assertEquals(self.exp,self.got)
    
    def test_queueTask1(self):
        print >>sys.stderr,"test_queueTask1:"
        self.exp = [1]
        self.tp.queueTask(lambda:self.do_task(1))
        
    def do_task(self,val):
        self.gotlock.acquire()
        print >>sys.stderr,"test: got task",val
        self.got.append(val)
        self.gotlock.release()
        
    def test_queueTask10lambda(self):
        print >>sys.stderr,"test_queueTask10lambda:"
        self.exp = range(1,11)
        def wrapper(x):
            self.tp.queueTask(lambda:self.do_task(x))
                          
        for i in range(1,11):
            print >>sys.stderr,"test: exp task",i
            wrapper(i)

    #
    # Confusing lambda crap, do explicit:
    #
    def test_queueTask10explicit(self):
        print >>sys.stderr,"test_queueTask10explicit:"
        self.exp = range(1,11)
        self.tp.queueTask(self.do_task1)
        self.tp.queueTask(self.do_task2)
        self.tp.queueTask(self.do_task3)
        self.tp.queueTask(self.do_task4)
        self.tp.queueTask(self.do_task5)
        self.tp.queueTask(self.do_task6)
        self.tp.queueTask(self.do_task7)
        self.tp.queueTask(self.do_task8)
        self.tp.queueTask(self.do_task9)
        self.tp.queueTask(self.do_task10)


    def test_joinAll(self):
        print >>sys.stderr,"test_joinall:"
        self.exp = range(1,6)
        print >>sys.stderr,"test: adding tasks"
        self.tp.queueTask(self.do_task1)
        self.tp.queueTask(self.do_task2)
        self.tp.queueTask(self.do_task3)
        self.tp.queueTask(self.do_task4)
        self.tp.queueTask(self.do_task5)
        print >>sys.stderr,"test: join all"
        self.tp.joinAll()
        print >>sys.stderr,"test: adding post tasks, shouldn't get run"
        self.tp.queueTask(self.do_task6)
        self.tp.queueTask(self.do_task7)
        self.tp.queueTask(self.do_task8)
        self.tp.queueTask(self.do_task9)
        self.tp.queueTask(self.do_task10)

    def test_setThreadCountPlus10(self):
        print >>sys.stderr,"test_setThreadCountPlus10:"
        print >>sys.stderr,"test: pre threads",self.tp.getThreadCount()
        self.tp.setThreadCount(20)
        print >>sys.stderr,"test: post threads",self.tp.getThreadCount()
        time.sleep(1)
        self.test_joinAll()

    def test_setThreadCountMinus8(self):
        print >>sys.stderr,"test_setThreadCountMinus8:"
        print >>sys.stderr,"test: pre threads",self.tp.getThreadCount()
        self.tp.setThreadCount(2)
        print >>sys.stderr,"test: post threads",self.tp.getThreadCount()
        time.sleep(1)
        self.test_joinAll()


    def do_task1(self):
        self.gotlock.acquire()
        self.got.append(1)
        self.gotlock.release()

    def do_task2(self):
        self.gotlock.acquire()
        self.got.append(2)
        self.gotlock.release()

    def do_task3(self):
        self.gotlock.acquire()
        self.got.append(3)
        self.gotlock.release()

    def do_task4(self):
        self.gotlock.acquire()
        self.got.append(4)
        self.gotlock.release()

    def do_task5(self):
        self.gotlock.acquire()
        self.got.append(5)
        self.gotlock.release()

    def do_task6(self):
        self.gotlock.acquire()
        self.got.append(6)
        self.gotlock.release()

    def do_task7(self):
        self.gotlock.acquire()
        self.got.append(7)
        self.gotlock.release()

    def do_task8(self):
        self.gotlock.acquire()
        self.got.append(8)
        self.gotlock.release()

    def do_task9(self):
        self.gotlock.acquire()
        self.got.append(9)
        self.gotlock.release()

    def do_task10(self):
        self.gotlock.acquire()
        self.got.append(10)
        self.gotlock.release()
コード例 #6
0
class UserCallbackHandler:
    
    def __init__(self,session):
        self.session = session
        self.sesslock = session.sesslock
        self.sessconfig = session.sessconfig

        # Notifier for callbacks to API user
        self.threadpool = ThreadPool(2)
        self.notifier = Notifier.getInstance(self.threadpool)

    def shutdown(self):
        # stop threadpool
        self.threadpool.joinAll()

    def perform_vod_usercallback(self,d,usercallback,event,params):
        """ Called by network thread """
        if DEBUG:
            print >>sys.stderr,"Session: perform_vod_usercallback()",`d.get_def().get_name_as_unicode()`
        def session_vod_usercallback_target():
            try:
                usercallback(d,event,params)
            except:
                print_exc()
        self.perform_usercallback(session_vod_usercallback_target)

    def perform_getstate_usercallback(self,usercallback,data,returncallback):
        """ Called by network thread """
        if DEBUG:
            print >>sys.stderr,"Session: perform_getstate_usercallback()"
        def session_getstate_usercallback_target():
            try:
                (when,getpeerlist) = usercallback(data)
                returncallback(usercallback,when,getpeerlist)
            except:
                print_exc()
        self.perform_usercallback(session_getstate_usercallback_target)


    def perform_removestate_callback(self,infohash,contentdest,removecontent):
        """ Called by network thread """
        if DEBUG:
            print >>sys.stderr,"Session: perform_removestate_callback()"
        def session_removestate_callback_target():
            if DEBUG:
                print >>sys.stderr,"Session: session_removestate_callback_target called",currentThread().getName()
            try:
                self.sesscb_removestate(infohash,contentdest,removecontent)
            except:
                print_exc()
        self.perform_usercallback(session_removestate_callback_target)
        
    def perform_usercallback(self,target):
        self.sesslock.acquire()
        try:
            # TODO: thread pool, etc.
            self.threadpool.queueTask(target)
            
        finally:
            self.sesslock.release()

    def sesscb_removestate(self,infohash,contentdest,removecontent):
        """  See DownloadImpl.setup().
        Called by SessionCallbackThread """
        if DEBUG:
            print >>sys.stderr,"Session: sesscb_removestate called",`infohash`,`contentdest`,removecontent
        self.sesslock.acquire()
        try:
            dlpstatedir = os.path.join(self.sessconfig['state_dir'],STATEDIR_DLPSTATE_DIR)
        finally:
            self.sesslock.release()

        # See if torrent uses internal tracker
        try:
            self.session.remove_from_internal_tracker_by_infohash(infohash)
        except:
            # Show must go on
            print_exc()

        # Remove checkpoint
        hexinfohash = binascii.hexlify(infohash)
        try:
            basename = hexinfohash+'.pickle'
            filename = os.path.join(dlpstatedir,basename)
            if DEBUG:
                print >>sys.stderr,"Session: sesscb_removestate: removing dlcheckpoint entry",filename
            if os.access(filename,os.F_OK):
                os.remove(filename)
        except:
            # Show must go on
            print_exc()

        # Remove downloaded content from disk
        if removecontent:
            if DEBUG:
                print >>sys.stderr,"Session: sesscb_removestate: removing saved content",contentdest
            if not os.path.isdir(contentdest):
                # single-file torrent
                os.remove(contentdest)
            else:
                # multi-file torrent
                shutil.rmtree(contentdest,True) # ignore errors


    def notify(self, subject, changeType, obj_id, *args):
        """
        Notify all interested observers about an event with threads from the pool
        """
        if DEBUG:
            print >>sys.stderr,"ucb: notify called:",subject,changeType,`obj_id`, args
        self.notifier.notify(subject,changeType,obj_id,*args)