- All Implemented Interfaces:
DataStructure
A NID is a 20-byte SHA1 hash that uniquely identifies a node in the DHT keyspace. It serves as the primary key for routing table entries and is used extensively throughout the DHT system for node identification, distance calculations, and bucket placement.
Beyond the basic SHA1 hash functionality, this class tracks node health metrics including last seen time and failure count. This information is crucial for maintaining a healthy routing table by identifying and removing unreliable or unreachable nodes.
Key features:
- Extends SHA1Hash for efficient storage and comparison
- Tracks last seen time for node freshness assessment
- Maintains failure count to detect unreachable nodes
- Provides timeout detection after multiple consecutive failures
- Used extensively as map keys throughout the DHT system
Failure handling allows up to 5 consecutive timeouts before marking a node as problematic, providing resilience against temporary network issues while eventually removing persistently unreachable nodes.
Field Summary
FieldsFields inherited from class SHA1Hash
HASH_LENGTHFields inherited from class SimpleDataStructure
_dataConstructor Summary
Method Summary
All MethodsModifier and TypeMethodDescriptionintReturns the number of consecutive timeouts or failures recorded.longlastSeen()Returns the timestamp when this node was last successfully seen.voidUpdates the last seen timestamp and resets failure count.booleantimeout()Records a timeout and checks if the node should be considered problematic.Methods inherited from class SHA1Hash
hashCode, length, readBytes, setDataModifier and TypeMethodDescriptioninthashCode()A Hash is a hash, so just use the first 4 bytes for speed.intlength()Length of a SHA-1 hash.voidreadBytes(InputStream in) Read the hash data from a stream.voidsetData(byte[] data) IllegalArgumentException if data is not 20 bytes (null is ok).Methods inherited from class SimpleDataStructure
calculateHash, equals, fromBase64, fromByteArray, getData, read, toBase64, 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.toBase64()Renders the structure into modified base 64 notation.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 long lastSeenTimestamp when this node was last successfully contacted.fails
private int failsNumber of consecutive timeouts or failures.MAX_FAILS
private static final int MAX_FAILSMaximum allowed consecutive failures before considering node problematic.- See Also:
Constructor Details
NID
public NID()Creates a new empty NID.Creates an NID with null data, typically used as a placeholder or for initialization purposes where the actual NID value will be set later.
NID
public NID(byte[] data) Creates a new NID with the specified data.- Parameters:
data- the 20-byte SHA1 hash data for this node identifier, or null for an empty NID
Method Details
lastSeen
public long lastSeen()Returns the timestamp when this node was last successfully seen.- Returns:
- the timestamp in milliseconds since epoch, or 0 if never seen
setLastSeen
public void setLastSeen()Updates the last seen timestamp and resets failure count.This method should be called whenever successful communication with the node occurs. It updates the freshness metric and resets the failure counter, indicating the node is currently reachable.
getFailCount
public int getFailCount()Returns the number of consecutive timeouts or failures recorded.- Returns:
- the current failure count
timeout
public boolean timeout()Records a timeout and checks if the node should be considered problematic.Increments the failure counter and returns whether the node has exceeded the maximum allowed consecutive failures. This helps identify nodes that are consistently unreachable and should be removed from the routing table.
- Returns:
- true if the node has exceeded the maximum allowed failures, false otherwise