(file) Return to RateMeasure.py CVS log (file) (dir) Up to [Development] / shadowsclient / BitTorrent

File: [Development] / shadowsclient / BitTorrent / RateMeasure.py (download) / (as text)
Revision: 1.1, Tue Sep 23 21:31:07 2003 UTC (6 years, 11 months ago) by theshadow
Branch: MAIN
CVS Tags: HEAD
BitTorrent 3.2.1b

# Written by Bram Cohen
# see LICENSE.txt for license information

from time import time
true = 1
false = 0

class RateMeasure:
    def __init__(self, left):
        self.start = None
        self.last = None
        self.rate = 0
        self.remaining = None
        self.left = left
        self.broke = false
        self.got_anything = false

    def data_came_in(self, amount):
        if not self.got_anything:
            self.got_anything = true
            self.start = time() - 2
            self.last = self.start
            self.left -= amount
            return
        self.update(time(), amount)

    def data_rejected(self, amount):
        self.left += amount

    def get_time_left(self):
        if not self.got_anything:
            return None
        t = time()
        if t - self.last > 15:
            self.update(t, 0)
        return self.remaining

    def get_size_left(self):
        return self.left

    def update(self, t, amount):
        self.left -= amount
        try:
            self.rate = ((self.rate * (self.last - self.start)) + amount) / (t - self.start)
            self.last = t
            self.remaining = self.left / self.rate
            if self.start < self.last - self.remaining:
                self.start = self.last - self.remaining
        except ZeroDivisionError:
            self.remaining = None
        if self.broke and self.last - self.start < 20:
            self.start = self.last - 20
        if self.last - self.start > 20:
            self.broke = true

No CVS admin address has been configured
Powered by
ViewCVS 0.9.3