本文整理汇总了Python中BitTorrent.CurrentRateMeasure.Measure.get_rate方法的典型用法代码示例。如果您正苦于以下问题:Python Measure.get_rate方法的具体用法?Python Measure.get_rate怎么用?Python Measure.get_rate使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BitTorrent.CurrentRateMeasure.Measure
的用法示例。
在下文中一共展示了Measure.get_rate方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Upload
# 需要导入模块: from BitTorrent.CurrentRateMeasure import Measure [as 别名]
# 或者: from BitTorrent.CurrentRateMeasure.Measure import get_rate [as 别名]
#.........这里部分代码省略.........
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 )
def got_cancel(self, index, begin, length):
req = (index, begin, length)
for pos, (r, p) in enumerate(self.buffer):
if r == req:
del self.buffer[pos]
if self.connection.uses_fast_extension:
self.connection.send_reject_request(*req)
break
def choke(self):
if not self.choked:
self.choked = True
self.connection.send_choke()
def sent_choke(self):
assert self.choked
if self.connection.uses_fast_extension:
b2 = []
for r in self.buffer:
((index,begin,length),piecedata) = r
if index not in self.allowed_fast_pieces:
self.connection.send_reject_request( index, begin, length )
else:
b2.append(r)
self.buffer = b2
else:
del self.buffer[:]
def unchoke(self, time):
if self.choked:
self.choked = False
self.unchoke_time = time
self.connection.send_unchoke()
def has_queries(self):
return len(self.buffer) > 0
def get_rate(self):
return self.measure.get_rate()
示例2: SingleDownload
# 需要导入模块: from BitTorrent.CurrentRateMeasure import Measure [as 别名]
# 或者: from BitTorrent.CurrentRateMeasure.Measure import get_rate [as 别名]
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:
#.........这里部分代码省略.........
示例3: Upload
# 需要导入模块: from BitTorrent.CurrentRateMeasure import Measure [as 别名]
# 或者: from BitTorrent.CurrentRateMeasure.Measure import get_rate [as 别名]
#.........这里部分代码省略.........
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':
return False
if self.choker.tm_first_req == 0:
self.choker.tm_first_req = bttime()
if index in self.r:
self.r[index] += 1
else:
self.r[index] = 1
if index in self.choker.r:
self.choker.r[index] += 1.0
else:
self.choker.r[index] = 1.0
self.logcollector.log(None, 'PFS ' + str(self.connection.ip) + \
' r[' + str(index) + '] ' + str(self.choker.r[index]))
return True
# EPFS end
def got_cancel(self, index, begin, length):
try:
self.buffer.remove((index, begin, length))
except ValueError:
pass
def choke(self):
if not self.choked:
self.choked = True
self.connection.send_choke()
def sent_choke(self):
assert self.choked
del self.buffer[:]
def unchoke(self, time):
if self.choked:
self.choked = False
self.unchoke_time = time
self.connection.send_unchoke()
def has_queries(self):
return len(self.buffer) > 0
def get_rate(self):
return self.measure.get_rate()
示例4: Upload
# 需要导入模块: from BitTorrent.CurrentRateMeasure import Measure [as 别名]
# 或者: from BitTorrent.CurrentRateMeasure.Measure import get_rate [as 别名]
class Upload(object):
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())
def got_not_interested(self):
if self.interested:
self.interested = False
del self.buffer[:]
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):
if not self.buffer:
return None
index, begin, length = self.buffer.pop(0)
piece = self.storage.get_piece(index, begin, length)
if piece is None:
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.connection.close()
return
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)
def got_cancel(self, index, begin, length):
try:
self.buffer.remove((index, begin, length))
except ValueError:
pass
def choke(self):
if not self.choked:
self.choked = True
self.connection.send_choke()
def sent_choke(self):
assert self.choked
del self.buffer[:]
def unchoke(self, time):
if self.choked:
self.choked = False
self.unchoke_time = time
self.connection.send_unchoke()
def has_queries(self):
return len(self.buffer) > 0
def get_rate(self):
return self.measure.get_rate()
示例5: Download
# 需要导入模块: from BitTorrent.CurrentRateMeasure import Measure [as 别名]
# 或者: from BitTorrent.CurrentRateMeasure.Measure import get_rate [as 别名]
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
#.........这里部分代码省略.........
示例6: SingleDownload
# 需要导入模块: from BitTorrent.CurrentRateMeasure import Measure [as 别名]
# 或者: from BitTorrent.CurrentRateMeasure.Measure import get_rate [as 别名]
class SingleDownload(object):
def __init__(self, downloader, connection, logcollector):
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))
#intialize a bitfield of lenght 'numpieces'
self.have = Bitfield(downloader.numpieces)
self.last = 0
self.example_interest = None
self.backlog = 2
self.guard = BadDataGuard(self)
self.logcollector=logcollector
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.logcollector.log(None, 'R C ' + str(self.connection.ip))
self.choked = True
self._letgo()
def got_unchoke(self):
if self.choked:
self.logcollector.log(None, 'R UC ' + str(self.connection.ip))
self.choked = False
if self.interested:
self._request_more()
#this method returns True if the block received completes a piece,
#false, otherwise. The result of this method is used to decide when
#to send the HAVE message.
def got_piece(self, index, begin, piece):
try:
#the received block was not requested if it is not in active_requests.
#It is discarded
self.active_requests.remove((index, begin, len(piece)))
except ValueError:
self.downloader.discarded_bytes += len(piece)
return False
#count all the received packet that are requested.
self.logcollector.log(None, 'R P ' + str(self.connection.ip) + ' i ' + str(index) + ' b ' + str(begin))
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
#.........这里部分代码省略.........
示例7: Upload
# 需要导入模块: from BitTorrent.CurrentRateMeasure import Measure [as 别名]
# 或者: from BitTorrent.CurrentRateMeasure.Measure import get_rate [as 别名]
#.........这里部分代码省略.........
if not self.interested:
self.interested = True
self.choker.interested(self.connector)
def get_upload_chunk(self, index, begin, length):
df = self.storage.read(index, begin, length)
df.addCallback(lambda piece: (index, begin, piece))
df.addErrback(self._failed_get_upload_chunk)
return df
def _failed_get_upload_chunk(self, f):
log("get_upload_chunk failed", exc_info=f.exc_info())
self.connector.close()
return f
def got_request(self, index, begin, length):
if not self.interested:
self.connector.protocol_violation("request when not interested")
self.connector.close()
return
if length > self.max_chunk_length:
if not self.had_length_error:
m = ("request length %r exceeds max %r" %
(length, self.max_chunk_length))
self.connector.protocol_violation(m)
self.had_length_error = True
#self.connector.close()
# we could still download...
if self.connector.uses_fast_extension:
self.connector.send_reject_request(index, begin, length)
return
if len(self.buffer) > MAX_REQUESTS:
if not self.had_max_requests_error:
m = ("max request limit %d" % MAX_REQUESTS)
self.connector.protocol_violation(m)
self.had_max_requests_error = True
if self.connector.uses_fast_extension:
self.connector.send_reject_request(index, begin, length)
return
if index in self.allowed_fast_pieces or not self.connector.choke_sent:
df = self.get_upload_chunk(index, begin, length)
df.addCallback(self._got_piece)
df.addErrback(self.multidownload.errorfunc)
elif self.connector.uses_fast_extension:
self.connector.send_reject_request(index, begin, length)
def _got_piece(self, piece_info):
index, begin, piece = piece_info
if self.connector.closed:
return
if self.choked:
if not self.connector.uses_fast_extension:
return
if index not in self.allowed_fast_pieces:
self.connector.send_reject_request(index, begin, len(piece))
return
self.buffer.append(((index, begin, len(piece)), piece))
if self.connector.next_upload is None and \
self.connector.connection.is_flushed():
self.ratelimiter.queue(self.connector)
def got_cancel(self, index, begin, length):
req = (index, begin, length)
for pos, (r, p) in enumerate(self.buffer):
if r == req:
del self.buffer[pos]
if self.connector.uses_fast_extension:
self.connector.send_reject_request(*req)
break
def choke(self):
if not self.choked:
self.choked = True
self.connector.send_choke()
def sent_choke(self):
assert self.choked
if self.connector.uses_fast_extension:
b2 = []
for r in self.buffer:
((index,begin,length),piecedata) = r
if index not in self.allowed_fast_pieces:
self.connector.send_reject_request(index, begin, length)
else:
b2.append(r)
self.buffer = b2
else:
del self.buffer[:]
def unchoke(self, time):
if self.choked:
self.choked = False
self.unchoke_time = time
self.connector.send_unchoke()
def has_queries(self):
return len(self.buffer) > 0
def get_rate(self):
return self.measure.get_rate()
示例8: __init__
# 需要导入模块: from BitTorrent.CurrentRateMeasure import Measure [as 别名]
# 或者: from BitTorrent.CurrentRateMeasure.Measure import get_rate [as 别名]
class DownloadPeer:
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)
def _backlog(self, just_unchoked):
# self.backlog = min(
# 2+int(4*self.measure.get_rate()/self.downloader.chunksize),
# (2*just_unchoked)+self.downloader.queue_limit() )
# if self.backlog > 50:
# self.backlog = max(50, self.backlog * 0.075)
# return self.backlog
self.backlog = 4+int(8*self.measure.get_rate()/self.downloader.chunksize)
return self.backlog
def disconnected(self):
self.downloader.lost_peer(self)
if self.have.complete():
self.downloader.picker.lost_seed()
else:
for i in xrange(len(self.have)):
if self.have[i]:
self.downloader.picker.lost_have(i)
if self.have.complete() and self.downloader.storage.is_endgame():
self.downloader.add_disconnected_seed(self.connection.get_readable_id())
self._letgo()
self.guard.download = None
def _letgo(self):
if self.downloader.queued_out.has_key(self):
del self.downloader.queued_out[self]
if not self.active_requests:
return
if self.downloader.endgamemode:
self.active_requests = []
return
lost = {}
for index, begin, length in self.active_requests:
self.downloader.storage.request_lost(index, begin, length)
lost[index] = 1
lost = lost.keys()
self.active_requests = []
if self.downloader.paused:
return
ds = [d for d in self.downloader.downloads if not d.choked]
shuffle(ds)
for d in ds:
d._request_more()
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.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(new_unchoke = True)
self.last2 = clock()
def is_choked(self):
return self.choked
def is_interested(self):
return self.interested
def send_interested(self):
if not self.interested:
self.interested = True
self.connection.send_interested()
if not self.choked:
self.last2 = clock()
def send_not_interested(self):
if self.interested:
self.interested = False
self.connection.send_not_interested()
def got_piece(self, index, begin, piece):
length = len(piece)
#.........这里部分代码省略.........
示例9: Download
# 需要导入模块: from BitTorrent.CurrentRateMeasure import Measure [as 别名]
# 或者: from BitTorrent.CurrentRateMeasure.Measure import get_rate [as 别名]
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
#.........这里部分代码省略.........
示例10: __init__
# 需要导入模块: from BitTorrent.CurrentRateMeasure import Measure [as 别名]
# 或者: from BitTorrent.CurrentRateMeasure.Measure import get_rate [as 别名]
#.........这里部分代码省略.........
self.piecedl = None
self.piecebuf = None
def got_not_interested(self):
if self.interested:
self.interested = False
del self.buffer[:]
self.piecedl = None
if self.piecebuf:
self.piecebuf.release()
self.piecebuf = None
self.choker.not_interested(self.connection)
def got_interested(self):
if not self.interested:
self.interested = True
self.was_ever_interested = True
self.choker.interested(self.connection)
def get_upload_chunk(self):
if self.choked or not self.buffer:
return None
index, begin, length = self.buffer.pop(0)
if self.config['buffer_reads']:
if index != self.piecedl:
if self.piecebuf:
self.piecebuf.release()
self.piecedl = index
self.piecebuf = self.storage.get_piece(index, 0, -1)
piece = None
if self.piecebuf:
piece = self.piecebuf[begin:begin+length]
# fails if storage.get_piece returns None or if out of range
if not piece or len(piece) != length:
self.connection.close()
return None
else:
if self.piecebuf:
self.piecebuf.release()
self.piecedl = None
piece = self.storage.get_piece(index, begin, length)
if piece is None:
self.connection.close()
return None
self.measure.update_rate(len(piece))
self.totalup.update_rate(len(piece))
return (index, begin, piece)
def got_request(self, index, begin, length):
if ( (self.super_seeding and not index in self.seed_have_list)
or not self.interested or length > self.max_slice_length ):
self.connection.close()
return
if not self.cleared:
self.buffer.append((index, begin, length))
if not self.choked and self.connection.next_upload is None:
self.ratelimiter.queue(self.connection)
def got_cancel(self, index, begin, length):
try:
self.buffer.remove((index, begin, length))
except ValueError:
pass
def choke(self):
if not self.choked:
self.choked = True
self.connection.send_choke()
self.piecedl = None
if self.piecebuf:
self.piecebuf.release()
self.piecebuf = None
def choke_sent(self):
del self.buffer[:]
self.cleared = True
def unchoke(self):
if self.choked:
self.choked = False
self.cleared = False
self.connection.send_unchoke()
def disconnected(self):
if self.piecebuf:
self.piecebuf.release()
self.piecebuf = None
def is_choked(self):
return self.choked
def is_interested(self):
return self.interested
def has_queries(self):
return not self.choked and len(self.buffer) > 0
def get_rate(self):
return self.measure.get_rate()