public class Peer extends Object implements Comparable<Peer>, BandwidthListener
This class handles:
Peers can be either incoming (they connected to us) or outgoing (we connected to them) connections.
| Modifier and Type | Field and Description |
|---|---|
protected Log | _logLogger for this peer instance. |
(package private) static long | CHECK_PERIODC h e c k p e r i o d |
(package private) MagnetState | magnetStateshared across all peers on this torrent |
protected MetaInfo | metainfowill start out null in magnet mode |
(package private) static int | RATE_DEPTHR a t e d e p t h |
(package private) PeerState | state` Keeps state for in/out connections. |
| Constructor and Description |
|---|
Peer(I2PSocket sock,
InputStream in,
OutputStream out,
byte[] my_id,
byte[] infohash,
MetaInfo metainfo)Incoming connection. |
Peer(PeerID peerID,
byte[] my_id,
byte[] infohash,
MetaInfo metainfo)Outgoing connection. |
| Modifier and Type | Method and Description |
|---|---|
(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 | compareTo(Peer p)Compares this peer to another by PeerID, then by internal ID. |
int | completed()Return how much the peer has |
(package private) void | disconnect()Disconnects this peer, closing the socket and cleaning up state. |
void | disconnect(boolean deregister)Disconnects this peer if it was connected. |
void | downloaded(int size)Increments the download counter for this peer. |
boolean | equals(Object o)Two Peers are equal when they have the same PeerID. |
Destination | getDestination()Returns the I2P destination of the connected peer. |
long | getDownBWLimit()Current limit in Bps |
long | getDownloaded()Returns the number of bytes that have been downloaded. |
long | getDownloadRate()Returns the average rate in Bps. |
Map<String,BEValue> | getHandshakeMap()Returns the extension handshake data from the peer. |
long | getInactiveTime()Returns the time in milliseconds since the last activity on either
the incoming or outgoing connection. |
MagnetState | getMagnetState()Returns the shared magnet state across all peers on this torrent. |
long | getMaxInactiveTime() |
int | getMaxPipeline()Returns the maximum request pipeline depth negotiated with the peer. |
PeerID | getPeerID()Returns the id of the peer. |
long | getPexLastSent()Returns the timestamp when PEX peers were last sent to this peer. |
String | getSocket() |
(package private) int | getTotalCommentsSent()Returns the total number of UT comments sent to this peer. |
long | getUpBWLimit()Current limit in Bps |
long | getUploaded()Returns the number of bytes that have been uploaded. |
long | getUploadRate()Returns the average rate in Bps. |
long | getWhenConnected()Returns the timestamp when the handshake completed. |
int | hashCode()The hash code of a Peer is the hash code of the peerID. |
void | have(int piece)Notifies the peer that we have a new 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()Returns whether the peer is in active communication. |
boolean | isIncoming()Returns whether this is an incoming connection (initiated by the remote peer). |
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()Returns whether this is a web peer. |
void | keepAlive()Send keepalive |
boolean | overDownBWLimit()Are we currently over the limit? |
boolean | overUpBWLimit()Is snark as a whole over its limit? |
(package private) void | request()Update the request queue. |
void | retransmitRequests()Retransmit outstanding requests if necessary |
void | runConnection(I2PSnarkUtil util,
PeerListener listener,
BandwidthListener bwl,
BitField bitfield,
MagnetState mState,
boolean uploadOnly)Runs the connection to the other peer. |
void | sendExtension(int type,
byte[] payload)Sends a protocol extension message to the peer. |
void | setChoking(boolean choke)Sets whether or not we are choking the peer. |
void | setHandshakeMap(Map<String,BEValue> map)Sets the extension handshake data and updates the request pipeline limit. |
void | setMetaInfo(MetaInfo meta)Switches from magnet mode to normal mode with metainfo. |
void | setPexLastSent(long now)Sets the timestamp when PEX peers were last sent to this peer. |
(package private) void | setRateHistory()Push the total uploaded/downloaded onto a RATE_DEPTH deep stack. |
(package private) void | setTotalCommentsSent(int count)Sets the total number of UT comments sent to this peer. |
boolean | shouldRequest(int size)Should we request this many bytes? |
boolean | shouldRequest(Peer peer,
int size)Should we request this many bytes? |
boolean | shouldSend(int size)Should we send this many bytes? Do NOT call uploaded() after this. |
boolean | supportsFast()Returns whether the peer supports the FAST protocol extension (BEP 6). |
String | toString()Returns the String representation of the peerID. |
void | uploaded(int size)Increments the upload counter for this peer. |
protected final Log _log
static final long CHECK_PERIOD
MagnetState magnetState
protected MetaInfo metainfo
static final int RATE_DEPTH
volatile PeerState state
public Peer(I2PSocket sock, InputStream in, OutputStream out, byte[] my_id, byte[] infohash, MetaInfo metainfo) throws IOException
metainfo - null if in magnet modeIOException - when an error occurred during the handshake.void cancel(int piece)
piece - the piece indexpublic int compareTo(Peer p)
compareTo in interface Comparable<Peer>p - the peer to compare topublic int completed()
void disconnect()
public void disconnect(boolean deregister)
deregister - true if partial pieces should be returnedpublic void downloaded(int size)
downloaded in interface BandwidthListenersize - the number of bytes downloadedpublic boolean equals(Object o)
public Destination getDestination()
public long getDownBWLimit()
getDownBWLimit in interface BandwidthListenerpublic long getDownloaded()
resetCounters() which is called every CHECK_PERIOD by PeerCheckerTask.public long getDownloadRate()
getDownloadRate in interface BandwidthListenerpublic Map<String,BEValue> getHandshakeMap()
public long getInactiveTime()
public MagnetState getMagnetState()
public long getMaxInactiveTime()
public int getMaxPipeline()
public PeerID getPeerID()
public long getPexLastSent()
public String getSocket()
int getTotalCommentsSent()
public long getUpBWLimit()
getUpBWLimit in interface BandwidthListenerpublic long getUploaded()
resetCounters() which is called every CHECK_PERIOD by PeerCheckerTask.public long getUploadRate()
getUploadRate in interface BandwidthListenerpublic long getWhenConnected()
public int hashCode()
public void have(int piece)
piece - the piece indexpublic boolean isChoked()
public boolean isChoking()
public boolean isCompleted()
public boolean isConnected()
public boolean isIncoming()
public boolean isInterested()
public boolean isInteresting()
public boolean isWebPeer()
public void keepAlive()
public boolean overDownBWLimit()
overDownBWLimit in interface BandwidthListenerpublic boolean overUpBWLimit()
overUpBWLimit in interface BandwidthListenervoid request()
public void retransmitRequests()
public void runConnection(I2PSnarkUtil util, PeerListener listener, BandwidthListener bwl, BitField bitfield, MagnetState mState, boolean uploadOnly)
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.
uploadOnly - if we are complete with skipped files, i.e. a partial seedpublic void sendExtension(int type,
byte[] payload)type - the extension message typepayload - the extension message payloadpublic void setChoking(boolean choke)
choke - true to choke (stop uploading), false to unchoke (allow uploads)public void setHandshakeMap(Map<String,BEValue> map)
map - the handshake map (non-null)public void setMetaInfo(MetaInfo meta)
meta - the metainfo to setpublic void setPexLastSent(long now)
now - the current timestamp in millisecondsvoid setRateHistory()
void setTotalCommentsSent(int count)
count - the count to setpublic boolean shouldRequest(int size)
public boolean shouldRequest(Peer peer, int size)
shouldRequest in interface BandwidthListenerpeer - the peersize - the number of bytes to requestpublic boolean shouldSend(int size)
shouldSend in interface BandwidthListenersize - the number of bytes to sendpublic boolean supportsFast()
public String toString()
public void uploaded(int size)
uploaded in interface BandwidthListenersize - the number of bytes uploaded