class Piece extends Object implements Comparable<Piece>
This class is used internally by PeerCoordinator to manage piece selection and distribution strategies. It maintains:
Pieces are sorted by priority (highest first) and then by rarity (fewest peers having it first) to optimize download performance.
Thread Safety: This class is not thread-safe. Callers must synchronize on access to most methods.
| Constructor and Description |
|---|
Piece(int id) |
| Modifier and Type | Method and Description |
|---|---|
boolean |
addPeer(Peer peer)
caller must synchronize
|
void |
clear()
Clear all knowledge of peers Caller must synchronize
|
int |
compareTo(Piece op)
Highest priority first, then rarest first
|
boolean |
equals(Object o) |
int |
getId() |
int |
getPeerCount()
How many peers have this piece? Caller must synchronize
|
int |
getPriority()
Gets the current priority level for this piece.
|
int |
getRequestCount()
How many peers are requesting this piece? Caller must synchronize
|
int |
hashCode() |
boolean |
isDisabled()
Checks if this piece is disabled (should not be downloaded).
|
boolean |
isRequested()
caller must synchronize
|
boolean |
isRequestedBy(Peer peer)
Checks if the given peer is currently requesting this piece.
|
boolean |
removePeer(Peer peer)
Caller must synchronize.
|
void |
setDisabled()
Disables this piece so it will not be downloaded.
|
void |
setPriority(int p)
Sets the priority level for this piece.
|
void |
setRequested(Peer peer,
boolean requested)
Marks a peer as requesting or no longer requesting this piece.
|
String |
toString() |
public boolean addPeer(Peer peer)
public void clear()
public int compareTo(Piece op)
compareTo in interface Comparable<Piece>public int getId()
public int getPeerCount()
public int getPriority()
public int getRequestCount()
public boolean isDisabled()
public boolean isRequested()
public boolean isRequestedBy(Peer peer)
peer - the peer to checkpublic boolean removePeer(Peer peer)
public void setDisabled()
public void setPriority(int p)
p - priority value (negative to disable, 0 or positive for normal priority)public void setRequested(Peer peer, boolean requested)
peer - the peer requestingrequested - true to mark as requested, false to unmark