java.lang.Object
net.i2p.data.SimpleDataStructure
net.i2p.data.Hash
org.klomp.snark.dht.Peer
- All Implemented Interfaces:
DataStructure
Represents a single peer participating in a specific torrent within the DHT tracker.
This class extends Hash to store the peer's identification (typically their destination hash) along with metadata about their participation in the torrent. The DHT tracker maintains collections of these peers to respond to peer discovery requests from other clients.
Each peer tracks when it was last seen and whether it's a seed (has the complete torrent). This information is used to prioritize peers in responses and to maintain an accurate view of the torrent's swarm health.
Field Summary
FieldsFields inherited from class Hash
FAKE_HASH, HASH_LENGTHModifier and TypeFieldDescriptionstatic final HashPlaceholder all-zero hash, used as a stand-in when no real router hash is known.static final intLength of SHA-256 hash in bytes.Fields inherited from class SimpleDataStructure
_dataConstructor Summary
ConstructorsMethod Summary
All MethodsModifier and TypeMethodDescriptionbooleanisSeed()Returns whether this peer is a seed (has the complete torrent).longlastSeen()Returns the timestamp when this peer was last seen active in the torrent.voidsetLastSeen(long now) Updates the timestamp when this peer was last seen.voidsetSeed(boolean isSeed) Whether this peer is a seed.Methods inherited from class Hash
clearCache, create, create, create, hashCode, length, readBytes, setData, toBase32, toBase64Modifier and TypeMethodDescriptionstatic voidDrop the shared cache of Hash instances, so cached instances become eligible for collection.static Hashcreate(byte[] data) Pull from cache or return newstatic Hashcreate(byte[] data, int off) Pull from cache or return new.static Hashcreate(InputStream in) Pull from cache or return new.inthashCode()A Hash is a hash, so just use the first 4 bytes for speed.intlength()Hash length in bytes.voidreadBytes(InputStream in) Reads the hash from an input stream.voidsetData(byte[] data) IllegalArgumentException if data is not 32 bytes (null is ok).toBase32()The .b32.i2p form of the hash.toBase64()Returns the hash as a Base64 string.Methods inherited from class SimpleDataStructure
calculateHash, equals, fromBase64, fromByteArray, getData, read, toByteArray, toString, writeBytesModifier and TypeMethodDescriptionSHA256 hash of the byte array, or null if data is null.booleanCompares the raw data only: two different SimpleDataStructure subclasses of the same length holding the same bytes are equal, e.g.voidfromBase64(String data) Data from the base64 string.voidfromByteArray(byte[] data) Does the same thing asSimpleDataStructure.setData(byte[])but null not allowed.byte[]getData()Data reference (not a copy).protected intread(InputStream in, byte[] target) Repeated reads until the buffer is full or IOException is thrown.byte[]Same thing as getData().toString()Returns a string representation of this data structure.voidwriteBytes(OutputStream out) Writes the data to the output stream.
Field Details
lastSeen
private volatile long lastSeenTimestamp when this peer was last active in the torrent.isSeed
private volatile boolean isSeedFlag indicating whether this peer has the complete torrent.
Constructor Details
Peer
public Peer(byte[] data) Creates a new peer with the specified identification data.- Parameters:
data- the peer's identification data (typically destination hash)
Method Details
lastSeen
public long lastSeen()Returns the timestamp when this peer was last seen active in the torrent.- Returns:
- the timestamp in milliseconds since epoch, or 0 if never seen
setLastSeen
public void setLastSeen(long now) Updates the timestamp when this peer was last seen.- Parameters:
now- the current timestamp in milliseconds since epoch
isSeed
public boolean isSeed()Returns whether this peer is a seed (has the complete torrent).Seeds are prioritized differently in peer responses as they can provide the complete torrent to leechers. This information is also used to calculate swarm health statistics.
- Returns:
- true if this peer has the complete torrent, false otherwise
setSeed
public void setSeed(boolean isSeed) Whether this peer is a seed.- Parameters:
isSeed- true if this peer has the complete torrent, false otherwise