RoutingTable

Represents a kademlia routing table

Constructor

new RoutingTable(identity)

Constructs a routing table

Parameters:
NameTypeDescription
identitybuffer

Reference point for calculating distances

Members

length

Returns the total buckets in the routing table

Properties
NameTypeDescription
lengthnumber

size

Returns the total contacts in the routing table

Properties
NameTypeDescription
sizenumber

Methods

addContactByNodeId(nodeId, contact) → {array}

Adds the contact to the routing table in the proper bucket position, returning the [bucketIndex, bucket, contactIndex, contact]; if the returned contactIndex is -1, it indicates the bucket is full and the contact was not added; kademlia implementations should PING the contact at bucket.head to determine if it should be dropped before calling this method again

Parameters:
NameTypeDescription
nodeIdstring | buffer

Node identity to add

contactobject

contact information for peer

Returns:
Type: 
array

getClosestBucket() → {Bucket}

Returns the [index, bucket] of the occupied bucket with the lowest index

Returns:
Type: 
Bucket

getClosestContactsToKey(key, nopt, exclusiveopt) → {map}

Returns a array of N contacts closest to the supplied key

Parameters:
NameTypeAttributesDefaultDescription
keystring | buffer

Key to get buckets for

nnumber<optional>
20

Number of results to return

exclusiveboolean<optional>
false

Exclude exact matches

Returns:
Type: 
map

getContactByNodeId(nodeId) → {Bucket~contact}

Returns the contact object associated with the given node id

Parameters:
NameTypeDescription
nodeIdstring | buffer

Node identity of the contact

Returns:
Type: 
Bucket~contact

indexOf(nodeId) → {number}

Returns the bucket index of the given node id

Parameters:
NameTypeDescription
nodeIdstring | buffer

Node identity to get index for

Returns:
Type: 
number

removeContactByNodeId(nodeId) → {boolean}

Removes the contact from the routing table given a node id

Parameters:
NameTypeDescription
nodeIdstring | buffer

Node identity to remove

Returns:
Type: 
boolean