DownloaderFeedback.py :  » RSS » PenguinTV » PenguinTV-4.1.0 » penguintv » ptvbittorrent » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » RSS » PenguinTV 
PenguinTV » PenguinTV 4.1.0 » penguintv » ptvbittorrent » DownloaderFeedback.py
# Written by Bram Cohen
# see LICENSE.txt for license information

from time import time

class DownloaderFeedback:
    def __init__(self, choker, add_task, statusfunc, upfunc, downfunc, uptotal, downtotal,
            remainingfunc, leftfunc, file_length, finflag, interval, spewflag):
        self.choker = choker
        self.add_task = add_task
        self.statusfunc = statusfunc
        self.upfunc = upfunc
        self.downfunc = downfunc
        self.uptotal = uptotal
        self.downtotal = downtotal
        self.remainingfunc = remainingfunc
        self.leftfunc = leftfunc
        self.file_length = file_length
        self.finflag = finflag
        self.interval = interval
        self.spewflag = spewflag
        self.lastids = []
        self.display()

    def _rotate(self):
        cs = self.choker.connections
        for id in self.lastids:
            for i in xrange(len(cs)):
                if cs[i].get_id() == id:
                    return cs[i:] + cs[:i]
        return cs

    def collect_spew(self):
        l = [ ]
        cs = self._rotate()
        self.lastids = [c.get_id() for c in cs]
        for c in cs:
            rec = {}
            rec["ip"] = c.get_ip()
            if c is self.choker.connections[0]:
                rec["is_optimistic_unchoke"] = 1
            else:
                rec["is_optimistic_unchoke"] = 0
            if c.is_locally_initiated():
                rec["initiation"] = "local"
            else:
                rec["initiation"] = "remote"
            u = c.get_upload()
            rec["upload"] = (int(u.measure.get_rate()), u.is_interested(), u.is_choked())

            d = c.get_download()
            rec["download"] = (int(d.measure.get_rate()), d.is_interested(), d.is_choked(), d.is_snubbed())
            
            l.append(rec)
        return l

    def display(self):
        self.add_task(self.display, self.interval)
        spew = []
        if self.finflag.isSet():
            status = {"upRate" : self.upfunc(), "upTotal" : self.uptotal() / 1048576.0}
            if self.spewflag.isSet():
                status['spew'] = self.collect_spew()
            self.statusfunc(status)
            return
        timeEst = self.remainingfunc()

        if self.file_length > 0:
            fractionDone = (self.file_length - self.leftfunc()) / float(self.file_length)
        else:
            fractionDone = 1
        status = {
            "fractionDone" : fractionDone, 
            "downRate" : self.downfunc(), 
            "upRate" : self.upfunc(),
            "upTotal" : self.uptotal() / 1048576.0,
            "downTotal" : self.downtotal() / 1048576.0
            }
        if timeEst is not None:
            status['timeEst'] = timeEst
        if self.spewflag.isSet():
            status['spew'] = self.collect_spew()
        self.statusfunc(status)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.