org.klomp.snark
Class Peer

java.lang.Object
  extended by org.klomp.snark.Peer
All Implemented Interfaces:
java.lang.Comparable

public class Peer
extends java.lang.Object
implements java.lang.Comparable


Field Summary
(package private) static long CHECK_PERIOD
           
(package private)  MetaInfo metainfo
           
(package private) static int RATE_DEPTH
           
(package private)  PeerState state
           
 
Constructor Summary
Peer(I2PSocket sock, java.io.InputStream in, java.io.OutputStream out, byte[] my_id, MetaInfo metainfo)
          Creates a unconnected peer from the input and output stream got from the socket.
Peer(PeerID peerID, byte[] my_id, MetaInfo metainfo)
          Creates a disconnected peer given a PeerID, your own id and the relevant MetaInfo.
 
Method Summary
 int compareTo(java.lang.Object o)
          Compares the PeerIDs.
 int completed()
          Return how much the peer has
(package private)  void disconnect()
           
 void disconnect(boolean deregister)
          Disconnects this peer if it was connected.
 boolean equals(java.lang.Object o)
          Two Peers are equal when they have the same PeerID.
 long getDownloaded()
          Returns the number of bytes that have been downloaded.
 long getDownloadRate()
           
 long getInactiveTime()
           
 PeerID getPeerID()
          Returns the id of the peer.
 java.lang.String getSocket()
          Returns socket (for debug printing)
 long getUploaded()
          Returns the number of bytes that have been uploaded.
 long getUploadRate()
          Returns the 4-minute-average rate in Bps
 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.
 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.
 void keepAlive()
          Send keepalive
 void resetCounters()
          Resets the downloaded and uploaded counters to zero.
 void retransmitRequests()
          Retransmit outstanding requests if necessary
 void runConnection(I2PSnarkUtil util, PeerListener listener, BitField bitfield)
          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. unused
 void setRateHistory(long up, long down)
          Push the total uploaded/downloaded onto a RATE_DEPTH deep stack
 java.lang.String toString()
          Returns the String representation of the peerID.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

metainfo

final MetaInfo metainfo

state

PeerState state

CHECK_PERIOD

static final long CHECK_PERIOD
See Also:
Constant Field Values

RATE_DEPTH

static final int RATE_DEPTH
See Also:
Constant Field Values
Constructor Detail

Peer

public Peer(PeerID peerID,
            byte[] my_id,
            MetaInfo metainfo)
     throws java.io.IOException
Creates a disconnected peer given a PeerID, your own id and the relevant MetaInfo.

Throws:
java.io.IOException

Peer

public Peer(I2PSocket sock,
            java.io.InputStream in,
            java.io.OutputStream out,
            byte[] my_id,
            MetaInfo metainfo)
     throws java.io.IOException
Creates a unconnected peer from the input and output stream got from the socket. Note that the complete handshake (which can take some time or block indefinitely) is done in the calling Thread to get the remote peer id. To completely start the connection call the connect() method.

Throws:
java.io.IOException - when an error occurred during the handshake.
Method Detail

getPeerID

public PeerID getPeerID()
Returns the id of the peer.


toString

public java.lang.String toString()
Returns the String representation of the peerID.

Overrides:
toString in class java.lang.Object

getSocket

public java.lang.String getSocket()
Returns socket (for debug printing)


hashCode

public int hashCode()
The hash code of a Peer is the hash code of the peerID.

Overrides:
hashCode in class java.lang.Object

equals

public boolean equals(java.lang.Object o)
Two Peers are equal when they have the same PeerID. All other properties are ignored.

Overrides:
equals in class java.lang.Object

compareTo

public int compareTo(java.lang.Object o)
Compares the PeerIDs.

Specified by:
compareTo in interface java.lang.Comparable

runConnection

public void runConnection(I2PSnarkUtil util,
                          PeerListener listener,
                          BitField bitfield)
Runs the connection to the other peer. This method does not return until the connection is terminated. When the connection is correctly started the connected() method of the given PeerListener is called. If the connection ends or the connection could not be setup correctly the disconnected() method is called. If the given BitField is non-null it is send to the peer as first message.


isConnected

public boolean isConnected()

disconnect

public void disconnect(boolean deregister)
Disconnects this peer if it was connected. If deregister is true, PeerListener.disconnected() will be called when the connection is completely terminated. Otherwise the connection is silently terminated.


disconnect

void disconnect()

have

public void have(int piece)
Tell the peer we have another piece.


isInterested

public boolean isInterested()
Whether or not the peer is interested in pieces we have. Returns false if not connected.


setInteresting

public void setInteresting(boolean interest)
Deprecated. unused

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.


isInteresting

public boolean isInteresting()
Whether or not the peer has pieces we want from it. Returns false if not connected.


setChoking

public void setChoking(boolean choke)
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.


isChoking

public boolean isChoking()
Whether or not we are choking the peer. Returns true when not connected.


isChoked

public boolean isChoked()
Whether or not the peer choked us. Returns true when not connected.


getDownloaded

public long getDownloaded()
Returns the number of bytes that have been downloaded. Can be reset to zero with resetCounters()/


getUploaded

public long getUploaded()
Returns the number of bytes that have been uploaded. Can be reset to zero with resetCounters()/


resetCounters

public void resetCounters()
Resets the downloaded and uploaded counters to zero.


getInactiveTime

public long getInactiveTime()

keepAlive

public void keepAlive()
Send keepalive


retransmitRequests

public void retransmitRequests()
Retransmit outstanding requests if necessary


completed

public int completed()
Return how much the peer has


isCompleted

public boolean isCompleted()
Return if a peer is a seeder


setRateHistory

public void setRateHistory(long up,
                           long down)
Push the total uploaded/downloaded onto a RATE_DEPTH deep stack


getUploadRate

public long getUploadRate()
Returns the 4-minute-average rate in Bps


getDownloadRate

public long getDownloadRate()