def __init__(self, host, port=80):
self.host = host
self.port = port
+ self.mirror = False
self.rank = 0.5
self.busy = False
self.pipeline = False
self._responseTimes = []
def __repr__(self):
- return "(%s, %d, %0.5f)" % (self.host, self.port, self.rank)
+ return "(%r, %r, %r)" % (self.host, self.port, self.rank)
#{ Manage the request queue
def connect(self):
from util import uncompact
from Hash import PIECE_SIZE
from apt_p2p_Khashmir.bencode import bdecode
+from apt_p2p_conf import config
class GrowingFileStream(stream.FileStream):
"""Modified to stream data from a file as it becomes available.
self.compact_peers = compact_peers
self.path = '/~/' + quote_plus(hash.expected())
+ self.mirror_path = None
self.pieces = None
self.started = False
assert self.pieces, "You must initialize the piece hashes first"
self.peerlist = [self.peers[site]['peer'] for site in self.peers]
+ # Use the mirror if there are few peers
+ if len(self.peerlist) < config.getint('DEFAULT', 'MIN_DOWNLOAD_PEERS'):
+ parsed = urlparse(self.mirror)
+ if parsed[0] == "http":
+ site = splitHostPort(parsed[0], parsed[1])
+ self.mirror_path = urlunparse(('', '') + parsed[2:])
+ peer = self.manager.getPeer(site)
+ peer.mirror = True
+ self.peerlist.append(peer)
+
# Special case if there's only one good peer left
# if len(self.peerlist) == 1:
# log.msg('Downloading from peer %r' % (self.peerlist[0], ))
log.msg('Sending a request for piece %d to peer %r' % (piece, peer))
self.outstanding += 1
- df = peer.getRange(self.path, piece*PIECE_SIZE, (piece+1)*PIECE_SIZE - 1)
+ if peer.mirror:
+ df = peer.getRange(self.mirror_path, piece*PIECE_SIZE, (piece+1)*PIECE_SIZE - 1)
+ else:
+ df = peer.getRange(self.path, piece*PIECE_SIZE, (piece+1)*PIECE_SIZE - 1)
reactor.callLater(0, df.addCallbacks,
*(self._getPiece, self._getError),
**{'callbackArgs': (piece, peer),
site = splitHostPort(parsed[0], parsed[1])
path = urlunparse(('', '') + parsed[2:])
peer = self.getPeer(site)
+ peer.mirror = True
return peer.get(path, method, modtime)
# elif len(peers) == 1:
# site = uncompact(peers[0]['c'])
# Set this to 0 to not limit the upload bandwidth.
'UPLOAD_LIMIT': '0',
+ # The minimum number of peers before the mirror is not used.
+ # If there are fewer peers than this for a file, the mirror will also be
+ # used to speed up the download. Set to 0 to never use the mirror if
+ # there are peers.
+ 'MIN_DOWNLOAD_PEERS': '3',
+
# Directory to store the downloaded files in
'CACHE_DIR': home + '/.apt-p2p/cache',