本文整理汇总了Python中BitTorrent.CurrentRateMeasure.Measure.update_rate方法的典型用法代码示例。如果您正苦于以下问题:Python Measure.update_rate方法的具体用法?Python Measure.update_rate怎么用?Python Measure.update_rate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BitTorrent.CurrentRateMeasure.Measure
的用法示例。
在下文中一共展示了Measure.update_rate方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from BitTorrent.CurrentRateMeasure import Measure [as 别名]
# 或者: from BitTorrent.CurrentRateMeasure.Measure import update_rate [as 别名]
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: Upload
# 需要导入模块: from BitTorrent.CurrentRateMeasure import Measure [as 别名]
# 或者: from BitTorrent.CurrentRateMeasure.Measure import update_rate [as 别名]
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 )
#.........这里部分代码省略.........
示例3: Upload
# 需要导入模块: from BitTorrent.CurrentRateMeasure import Measure [as 别名]
# 或者: from BitTorrent.CurrentRateMeasure.Measure import update_rate [as 别名]
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':
#.........这里部分代码省略.........
示例4: SingleDownload
# 需要导入模块: from BitTorrent.CurrentRateMeasure import Measure [as 别名]
# 或者: from BitTorrent.CurrentRateMeasure.Measure import update_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:
#.........这里部分代码省略.........
示例5: Download
# 需要导入模块: from BitTorrent.CurrentRateMeasure import Measure [as 别名]
# 或者: from BitTorrent.CurrentRateMeasure.Measure import update_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: Upload
# 需要导入模块: from BitTorrent.CurrentRateMeasure import Measure [as 别名]
# 或者: from BitTorrent.CurrentRateMeasure.Measure import update_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()
示例7: SingleDownload
# 需要导入模块: from BitTorrent.CurrentRateMeasure import Measure [as 别名]
# 或者: from BitTorrent.CurrentRateMeasure.Measure import update_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
#.........这里部分代码省略.........
示例8: __init__
# 需要导入模块: from BitTorrent.CurrentRateMeasure import Measure [as 别名]
# 或者: from BitTorrent.CurrentRateMeasure.Measure import update_rate [as 别名]
#.........这里部分代码省略.........
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)
try:
self.active_requests.remove((index, begin, length))
except ValueError:
self.downloader.discarded += length
return False
if self.downloader.endgamemode:
self.downloader.all_requests.remove((index, begin, length))
self.last = clock()
self.last2 = clock()
self.measure.update_rate(length)
self.downloader.measurefunc(length)
if not self.downloader.storage.piece_came_in(index, begin, piece, self.guard):
self.downloader.piece_flunked(index)
return False
if self.downloader.storage.do_I_have(index):
self.downloader.picker.complete(index)
if self.downloader.endgamemode:
for d in self.downloader.downloads:
if d is not self:
if d.interested:
if d.choked:
assert not d.active_requests
d.fix_download_endgame()
else:
try:
d.active_requests.remove((index, begin, length))
except ValueError:
continue
d.connection.send_cancel(index, begin, length)
d.fix_download_endgame()
else:
assert not d.active_requests
self._request_more()
self.downloader.check_complete(index)
return self.downloader.storage.do_I_have(index)
def _request_more(self, new_unchoke = False):
assert not self.choked
if self.downloader.endgamemode:
self.fix_download_endgame(new_unchoke)
return
if self.downloader.paused:
示例9: Download
# 需要导入模块: from BitTorrent.CurrentRateMeasure import Measure [as 别名]
# 或者: from BitTorrent.CurrentRateMeasure.Measure import update_rate [as 别名]
#.........这里部分代码省略.........
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
self.multidownload.all_requests.remove(req)
for d in self.multidownload.downloads:
if d.interested:
if not d.choked:
if req in d.active_requests:
d.connection.send_cancel(*req)
if not self.connection.uses_fast_extension:
d.active_requests.remove(req)
d.fix_download_endgame()
else:
self._request_more()
self.last = bttime()
self.update_rate(len(piece))
df = self.multidownload.storage.write(index, begin, piece, self.guard)
df.addCallback(self._got_piece, index)
def _got_piece(self, hashchecked, index):
if hashchecked:
self.multidownload.hashchecked(index)
def _want(self, index):
return self.have[index] and \
self.multidownload.storage.want_requests(index)
def _request_more(self, indices = []):
if self.choked:
self._request_when_choked()
return
#log( "_request_more.active_requests=%s" % self.active_requests )
b = self._backlog()
if len(self.active_requests) >= b:
return
if self.multidownload.storage.endgame:
self.fix_download_endgame()
return
self.suggested_pieces = [i for i in self.suggested_pieces
if not self.multidownload.storage.do_I_have(i)]
lost_interests = []
while len(self.active_requests) < b:
if not indices:
interest = self.multidownload.picker.next(self.have,
self.multidownload.active_requests,
self.multidownload.storage.full_pieces,
示例10: __init__
# 需要导入模块: from BitTorrent.CurrentRateMeasure import Measure [as 别名]
# 或者: from BitTorrent.CurrentRateMeasure.Measure import update_rate [as 别名]
class UploadPeer:
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
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):
#.........这里部分代码省略.........