diff options
-rw-r--r-- | lib/TorrentManager.js | 106 | ||||
-rw-r--r-- | lib/torrent-manager.js | 127 | ||||
-rw-r--r-- | lib/torrent.js (renamed from lib/Torrent.js) | 9 |
3 files changed, 131 insertions, 111 deletions
diff --git a/lib/TorrentManager.js b/lib/TorrentManager.js deleted file mode 100644 index 8c3e570..0000000 --- a/lib/TorrentManager.js +++ /dev/null @@ -1,106 +0,0 @@ -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) - -function TorrentManager () { - if (!(this instanceof TorrentManager)) return new TorrentManager() - EventEmitter.call(this) - - // TODO: should these ids be consistent between restarts? - this.peerId = new Buffer('-WW0001-' + hat(48), 'utf8') - this.nodeId = new Buffer(hat(160), 'hex') - - this.torrents = {} - - this.dht = new DHT({ nodeId: this.nodeId }) - - this.dht.on('node', this.updateUI.bind(this)) - this.dht.on('peer', this.updateUI.bind(this)) - - this.dht.on('peer', function (addr, infoHash) { - var torrent = this.torrents[infoHash] - torrent.addPeer(addr) - }.bind(this)) - - 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) { - 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 - }) - - torrent.on('metadata', this.updateUI.bind(this)) - - this.dht.setInfoHash(torrent.infoHash) - this.dht.findPeers(MAX_PEERS) // TODO: should the DHT be concerned with max peers? - - this.updateUI() - - var t = this.torrents['d2474e86c95b19b8bcfdb92bc12c9d44667cfa36'] - $('.downloadMetadata').click(function () { - t.downloadMetadata() - }) -} - -// TODO: show multiple torrents -TorrentManager.prototype.updateUI = function () { - // console.log('Peer ID: ' + this.peerId.toString('utf8')) - // console.log('Node ID: ' + this.nodeId.toString('hex')) - - var t = this.torrents['d2474e86c95b19b8bcfdb92bc12c9d44667cfa36'] - - $('.infoHash span').text(t.infoHash) - $('.displayName span').text(t.displayName) - - $('.dhtNodes span').text(Object.keys(this.dht.nodes).length) - $('.dhtPeers span').text(Object.keys(this.dht.peers).length) - - var connectedPeers = 0 - for (var infoHash in this.torrents) { - var torrent = this.torrents[infoHash] - connectedPeers += torrent.numPeers - } - $('.connectedPeers span').text(connectedPeers) - $('.downloadMetadata').toggleClass('highlight', !!t.metadata) -} - diff --git a/lib/torrent-manager.js b/lib/torrent-manager.js new file mode 100644 index 0000000..7222b80 --- /dev/null +++ b/lib/torrent-manager.js @@ -0,0 +1,127 @@ +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) + +function TorrentManager () { + var self = this + if (!(self instanceof TorrentManager)) return new TorrentManager() + EventEmitter.call(self) + + // TODO: should these ids be consistent between restarts? + self.peerId = new Buffer('-WW0001-' + hat(48), 'utf8') + self.nodeId = new Buffer(hat(160), 'hex') + + self.torrents = [] + + self.dht = new DHT({ nodeId: self.nodeId }) + + // self.dht.on('node', self.updateUI.bind(self)) + // self.dht.on('peer', self.updateUI.bind(self)) + + self.dht.on('peer', function (addr, infoHash) { + var torrent = self.getTorrent(infoHash) + torrent.addPeer(addr) + }) + + self.ready = false + + self._installWindowEvents() + + async.auto({ + dhtPort: function (cb) { + portfinder.getPort(cb) + }, + torrentPort: function (cb) { + portfinder.getPort(cb) + } + }, function (err, r) { + self.dhtPort = r.dhtPort + self.torrentPort = r.torrentPort + + self.dht.listen(self.dhtPort, function () { + self.ready = true + self.emit('ready') + }) + }) +} + +TorrentManager.prototype.getTorrent = function (infoHash) { + var self = this + var index + for (var i = 0, len = self.torrents.length; i < len; i += 1) { + var torrent = self.torrents[i] + if (torrent.infoHash === infoHash) + return torrent + } + return null +} + +TorrentManager.prototype.add = function (uri) { + var self = this + if (!self.ready) + return self.once('ready', self.add.bind(self, uri)) + + var torrent = new Torrent(uri, { + peerId: self.peerId, + port: self.torrentPort + }) + self.torrents.push(torrent) + + self._installTorrentEvents(torrent) + self.emit('addTorrent', 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 + }) + + self.dht.setInfoHash(torrent.infoHash) + self.dht.findPeers(MAX_PEERS) // TODO: should the DHT be concerned with max peers? +} + +TorrentManager.prototype._installTorrentEvents = function (torrent) { + var self = this + var events = ['listening', 'metadata'] + events.forEach(function (event) { + torrent.on(event, function () { + var args = Array.prototype.slice.call(arguments) + args.unshift('torrent:' + event, torrent) + self.emit.apply(self, args) + }) + }) +} + +TorrentManager.prototype._installWindowEvents = function () { + var self = this + var appWindow + // TODO: on add/remove torrent call resizeTo to set window size + + chrome.app.runtime.onLaunched.addListener(function () { + chrome.app.window.create('app.html', { + bounds: { + width: 500, + height: 600 + }, + frame: 'none', + id: 'app', + minWidth: 350, + minHeight: 165 + }, function (_appWindow) { + appWindow = _appWindow + appWindow.contentWindow.name = 'app' + appWindow.contentWindow.torrentManager = self + }) + }) +} diff --git a/lib/Torrent.js b/lib/torrent.js index 38fc18d..5710c52 100644 --- a/lib/Torrent.js +++ b/lib/torrent.js @@ -18,9 +18,8 @@ function Torrent (uri, opts) { var info = this._parseMagnetUri(uri) if (!info.infoHash) throw new Error('invalid torrent uri') - this.infoHash = info.infoHash - this.displayName = info.displayName + this.title = info.title this.metadata = null this.swarm = new Swarm(this.infoHash, opts.peerId, { dht: true }) @@ -155,7 +154,7 @@ Torrent.prototype.downloadMetadata = function () { chrome.fileSystem.chooseEntry({ type: 'saveFile', - suggestedName: this.displayName + '.torrent' + suggestedName: this.title + '.torrent' }, function (fileEntry) { if (!fileEntry) return @@ -192,14 +191,14 @@ Torrent.prototype.addPeer = function (addr) { // /** - * Given a magnet URI, return infoHash and displayName + * Given a magnet URI, return infoHash and title * @param {string} uri * @return {Object} */ Torrent.prototype._parseMagnetUri = function (uri) { var parsed = magnet(uri) return { - displayName: parsed.dn, + title: parsed.dn, // displayName infoHash: parsed.xt && parsed.xt.split('urn:btih:')[1] } } |