Exemplo n.º 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)
    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)
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()`
        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:
            if self.session.lm.download_exists(infohash):
                print >>sys.stderr,"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,"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)
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()`
        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,contentdests,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,contentdests,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,contentdests,removecontent):
        """  See DownloadImpl.setup().
        Called by SessionCallbackThread """
        if DEBUG:
            print >>sys.stderr,"Session: sesscb_removestate called",`infohash`,contentdests,removecontent
        self.sesslock.acquire()
        try:
            if self.session.lm.download_exists(infohash):
                print >>sys.stderr,"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,"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",contentdests
            
            contentdirs = set()
            for filename in contentdests:
                if os.path.isfile(filename):
                    os.remove(filename)
                contentdirs.add(os.path.dirname(filename))
            
            #multifile, see if we need to remove any empty dirs
            if len(contentdests) > 1:
                def remove_if_empty(basedir):
                    #first try to remove sub-dirs
                    files = os.listdir(basedir)
                    for filename in files:
                        absfilename = os.path.join(basedir, filename)
                        if os.path.isdir(absfilename) and absfilename in contentdirs:
                            remove_if_empty(absfilename)
                    
                    #see if we are empty
                    files = os.listdir(basedir)
                    #ignore thumbs.db files
                    files = [file for file in files if not file.lower().endswith('thumbs.db')]
                    
                    if len(files) == 0:
                        os.rmdir(basedir)
                
                basedir = os.path.commonprefix(contentdests)
                remove_if_empty(basedir)

    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)
Exemplo n.º 5
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)
            trackerdir = os.path.join(self.sessconfig['state_dir'],
                                      STATEDIR_ITRACKER_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)
Exemplo n.º 6
0
 def setUp(self):
     """ unittest test setup code """
     self.tp = ThreadPool(10)
     self.exp = []
     self.gotlock = RLock()
     self.got = []
Exemplo n.º 7
0
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()
Exemplo n.º 8
0
 def setUp(self):
     """ unittest test setup code """
     self.tp = ThreadPool(10)
     self.exp = []
     self.gotlock = RLock()
     self.got = []
Exemplo n.º 9
0
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 """
        self.tp.joinAll()

        time.sleep(2)
        self.got.sort()
        self.assertEquals(self.exp, self.got)

        ts = enumerate_threads()
        print >> sys.stderr, "test_threadpool: Number of threads still running", len(
            ts)
        for t in ts:
            print >> sys.stderr, "test_threadpool: Thread still running", t.getName(
            ), "daemon", t.isDaemon(), "instance:", t

    def test_queueTask1(self):
        if DEBUG:
            print >> sys.stderr, "test_queueTask1:"
        self.exp = [1]
        self.tp.queueTask(lambda: self.do_task(1))

    def do_task(self, val):
        self.gotlock.acquire()
        if DEBUG:
            print >> sys.stderr, "test: got task", val
        self.got.append(val)
        self.gotlock.release()

    def test_queueTask10lambda(self):
        if DEBUG:
            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):
            if DEBUG:
                print >> sys.stderr, "test: exp task", i
            wrapper(i)

    #
    # Confusing lambda crap, do explicit:
    #
    def test_queueTask10explicit(self):
        if DEBUG:
            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):
        if DEBUG:
            print >> sys.stderr, "test_joinall:"
        self.exp = range(1, 6)
        if DEBUG:
            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)
        if DEBUG:
            print >> sys.stderr, "test: join all"
        self.tp.joinAll()
        if DEBUG:
            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):
        if DEBUG:
            print >> sys.stderr, "test_setThreadCountPlus10:"
            print >> sys.stderr, "test: pre threads", self.tp.getThreadCount()
        self.tp.setThreadCount(20)
        if DEBUG:
            print >> sys.stderr, "test: post threads", self.tp.getThreadCount()
        time.sleep(1)
        self.test_joinAll()

    def test_setThreadCountMinus8(self):
        if DEBUG:
            print >> sys.stderr, "test_setThreadCountMinus8:"
            print >> sys.stderr, "test: pre threads", self.tp.getThreadCount()
        self.tp.setThreadCount(2)
        if DEBUG:
            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()