The base class from which custom contacts inherit; used by the included
AddressPortContact
. Nodes provide each other with contact information which
indicates how others should communicate with them.
Updates the lastSeen
property to the current UNIX time. Used by the Router
for organizing the routing table.
Creating a custom contact is very simple. You need only to:
- Define a constructor function that accepts a dictionary
- Inherit from
kademlia.Contact
- Implement
_createNodeID
- Call super class at bottom of your constructor
var kademlia = require('kad');
var inherits = require('util').inherits;
var utils = require('../utils');
// Define you constructor function
function AddressPortContact(options) {
// Make sure the `new` keyword is not required
if (!(this instanceof AddressPortContact)) {
return new AddressPortContact(options);
}
// Store relevant contact information
this.address = options.address;
this.port = options.port;
// Call super class to setup bindings
kademlia.Contact.call(this, options);
}
// Inherit from `kademlia.Contact`
inherits(AddressPortContact, kademlia.Contact);
// Implement `_createNodeID` for super class to use
AddressPortContact.prototype._createNodeID = function() {
return utils.createID(this.address + ':' + this.port);
};