class PeerCoordinator extends Object implements PeerListener, BandwidthListener
Modifier and Type | Field and Description |
---|---|
(package private) static long |
CHECK_PERIOD |
static long |
MAX_INACTIVE |
static long |
MAX_SEED_INACTIVE |
(package private) static int |
MAX_UPLOADERS |
(package private) MetaInfo |
metainfo
External use by PeerMonitorTask only.
|
(package private) Deque<Peer> |
peers
synchronize on this when changing peers or downloaders.
|
(package private) static int |
RATE_DEPTH |
(package private) Storage |
storage
External use by PeerMonitorTask only.
|
Constructor and Description |
---|
PeerCoordinator(I2PSnarkUtil util,
byte[] id,
byte[] infohash,
MetaInfo metainfo,
Storage storage,
CoordinatorListener listener,
Snark torrent,
BandwidthListener bwl) |
Modifier and Type | Method and Description |
---|---|
void |
addInterestedAndChoking(int toAdd) |
boolean |
addPeer(Peer peer)
Add peer (inbound or outbound)
|
int |
allowedUploaders()
Return number of allowed uploaders for this torrent.
|
void |
banWebPeer(String host,
boolean isPermanent)
Ban a web peer for this torrent, for while or permanently.
|
boolean |
completed() |
void |
connected(Peer peer)
Called when the connection to the peer has started and the
handshake was successfull.
|
void |
decrementUploaders(boolean isInterested)
Decrement the uploaders and (if set) the interestedUploaders counts
|
void |
disconnected(Peer peer)
Called when the connection to the peer was terminated or the
connection handshake failed.
|
void |
downloaded(int size)
Called when a peer has downloaded some bytes of a piece.
|
long |
getCurrentUploadRate()
Returns the rate in Bps
over last complete CHECK_PERIOD seconds
|
long |
getDownBWLimit()
Current limit in Bps
|
long |
getDownloaded()
Returns the total number of downloaded bytes of all peers.
|
long |
getDownloadRate()
Returns the average rate in Bps
over last RATE_DEPTH * CHECK_PERIOD seconds
|
byte[] |
getID() |
byte[] |
getInfoHash() |
int |
getInterestedAndChoking() |
int |
getInterestedUploaders()
Uploaders, interested only.
|
long |
getLeft()
Bytes not yet in storage.
|
MetaInfo |
getMetaInfo() |
String |
getName() |
long |
getNeededLength()
Bytes still wanted.
|
PartialPiece |
getPartialPiece(Peer peer,
BitField havePieces)
Return partial piece to the PeerState if it's still wanted and peer has it.
|
int |
getPeerCount()
might be wrong
|
int |
getPeers()
should be right
|
(package private) Set<PeerID> |
getPEXPeers()
Called by TrackerClient
|
(package private) static long |
getRate(long[] array) |
Storage |
getStorage() |
long |
getUpBWLimit()
Current limit in Bps
|
long |
getUploaded()
Returns the total number of uploaded bytes of all peers.
|
int |
getUploaders()
Uploaders whether interested or not
Use this for per-torrent limits.
|
long |
getUploadRate()
Returns the average rate in Bps
over last RATE_DEPTH * CHECK_PERIOD seconds
|
I2PSnarkUtil |
getUtil()
Convenience
|
boolean |
gotBitField(Peer peer,
BitField bitfield)
Returns true if the given bitfield contains at least one piece we
are interested in.
|
void |
gotChoke(Peer peer,
boolean choke)
this does nothing but logging
|
void |
gotCommentReq(Peer peer,
int num)
Called when comments are requested via ut_comment
|
void |
gotComments(Peer peer,
List<Comment> comments)
Called when comments are received via ut_comment
|
void |
gotExtension(Peer peer,
int id,
byte[] bs)
PeerListener callback
|
boolean |
gotHave(Peer peer,
int piece)
Called when a have piece message is received.
|
void |
gotInterest(Peer peer,
boolean interest)
Called when an interested message is received.
|
void |
gotPeers(Peer peer,
List<PeerID> peers)
Get peers from PEX -
PeerListener callback
|
boolean |
gotPiece(Peer peer,
PartialPiece pp)
Returns false if the piece is no good (according to the hash).
|
void |
gotPort(Peer peer,
int port,
int rport)
PeerListener callback
Tell the DHT to ping it, this will get back the node info
|
ByteArray |
gotRequest(Peer peer,
int piece,
int off,
int len)
Returns a byte array containing the requested piece or null of
the piece is unknown.
|
void |
halt() |
boolean |
halted() |
boolean |
isWebPeerBanned(String host)
Is a web peer banned?
|
boolean |
needOutboundPeers()
Outbound.
|
boolean |
needPeers()
Inbound.
|
boolean |
needPiece(Peer peer,
BitField havePieces)
Called when we are downloading from the peer and may need to ask for
a new piece.
|
boolean |
overDownBWLimit()
Are we currently over the limit?
|
boolean |
overUpBWLimit()
Is snark as a whole over its limit?
|
boolean |
overUpBWLimit(long total)
Is a particular peer who has downloaded this many bytes from us
in the last CHECK_PERIOD over its limit?
|
List<Peer> |
peerList()
for web page detailed stats
|
void |
restart() |
void |
savePartialPieces(Peer peer,
List<Request> partials)
Save partial pieces on peer disconnection
and hopefully restart it later.
|
(package private) void |
sendCommentReq(Peer peer)
Send a commment request message to the peer, if he supports it.
|
(package private) void |
sendDHT(Peer peer)
Send a DHT message to the peer, if we both support DHT.
|
(package private) void |
sendPeers(Peer peer)
Send a PEX message to the peer, if he supports PEX.
|
(package private) static void |
setRate(long val,
long[] array) |
void |
setRateHistory(long up,
long down)
Push the total uploaded/downloaded onto a RATE_DEPTH deep stack
|
void |
setStorage(Storage stg)
Sets the storage after transition out of magnet mode
Snark calls this after we call gotMetaInfo()
|
void |
setUploaded(long up)
Sets the initial total of uploaded bytes of all peers (from a saved status)
|
void |
setUploaders(int upl,
int inter)
Set the uploaders and interestedUploaders counts
|
void |
setWantedPieces()
Only called externally from Storage after the double-check fails.
|
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() if this returns true.
|
(package private) void |
unchokePeer()
(Optimistically) unchoke.
|
void |
updatePiecePriorities()
Maps file priorities to piece priorities.
|
void |
uploaded(int size)
Called when a peer has uploaded some bytes of a piece.
|
MetaInfo metainfo
Storage storage
static final long CHECK_PERIOD
static final int MAX_UPLOADERS
public static final long MAX_INACTIVE
public static final long MAX_SEED_INACTIVE
static final int RATE_DEPTH
public PeerCoordinator(I2PSnarkUtil util, byte[] id, byte[] infohash, MetaInfo metainfo, Storage storage, CoordinatorListener listener, Snark torrent, BandwidthListener bwl)
metainfo
- null if in magnet modestorage
- null if in magnet modepublic void setWantedPieces()
public Storage getStorage()
public byte[] getID()
public String getName()
public boolean completed()
public int getPeerCount()
public int getPeers()
public long getLeft()
public long getNeededLength()
public long getUploaded()
public void setUploaded(long up)
public long getDownloaded()
public void uploaded(int size)
uploaded
in interface BandwidthListener
public void downloaded(int size)
downloaded
in interface BandwidthListener
public boolean shouldSend(int size)
shouldSend
in interface BandwidthListener
public boolean shouldRequest(Peer peer, int size)
shouldRequest
in interface BandwidthListener
public void setRateHistory(long up, long down)
static void setRate(long val, long[] array)
public long getDownloadRate()
getDownloadRate
in interface BandwidthListener
public long getUploadRate()
getUploadRate
in interface BandwidthListener
public long getCurrentUploadRate()
static long getRate(long[] array)
public long getUpBWLimit()
getUpBWLimit
in interface BandwidthListener
public boolean overUpBWLimit()
overUpBWLimit
in interface BandwidthListener
public boolean overUpBWLimit(long total)
public long getDownBWLimit()
getDownBWLimit
in interface BandwidthListener
public boolean overDownBWLimit()
overDownBWLimit
in interface BandwidthListener
public MetaInfo getMetaInfo()
public byte[] getInfoHash()
public boolean needPeers()
public boolean needOutboundPeers()
public boolean halted()
public void halt()
public void restart()
public void connected(Peer peer)
PeerListener
connected
in interface PeerListener
peer
- the Peer that just got connected.public boolean addPeer(Peer peer)
void unchokePeer()
public boolean gotHave(Peer peer, int piece)
PeerListener
gotHave
in interface PeerListener
peer
- the Peer that got the message.piece
- the piece number that the per just got.public boolean gotBitField(Peer peer, BitField bitfield)
gotBitField
in interface PeerListener
peer
- the Peer that got the message.bitfield
- a BitField containing the pieces that the other
side has.public void updatePiecePriorities()
public ByteArray gotRequest(Peer peer, int piece, int off, int len)
gotRequest
in interface PeerListener
peer
- the Peer that wants the piece.piece
- the piece number requested.off
- byte offset into the piece.len
- length of the chunk requested.RuntimeException
- on IOE getting the datapublic boolean gotPiece(Peer peer, PartialPiece pp)
gotPiece
in interface PeerListener
peer
- the Peer that got the piece.pp
- the piece received.RuntimeException
- on IOE saving the piecepublic void gotChoke(Peer peer, boolean choke)
gotChoke
in interface PeerListener
peer
- the Peer that got the message.choke
- true when the peer got a choke message, false when
the peer got an unchoke message.public void gotInterest(Peer peer, boolean interest)
PeerListener
gotInterest
in interface PeerListener
peer
- the Peer that got the message.interest
- true when the peer got a interested message, false when
the peer got an uninterested message.public void disconnected(Peer peer)
PeerListener
disconnected
in interface PeerListener
peer
- the Peer that just got disconnected.public void savePartialPieces(Peer peer, List<Request> partials)
savePartialPieces
in interface PeerListener
peer
- partials, must include the zero-offset (empty) ones too.
No dup pieces.
len field in Requests is ignored.public PartialPiece getPartialPiece(Peer peer, BitField havePieces)
getPartialPiece
in interface PeerListener
havePieces
- pieces the peer has, the rv will be one of thesepublic boolean needPiece(Peer peer, BitField havePieces)
needPiece
in interface PeerListener
peer
- the Peer that will be asked to provide the piece.havePieces
- a BitField containing the pieces that the other
side has.public void gotExtension(Peer peer, int id, byte[] bs)
gotExtension
in interface PeerListener
peer
- the Peer that got the message.id
- the message IDbs
- the message payloadvoid sendPeers(Peer peer)
void sendDHT(Peer peer)
void sendCommentReq(Peer peer)
public void setStorage(Storage stg)
public void gotPort(Peer peer, int port, int rport)
gotPort
in interface PeerListener
rport
- must be port + 1peer
- the Peer that got the message.port
- the query portpublic void gotPeers(Peer peer, List<PeerID> peers)
gotPeers
in interface PeerListener
peer
- the Peer that got the message.peers
- the peer IDs (dest hashes)public void gotCommentReq(Peer peer, int num)
gotCommentReq
in interface PeerListener
public void gotComments(Peer peer, List<Comment> comments)
gotComments
in interface PeerListener
comments
- non-nullSet<PeerID> getPEXPeers()
public int allowedUploaders()
public int getUploaders()
public int getInterestedUploaders()
public void setUploaders(int upl, int inter)
upl
- whether interested or notinter
- interested onlypublic void decrementUploaders(boolean isInterested)
public int getInterestedAndChoking()
public void addInterestedAndChoking(int toAdd)
public I2PSnarkUtil getUtil()
getUtil
in interface PeerListener
public void banWebPeer(String host, boolean isPermanent)
host
- the hostnamepublic boolean isWebPeerBanned(String host)
host
- the hostname