本文整理汇总了Python中BitTorrent.CurrentRateMeasure.Measure类的典型用法代码示例。如果您正苦于以下问题:Python Measure类的具体用法?Python Measure怎么用?Python Measure使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Measure类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
class KRateLimiter:
# special rate limiter that drops entries that have been sitting in the queue for longer than self.age seconds
# by default we toss anything that has less than 5 seconds to live
def __init__(self, transport, rate, call_later, rlcount, rate_period, age=(KRPC_TIMEOUT - 5)):
self.q = []
self.transport = transport
self.rate = rate
self.curr = 0
self.running = False
self.age = age
self.last = 0
self.call_later = call_later
self.rlcount = rlcount
self.measure = Measure(rate_period)
self.sent=self.dropped=0
if self.rate == 0:
self.rate = 1e10
def sendto(self, s, i, addr):
self.q.append((time(), (s, i, addr)))
if not self.running:
self.run(check=True)
def run(self, check=False):
t = time()
self.expire(t)
self.curr -= (t - self.last) * self.rate
self.last = t
if check:
self.curr = max(self.curr, 0 - self.rate)
shuffle(self.q)
while self.q and self.curr <= 0:
x, tup = self.q.pop()
size = len(tup[0])
self.curr += size
try:
self.transport.sendto(*tup)
self.sent+=1
self.rlcount(size)
self.measure.update_rate(size)
except:
if tup[2][1] != 0:
print ">>> sendto exception", tup
print_exc()
self.q.sort()
if self.q or self.curr > 0:
self.running = True
# sleep for at least a half second
self.call_later(max(self.curr / self.rate, 0.5), self.run)
else:
self.running = False
def expire(self, t=time()):
if self.q:
expire_time = t - self.age
while self.q and self.q[0][0] < expire_time:
self.q.pop(0)
self.dropped+=1
示例2: __init__
def __init__(self, downloader, connection):
self.downloader = downloader
self.connection = connection
self.choked = True
self.interested = False
self.active_requests = []
self.measure = Measure(downloader.config['max_rate_period'])
self.peermeasure = Measure(max(downloader.storage.piece_size / 10000,
20))
self.have = Bitfield(downloader.numpieces)
self.last = 0
self.example_interest = None
self.backlog = 2
self.guard = BadDataGuard(self)
示例3: __init__
def __init__(self, multidownload, connection):
self.multidownload = multidownload
self.connection = connection
self.choked = True
self.interested = False
self.prefer_full = False
self.active_requests = set()
self.measure = Measure(multidownload.config['max_rate_period'])
self.peermeasure = Measure(
max(multidownload.storage.piece_size / 10000, 20))
self.have = Bitfield(multidownload.numpieces)
self.last = 0
self.example_interest = None
self.guard = BadDataGuard(self)
self.suggested_pieces = []
self.allowed_fast_pieces = []
示例4: __init__
def __init__(self, downloader, connection):
self.downloader = downloader
self.connection = connection
self.choked = True
self.interested = False
self.active_requests = []
self.measure = Measure(downloader.max_rate_period)
self.peermeasure = Measure(downloader.max_rate_period)
self.have = Bitfield(downloader.numpieces)
self.last = -1000
self.last2 = -1000
self.example_interest = None
# self.backlog = 2
self.backlog = 8
self.ip = connection.get_ip()
self.guard = BadDataGuard(self)
示例5: __init__
def __init__(self, connection, ratelimiter, totalup, totalup2, choker,
storage, max_slice_length, max_rate_period, logcollector):
self.connection = connection
self.ratelimiter = ratelimiter
self.totalup = totalup
self.totalup2 = totalup2
self.choker = choker
self.storage = storage
self.max_slice_length = max_slice_length
self.max_rate_period = max_rate_period
self.choked = True
self.unchoke_time = None
self.interested = False
#the list buffer contains tuples (index, begin, lenght) for each
#block requested by the remote peer. A non empty buffer means that
#there is data to send to the remote peer already requested by the
#remote peer.
self.buffer = []
#PFS begin
self.config = choker.config
self.I = {} # I[piece id] = block uploaded count in the piece id
self.r = {} # r[piece_id] = block requested count in the piece id
#PFS end
self.measure = Measure(max_rate_period)
#send the bittfield of the peer the first time it connects to the peers.
if storage.do_I_have_anything():
connection.send_bitfield(storage.get_have_list())
self.logcollector = logcollector
示例6: __init__
def __init__(self, connection, ratelimiter, totalup,choker,
storage, max_slice_length, max_rate_period, num_fast,
torrent):
assert isinstance(connection, BitTorrent.Connector.Connection)
assert isinstance(torrent, BitTorrent.Torrent.Torrent)
self.connection = connection
self.ratelimiter = ratelimiter
self.totalup = totalup
self.torrent = torrent
self.choker = choker
self.num_fast = num_fast
self.storage = storage
self.max_slice_length = max_slice_length
self.max_rate_period = max_rate_period
self.choked = True
self.unchoke_time = None
self.interested = False
self.buffer = [] # contains piece data about to be sent.
self.measure = Measure(max_rate_period)
self.allowed_fast_pieces = []
if connection.uses_fast_extension:
if storage.get_amount_left() == 0:
connection.send_have_all()
elif storage.do_I_have_anything():
connection.send_bitfield(storage.get_have_list())
else:
connection.send_have_none()
self._send_allowed_fast_list()
elif storage.do_I_have_anything():
connection.send_bitfield(storage.get_have_list())
示例7: __init__
def __init__(self, connection, ratelimiter, totalup, choker, storage,
picker, config):
self.connection = connection
self.ratelimiter = ratelimiter
self.totalup = totalup
self.choker = choker
self.storage = storage
self.picker = picker
self.config = config
self.max_slice_length = config['max_slice_length']
self.choked = True
self.cleared = True
self.interested = False
self.super_seeding = False
self.buffer = []
self.measure = Measure(config['max_rate_period'], config['upload_rate_fudge'])
self.was_ever_interested = False
if storage.get_amount_left() == 0:
if choker.super_seed:
self.super_seeding = True # flag, and don't send bitfield
self.seed_have_list = [] # set from piecepicker
self.skipped_count = 0
else:
if config['breakup_seed_bitfield']:
bitfield, msgs = storage.get_have_list_cloaked()
connection.send_bitfield(bitfield)
for have in msgs:
connection.send_have(have)
else:
connection.send_bitfield(storage.get_have_list())
else:
if storage.do_I_have_anything():
connection.send_bitfield(storage.get_have_list())
self.piecedl = None
self.piecebuf = None
示例8: __init__
def __init__(self, multidownload, connector, ratelimiter, choker, storage,
max_chunk_length, max_rate_period, num_fast, infohash):
assert isinstance(connector, BitTorrent.Connector.Connector)
self.multidownload = multidownload
self.connector = connector
self.ratelimiter = ratelimiter
self.infohash = infohash
self.choker = choker
self.num_fast = num_fast
self.storage = storage
self.max_chunk_length = max_chunk_length
self.choked = True
self.unchoke_time = None
self.interested = False
self.had_length_error = False
self.had_max_requests_error = False
self.buffer = [] # contains piece data about to be sent.
self.measure = Measure(max_rate_period)
connector.add_sent_listener(self.measure.update_rate)
self.allowed_fast_pieces = []
if connector.uses_fast_extension:
if storage.get_amount_left() == 0:
connector.send_have_all()
elif storage.do_I_have_anything():
connector.send_bitfield(storage.get_have_list())
else:
connector.send_have_none()
self._send_allowed_fast_list()
elif storage.do_I_have_anything():
connector.send_bitfield(storage.get_have_list())
示例9: __init__
def __init__(self, multidownload, connection):
self.multidownload = multidownload
self.connection = connection
self.choked = True
self.interested = False
self.prefer_full = False
self.active_requests = set()
self.intro_size = self.multidownload.chunksize * 4 # just a guess
self.measure = Measure(multidownload.config['max_rate_period'])
self.peermeasure = Measure(
max(multidownload.storage.piece_size / 10000, 20))
self.have = Bitfield(multidownload.numpieces)
self.last = 0
self.example_interest = None
self.guard = BadDataGuard(self)
self.suggested_pieces = []
self.allowed_fast_pieces = []
self._received_listeners = set()
self.add_useful_received_listener(self.measure.update_rate)
self.total_bytes = 0
def lambda_sucks(x): self.total_bytes += x
self.add_useful_received_listener(lambda_sucks)
示例10: __init__
def __init__(self, connection, ratelimiter, totalup, totalup2, choker,
storage, max_slice_length, max_rate_period):
self.connection = connection
self.ratelimiter = ratelimiter
self.totalup = totalup
self.totalup2 = totalup2
self.choker = choker
self.storage = storage
self.max_slice_length = max_slice_length
self.max_rate_period = max_rate_period
self.choked = True
self.unchoke_time = None
self.interested = False
self.buffer = []
self.measure = Measure(max_rate_period)
if storage.do_I_have_anything():
connection.send_bitfield(storage.get_have_list())
示例11: Upload
class Upload(object):
def __init__(self, connection, ratelimiter, totalup, totalup2, choker,
storage, max_slice_length, max_rate_period, logcollector):
self.connection = connection
self.ratelimiter = ratelimiter
self.totalup = totalup
self.totalup2 = totalup2
self.choker = choker
self.storage = storage
self.max_slice_length = max_slice_length
self.max_rate_period = max_rate_period
self.choked = True
self.unchoke_time = None
self.interested = False
#the list buffer contains tuples (index, begin, lenght) for each
#block requested by the remote peer. A non empty buffer means that
#there is data to send to the remote peer already requested by the
#remote peer.
self.buffer = []
#PFS begin
self.config = choker.config
self.I = {} # I[piece id] = block uploaded count in the piece id
self.r = {} # r[piece_id] = block requested count in the piece id
#PFS end
self.measure = Measure(max_rate_period)
#send the bittfield of the peer the first time it connects to the peers.
if storage.do_I_have_anything():
connection.send_bitfield(storage.get_have_list())
self.logcollector = logcollector
def got_not_interested(self):
if self.interested:
self.logcollector.log(None, 'R NI ' + str(self.connection.ip))
self.interested = False
del self.buffer[:]
self.choker.not_interested(self.connection)
def got_interested(self):
if not self.interested:
self.logcollector.log(None, 'R I ' + str(self.connection.ip))
self.interested = True
self.choker.interested(self.connection)
def get_upload_chunk(self):
if not self.buffer:
return None
#buffer.pop(0) return the element with index 0 and remove
#this element from buffer.
index, begin, length = self.buffer.pop(0)
#PFS begin
if self.choker.done():
if index in self.I:
self.I[index] += 1
else:
self.I[index] = 1
if index in self.choker.I:
self.choker.I[index] += 1
else:
self.choker.I[index] = 1
self.logcollector.log(None, 'PFS ' + str(self.connection.ip) + \
' theta(' + str(index) + ') ' + str(self.choker.I[index]))
if index not in self.choker.theta:
self.choker.theta[index] = 1.0
#PFS end
piece = self.storage.get_piece(index, begin, length)
if piece is None:
self.logcollector.log(None, 'CON C ' + str(self.connection.ip) + ' E 1')
self.connection.close()
return None
self.measure.update_rate(len(piece))
self.totalup.update_rate(len(piece))
self.totalup2.update_rate(len(piece))
return (index, begin, piece)
def got_request(self, index, begin, length):
if not self.interested or length > self.max_slice_length:
self.logcollector.log(None, 'CON C ' + str(self.connection.ip) + ' E 2')
self.connection.close()
return
self.logcollector.log(None, 'R R ' + str(self.connection.ip) + ' i ' + str(index) + ' b ' + str(begin) + \
' l ' + str(length))
if not self.connection.choke_sent:
self.buffer.append((index, begin, length))
if self.connection.next_upload is None and \
self.connection.connection.is_flushed():
self.ratelimiter.queue(self.connection)
# EPFS begin
if self.choker.done():
# update vector of requests {r1,...}
self.PFS_update_r(index)
# EPFS end
# EPFS step 5: Seed updates his data structure when receiving REQUEST from leechers
def PFS_update_r(self, index):
if self.config['scheduling_algorithm'] == 'BT':
#.........这里部分代码省略.........
示例12: Download
class Download(object):
"""Implements BitTorrent protocol semantics for downloading over a single
connection. See Upload for the protocol semantics in the upload
direction. See Connector.Connection for the protocol syntax
implementation."""
def __init__(self, multidownload, connection):
self.multidownload = multidownload
self.connection = connection
self.choked = True
self.interested = False
self.prefer_full = False
self.active_requests = set()
self.measure = Measure(multidownload.config['max_rate_period'])
self.peermeasure = Measure(
max(multidownload.storage.piece_size / 10000, 20))
self.have = Bitfield(multidownload.numpieces)
self.last = 0
self.example_interest = None
self.guard = BadDataGuard(self)
self.suggested_pieces = []
self.allowed_fast_pieces = []
def _backlog(self):
backlog = 2 + int(4 * self.measure.get_rate() /
self.multidownload.chunksize)
if backlog > 50:
backlog = max(50, int(.075 * backlog))
return backlog
def disconnected(self):
self.multidownload.lost_peer(self)
if self.have.numfalse == 0:
self.multidownload.lost_have_all()
else:
# arg, slow
count = 0
target = len(self.have) - self.have.numfalse
for i in xrange(len(self.have)):
if count == target:
break
if self.have[i]:
self.multidownload.lost_have(i)
count += 1
self._letgo()
self.guard.download = None
def _letgo(self):
if not self.active_requests:
return
if self.multidownload.storage.endgame:
self.active_requests.clear()
return
lost = []
for index, begin, length in self.active_requests:
self.multidownload.storage.request_lost(index, begin, length)
self.multidownload.active_requests.remove(index)
if index not in lost:
lost.append(index)
self.active_requests.clear()
ds = [d for d in self.multidownload.downloads if not d.choked]
random.shuffle(ds)
for d in ds:
d._request_more(lost)
for d in self.multidownload.downloads:
if d.choked and not d.interested:
for l in lost:
if d.have[l] and \
self.multidownload.storage.want_requests(l):
d.interested = True
d.connection.send_interested()
break
def got_choke(self):
if not self.choked:
self.choked = True
if not self.connection.uses_fast_extension:
self._letgo()
def got_unchoke(self):
if self.choked:
self.choked = False
if self.interested:
self._request_more()
def got_piece(self, index, begin, piece):
req = (index, begin, len(piece))
if req not in self.active_requests:
self.multidownload.discarded_bytes += len(piece)
if self.connection.uses_fast_extension:
self.connection.close()
return
self.active_requests.remove(req)
if self.multidownload.storage.endgame:
if req not in self.multidownload.all_requests:
self.multidownload.discarded_bytes += len(piece)
return
#.........这里部分代码省略.........
示例13: SingleDownload
class SingleDownload(object):
def __init__(self, downloader, connection):
self.downloader = downloader
self.connection = connection
self.choked = True
self.interested = False
self.active_requests = []
self.measure = Measure(downloader.config['max_rate_period'])
self.peermeasure = Measure(max(downloader.storage.piece_size / 10000,
20))
self.have = Bitfield(downloader.numpieces)
self.last = 0
self.example_interest = None
self.backlog = 2
self.guard = BadDataGuard(self)
def _backlog(self):
backlog = 2 + int(4 * self.measure.get_rate() /
self.downloader.chunksize)
if backlog > 50:
backlog = max(50, int(.075 * backlog))
self.backlog = backlog
return backlog
def disconnected(self):
self.downloader.lost_peer(self)
for i in xrange(len(self.have)):
if self.have[i]:
self.downloader.picker.lost_have(i)
self._letgo()
self.guard.download = None
def _letgo(self):
if not self.active_requests:
return
if self.downloader.storage.endgame:
self.active_requests = []
return
lost = []
for index, begin, length in self.active_requests:
self.downloader.storage.request_lost(index, begin, length)
if index not in lost:
lost.append(index)
self.active_requests = []
ds = [d for d in self.downloader.downloads if not d.choked]
shuffle(ds)
for d in ds:
d._request_more(lost)
for d in self.downloader.downloads:
if d.choked and not d.interested:
for l in lost:
if d.have[l] and self.downloader.storage.do_I_have_requests(l):
d.interested = True
d.connection.send_interested()
break
def got_choke(self):
if not self.choked:
self.choked = True
self._letgo()
def got_unchoke(self):
if self.choked:
self.choked = False
if self.interested:
self._request_more()
def got_piece(self, index, begin, piece):
try:
self.active_requests.remove((index, begin, len(piece)))
except ValueError:
self.downloader.discarded_bytes += len(piece)
return False
if self.downloader.storage.endgame:
self.downloader.all_requests.remove((index, begin, len(piece)))
self.last = bttime()
self.measure.update_rate(len(piece))
self.downloader.measurefunc(len(piece))
self.downloader.downmeasure.update_rate(len(piece))
if not self.downloader.storage.piece_came_in(index, begin, piece,
self.guard):
if self.downloader.storage.endgame:
while self.downloader.storage.do_I_have_requests(index):
nb, nl = self.downloader.storage.new_request(index)
self.downloader.all_requests.append((index, nb, nl))
for d in self.downloader.downloads:
d.fix_download_endgame()
return False
ds = [d for d in self.downloader.downloads if not d.choked]
shuffle(ds)
for d in ds:
d._request_more([index])
return False
if self.downloader.storage.do_I_have(index):
self.downloader.picker.complete(index)
if self.downloader.storage.endgame:
for d in self.downloader.downloads:
if d is not self and d.interested:
if d.choked:
#.........这里部分代码省略.........
示例14: Download
class Download(object):
"""Implements BitTorrent protocol semantics for downloading over a single
connection. See Upload for the protocol semantics in the upload
direction. See Connector for the protocol syntax implementation."""
def __init__(self, multidownload, connector):
self.multidownload = multidownload
self.connector = connector
self.choked = True
self.interested = False
self.prefer_full = False
self.active_requests = set()
self.expecting_reject = set()
self.intro_size = self.multidownload.chunksize * 4 # just a guess
self.measure = Measure(multidownload.config['max_rate_period'])
self.peermeasure = Measure(
max(multidownload.storage.piece_size / 10000, 20))
self.have = Bitfield(multidownload.numpieces)
self.last = 0
self.example_interest = None
self.guard = BadDataGuard(self)
self.suggested_pieces = []
self.allowed_fast_pieces = []
self._useful_received_listeners = set()
self._raw_received_listeners = set()
self.add_useful_received_listener(self.measure.update_rate)
self.total_bytes = 0
self.add_useful_received_listener(self.accumulate_total)
def accumulate_total(self, x):
self.total_bytes += x
def add_useful_received_listener(self, listener):
# "useful received bytes are used in measuring goodput.
self._useful_received_listeners.add(listener)
def remove_useful_received_listener(self, listener):
self._useful_received_listeners.remove(listener)
def fire_useful_received_listeners(self, bytes):
for f in self._useful_received_listeners:
f(bytes)
def add_raw_received_listener(self, listener):
self._raw_received_listeners.add(listener)
def remove_raw_received_listener(self, listener):
self._raw_received_listeners.remove(listener)
def fire_raw_received_listeners(self, bytes):
for f in self._raw_received_listeners:
f(bytes)
def _backlog(self):
# Dave's suggestion:
# backlog = 2 + thruput delay product in chunks.
# Assume one-way download propagation delay is always less than 200ms.
# backlog = 2 + int(0.2 * self.measure.get_rate() /
# self.multidownload.chunksize
# Then eliminate the cap of 50 and the 0.075*backlog.
backlog = 2 + int(4 * self.measure.get_rate() /
self.multidownload.chunksize)
if self.total_bytes < self.intro_size:
# optimistic backlog to get things started
backlog = max(10, backlog)
if backlog > 50:
backlog = max(50, int(.075 * backlog))
if self.multidownload.rm.endgame:
# OPTIONAL: zero pipelining during endgame
#b = 1
pass
return backlog
def disconnected(self):
self.multidownload.lost_peer(self)
if self.have.numfalse == 0:
self.multidownload.lost_have_all()
else:
# arg, slow
count = 0
target = len(self.have) - self.have.numfalse
for i in xrange(len(self.have)):
if count == target:
break
if self.have[i]:
self.multidownload.lost_have(i)
count += 1
self._letgo()
self.guard.download = None
def _letgo(self):
if not self.active_requests:
return
if self.multidownload.rm.endgame:
self.active_requests.clear()
return
#.........这里部分代码省略.........
示例15: Upload
class Upload(object):
"""Upload over a single connection."""
def __init__(self, connection, ratelimiter, totalup,choker,
storage, max_slice_length, max_rate_period, num_fast,
torrent):
assert isinstance(connection, BitTorrent.Connector.Connection)
assert isinstance(torrent, BitTorrent.Torrent.Torrent)
self.connection = connection
self.ratelimiter = ratelimiter
self.totalup = totalup
self.torrent = torrent
self.choker = choker
self.num_fast = num_fast
self.storage = storage
self.max_slice_length = max_slice_length
self.max_rate_period = max_rate_period
self.choked = True
self.unchoke_time = None
self.interested = False
self.buffer = [] # contains piece data about to be sent.
self.measure = Measure(max_rate_period)
self.allowed_fast_pieces = []
if connection.uses_fast_extension:
if storage.get_amount_left() == 0:
connection.send_have_all()
elif storage.do_I_have_anything():
connection.send_bitfield(storage.get_have_list())
else:
connection.send_have_none()
self._send_allowed_fast_list()
elif storage.do_I_have_anything():
connection.send_bitfield(storage.get_have_list())
def _send_allowed_fast_list(self):
"""Computes and sends the 'allowed fast' set. """
self.allowed_fast_pieces = _compute_allowed_fast_list(
self.torrent.infohash,
self.connection.ip, self.num_fast,
self.storage.get_num_pieces())
for index in self.allowed_fast_pieces:
self.connection.send_allowed_fast(index)
def got_not_interested(self):
if self.interested:
self.interested = False
self.choker.not_interested(self.connection)
def got_interested(self):
if not self.interested:
self.interested = True
self.choker.interested(self.connection)
def get_upload_chunk(self, index, begin, length):
df = self.storage.read(index, begin, length)
def fail(e):
log( "get_upload_chunk failed", exc_info=e )
self.connection.close()
return None
def update_rate(piece): # piece is actual data.
if piece is None:
return fail("Piece is None")
return (index, begin, piece)
df.addCallback(update_rate)
df.addErrback(fail)
return df
def update_rate(self, bytes):
self.measure.update_rate(bytes)
self.totalup.update_rate(bytes)
def got_request(self, index, begin, length):
if not self.interested or length > self.max_slice_length:
self.connection.close()
return
if index in self.allowed_fast_pieces or not self.connection.choke_sent:
df = self.get_upload_chunk(index, begin, length)
def got_piece(piece): # 3rd elem in tuple is piece data.
if self.connection.closed or piece is None:
return
index, begin, piece = piece # piece changes from tuple to data.
if self.choked:
if not self.connection.uses_fast_extension:
return
if index not in self.allowed_fast_pieces:
self.connection.send_reject_request(
index, begin, len(piece))
return
self.buffer.append(((index, begin, len(piece)), piece))
if self.connection.next_upload is None and \
self.connection.connection.is_flushed():
self.ratelimiter.queue(self.connection)
df.addCallback(got_piece)
elif self.connection.uses_fast_extension:
self.connection.send_reject_request( index, begin, length )
#.........这里部分代码省略.........