diff options
author | Feross Aboukhadijeh <feross@feross.org> | 2014-02-05 09:56:09 +0400 |
---|---|---|
committer | Feross Aboukhadijeh <feross@feross.org> | 2014-02-05 10:37:39 +0400 |
commit | 3b24dd52618912b9ecf988399498695627a08cf5 (patch) | |
tree | 1272f93c4adeb318793e4e1a16cbdab17ab657bd | |
parent | 3ef516028dd4e601cf69f61e3a69f42efed8ec87 (diff) |
select ports in TorrentManager
-rw-r--r-- | lib/Torrent.js | 18 | ||||
-rw-r--r-- | lib/TorrentManager.js | 33 | ||||
-rw-r--r-- | package.json | 3 |
3 files changed, 40 insertions, 14 deletions
diff --git a/lib/Torrent.js b/lib/Torrent.js index bdbac49..b0b2599 100644 --- a/lib/Torrent.js +++ b/lib/Torrent.js @@ -24,11 +24,15 @@ function Torrent (uri, opts) { this.swarm = new Swarm(this.infoHash, opts.peerId, { dht: true }) - // TODO: swarm pooling should be smart about picking port - // this.swarm.listen(function (port) { - // console.log('Swarm listening on port ' + port) - // this.emit('listening', port) - // }.bind(this)) + if (opts.port) { + this.swarm.listen(opts.port, function (port) { + this.emit('listening', port) + }.bind(this)) + } + + this.swarm.on('error', function (err) { + console.error(err.message) + }) this.swarm.on('wire', function (wire) { $('.connectedPeers span').text(this.swarm.wires.length) @@ -147,10 +151,6 @@ function Torrent (uri, opts) { }.bind(this)) }.bind(this)) - - this.swarm.on('error', function (err) { - console.error(err.message) - }) } /** diff --git a/lib/TorrentManager.js b/lib/TorrentManager.js index bc9c4eb..cac04eb 100644 --- a/lib/TorrentManager.js +++ b/lib/TorrentManager.js @@ -1,13 +1,16 @@ module.exports = TorrentManager var $ = require('jquery') +var async = require('async') var DHT = require('bittorrent-dht') var EventEmitter = require('events').EventEmitter var hat = require('hat') var inherits = require('inherits') +var portfinder = require('chrome-portfinder') var Torrent = require('./Torrent') var MAX_PEERS = 200 +portfinder.basePort = Math.floor(Math.random() * 60000) + 1025 // >1024 inherits(TorrentManager, EventEmitter) @@ -31,19 +34,41 @@ function TorrentManager () { torrent.addPeer(addr) }.bind(this)) - // this.dht.listen() - + this.ready = false + + async.auto({ + dhtPort: function (cb) { + portfinder.getPort(cb) + }, + torrentPort: function (cb) { + portfinder.getPort(cb) + } + }, function (err, r) { + this.dhtPort = r.dhtPort + this.torrentPort = r.torrentPort + + this.dht.listen(this.dhtPort, function () { + this.ready = true + this.emit('ready') + }.bind(this)) + }.bind(this)) } TorrentManager.prototype.add = function (uri) { - var torrent = new Torrent(uri, { peerId: this.peerId }) + if (!this.ready) + return this.once('ready', this.add.bind(this, uri)) + + var torrent = new Torrent(uri, { + peerId: this.peerId, + port: this.torrentPort + }) this.torrents[torrent.infoHash] = torrent torrent.on('listening', function (port) { + console.log('Swarm listening on port ' + port) // TODO: Add the torrent to the public DHT so peers know to find up }) - // TODO: DHT should support multiple infoHashes this.dht.setInfoHash(torrent.infoHash) this.dht.findPeers(MAX_PEERS) // TODO: should the DHT be concerned with max peers? diff --git a/package.json b/package.json index 7025e1d..36acf6c 100644 --- a/package.json +++ b/package.json @@ -22,17 +22,18 @@ "url": "https://github.com/feross/webtorrent/issues" }, "dependencies": { + "async": "~0.2.10", "bittorrent-dht": "0.x", "bittorrent-protocol": "0.x", "bittorrent-swarm": "0.x", "bncode": "~0.5.2", "browserify": "3.x", "browserify-shim": "~3.2.2", + "chrome-portfinder": "~0.2.4", "hat": "0.0.3", "inherits": "~2.0.1", "jquery": "~2.1.0", "magnet-uri": "1.x", - "portfinder": "~0.2.1", "read-torrent": "~0.2.0", "speedometer": "~0.1.2" }, |