+class FileDownload(defer.Deferred):
+ """Manage a download from a list of peers or a mirror.
+
+
+ """
+
+ def __init__(self, manager, hash, mirror, compact_peers):
+ """Initialize the instance.
+
+ @type hash: L{Hash.HashObject}
+ @param hash: the hash object containing the expected hash for the file
+ @param mirror: the URI of the file on the mirror
+ @type compact_peers: C{list} of C{string}
+ @param compact_peers: a list of the peer info where the file can be found
+ """
+ defer.Deferred.__init__(self)
+ self.manager = manager
+ self.hash = hash
+ self.mirror = mirror
+
+ self.peers = {}
+ no_pieces = 0
+ pieces_string = {}
+ pieces_hash = {}
+ pieces_dl_hash = {}
+
+ for compact_peer in compact_peers:
+ # Build a list of all the peers for this download
+ site = uncompact(compact_peer['c'])
+ peer = manager.getPeer(site)
+ self.peers[site] = peer
+
+ # Extract any piece information from the peers list
+ if 't' in compact_peer:
+ pieces_string.setdefault(compact_peer['t']['t'], 0)
+ pieces_string[compact_peer['t']['t']] += 1
+ elif 'h' in compact_peer:
+ pieces_hash.setdefault(compact_peer['h'], 0)
+ pieces_hash[compact_peer['h']] += 1
+ elif 'l' in compact_peer:
+ pieces_dl_hash.setdefault(compact_peer['l'], 0)
+ pieces_dl_hash[compact_peer['l']] += 1
+ else:
+ no_pieces += 1
+
+ max_found = max(no_pieces, max(pieces_string.values()),
+ max(pieces_hash.values()), max(pieces_dl_hash.values()))
+
+ if max_found == no_pieces:
+ self.sort()
+ pieces = []
+ if max_found < len(self.peers):
+ pass
+ elif max_found == max(pieces_string.values()):
+ pass
+
+ def sort(self):
+ def sort(a, b):
+ """Sort peers by their rank."""
+ if a.rank > b.rank:
+ return 1
+ elif a.rank < b.rank:
+ return -1
+ return 0
+ self.peers.sort(sort)
+