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 |
_log |
(package private) static long |
CHECK_PERIOD |
(package private) MagnetState |
magnetState
shared across all peers on this torrent
|
protected MetaInfo |
metainfo
will start out null in magnet mode
|
(package private) static int |
RATE_DEPTH |
(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 |
completed()
Return how much the peer has
|
(package private) void |
disconnect() |
void |
disconnect(boolean deregister)
Disconnects this peer if it was connected.
|
void |
downloaded(int size)
Increment the counter.
|
boolean |
equals(Object o)
Two Peers are equal when they have the same PeerID.
|
Destination |
getDestination() |
long |
getDownBWLimit()
Current limit in Bps
|
long |
getDownloaded()
Returns the number of bytes that have been downloaded.
|
long |
getDownloadRate()
Returns the average download rate in bytes per second.
|
Map<String,BEValue> |
getHandshakeMap() |
long |
getInactiveTime() |
MagnetState |
getMagnetState()
Shared state across all peers, callers must sync on returned object
|
long |
getMaxInactiveTime() |
int |
getMaxPipeline() |
PeerID |
getPeerID()
Returns the id of the peer.
|
long |
getPexLastSent()
when did we last send pex peers?
|
String |
getSocket() |
(package private) int |
getTotalCommentsSent() |
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()
when did handshake complete?
|
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 |
isIncoming()
Is this an incoming connection? For RPC
|
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
|
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) |
void |
setChoking(boolean choke)
Sets whether or not we are choking the peer.
|
void |
setHandshakeMap(Map<String,BEValue> map) |
void |
setMetaInfo(MetaInfo meta)
Switch from magnet mode to normal mode
|
void |
setPexLastSent(long now)
when did we last send pex peers?
|
(package private) void |
setRateHistory()
Push the total uploaded/downloaded onto a RATE_DEPTH deep stack Resets the downloaded and
uploaded counters to zero.
|
(package private) void |
setTotalCommentsSent(int count) |
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() |
String |
toString()
Returns the String representation of the peerID.
|
void |
uploaded(int size)
Increment the counter.
|
clone, finalize, getClass, notify, notifyAll, wait, wait, waitcompareToprotected 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)
public int completed()
void disconnect()
public void disconnect(boolean deregister)
public void downloaded(int size)
downloaded in interface BandwidthListenersize - the number of bytes receivedpublic 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()
BandwidthListenergetDownloadRate in interface BandwidthListenerpublic 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)
public 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)
public void setChoking(boolean choke)
public void setMetaInfo(MetaInfo meta)
public void setPexLastSent(long now)
void setRateHistory()
void setTotalCommentsSent(int count)
public boolean shouldRequest(int size)
public boolean shouldRequest(Peer peer, int size)
shouldRequest in interface BandwidthListenerpeer - the peer to request fromsize - 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 sent