Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/webtorrent/webtorrent.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorFeross Aboukhadijeh <feross@feross.org>2013-10-27 16:24:03 +0400
committerFeross Aboukhadijeh <feross@feross.org>2013-10-27 16:24:03 +0400
commit0584f3d75b56c2ee6e5720c802442b241595a410 (patch)
tree798f562e4dc143a5fa2cf4f34d02edb4c6f93c6d /lib
parent20bec00bdae0852b90a2bef2639c4a13c5aceb0c (diff)
convert compact2string to use bops
see https://github.com/bencevans/node-compact2string/pull/1
Diffstat (limited to 'lib')
-rw-r--r--lib/bittorrent-dht/index.js32
-rw-r--r--lib/string.js2
2 files changed, 20 insertions, 14 deletions
diff --git a/lib/bittorrent-dht/index.js b/lib/bittorrent-dht/index.js
index ed26557..d83e761 100644
--- a/lib/bittorrent-dht/index.js
+++ b/lib/bittorrent-dht/index.js
@@ -1,6 +1,7 @@
// TODO:
// - Use the same DHT object for looking up multiple torrents
// - Persist the routing table for later bootstrapping
+// - Should work in Node, not just browser
module.exports = DHT
@@ -26,10 +27,11 @@ function randomId () {
}
function parseNodeInfo (compact) {
+ console.log(parseNodeInfo)
try {
var nodes = []
for (var i = 0; i < compact.length; i += 26) {
- nodes.push(compact2string(compact.slice(i+20, i+26)))
+ nodes.push(compact2string(bops.subarray(compact, i+20, i+26)))
}
return nodes
} catch (err) {
@@ -101,11 +103,11 @@ function DHT (infoHash) {
DHT.prototype._onNode = function (addr) {
var self = this
if (self.nodes[addr]) return // already know about this node
- // if (self.missingNodes > 0) return self.query(addr)
- // if (self.queue.length < 50) self.queue.push(addr)
process.nextTick(function () {
self.emit('node', self.infoHash, addr)
})
+ if (self.missingNodes > 0) return self.query(addr)
+ if (self.queue.length < 50) self.queue.push(addr)
}
DHT.prototype._onPeer = function (addr) {
@@ -120,31 +122,33 @@ DHT.prototype._onPeer = function (addr) {
DHT.prototype._onData = function (data, host, port) {
var self = this
- log(data)
- log(host)
- log(port)
- self.nodes[host+':'+port] = true
+ self.nodes[host + ':' + port] = true
- throw new Error('on Data')
var message
try {
- console.log(data)
+ console.log('got response from ' + host + ':' + port)
message = bencode.decode(data)
+ console.log(message)
+ if (!message) throw new Error('message is undefined')
} catch (err) {
- console.error(err.message)
- console.warn('Failed to decode UDP data from node ' + host + ':' + port)
+ console.error('Failed to decode UDP data from node ' + host + ':' + port)
+ console.error(err)
return
}
- // TODO: remove the toStrings here?
- if (message.t.toString() != self.requestId.toString()) return
+ if (!message.t || (bops.from(message.t) !== self.requestId.toString())) {
+ console.log(message.t)
+ console.log(self.requestId)
+ console.log('wrong message requestId')
+ return
+ }
var r = message && message.r
var nodes = r && r.nodes || []
var values = r && r.values || []
- parsePeerInfo(values).forEach(self._onPeer.bind(self))
parseNodeInfo(nodes).forEach(self._onNode.bind(self))
+ parsePeerInfo(values).forEach(self._onPeer.bind(self))
}
DHT.prototype.query = function (addr) {
diff --git a/lib/string.js b/lib/string.js
index 9df92a2..2e91033 100644
--- a/lib/string.js
+++ b/lib/string.js
@@ -1,3 +1,5 @@
+// Eliminate this in favor of bops?
+
/* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Base64_encoding_and_decoding */
exports.fromUTF8Arr = UTF8ArrToStr