-
Notifications
You must be signed in to change notification settings - Fork 87
/
rarest_piece.py
37 lines (26 loc) · 1.27 KB
/
rarest_piece.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import logging
__author__ = 'alexisgallepe'
class RarestPieces(object):
def __init__(self, pieces_manager):
self.pieces_manager = pieces_manager
self.rarest_pieces = []
for piece_number in range(self.pieces_manager.number_of_pieces):
self.rarest_pieces.append({"idPiece": piece_number, "numberOfPeers": 0, "peers": []})
# pub.subscribe(self.peersBitfield, 'RarestPiece.updatePeersBitfield')
def peers_bitfield(self, bitfield=None, peer=None, piece_index=None):
if len(self.rarest_pieces) == 0:
raise Exception("No more piece")
# Piece complete
try:
if not piece_index == None:
self.rarest_pieces.__delitem__(piece_index)
except Exception:
logging.exception("Failed to remove rarest piece")
# Peer's bitfield updated
else:
for i in range(len(self.rarest_pieces)):
if bitfield[i] == 1 and peer not in self.rarest_pieces[i]["peers"]:
self.rarest_pieces[i]["peers"].append(peer)
self.rarest_pieces[i]["numberOfPeers"] = len(self.rarest_pieces[i]["peers"])
def get_sorted_pieces(self):
return sorted(self.rarest_pieces, key=lambda x: x['numberOfPeers'])