home *** CD-ROM | disk | FTP | other *** search
/ Chip 2006 June / CHIP 2006-06.2.iso / program / freeware / Democracy-0.8.2.exe / xulrunner / python / BitTorrent / DownloaderFeedback.py < prev    next >
Encoding:
Python Source  |  2006-04-10  |  5.0 KB  |  137 lines

  1. # The contents of this file are subject to the BitTorrent Open Source License
  2. # Version 1.0 (the License).  You may not copy or use this file, in either
  3. # source code or executable form, except in compliance with the License.  You
  4. # may obtain a copy of the License at http://www.bittorrent.com/license/.
  5. #
  6. # Software distributed under the License is distributed on an AS IS basis,
  7. # WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the License
  8. # for the specific language governing rights and limitations under the
  9. # License.
  10.  
  11. # Written by Bram Cohen
  12.  
  13. from __future__ import division
  14.  
  15. from time import time
  16.  
  17.  
  18. class DownloaderFeedback(object):
  19.  
  20.     def __init__(self, choker, upfunc, upfunc2, downfunc, uptotal, downtotal,
  21.                  remainingfunc, leftfunc, file_length, finflag, downloader,
  22.                  files):
  23.         self.downloader = downloader
  24.         self.picker = downloader.picker
  25.         self.storage = downloader.storage
  26.         self.choker = choker
  27.         self.upfunc = upfunc
  28.         self.upfunc2 = upfunc2
  29.         self.downfunc = downfunc
  30.         self.uptotal = uptotal
  31.         self.downtotal = downtotal
  32.         self.remainingfunc = remainingfunc
  33.         self.leftfunc = leftfunc
  34.         self.file_length = file_length
  35.         self.finflag = finflag
  36.         self.files = files
  37.         self.lastids = []
  38.  
  39.     def _rotate(self):
  40.         cs = self.choker.connections
  41.         for peerid in self.lastids:
  42.             for i in xrange(len(cs)):
  43.                 if cs[i].id == peerid:
  44.                     return cs[i:] + cs[:i]
  45.         return cs
  46.  
  47.     def collect_spew(self):
  48.         l = [ ]
  49.         cs = self._rotate()
  50.         self.lastids = [c.id for c in cs]
  51.         for c in cs:
  52.             rec = {}
  53.             rec['id'] = c.id
  54.             rec["ip"] = c.ip
  55.             rec["is_optimistic_unchoke"] = (c is self.choker.connections[0])
  56.             if c.locally_initiated:
  57.                 rec["initiation"] = "L"
  58.             else:
  59.                 rec["initiation"] = "R"
  60.             u = c.upload
  61.             rec["upload"] = (u.measure.get_total(), int(u.measure.get_rate()),
  62.                              u.interested, u.choked)
  63.  
  64.             d = c.download
  65.             rec["download"] = (d.measure.get_total(),int(d.measure.get_rate()),
  66.                                d.interested, d.choked, d.is_snubbed())
  67.             rec['completed'] = 1 - d.have.numfalse / len(d.have)
  68.             rec['speed'] = d.connection.download.peermeasure.get_rate()
  69.             l.append(rec)
  70.         return l
  71.  
  72.     def get_statistics(self, spewflag = False, fileflag = False):
  73.         status = {}
  74.         numSeeds = 0
  75.         numPeers = 0
  76.         for d in self.downloader.downloads:
  77.             if d.have.numfalse == 0:
  78.                 numSeeds += 1
  79.             else:
  80.                 numPeers += 1
  81.         status['numSeeds'] = numSeeds
  82.         status['numPeers'] = numPeers
  83.         status['upRate'] = self.upfunc()
  84.         status['upRate2'] = self.upfunc2()
  85.         status['upTotal'] = self.uptotal()
  86.         missingPieces = 0
  87.         numCopyList = []
  88.         numCopies = 0
  89.         for i in self.picker.crosscount:
  90.             missingPieces += i
  91.             if missingPieces == 0:
  92.                 numCopies += 1
  93.             else:
  94.                 fraction = 1 - missingPieces / self.picker.numpieces
  95.                 numCopyList.append(fraction)
  96.                 if fraction == 0 or len(numCopyList) >= 3:
  97.                     break
  98.         numCopies -= numSeeds
  99.         if self.picker.numgot == self.picker.numpieces:
  100.             numCopies -= 1
  101.         status['numCopies'] = numCopies
  102.         status['numCopyList'] = numCopyList
  103.         status['discarded'] = self.downloader.discarded_bytes
  104.         status['storage_numcomplete'] = self.storage.stat_numfound + \
  105.                                         self.storage.stat_numdownloaded
  106.         status['storage_dirty'] = len(self.storage.stat_dirty)
  107.         status['storage_active'] = len(self.storage.stat_active)
  108.         status['storage_new'] = len(self.storage.stat_new)
  109.         status['storage_numflunked'] = self.storage.stat_numflunked
  110.  
  111.         if spewflag:
  112.             status['spew'] = self.collect_spew()
  113.             status['bad_peers'] = self.downloader.bad_peers
  114.         if fileflag:
  115.             undl = self.storage.storage.undownloaded
  116.             unal = self.storage.storage.unallocated
  117.             status['files_left'] = [undl[fname] for fname in self.files]
  118.             status['files_allocated'] = [not unal[fn] for fn in self.files]
  119.         if self.finflag.isSet():
  120.             status['downRate'] = 0
  121.             status['downTotal'] = self.downtotal()
  122.             status['fractionDone'] = 1
  123.             return status
  124.         timeEst = self.remainingfunc()
  125.         status['timeEst'] = timeEst
  126.  
  127.         if self.file_length > 0:
  128.             fractionDone = 1 - self.leftfunc() / self.file_length
  129.         else:
  130.             fractionDone = 1
  131.         status.update({
  132.             "fractionDone" : fractionDone,
  133.             "downRate" : self.downfunc(),
  134.             "downTotal" : self.downtotal()
  135.             })
  136.         return status
  137.