Comparing version 0.0.0 to 0.1.0
@@ -5,4 +5,3 @@ var dht = require('./') | ||
var node = dht({ | ||
bootstrap: 'dht.mafintosh.com:10000', | ||
ephemeral: true | ||
bootstrap: 'dht.mafintosh.com:10000' | ||
}) | ||
@@ -28,4 +27,3 @@ | ||
if (process.argv[2] === 'put') { | ||
var s = node.closest({command: 'store', target: sha256(val), value: val}, function (err, res) { | ||
console.log(res, s.responses, s.errors) | ||
node.closest({command: 'store', target: sha256(val), value: val}, function (err) { | ||
if (err) throw err | ||
@@ -32,0 +30,0 @@ console.log('Inserted', sha256(val).toString('hex')) |
83
index.js
@@ -48,2 +48,4 @@ var udp = require('udp-request') | ||
this._tickInterval = setInterval(tick, 5 * 1000) | ||
this._top = null | ||
this._bottom = null | ||
@@ -109,3 +111,13 @@ if (opts.nodes) { | ||
DHT.prototype._pingSome = function () { | ||
// console.log('ping some ...') | ||
var all = this.nodes.toArray() | ||
if (!all.length) return | ||
var cnt = this.inflightQueries > 2 ? 1 : 3 | ||
var oldest = this._bottom | ||
while (cnt--) { | ||
if (!oldest || this._tick - oldest.tick < 3) continue | ||
this._check(oldest) | ||
oldest = oldest.next | ||
} | ||
} | ||
@@ -168,3 +180,2 @@ | ||
DHT.prototype._bootstrap = function () { | ||
// TODO: run in the background | ||
// TODO: check stats, to determine wheather to rerun? | ||
@@ -358,2 +369,9 @@ var self = this | ||
DHT.prototype._check = function (node) { | ||
var self = this | ||
this._request({command: '_ping', id: this._queryId}, node, false, function (err) { | ||
if (err) self._removeNode(node) | ||
}) | ||
} | ||
DHT.prototype._reping = function (oldContacts, newContact) { | ||
@@ -373,3 +391,3 @@ var self = this | ||
self.nodes.remove(next.id) | ||
self._removeNode(next) | ||
self.nodes.add(newContact) | ||
@@ -385,11 +403,27 @@ } | ||
if (bufferEquals(id, this.id)) return | ||
this.nodes.add({ | ||
id: id, | ||
port: peer.port, | ||
host: peer.host, | ||
roundtripToken: token, | ||
tick: this._tick | ||
}) | ||
var node = this.nodes.get(id) | ||
var fresh = !node | ||
if (!node) node = {} | ||
node.id = id, | ||
node.port = peer.port, | ||
node.host = peer.host, | ||
node.roundtripToken = token, | ||
node.tick = this._tick | ||
if (!fresh) remove(this, node) | ||
add(this, node) | ||
this.nodes.add(node) | ||
if (fresh) this.emit('add-node', node) | ||
} | ||
DHT.prototype._removeNode = function (node) { | ||
remove(this, node) | ||
this.nodes.remove(node.id) | ||
this.emit('remove-node', node) | ||
} | ||
DHT.prototype.listen = function (port, cb) { | ||
@@ -427,1 +461,30 @@ this.socket.listen(port, cb) | ||
} | ||
function remove (self, node) { | ||
if (self._bottom !== node && self._top !== node) { | ||
node.prev.next = node.next | ||
node.next.prev = node.prev | ||
node.next = node.prev = null | ||
} else { | ||
if (self._bottom === node) { | ||
self._bottom = node.next | ||
if (self._bottom) self._bottom.prev = null | ||
} | ||
if (self._top === node) { | ||
self._top = node.prev | ||
if (self._top) self._top.next = null | ||
} | ||
} | ||
} | ||
function add (self, node) { | ||
if (!self._top && !self._bottom) { | ||
self._top = self._bottom = node | ||
node.prev = node.next = null | ||
} else { | ||
self._top.next = node | ||
node.prev = self._top | ||
node.next = null | ||
self._top = node | ||
} | ||
} |
{ | ||
"name": "dht-rpc", | ||
"version": "0.0.0", | ||
"version": "0.1.0", | ||
"description": "Make RPC calls over a Kademlia based DHT.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28803
11
863