kdns/router~ Router

new Router(identity)

Kademlia routing table consisting of module:kdns/constants~B total module:kdns/bucket~Buckets - each holding up to module:kdns/constants~K total module:kdns/contacts~Contacts.

Parameters:
NameTypeDescription
identitybuffer

Reference point for calculating distances

Extends

  • Map

Members

events

Exposes events arising from handling protocol messages. Some of these events may require handling if they pass a Protocol~HandlerResponse

Properties
NameTypeDescription
eventsEventEmitter

Router events interface

identity

Exposes events arising from handling protocol messages. Some of these events may require handling if they pass a Protocol~HandlerResponse

Properties
NameTypeDescription
identitybuffer

Reference key for distance calculation

length

The total buckets in the routing table

Properties
NameTypeDescription
lengthnumber

size

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

contactmodule:kdns/contacts~Contact

Contact information for peer

Returns:
Type: 
array

getClosestBucket() → {module:kdns/bucket~Bucket}

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

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) → {module:kdns/contacts~Contact}

Returns the contact object associated with the given node id

Parameters:
NameTypeDescription
nodeIdstring | buffer

Node identity of the 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) → {undefined}

Removes the contact from the routing table given a node id

Parameters:
NameTypeDescription
nodeIdstring | buffer

Node identity to remove

Returns:
Type: 
undefined

Events

contact_added

Contact is inserted into the routing table

Parameters:
NameTypeDescription
fingerprintstring

Node ID of the inserted contact

contact_deleted

Contact is evicted from the routing table

Parameters:
NameTypeDescription
fingerprintstring

Node ID of the evicted contact