def __init__(self, myid, tracker, hash, interval, peers, timeout, rawserver, disallow, isdisallowed): self.tracker = tracker self.interval = interval self.hash = hash self.operatinginterval = interval self.peers = peers self.rawserver = rawserver self.disallow = disallow self.isdisallowed = isdisallowed self.active = True self.busy = False self.errors = 0 self.rejected = 0 self.trackererror = False self.peerlists = [] cfg = { 'min_peers': peers, 'max_initiate': peers, 'rerequest_interval': interval, 'http_timeout': timeout } self.rerequester = Rerequester(0, myid, hash, [[tracker]], cfg, rawserver.add_task, rawserver.add_task, self.errorfunc, excfunc, self.addtolist, R_0, R_1, R_0, R_0, R_0, R_0, Event()) if self.isactive(): rawserver.add_task( self.refresh, randrange(int(self.interval / 10), self.interval))
def __init__(self, myid, tracker, hash, interval, peers, timeout, rawserver, disallow, isdisallowed): self.tracker = tracker self.interval = interval self.hash = hash self.operatinginterval = interval self.peers = peers self.rawserver = rawserver self.disallow = disallow self.isdisallowed = isdisallowed self.active = True self.busy = False self.errors = 0 self.rejected = 0 self.trackererror = False self.peerlists = [] self.rerequester = Rerequester([[tracker]], interval, rawserver.add_task, lambda: 0, peers, self.addtolist, rawserver.external_add_task, lambda: 1, 0, 0, 0, '', myid, hash, timeout, self.errorfunc, excfunc, peers, Event(), lambda: 0, lambda: 0) if self.isactive(): rawserver.add_task(self.refresh, randrange(int(self.interval/10), self.interval))
def __init__(self, myid, tracker, hash, interval, peers, timeout, rawserver, disallow, isdisallowed): self.tracker = tracker self.interval = interval self.hash = hash self.operatinginterval = interval self.peers = peers self.rawserver = rawserver self.disallow = disallow self.isdisallowed = isdisallowed self.active = True self.busy = False self.errors = 0 self.rejected = 0 self.trackererror = False self.peerlists = [] cfg = { 'min_peers': peers, 'max_initiate': peers, 'rerequest_interval': interval, 'http_timeout': timeout } self.rerequester = Rerequester( 0, myid, hash, [[tracker]], cfg, rawserver.add_task, rawserver.add_task, self.errorfunc, excfunc, self.addtolist, R_0, R_1, R_0, R_0, R_0, R_0, Event() ) if self.isactive(): rawserver.add_task(self.refresh, randrange(int(self.interval/10), self.interval))
if storagewrapper.do_I_have(i): picker.complete(i) downloader = Downloader(storagewrapper, picker, config['request_backlog'], config['max_rate_period'], len(pieces), downmeasure, config['snub_time'], ratemeasure.data_came_in) connecter = Connecter(make_upload, downloader, choker, len(pieces), upmeasure, config['max_upload_rate'] * 1024, rawserver.add_task) infohash = sha(bencode(info)).digest() encoder = Encoder(connecter, rawserver, myid, config['max_message_length'], rawserver.add_task, config['keepalive_interval'], infohash, config['max_initiate']) rerequest = Rerequester(response['announce'], config['rerequest_interval'], rawserver.add_task, connecter.how_many_connections, config['min_peers'], encoder.start_connection, rawserver.add_task, storagewrapper.get_amount_left, upmeasure.get_total, downmeasure.get_total, listen_port, config['ip'], myid, infohash, config['http_timeout'], errorfunc, config['max_initiate'], doneflag, upmeasure.get_rate, downmeasure.get_rate, encoder.ever_got_incoming) if config['spew']: spewflag.set() DownloaderFeedback(choker, rawserver.add_task, statusfunc, upmeasure.get_rate, downmeasure.get_rate, upmeasure.get_total, downmeasure.get_total, ratemeasure.get_time_left, ratemeasure.get_size_left, file_length, finflag, config['display_interval'], spewflag) # useful info and functions for the UI if paramfunc: paramfunc({ 'max_upload_rate' : connecter.change_max_upload_rate, # change_max_upload_rate(<int bytes/sec>)
class T2TConnection: def __init__(self, myid, tracker, hash, interval, peers, timeout, rawserver, disallow, isdisallowed): self.tracker = tracker self.interval = interval self.hash = hash self.operatinginterval = interval self.peers = peers self.rawserver = rawserver self.disallow = disallow self.isdisallowed = isdisallowed self.active = True self.busy = False self.errors = 0 self.rejected = 0 self.trackererror = False self.peerlists = [] self.rerequester = Rerequester( [[tracker]], interval, rawserver.add_task, lambda: 0, peers, self.addtolist, rawserver.add_task, lambda: 1, 0, 0, 0, "", myid, hash, timeout, self.errorfunc, excfunc, peers, Event(), lambda: 0, lambda: 0, ) if self.isactive(): rawserver.add_task(self.refresh, randrange(int(self.interval / 10), self.interval)) # stagger announces def isactive(self): if self.isdisallowed(self.tracker): # whoops! self.deactivate() return self.active def deactivate(self): self.active = False def refresh(self): if not self.isactive(): return self.lastsuccessful = True self.newpeerdata = [] if DEBUG: print "contacting %s for info_hash=%s" % (self.tracker, quote(self.hash)) self.rerequester.snoop(self.peers, self.callback) def callback(self): self.busy = False if self.lastsuccessful: self.errors = 0 self.rejected = 0 if self.rerequester.announce_interval > (3 * self.interval): # I think I'm stripping from a regular tracker; boost the number of peers requested self.peers = int(self.peers * (self.rerequester.announce_interval / self.interval)) self.operatinginterval = self.rerequester.announce_interval if DEBUG: print ( "%s with info_hash=%s returned %d peers" % (self.tracker, quote(self.hash), len(self.newpeerdata)) ) self.peerlists.append(self.newpeerdata) self.peerlists = self.peerlists[-10:] # keep up to the last 10 announces if self.isactive(): self.rawserver.add_task(self.refresh, self.operatinginterval) def addtolist(self, peers): for peer in peers: self.newpeerdata.append((peer[1], peer[0][0], peer[0][1])) def errorfunc(self, r): self.lastsuccessful = False if DEBUG: print "%s with info_hash=%s gives error: '%s'" % (self.tracker, quote(self.hash), r) if r == self.rerequester.rejectedmessage + "disallowed": # whoops! if DEBUG: print " -- disallowed - deactivating" self.deactivate() self.disallow(self.tracker) # signal other torrents on this tracker return if r[:8].lower() == "rejected": # tracker rejected this particular torrent self.rejected += 1 if self.rejected == 3: # rejected 3 times if DEBUG: print " -- rejected 3 times - deactivating" self.deactivate() return self.errors += 1 if self.errors >= 3: # three or more errors in a row self.operatinginterval += self.interval # lengthen the interval if DEBUG: print " -- lengthening interval to " + str(self.operatinginterval) + " seconds" def harvest(self): x = [] for list in self.peerlists: x += list self.peerlists = [] return x
class T2TConnection: def __init__(self, myid, tracker, hash, interval, peers, timeout, rawserver, disallow, isdisallowed): self.tracker = tracker self.interval = interval self.hash = hash self.operatinginterval = interval self.peers = peers self.rawserver = rawserver self.disallow = disallow self.isdisallowed = isdisallowed self.active = True self.busy = False self.errors = 0 self.rejected = 0 self.trackererror = False self.peerlists = [] self.rerequester = Rerequester([[tracker]], interval, rawserver.add_task, lambda: 0, peers, self.addtolist, rawserver.external_add_task, lambda: 1, 0, 0, 0, '', myid, hash, timeout, self.errorfunc, excfunc, peers, Event(), lambda: 0, lambda: 0) if self.isactive(): rawserver.add_task(self.refresh, randrange(int(self.interval/10), self.interval)) # stagger announces def isactive(self): if self.isdisallowed(self.tracker): # whoops! self.deactivate() return self.active def deactivate(self): self.active = False def refresh(self): if not self.isactive(): return self.lastsuccessful = True self.newpeerdata = [] if DEBUG: print 'contacting %s for info_hash=%s' % (self.tracker, quote(self.hash)) self.rerequester.snoop(self.peers, self.callback) def callback(self): self.busy = False if self.lastsuccessful: self.errors = 0 self.rejected = 0 if self.rerequester.announce_interval > (3*self.interval): # I think I'm stripping from a regular tracker; boost the number of peers requested self.peers = int(self.peers * (self.rerequester.announce_interval / self.interval)) self.operatinginterval = self.rerequester.announce_interval if DEBUG: print ("%s with info_hash=%s returned %d peers" % (self.tracker, quote(self.hash), len(self.newpeerdata))) self.peerlists.append(self.newpeerdata) self.peerlists = self.peerlists[-10:] # keep up to the last 10 announces if self.isactive(): self.rawserver.add_task(self.refresh, self.operatinginterval) def addtolist(self, peers): for peer in peers: self.newpeerdata.append((peer[1],peer[0][0],peer[0][1])) def errorfunc(self, r): self.lastsuccessful = False if DEBUG: print "%s with info_hash=%s gives error: '%s'" % (self.tracker, quote(self.hash), r) if r == self.rerequester.rejectedmessage + 'disallowed': # whoops! if DEBUG: print ' -- disallowed - deactivating' self.deactivate() self.disallow(self.tracker) # signal other torrents on this tracker return if lower(r[:8]) == 'rejected': # tracker rejected this particular torrent self.rejected += 1 if self.rejected == 3: # rejected 3 times if DEBUG: print ' -- rejected 3 times - deactivating' self.deactivate() return self.errors += 1 if self.errors >= 3: # three or more errors in a row self.operatinginterval += self.interval # lengthen the interval if DEBUG: print ' -- lengthening interval to '+str(self.operatinginterval)+' seconds' def harvest(self): x = [] for list in self.peerlists: x += list self.peerlists = [] return x
class T2TConnection: def __init__(self, myid, tracker, hash, interval, peers, timeout, rawserver, disallow, isdisallowed): self.tracker = tracker self.interval = interval self.hash = hash self.operatinginterval = interval self.peers = peers self.rawserver = rawserver self.disallow = disallow self.isdisallowed = isdisallowed self.active = True self.busy = False self.errors = 0 self.rejected = 0 self.trackererror = False self.peerlists = [] self.rerequester = Rerequester([[tracker]], interval, rawserver.add_task, lambda: 0, peers, self.addtolist, rawserver.add_task, lambda: 1, 0, 0, 0, '', myid, hash, timeout, self.errorfunc, excfunc, peers, Event(), lambda: 0, lambda: 0) if self.isactive(): rawserver.add_task( self.refresh, randrange(int(self.interval / 10), self.interval)) def isactive(self): if self.isdisallowed(self.tracker): self.deactivate() return self.active def deactivate(self): self.active = False def refresh(self): if not self.isactive(): return self.lastsuccessful = True self.newpeerdata = [] if DEBUG: print 'contacting %s for info_hash=%s' % (self.tracker, quote(self.hash)) self.rerequester.snoop(self.peers, self.callback) def callback(self): self.busy = False if self.lastsuccessful: self.errors = 0 self.rejected = 0 if self.rerequester.announce_interval > 3 * self.interval: self.peers = int( self.peers * (self.rerequester.announce_interval / self.interval)) self.operatinginterval = self.rerequester.announce_interval if DEBUG: print '%s with info_hash=%s returned %d peers' % ( self.tracker, quote(self.hash), len(self.newpeerdata)) self.peerlists.append(self.newpeerdata) self.peerlists = self.peerlists[-10:] if self.isactive(): self.rawserver.add_task(self.refresh, self.operatinginterval) def addtolist(self, peers): for peer in peers: self.newpeerdata.append((peer[1], peer[0][0], peer[0][1])) def errorfunc(self, r): self.lastsuccessful = False if DEBUG: print "%s with info_hash=%s gives error: '%s'" % ( self.tracker, quote(self.hash), r) if r == self.rerequester.rejectedmessage + 'disallowed': if DEBUG: print ' -- disallowed - deactivating' self.deactivate() self.disallow(self.tracker) return if r[:8].lower() == 'rejected': self.rejected += 1 if self.rejected == 3: if DEBUG: print ' -- rejected 3 times - deactivating' self.deactivate() return self.errors += 1 if self.errors >= 3: self.operatinginterval += self.interval if DEBUG: print ' -- lengthening interval to ' + str( self.operatinginterval) + ' seconds' def harvest(self): x = [] for list in self.peerlists: x += list self.peerlists = [] return x
class T2TConnection: def __init__(self, myid, tracker, hash, interval, peers, timeout, rawserver, disallow, isdisallowed): self.tracker = tracker self.interval = interval self.hash = hash self.operatinginterval = interval self.peers = peers self.rawserver = rawserver self.disallow = disallow self.isdisallowed = isdisallowed self.active = True self.busy = False self.errors = 0 self.rejected = 0 self.trackererror = False self.peerlists = [] self.rerequester = Rerequester([[tracker]], interval, rawserver.add_task, lambda : 0, peers, self.addtolist, rawserver.add_task, lambda : 1, 0, 0, 0, '', myid, hash, timeout, self.errorfunc, excfunc, peers, Event(), lambda : 0, lambda : 0) if self.isactive(): rawserver.add_task(self.refresh, randrange(int(self.interval / 10), self.interval)) def isactive(self): if self.isdisallowed(self.tracker): self.deactivate() return self.active def deactivate(self): self.active = False def refresh(self): if not self.isactive(): return self.lastsuccessful = True self.newpeerdata = [] if DEBUG: print 'contacting %s for info_hash=%s' % (self.tracker, quote(self.hash)) self.rerequester.snoop(self.peers, self.callback) def callback(self): self.busy = False if self.lastsuccessful: self.errors = 0 self.rejected = 0 if self.rerequester.announce_interval > 3 * self.interval: self.peers = int(self.peers * (self.rerequester.announce_interval / self.interval)) self.operatinginterval = self.rerequester.announce_interval if DEBUG: print '%s with info_hash=%s returned %d peers' % (self.tracker, quote(self.hash), len(self.newpeerdata)) self.peerlists.append(self.newpeerdata) self.peerlists = self.peerlists[-10:] if self.isactive(): self.rawserver.add_task(self.refresh, self.operatinginterval) def addtolist(self, peers): for peer in peers: self.newpeerdata.append((peer[1], peer[0][0], peer[0][1])) def errorfunc(self, r): self.lastsuccessful = False if DEBUG: print "%s with info_hash=%s gives error: '%s'" % (self.tracker, quote(self.hash), r) if r == self.rerequester.rejectedmessage + 'disallowed': if DEBUG: print ' -- disallowed - deactivating' self.deactivate() self.disallow(self.tracker) return if r[:8].lower() == 'rejected': self.rejected += 1 if self.rejected == 3: if DEBUG: print ' -- rejected 3 times - deactivating' self.deactivate() return self.errors += 1 if self.errors >= 3: self.operatinginterval += self.interval if DEBUG: print ' -- lengthening interval to ' + str(self.operatinginterval) + ' seconds' def harvest(self): x = [] for list in self.peerlists: x += list self.peerlists = [] return x
class T2TConnection: def __init__(self, myid, tracker, hash, interval, peers, timeout, rawserver, disallow, isdisallowed): self.tracker = tracker self.interval = interval self.hash = hash self.operatinginterval = interval self.peers = peers self.rawserver = rawserver self.disallow = disallow self.isdisallowed = isdisallowed self.active = True self.busy = False self.errors = 0 self.rejected = 0 self.trackererror = False self.peerlists = [] cfg = {'min_peers': peers, 'max_initiate': peers, 'rerequest_interval': interval, 'http_timeout': timeout} self.rerequester = Rerequester( 0, myid, hash, [[tracker]], cfg, rawserver.add_task, rawserver.add_task, self.errorfunc, excfunc, self.addtolist, R_0, R_1, R_0, R_0, R_0, R_0, threading.Event()) if self.isactive(): rawserver.add_task(self.refresh, random.randrange(int(self.interval / 10), self.interval)) # stagger announces def isactive(self): if self.isdisallowed(self.tracker): # whoops! self.deactivate() return self.active def deactivate(self): self.active = False def refresh(self): if not self.isactive(): return self.lastsuccessful = True self.newpeerdata = [] if DEBUG: print 'contacting %s for info_hash=%s' % (self.tracker, quote(self.hash)) self.rerequester.snoop(self.peers, self.callback) def callback(self): self.busy = False if self.lastsuccessful: self.errors = 0 self.rejected = 0 if self.rerequester.announce_interval > (3 * self.interval): # I think I'm stripping from a regular tracker # boost the number of peers requested self.peers = int( self.peers * (self.rerequester.announce_interval / self.interval)) self.operatinginterval = self.rerequester.announce_interval if DEBUG: print "{} with info_hash={} returned {:d} peers".format( self.tracker, quote(self.hash), len(self.newpeerdata)) self.peerlists.append(self.newpeerdata) # keep up to the last 10 announces self.peerlists = self.peerlists[-10:] if self.isactive(): self.rawserver.add_task(self.refresh, self.operatinginterval) def addtolist(self, peers): for peer in peers: self.newpeerdata.append((peer[1], peer[0][0], peer[0][1])) def errorfunc(self, r): self.lastsuccessful = False if DEBUG: print "{} with info_hash={} gives error: '{}'".format( self.tracker, quote(self.hash), r) if r == self.rerequester.rejectedmessage + 'disallowed': # whoops! if DEBUG: print ' -- disallowed - deactivating' self.deactivate() # signal other torrents on this tracker self.disallow(self.tracker) return # tracker rejected this particular torrent if lower(r[:8]) == 'rejected': self.rejected += 1 if self.rejected == 3: # rejected 3 times if DEBUG: print ' -- rejected 3 times - deactivating' self.deactivate() return self.errors += 1 if self.errors >= 3: # three or more errors in a row self.operatinginterval += self.interval # lengthen the interval if DEBUG: print ' -- lengthening interval to {} seconds'.format( self.operatinginterval) def harvest(self): x = [] for list in self.peerlists: x += list self.peerlists = [] return x