Package org.klomp.snark
Class Peer
java.lang.Object
org.klomp.snark.Peer
- All Implemented Interfaces:
Comparable<Peer>,BandwidthListener
- Direct Known Subclasses:
WebPeer
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final Log(package private) static final long(package private) MagnetStateshared across all peers on this torrentprotected MetaInfowill start out null in magnet mode(package private) static final int(package private) PeerState` Keeps state for in/out connections. -
Constructor Summary
ConstructorsConstructorDescriptionPeer(I2PSocket sock, InputStream in, OutputStream out, byte[] my_id, byte[] infohash, MetaInfo metainfo) Incoming connection.Outgoing connection. -
Method Summary
Modifier and TypeMethodDescription(package private) voidcancel(int piece) Tell the other side that we are no longer interested in any of the outstanding requests (if any) for this piece.intDeprecated.unused?intReturn how much the peer has(package private) voidvoiddisconnect(boolean deregister) Disconnects this peer if it was connected.voiddownloaded(int size) Increment the counter.booleanTwo Peers are equal when they have the same PeerID.longCurrent limit in BpslongReturns the number of bytes that have been downloaded.longThe average rate in BpslongShared state across all peers, callers must sync on returned objectlongintReturns the id of the peer.longwhen did we last send pex peers?(package private) intlongCurrent limit in BpslongReturns the number of bytes that have been uploaded.longReturns the average rate in Bpslongwhen did handshake complete?inthashCode()The hash code of a Peer is the hash code of the peerID.voidhave(int piece) Tell the peer we have another piece.booleanisChoked()Whether or not the peer choked us.booleanWhether or not we are choking the peer.booleanReturn if a peer is a seederbooleanbooleanIs this an incoming connection? For RPCbooleanWhether or not the peer is interested in pieces we have.booleanWhether or not the peer has pieces we want from it.(package private) booleanisRequesting(int p) Deprecated.deadlocksbooleanvoidSend keepalivebooleanAre we currently over the limit?booleanIs snark as a whole over its limit?(package private) voidrequest()Update the request queue.voidRetransmit outstanding requests if necessaryvoidrunConnection(I2PSnarkUtil util, PeerListener listener, BandwidthListener bwl, BitField bitfield, MagnetState mState, boolean uploadOnly) Runs the connection to the other peer.voidsendExtension(int type, byte[] payload) voidsetChoking(boolean choke) Sets whether or not we are choking the peer.voidsetHandshakeMap(Map<String, BEValue> map) voidsetInteresting(boolean interest) Deprecated.unusedvoidsetMetaInfo(MetaInfo meta) Switch from magnet mode to normal modevoidsetPexLastSent(long now) when did we last send pex peers?(package private) voidPush the total uploaded/downloaded onto a RATE_DEPTH deep stack Resets the downloaded and uploaded counters to zero.(package private) voidsetTotalCommentsSent(int count) booleanshouldRequest(int size) Should we request this many bytes?booleanshouldRequest(Peer peer, int size) Should we request this many bytes?booleanshouldSend(int size) Should we send this many bytes? Do NOT call uploaded() after this.booleantoString()Returns the String representation of the peerID.voiduploaded(int size) Increment the counter.
-
Field Details
-
_log
-
metainfo
will start out null in magnet mode -
state
` Keeps state for in/out connections. Non-null when the handshake was successful, the connection setup and runs. Do not access directly. All actions should be through Peer methods. -
magnetState
MagnetState magnetStateshared across all peers on this torrent -
CHECK_PERIOD
static final long CHECK_PERIOD- See Also:
-
RATE_DEPTH
static final int RATE_DEPTH- See Also:
-
-
Constructor Details
-
Peer
Outgoing connection. Creates a disconnected peer given a PeerID, your own id and the relevant MetaInfo.- Parameters:
metainfo- null if in magnet mode
-
Peer
public Peer(I2PSocket sock, InputStream in, OutputStream out, byte[] my_id, byte[] infohash, MetaInfo metainfo) throws IOException Incoming connection. 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.- Parameters:
metainfo- null if in magnet mode- Throws:
IOException- when an error occurred during the handshake.
-
-
Method Details
-
isIncoming
public boolean isIncoming()Is this an incoming connection? For RPC- Since:
- 0.9.30
-
getPeerID
Returns the id of the peer. -
toString
Returns the String representation of the peerID. -
getSocket
- Returns:
- socket debug string (for debug printing)
-
hashCode
public int hashCode()The hash code of a Peer is the hash code of the peerID. -
equals
Two Peers are equal when they have the same PeerID. All other properties are ignored. -
compareTo
Deprecated.unused?Compares the PeerIDs.- Specified by:
compareToin interfaceComparable<Peer>
-
runConnection
public void runConnection(I2PSnarkUtil util, PeerListener listener, BandwidthListener bwl, BitField bitfield, MagnetState mState, boolean uploadOnly) 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.- Parameters:
uploadOnly- if we are complete with skipped files, i.e. a partial seed
-
supportsFast
public boolean supportsFast()- Since:
- 0.9.21
-
getDestination
- Since:
- 0.8.4
-
getMagnetState
Shared state across all peers, callers must sync on returned object- Returns:
- non-null
- Since:
- 0.8.4
-
getHandshakeMap
- Returns:
- could be null @since 0.8.4
-
setHandshakeMap
- Parameters:
map- non-null- Since:
- 0.8.4
-
getMaxPipeline
public int getMaxPipeline()- Returns:
- min of PeerState.MIN_PIPELINE, max of PeerState.MAX_PIPELINE
- Since:
- 0.9.47
-
sendExtension
public void sendExtension(int type, byte[] payload) - Since:
- 0.8.4
-
setMetaInfo
Switch from magnet mode to normal mode- Since:
- 0.8.4
-
isConnected
public boolean isConnected() -
disconnect
public void disconnect(boolean deregister) Disconnects this peer if it was connected. PeerListener.disconnected() will be called when the connection is completely terminated. If deregister is true, partial pieces will be returned. -
disconnect
void disconnect() -
have
public void have(int piece) Tell the peer we have another piece. -
cancel
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.- Since:
- 0.8.1
-
isRequesting
Deprecated.deadlocksAre we currently requesting the piece?- Since:
- 0.8.1
-
request
void request()Update the request queue. Call after adding wanted pieces.- Since:
- 0.8.1
-
isInterested
public boolean isInterested()Whether or not the peer is interested in pieces we have. Returns false if not connected. -
setInteresting
Deprecated.unusedSets 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. -
downloaded
public void downloaded(int size) Increment the counter.- Specified by:
downloadedin interfaceBandwidthListener- Since:
- 0.8.4
-
uploaded
public void uploaded(int size) Increment the counter.- Specified by:
uploadedin interfaceBandwidthListener- Since:
- 0.8.4
-
getDownloaded
public long getDownloaded()Returns the number of bytes that have been downloaded. Can be reset to zero withresetCounters()which is called every CHECK_PERIOD by PeerCheckerTask. -
getUploaded
public long getUploaded()Returns the number of bytes that have been uploaded. Can be reset to zero withresetCounters()which is called every CHECK_PERIOD by PeerCheckerTask. -
getUploadRate
public long getUploadRate()Returns the average rate in Bps- Specified by:
getUploadRatein interfaceBandwidthListener
-
getDownloadRate
public long getDownloadRate()Description copied from interface:BandwidthListenerThe average rate in Bps- Specified by:
getDownloadRatein interfaceBandwidthListener
-
shouldSend
public boolean shouldSend(int size) Should we send this many bytes? Do NOT call uploaded() after this.- Specified by:
shouldSendin interfaceBandwidthListener- Since:
- 0.9.62
-
shouldRequest
public boolean shouldRequest(int size) Should we request this many bytes?- Since:
- 0.9.62
-
shouldRequest
Should we request this many bytes?- Specified by:
shouldRequestin interfaceBandwidthListener- Since:
- 0.9.62
-
getUpBWLimit
public long getUpBWLimit()Current limit in Bps- Specified by:
getUpBWLimitin interfaceBandwidthListener- Since:
- 0.9.62
-
overUpBWLimit
public boolean overUpBWLimit()Is snark as a whole over its limit?- Specified by:
overUpBWLimitin interfaceBandwidthListener- Since:
- 0.9.62
-
getDownBWLimit
public long getDownBWLimit()Current limit in Bps- Specified by:
getDownBWLimitin interfaceBandwidthListener- Since:
- 0.9.62
-
overDownBWLimit
public boolean overDownBWLimit()Are we currently over the limit?- Specified by:
overDownBWLimitin interfaceBandwidthListener- Since:
- 0.9.62
-
setRateHistory
void setRateHistory()Push the total uploaded/downloaded onto a RATE_DEPTH deep stack Resets the downloaded and uploaded counters to zero. -
getInactiveTime
public long getInactiveTime() -
getMaxInactiveTime
public long getMaxInactiveTime()- Since:
- 0.9.36
-
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- Returns:
- number of completed pieces (not bytes)
-
isCompleted
public boolean isCompleted()Return if a peer is a seeder -
getTotalCommentsSent
int getTotalCommentsSent()- Since:
- 0.9.31
-
setTotalCommentsSent
void setTotalCommentsSent(int count) - Since:
- 0.9.31
-
isWebPeer
public boolean isWebPeer()- Returns:
- false
- Since:
- 0.9.49
-
getWhenConnected
public long getWhenConnected()when did handshake complete?- Since:
- 0.9.63
-
getPexLastSent
public long getPexLastSent()when did we last send pex peers?- Since:
- 0.9.63
-
setPexLastSent
public void setPexLastSent(long now) when did we last send pex peers?- Since:
- 0.9.63
-