Class Peer


  • public class Peer
    extends Object
    • Method Detail

      • addModuleInfo

        public void addModuleInfo​(Object infoObject)
        Registers the IModule peer 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 with addModuleInfo(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 the extensionBytes has 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 a MessageBlock for 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:
        true when the block has been requested, otherwise false
      • 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 a MessageCancel for 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 by absoluteRequestLimit
        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 changed
        choked - 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 changed
        interested - The interested state
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object
      • 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:
        true when 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:
        true when 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.