Beispiel #1
0
 def __init__(self, choker, config, connBuilder, connListener, connHandler, eventSched, httpRequester, inRate, outRate,
              ownAddrWatcher, peerId, peerPool, persister, progPath, curVersion):
     
     #given classes
     self.choker = choker
     self.config = config
     self.connBuilder = connBuilder
     self.connListener = connListener
     self.connHandler = connHandler
     self.eventSched = eventSched
     self.httpRequester = httpRequester
     self.inRate = inRate
     self.outRate = outRate
     self.ownAddrWatcher = ownAddrWatcher
     self.peerId = peerId
     self.peerPool = peerPool
     self.persister = persister
     self.progPath = progPath
     self.curVersion = curVersion
     
     #log
     self.log = logging.getLogger('BtQueueManager')
     
     #lock
     self.lock = threading.Lock()
     
     #queue
     self.queue = BtQueue(self.curVersion, self.persister)
     self.queueJobs = {}
     self._load()
Beispiel #2
0
class BtQueueManager:
    def __init__(self, choker, config, connBuilder, connListener, connHandler, eventSched, httpRequester, inRate, outRate,
                 ownAddrWatcher, peerId, peerPool, persister, progPath, curVersion):
        
        #given classes
        self.choker = choker
        self.config = config
        self.connBuilder = connBuilder
        self.connListener = connListener
        self.connHandler = connHandler
        self.eventSched = eventSched
        self.httpRequester = httpRequester
        self.inRate = inRate
        self.outRate = outRate
        self.ownAddrWatcher = ownAddrWatcher
        self.peerId = peerId
        self.peerPool = peerPool
        self.persister = persister
        self.progPath = progPath
        self.curVersion = curVersion
        
        #log
        self.log = logging.getLogger('BtQueueManager')
        
        #lock
        self.lock = threading.Lock()
        
        #queue
        self.queue = BtQueue(self.curVersion, self.persister)
        self.queueJobs = {}
        self._load()
        
        
    ##internal functions - loading
    
    def _load(self):
        #restart all queued jobs
        queueIds = self.queue.queueGet()
        queueInfo = self.queue.infoGetAll()
        for queueId in queueIds:
            #load one job
            failureMsg, obj = self._getJobObj(queueId, queueInfo[queueId])
            if failureMsg is None:
                #success
                self.queueJobs[queueId] = obj
            else:
                #failed to add
                self.log.warn("Failed to add queue job %i (type %s), reason: %s", queueId, queueInfo[queueId]['type'], failureMsg)
                self.queue.queueRemove(queueId)
                
        #cleanup persister
        btKeyMatcher = re.compile('^Bt([0-9]+)-')
        btKeys = self.persister.keys('^Bt[0-9]+-')
        for key in btKeys:
            matchObj = btKeyMatcher.match(key)
            assert matchObj is not None, 'passed key regex but still not valid: "%s"' % (key,)
            queueId = int(matchObj.group(1))
            if self.queue.queueContains(queueId):
                self.log.debug('Key "%s" belongs to an active queue job, not removing it', key)
            else:
                self.log.info('Key "%s" belongs to an inactive queue job, removing it', key)
                self.persister.remove(key, strict=False)
    
    
    ##internal functions - torrents
    
    def _getTorrentFilePath(self, torrentId):
        return os.path.join(self.progPath, 'Torrents', str(torrentId)+'.torrent')
    
    
    def _getTorrentObj(self, torrentId):
        failureMsg = None
        torrent = None
        
        #try to load torrent data from the usual place
        torrentFilePath = self._getTorrentFilePath(torrentId)
        self.log.debug('Torrent %i: trying to read torrent data from "%s"', torrentId, torrentFilePath)
        try:
            fl = open(torrentFilePath, 'rb')
            with fl:
                torrentFileData = fl.read()
        except (IOError, OSError):
            failureMsg = 'Could not read torrent file from "%s"' % encodeStrForPrinting(torrentFilePath)
    
        if failureMsg is None:
            #successfully read the torrent data
            self.log.debug('Torrent %i: trying to parse read torrent data', torrentId)
            torrent = Torrent()
            try:
                torrent.load(torrentFileData)
            except TorrentException, e:
                failureMsg = e.reason
            except: