org.klomp.snark

## Class WebPeer

- java.lang.Object
- - org.klomp.snark.Peer
  - - org.klomp.snark.WebPeer

- All Implemented Interfaces:
    Comparable<Peer>, EepGet.StatusListener, BandwidthListener

      class WebPeer
      extends Peer
      implements EepGet.StatusListener

  BEP 19. Does not have an associated PeerState. All request tracking is done here.

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static byte[]` | `IDBytes`

    - ### Fields inherited from class org.klomp.snark.Peer

      `_log, CHECK_PERIOD, magnetState, metainfo, RATE_DEPTH, state`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `WebPeer(PeerCoordinator coord, URI uri, PeerID peerID, MetaInfo metainfo)` Outgoing connection.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods Deprecated Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `attemptFailed(String url, long bytesTransferred, long bytesRemaining, int currentAttempt, int numRetries, Exception cause)`
    `void` | `attempting(String url)`
    `void` | `bytesTransferred(long alreadyTransferred, int currentWrite, long bytesTransferred, long bytesRemaining, String url)` Total length should be == alreadyTransferred + currentWrite + bytesRemaining for all calls
    `(package private) void` | `cancel(int piece)` Tell the other side that we are no longer interested in any of the outstanding requests (if any) for this piece.
    `int` | `completed()` Return how much the peer has
    `(package private) void` | `disconnect()`
    `void` | `downloaded(int size)` Increment the counter.
    `boolean` | `equals(Object o)` Two Peers are equal when they have the same PeerID.
    `long` | `getInactiveTime()`
    `long` | `getMaxInactiveTime()`
    `int` | `getMaxPipeline()`
    `String` | `getSocket()`
    `int` | `hashCode()` The hash code of a Peer is the hash code of the peerID.
    `void` | `have(int piece)` Tell the peer we have another piece.
    `void` | `headerReceived(String url, int attemptNum, String key, String val)` Note: Headers are not processed, and this is not called, for most error response codes, unless setWriteErrorToOutput() is called before fetch().
    `boolean` | `isChoked()` Whether or not the peer choked us.
    `boolean` | `isChoking()` Whether or not we are choking the peer.
    `boolean` | `isCompleted()` Return if a peer is a seeder
    `boolean` | `isConnected()`
    `boolean` | `isInterested()` Whether or not the peer is interested in pieces we have.
    `boolean` | `isInteresting()` Whether or not the peer has pieces we want from it.
    `boolean` | `isWebPeer()`
    `void` | `keepAlive()` Send keepalive
    `(package private) void` | `request()` Update the request queue.
    `void` | `retransmitRequests()` Retransmit outstanding requests if necessary
    `void` | `runConnection(I2PSnarkUtil util, PeerListener listener, BandwidthListener bwl, BitField ignore, MagnetState mState, boolean uploadOnly)` Runs the connection to the other peer.
    `void` | `setChoking(boolean choke)` Sets whether or not we are choking the peer.
    `void` | `setInteresting(boolean interest)` Deprecated.
    `boolean` | `shouldRequest(int size)` Should we request this many bytes?
    `String` | `toString()` Returns the String representation of the peerID.
    `void` | `transferComplete(long alreadyTransferred, long bytesTransferred, long bytesRemaining, String url, String outputFile, boolean notModified)`
    `void` | `transferFailed(String url, long bytesTransferred, long bytesRemaining, int currentAttempt)`

    - ### Methods inherited from class org.klomp.snark.Peer

      `disconnect, getDestination, getDownBWLimit, getDownloaded, getDownloadRate, getHandshakeMap, getMagnetState, getPeerID, getPexLastSent, getTotalCommentsSent, getUpBWLimit, getUploaded, getUploadRate, getWhenConnected, isIncoming, overDownBWLimit, overUpBWLimit, sendExtension, setHandshakeMap, setMetaInfo, setPexLastSent, setRateHistory, setTotalCommentsSent, shouldRequest, shouldSend, supportsFast, uploaded`

    - ### Methods inherited from class java.lang.Object

      `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

    - ### Methods inherited from interface java.lang.Comparable

      `compareTo`

- - ### Field Detail

    - #### IDBytes

          public static final byte[] IDBytes

  - ### Constructor Detail

    - #### WebPeer

          public WebPeer(PeerCoordinator coord,
                         URI uri,
                         PeerID peerID,
                         MetaInfo metainfo)

      Outgoing connection. Creates a disconnected peer given a PeerID, your own id and the relevant MetaInfo.
      Parameters:
        `uri` - must be http with .i2p host
        `metainfo` - non-null

  - ### Method Detail

    - #### attemptFailed

          public void attemptFailed(String url,
                                    long bytesTransferred,
                                    long bytesRemaining,
                                    int currentAttempt,
                                    int numRetries,
                                    Exception cause)

      Specified by:
        `attemptFailed` in interface `EepGet.StatusListener`
      See Also:
        `EepGet.StatusListener.bytesTransferred(long, int, long, long, java.lang.String)`

    - #### attempting

          public void attempting(String url)

      Specified by:
        `attempting` in interface `EepGet.StatusListener`

    - #### bytesTransferred

          public void bytesTransferred(long alreadyTransferred,
                                       int currentWrite,
                                       long bytesTransferred,
                                       long bytesRemaining,
                                       String url)

      Description copied from interface: `EepGet.StatusListener`
      Total length should be == alreadyTransferred + currentWrite + bytesRemaining for all calls
      Specified by:
        `bytesTransferred` in interface `EepGet.StatusListener`
      Parameters:
        `alreadyTransferred` - total of all attempts, not including currentWrite If nonzero on the first call, a partial file of that length was found, _and_ the server supports resume. If zero on a subsequent call after some bytes are transferred (and presumably after an attemptFailed), the server does _not_ support resume and we had to start over. To track _actual_ transfer if the output file could already exist, the listener should keep its own counter, or subtract the initial alreadyTransferred value. And watch out for alreadyTransferred resetting if a resume failed...
        `currentWrite` - since last call to the listener
        `bytesTransferred` - includes headers, retries, redirects, discarded partial downloads, ...
        `bytesRemaining` - on this attempt only, currentWrite already subtracted - or -1 if chunked encoding or server does not return a length

    - #### cancel

          void cancel(int piece)

      Description copied from class: `Peer`
      Tell the other side that we are no longer interested in any of the outstanding requests (if any) for this piece.
      Overrides:
        `cancel` in class `Peer`

    - #### completed

          public int completed()

      Description copied from class: `Peer`
      Return how much the peer has
      Overrides:
        `completed` in class `Peer`
      Returns:
        number of completed pieces (not bytes)

    - #### disconnect

          void disconnect()

      Overrides:
        `disconnect` in class `Peer`

    - #### downloaded

          public void downloaded(int size)

      Description copied from class: `Peer`
      Increment the counter.
      Specified by:
        `downloaded` in interface `BandwidthListener`
      Overrides:
        `downloaded` in class `Peer`
      Parameters:
        `size` - the number of bytes received

    - #### equals

          public boolean equals(Object o)

      Two Peers are equal when they have the same PeerID. All other properties are ignored.
      Overrides:
        `equals` in class `Peer`

    - #### getInactiveTime

          public long getInactiveTime()

      Overrides:
        `getInactiveTime` in class `Peer`

    - #### getMaxInactiveTime

          public long getMaxInactiveTime()

      Overrides:
        `getMaxInactiveTime` in class `Peer`

    - #### getMaxPipeline

          public int getMaxPipeline()

      Overrides:
        `getMaxPipeline` in class `Peer`
      Returns:
        min of PeerState.MIN_PIPELINE, max of PeerState.MAX_PIPELINE

    - #### getSocket

          public String getSocket()

      Overrides:
        `getSocket` in class `Peer`
      Returns:
        socket debug string (for debug printing)

    - #### hashCode

          public int hashCode()

      The hash code of a Peer is the hash code of the peerID.
      Overrides:
        `hashCode` in class `Peer`

    - #### have

          public void have(int piece)

      Description copied from class: `Peer`
      Tell the peer we have another piece.
      Overrides:
        `have` in class `Peer`

    - #### headerReceived

          public void headerReceived(String url,
                                     int attemptNum,
                                     String key,
                                     String val)

      Description copied from interface: `EepGet.StatusListener`
      Note: Headers are not processed, and this is not called, for most error response codes, unless setWriteErrorToOutput() is called before fetch(). To be changed?
      Specified by:
        `headerReceived` in interface `EepGet.StatusListener`

    - #### isChoked

          public boolean isChoked()

      Description copied from class: `Peer`
      Whether or not the peer choked us. Returns true when not connected.
      Overrides:
        `isChoked` in class `Peer`

    - #### isChoking

          public boolean isChoking()

      Description copied from class: `Peer`
      Whether or not we are choking the peer. Returns true when not connected.
      Overrides:
        `isChoking` in class `Peer`

    - #### isCompleted

          public boolean isCompleted()

      Description copied from class: `Peer`
      Return if a peer is a seeder
      Overrides:
        `isCompleted` in class `Peer`

    - #### isConnected

          public boolean isConnected()

      Overrides:
        `isConnected` in class `Peer`

    - #### isInterested

          public boolean isInterested()

      Description copied from class: `Peer`
      Whether or not the peer is interested in pieces we have. Returns false if not connected.
      Overrides:
        `isInterested` in class `Peer`

    - #### isInteresting

          public boolean isInteresting()

      Description copied from class: `Peer`
      Whether or not the peer has pieces we want from it. Returns false if not connected.
      Overrides:
        `isInteresting` in class `Peer`

    - #### isWebPeer

          public boolean isWebPeer()

      Overrides:
        `isWebPeer` in class `Peer`
      Returns:
        true

    - #### keepAlive

          public void keepAlive()

      Description copied from class: `Peer`
      Send keepalive
      Overrides:
        `keepAlive` in class `Peer`

    - #### request

          void request()

      Description copied from class: `Peer`
      Update the request queue. Call after adding wanted pieces.
      Overrides:
        `request` in class `Peer`

    - #### retransmitRequests

          public void retransmitRequests()

      Description copied from class: `Peer`
      Retransmit outstanding requests if necessary
      Overrides:
        `retransmitRequests` in class `Peer`

    - #### runConnection

          public void runConnection(I2PSnarkUtil util,
                                    PeerListener listener,
                                    BandwidthListener bwl,
                                    BitField ignore,
                                    MagnetState mState,
                                    boolean uploadOnly)

      Runs the connection to the other peer. This method does not return until the connection is terminated.
      Overrides:
        `runConnection` in class `Peer`
      Parameters:
        `ignore` - our bitfield, ignore
        `uploadOnly` - if we are complete with skipped files, i.e. a partial seed

    - #### setChoking

          public void setChoking(boolean choke)

      Description copied from class: `Peer`
      Sets whether or not we are choking the peer. Defaults to true. When choke is false and the peer requests some pieces we upload them, otherwise requests of this peer are ignored.
      Overrides:
        `setChoking` in class `Peer`

    - #### setInteresting

          @Deprecated
          public void setInteresting(boolean interest)

      Deprecated.
      Description copied from class: `Peer`
      Sets whether or not we are interested in pieces from this peer. Defaults to false. When interest is true and this peer unchokes us then we start downloading from it. Has no effect when not connected.
      Overrides:
        `setInteresting` in class `Peer`

    - #### shouldRequest

          public boolean shouldRequest(int size)

      Should we request this many bytes?
      Overrides:
        `shouldRequest` in class `Peer`

    - #### toString

          public String toString()

      Description copied from class: `Peer`
      Returns the String representation of the peerID.
      Overrides:
        `toString` in class `Peer`

    - #### transferComplete

          public void transferComplete(long alreadyTransferred,
                                       long bytesTransferred,
                                       long bytesRemaining,
                                       String url,
                                       String outputFile,
                                       boolean notModified)

      Specified by:
        `transferComplete` in interface `EepGet.StatusListener`
        `outputFile` - null if unknown (output stream constructor)
      See Also:
        `EepGet.StatusListener.bytesTransferred(long, int, long, long, java.lang.String)`

    - #### transferFailed

          public void transferFailed(String url,
                                     long bytesTransferred,
                                     long bytesRemaining,
                                     int currentAttempt)

      Specified by:
        `transferFailed` in interface `EepGet.StatusListener`
      See Also:
        `EepGet.StatusListener.bytesTransferred(long, int, long, long, java.lang.String)`