~cosmin.lupu/+junk/penguintv

« back to all changes in this revision

Viewing changes to penguintv/ptvbittorrent/DownloaderFeedback.py

  • Committer: cosmin.lupu at gmail
  • Date: 2010-04-27 16:47:43 UTC
  • Revision ID: cosmin.lupu@gmail.com-20100427164743-ds8xrqonipp5ovdf
initial packaging

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Written by Bram Cohen
 
2
# see LICENSE.txt for license information
 
3
 
 
4
from time import time
 
5
 
 
6
class DownloaderFeedback:
 
7
    def __init__(self, choker, add_task, statusfunc, upfunc, downfunc, uptotal, downtotal,
 
8
            remainingfunc, leftfunc, file_length, finflag, interval, spewflag):
 
9
        self.choker = choker
 
10
        self.add_task = add_task
 
11
        self.statusfunc = statusfunc
 
12
        self.upfunc = upfunc
 
13
        self.downfunc = downfunc
 
14
        self.uptotal = uptotal
 
15
        self.downtotal = downtotal
 
16
        self.remainingfunc = remainingfunc
 
17
        self.leftfunc = leftfunc
 
18
        self.file_length = file_length
 
19
        self.finflag = finflag
 
20
        self.interval = interval
 
21
        self.spewflag = spewflag
 
22
        self.lastids = []
 
23
        self.display()
 
24
 
 
25
    def _rotate(self):
 
26
        cs = self.choker.connections
 
27
        for id in self.lastids:
 
28
            for i in xrange(len(cs)):
 
29
                if cs[i].get_id() == id:
 
30
                    return cs[i:] + cs[:i]
 
31
        return cs
 
32
 
 
33
    def collect_spew(self):
 
34
        l = [ ]
 
35
        cs = self._rotate()
 
36
        self.lastids = [c.get_id() for c in cs]
 
37
        for c in cs:
 
38
            rec = {}
 
39
            rec["ip"] = c.get_ip()
 
40
            if c is self.choker.connections[0]:
 
41
                rec["is_optimistic_unchoke"] = 1
 
42
            else:
 
43
                rec["is_optimistic_unchoke"] = 0
 
44
            if c.is_locally_initiated():
 
45
                rec["initiation"] = "local"
 
46
            else:
 
47
                rec["initiation"] = "remote"
 
48
            u = c.get_upload()
 
49
            rec["upload"] = (int(u.measure.get_rate()), u.is_interested(), u.is_choked())
 
50
 
 
51
            d = c.get_download()
 
52
            rec["download"] = (int(d.measure.get_rate()), d.is_interested(), d.is_choked(), d.is_snubbed())
 
53
            
 
54
            l.append(rec)
 
55
        return l
 
56
 
 
57
    def display(self):
 
58
        self.add_task(self.display, self.interval)
 
59
        spew = []
 
60
        if self.finflag.isSet():
 
61
            status = {"upRate" : self.upfunc(), "upTotal" : self.uptotal() / 1048576.0}
 
62
            if self.spewflag.isSet():
 
63
                status['spew'] = self.collect_spew()
 
64
            self.statusfunc(status)
 
65
            return
 
66
        timeEst = self.remainingfunc()
 
67
 
 
68
        if self.file_length > 0:
 
69
            fractionDone = (self.file_length - self.leftfunc()) / float(self.file_length)
 
70
        else:
 
71
            fractionDone = 1
 
72
        status = {
 
73
            "fractionDone" : fractionDone, 
 
74
            "downRate" : self.downfunc(), 
 
75
            "upRate" : self.upfunc(),
 
76
            "upTotal" : self.uptotal() / 1048576.0,
 
77
            "downTotal" : self.downtotal() / 1048576.0
 
78
            }
 
79
        if timeEst is not None:
 
80
            status['timeEst'] = timeEst
 
81
        if self.spewflag.isSet():
 
82
            status['spew'] = self.collect_spew()
 
83
        self.statusfunc(status)