Class Peer
- java.lang.Object
-
- org.johnnei.javatorrent.torrent.peer.Peer
-
public class Peer extends Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classPeer.Builder
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description booleanaddBlockRequest(Piece piece, int byteOffset, int blockLength, PeerDirection type)Adds a download or upload job to the peer.voidaddModuleInfo(Object infoObject)Registers theIModulepeer specific information based on the classvoidaddStrike(int i)Adds an amount of strikes to the peervoidcancelBlockRequest(Piece piece, int byteOffset, int blockLength, PeerDirection type)Removes the download or upload job from the peer.voidcheckDisconnect()intcountHavePieces()Gets the amount of pieces this peer hasvoiddiscardAllBlockRequests()Cancels all piecesbooleanequals(Object o)BitTorrentSocketgetBitTorrentSocket()Deprecated.Will be replaced by alternatives as this class is handling a non-extensible process.StringgetClientName()Gets the client name as reported by the BEP #10 extension if supported.intgetFreeWorkTime()Calculates the amount of blocks we can request without overflowing the peer and without slowing us down.StringgetIdAsString()LocalDateTimegetLastActivity()Gets the time at which the last byte has been read or written to the socket.<T> Optional<T>getModuleInfo(Class<T> infoClass)Gets the info object registered withaddModuleInfo(Object)intgetRequestLimit()Gets the amount of requests we are allowed to make to this peerTorrentgetTorrent()Gets the torrent to which this peer is linked.intgetWorkQueueSize(PeerDirection direction)Gets the amount of pieces the client still needs to sendbooleanhasExtension(int index, int bit)Checks if theextensionByteshas the given bit set for the extension which is part of the extension bytes in the handshakeinthashCode()booleanhasPiece(int pieceIndex)Checks if the peer has the piece with the given indexbooleanisChoked(PeerDirection direction)Checks if the client is choked.booleanisInterested(PeerDirection direction)Checks if the client is interested.voidonReceivedBlock(Piece piece, int byteOffset)Indicates that we've received the requested block from the peer.voidonTorrentPhaseChange()A callback method which gets invoked when the torrent starts a new phasevoidqueueNextPieceForSending()Requests to queue the next piece in the socket for sendingvoidsetAbsoluteRequestLimit(int absoluteRequestLimit)Sets the amount of requests this peer can support at mostvoidsetChoked(PeerDirection direction, boolean choked)Sets if this peer is choked or not.voidsetClientName(String clientName)Sets the client as reported by the Client in BEP #10.voidsetHavingPiece(int pieceIndex)Registers that this peer has the given piecevoidsetInterested(PeerDirection direction, boolean interested)Sets if this peer is interested or not.voidsetRequestLimit(int requestLimit)Sets the amount of requests we think this peer can handle properly.
This amount will be limited byabsoluteRequestLimitStringtoString()
-
-
-
Method Detail
-
addModuleInfo
public void addModuleInfo(Object infoObject)
Registers theIModulepeer specific information based on the class- Parameters:
infoObject- The object to add- Throws:
IllegalStateException- when the class is already registered.- Since:
- 0.5
-
getModuleInfo
public <T> Optional<T> getModuleInfo(Class<T> infoClass)
Gets the info object registered withaddModuleInfo(Object)- Parameters:
infoClass- The type of information which is stored- Returns:
- The module info or
Optional.empty()when not present. - Since:
- 0.5
-
hasExtension
public boolean hasExtension(int index, int bit)Checks if theextensionByteshas the given bit set for the extension which is part of the extension bytes in the handshake- Parameters:
index- The index wihtin the extension bytes.bit- The bit to test.- Returns:
- returns true if the extension bit is set. Otherwise false
-
checkDisconnect
public void checkDisconnect()
-
addBlockRequest
public boolean addBlockRequest(Piece piece, int byteOffset, int blockLength, PeerDirection type)
Adds a download or upload job to the peer. In case of a download request this will also send out aMessageBlockfor the given block.- Parameters:
piece- The requested piece.byteOffset- The offset in bytes within the piece.blockLength- The amount of bytes requested.type- The direction of the request.- Returns:
truewhen the block has been requested, otherwisefalse
-
cancelBlockRequest
public void cancelBlockRequest(Piece piece, int byteOffset, int blockLength, PeerDirection type)
Removes the download or upload job from the peer. In case of a download request this will also send out aMessageCancelfor the given block.- Parameters:
piece- The piece to cancel.byteOffset- The offset in bytes within the piece.blockLength- The amount of bytes requested.type- The direction of the request.
-
onReceivedBlock
public void onReceivedBlock(Piece piece, int byteOffset)
Indicates that we've received the requested block from the peer.- Parameters:
piece- The requested piece.byteOffset- The offset in bytes within the piece.
-
setClientName
public void setClientName(String clientName)
Sets the client as reported by the Client in BEP #10.- Parameters:
clientName- The name of the client.
-
getClientName
public String getClientName()
Gets the client name as reported by the BEP #10 extension if supported. Otherwise the name will be an extraction from the Peer ID.- Returns:
- The name of the client
-
onTorrentPhaseChange
public void onTorrentPhaseChange()
A callback method which gets invoked when the torrent starts a new phase
-
getFreeWorkTime
public int getFreeWorkTime()
Calculates the amount of blocks we can request without overflowing the peer and without slowing us down.- Returns:
- The amount of blocks which can still be requested.
-
getWorkQueueSize
public int getWorkQueueSize(PeerDirection direction)
Gets the amount of pieces the client still needs to send- Returns:
- The amount of blocks which still need to be send/received.
-
discardAllBlockRequests
public void discardAllBlockRequests()
Cancels all pieces
-
setHavingPiece
public void setHavingPiece(int pieceIndex)
Registers that this peer has the given piece- Parameters:
pieceIndex- the piece to marked as "have"
-
hasPiece
public boolean hasPiece(int pieceIndex)
Checks if the peer has the piece with the given index- Parameters:
pieceIndex- the piece to check for- Returns:
- returns true when the peer has the piece otherwise false
-
getLastActivity
public LocalDateTime getLastActivity()
Gets the time at which the last byte has been read or written to the socket.- Returns:
- The most recent activity time
-
getTorrent
public Torrent getTorrent()
Gets the torrent to which this peer is linked.- Returns:
- The torrent.
-
addStrike
public void addStrike(int i)
Adds an amount of strikes to the peer- Parameters:
i- The amount of strikes to add.
-
setAbsoluteRequestLimit
public void setAbsoluteRequestLimit(int absoluteRequestLimit)
Sets the amount of requests this peer can support at most- Parameters:
absoluteRequestLimit- The absolute maximum amount of outstanding requests the peer supports.
-
setRequestLimit
public void setRequestLimit(int requestLimit)
Sets the amount of requests we think this peer can handle properly.
This amount will be limited byabsoluteRequestLimit- Parameters:
requestLimit- The new limit.
-
setChoked
public void setChoked(PeerDirection direction, boolean choked)
Sets if this peer is choked or not. Choked meaning: We either can or cannot download pieces from this peer (or we don't allow them to do so).- Parameters:
direction- The side of the connection is being changedchoked- The choke state
-
setInterested
public void setInterested(PeerDirection direction, boolean interested)
Sets if this peer is interested or not. Interested meaning: We either want or don't want to download pieces from this peer (or they don't want pieces from us).- Parameters:
direction- The side of the connection is being changedinterested- The interested state
-
getRequestLimit
public int getRequestLimit()
Gets the amount of requests we are allowed to make to this peer- Returns:
- the maximum amount of concurrent requests we can make to this peer
-
isInterested
public boolean isInterested(PeerDirection direction)
Checks if the client is interested.- Parameters:
direction- The direction to check for.- Returns:
truewhen the peer for the given direction is in the choke state.
-
isChoked
public boolean isChoked(PeerDirection direction)
Checks if the client is choked.- Parameters:
direction- The direction to check for.- Returns:
truewhen the peer for the given direction is in the choke state.
-
countHavePieces
public int countHavePieces()
Gets the amount of pieces this peer has- Returns:
- returns the amount of pieces which this peer has
-
getBitTorrentSocket
@Deprecated public BitTorrentSocket getBitTorrentSocket()
Deprecated.Will be replaced by alternatives as this class is handling a non-extensible process.Gets the socket handler which handles the socket of this peer- Returns:
- The socket associated to this peer.
-
queueNextPieceForSending
public void queueNextPieceForSending()
Requests to queue the next piece in the socket for sending
-
getIdAsString
public String getIdAsString()
- Returns:
- The ID of the peer in base-12.
-
-