java.lang.Object
org.klomp.snark.Piece
- All Implemented Interfaces:
Comparable<Piece>
Represents a torrent piece and tracks which peers have it and are requesting it.
This class is used internally by PeerCoordinator to manage piece selection and distribution strategies. It maintains:
- The piece identifier within the torrent
- Set of peers that have this piece available
- Set of peers currently requesting this piece
- Priority level for piece selection
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.
Field Summary
FieldsModifier and TypeFieldDescription(package private) longTime of the last request or received chunk, for stall detection.private final intPiece number within the torrent.Peers this piece was requested from.private intDownload priority for this piece.Peers currently requested from for this piece.Constructor Summary
ConstructorsMethod Summary
Public MethodsModifier and TypeMethodDescriptionbooleanAdds a peer to the set of peers that have this piece.voidclear()Clear all knowledge of peers Caller must synchronize.intCompares by highest priority first, then by rarest first.booleanintgetId()Returns the piece index within the torrent.longReturns the time of the last request or received chunk for this piece.intHow many peers have this piece? Caller must synchronize.intThe current priority level for this piece.intHow many peers are requesting this piece? Caller must synchronize.Returns a snapshot of the peers currently requesting this piece.inthashCode()booleanChecks if this piece is disabled (should not be downloaded).booleanChecks if any peer is currently requesting this piece.booleanisRequestedBy(Peer peer) Checks if the given peer is currently requesting this piece.booleanremovePeer(Peer peer) Removes a peer from those that have this piece.voidRecords that this piece has just received a chunk, for stall detection.voidDisables this piece so it will not be downloaded.voidsetPriority(int p) The priority level for this piece.(package private) voidsetRequested(PeerID pid, boolean requested) Marks a peer as requesting or no longer requesting this piece, stamping the last-activity time when a request is made.voidsetRequested(Peer peer, boolean requested) Marks a peer as requesting or no longer requesting this piece.toString()
Field Details
id
private final int idPiece number within the torrent.peers
requests
priority
private int priorityDownload priority for this piece._lastActive
volatile long _lastActiveTime of the last request or received chunk, for stall detection.
Constructor Details
Piece
public Piece(int id) Creates a new Piece with the given ID.- Parameters:
id- the piece index within the torrent
Method Details
compareTo
Compares by highest priority first, then by rarest first.- Specified by:
compareToin interfaceComparable<Piece>- Parameters:
op- the other Piece to compare to- Returns:
- negative if this piece has higher priority, positive if lower
equals
hashCode
getId
public int getId()Returns the piece index within the torrent.- Returns:
- the piece ID
addPeer
Adds a peer to the set of peers that have this piece. Caller must synchronize.- Parameters:
peer- the peer to credit with holding this piece, stored by peer ID- Returns:
- true if the peer was added, false if already present
removePeer
Removes a peer from those that have this piece. Caller must synchronize.- Parameters:
peer- the peer to drop, matched against the stored peer ID- Returns:
- true if removed
getPeerCount
public int getPeerCount()How many peers have this piece? Caller must synchronize.- Returns:
- number of peers that have this piece
isRequested
public boolean isRequested()Checks if any peer is currently requesting this piece. Caller must synchronize.- Returns:
- true if at least one peer is requesting this piece
setRequested
Marks a peer as requesting or no longer requesting this piece. Used to avoid deadlocks when querying each peer. Caller must synchronize.- Parameters:
peer- the peer whose request state is being set, stored by peer IDrequested- true to mark as requested, false to unmark
setRequested
Marks a peer as requesting or no longer requesting this piece, stamping the last-activity time when a request is made. Used to avoid deadlocks when querying each peer. Caller must synchronize.- Parameters:
pid- the peer IDrequested- true to mark as requested, false to unmark
setActive
public void setActive()Records that this piece has just received a chunk, for stall detection.getLastActive
public long getLastActive()Returns the time of the last request or received chunk for this piece.- Returns:
- the time in milliseconds
getRequesters
isRequestedBy
Checks if the given peer is currently requesting this piece. Caller must synchronize.- Parameters:
peer- the peer to test for, matched against the stored peer ID- Returns:
- true if peer is requesting, false otherwise
getRequestCount
public int getRequestCount()How many peers are requesting this piece? Caller must synchronize.- Returns:
- number of peers requesting this piece
clear
public void clear()Clear all knowledge of peers Caller must synchronize.getPriority
public int getPriority()The current priority level for this piece.- Returns:
- priority value (negative values indicate disabled/skipped pieces)
setPriority
public void setPriority(int p) The priority level for this piece.- Parameters:
p- priority value (negative to disable, 0 or positive for normal priority)
isDisabled
public boolean isDisabled()Checks if this piece is disabled (should not be downloaded).- Returns:
- true if disabled (priority < 0), false otherwise
setDisabled
public void setDisabled()Disables this piece so it will not be downloaded. Sets priority to -1.toString