Browse Source

ephemeral id support

v4
Mathias Buus 8 years ago
parent
commit
63aeb6e7de
  1. 6
      example.js
  2. 12
      index.js
  3. 4
      query-stream.js

6
example.js

@ -9,7 +9,7 @@ dht().listen(10000)
var value = new Buffer('hello world')
var hash = require('crypto').createHash('sha256').update(value).digest()
var missing = 100
var missing = 1000
var KBucket = require('k-bucket')
var bingo = (0.6 * missing) | 0 //(Math.random() * missing) | 0
var t
@ -20,13 +20,13 @@ function createNode (i) {
var node = dht({bootstrap: 10000})
var store = {}
node.on('closest', function (request, cb) {
node.on('closest:store', function (request, cb) {
console.log('storing')
store[request.target.toString('hex')] = request.value
cb()
})
node.on('query', function (request, cb) {
node.on('query:lookup', function (request, cb) {
var value = store[request.target.toString('hex')]
cb(null, value)
})

12
index.js

@ -24,6 +24,7 @@ function DHT (opts) {
this.concurrency = opts.concurrency || 16
this.bootstrap = [].concat(opts.bootstrap || []).map(parseAddr)
this.id = opts.id || crypto.randomBytes(32)
this.ephemeral = !!opts.ephemeral
this.nodes = new KBucket({localNodeId: this.id, arbiter: arbiter})
this.nodes.on('ping', onnodeping)
@ -36,6 +37,7 @@ function DHT (opts) {
this.socket.on('response', onresponse)
this.socket.on('close', onclose)
this._queryId = this.ephemeral ? null : this.id
this._bootstrapped = false
this._pendingRequests = []
this._tick = 0
@ -168,7 +170,7 @@ DHT.prototype._bootstrap = function () {
}
DHT.prototype._ping = function (peer, cb) {
this._request({command: '_ping', id: this.id}, peer, false, cb)
this._request({command: '_ping', id: this._queryId}, peer, false, cb)
}
DHT.prototype._request = function (request, peer, important, cb) {
@ -230,7 +232,7 @@ DHT.prototype._onquery = function (request, peer) {
// TODO: support errors?
var res = {
id: self.id,
id: self._queryId,
value: value || null,
nodes: nodes.encode(self.nodes.closest(request.target, 20)),
roundtripToken: self._token(peer, 0)
@ -251,7 +253,7 @@ DHT.prototype._onresponse = function (response, peer) {
DHT.prototype._onping = function (request, peer) {
var res = {
id: this.id,
id: this._queryId,
value: peers.encode([peer]),
roundtripToken: this._token(peer, 0)
}
@ -263,7 +265,7 @@ DHT.prototype._onfindnode = function (request, peer) {
if (!validateId(request.target)) return
var res = {
id: this.id,
id: this._queryId,
nodes: nodes.encode(this.nodes.closest(request.target, 20)),
roundtripToken: this._token(peer, 0)
}
@ -298,7 +300,7 @@ DHT.prototype._reping = function (oldContacts, newContact) {
function ping () {
next = oldContacts.shift()
if (next) self._request({command: '_ping', id: self.id}, next, true, afterPing)
if (next) self._request({command: '_ping', id: self._queryId}, next, true, afterPing)
}
function afterPing (err) {

4
query-stream.js

@ -18,7 +18,7 @@ function QueryStream (dht, query, opts) {
var nodes = opts.node || opts.nodes
this.query = query
this.query.id = dht.id
this.query.id = dht._queryId
this.target = query.target
this.token = !!opts.token
this.responses = 0
@ -244,6 +244,6 @@ function copyNode (node) {
port: node.port,
host: node.host,
roundtripToken: node.roundtripToken,
referer: node.referer // TODO: is this the correct spelling?
referrer: node.referrer
}
}

Loading…
Cancel
Save