Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refractor torrent to make it easier to add new sources #177

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

Fixed it so tests will pass

  • Loading branch information
Mike Tamis
Mike Tamis committed Nov 11, 2014
commit a9c06acb68a2e4564ccc439622169f1b1799ea26
@@ -260,6 +260,7 @@ File.prototype._checkDone = function () {
})

if (self.done) {
debug('done');
process.nextTick(function () {
self.emit('done')
})
@@ -550,6 +551,7 @@ Storage.prototype._checkDone = function () {

if (!self.done && self.files.every(function (file) { return file.done })) {
self.done = true
debug('done');
self.emit('done')
}
}
@@ -7,12 +7,19 @@ var parseTorrent = require('parse-torrent')
var Torrent = require('./torrent')
var Storage = require('./storage')
var Server = require('./server') // browser exclude
var hh = require('http-https') // browser exclude
var once = require('once')
var concat = require('concat-stream') // browser exclude
var reemit = require('re-emitter')
var parallel = require('run-parallel')
var fs = require('fs') // browser exclude

inherits(torrentManager, EventEmitter)

function torrentManager(torrentId, opts) {
var self = this
self._storageImpl = opts.storage || Storage
self._storageImpl = opts.storage || Storage
self.files = []

var parsedTorrent = parseTorrent(torrentId)
if (parsedTorrent && parsedTorrent.infoHash) {
@@ -42,14 +49,21 @@ inherits(torrentManager, EventEmitter)
debug(self.infoHash);
if (parsedTorrent.name) self.name = parsedTorrent.name // preliminary name
self.torrent = new Torrent(self, parsedTorrent, opts);
reemit(self.torrent, self, ['ready', 'dhtAnnounce', 'listening']);
self.torrent.on('ready', function() { self.ready = true; });
if (parsedTorrent.info) self.onMetadata(parsedTorrent)
}
}

torrentManager.prototype.addPeer = function(peer) {
var self = this
self.torrent.addPeer(peer);
}

torrentManager.prototype.createServer = function (opts) {
var self = this
if (typeof Server === 'function' /* browser exclude */) {
return new Server(self.torrent, opts)
return new Server(self, opts)
}
}

@@ -75,6 +89,15 @@ torrentManager.prototype.onMetadata = function (metadata) {
}

self.storage = new self._storageImpl(self.parsedTorrent, self.storageOpts)
self.storage.files.forEach(function (file) {
self.files.push(file)
})

self.storage.on('done', function () {
debug('torrent ' + self.infoHash + ' done')
self.emit('done')
})

if (self.verify) {
process.nextTick(function () {
debug('verifying existing torrent data')
@@ -111,4 +134,52 @@ torrentManager.prototype.onMetadata = function (metadata) {
self.torrent.updateMetadata(self.parsedTorrent)
self.emit('metadata')
})
}
}

/**
* Destroy and cleanup this torrent.
*/
torrentManager.prototype.destroy = function (cb) {
var self = this
debug('destroy')
self._destroyed = true
clearInterval(self._rechokeIntervalId)

var tasks = []
if (self.torrent) tasks.push(function (cb) {
self.torrent.destroy(cb)
})

parallel(tasks, cb)
}


/**
* Make http or https request, following redirects.
* @param {string} u
* @param {function}
* @param {number=} maxRedirects
* @return {http.ClientRequest}
*/
function httpGet (u, cb, maxRedirects) {
cb = once(cb)
if (!maxRedirects) maxRedirects = 5
if (maxRedirects === 0) return cb(new Error('too many redirects'))

hh.get(u, function (res) {
// Check for redirect
if (res.statusCode >= 300 && res.statusCode < 400 && 'location' in res.headers) {
var location = res.headers.location
if (!url.parse(location).host) {
// If relative redirect, prepend host of current url
var parsed = url.parse(u)
location = parsed.protocol + '//' + parsed.host + location
}
res.resume() // discard response
return httpGet(location, cb, --maxRedirects)
}
res.pipe(concat(function (data) {
cb(null, data)
}))
}).on('error', cb)
}
@@ -59,7 +59,6 @@ function Torrent (manager, parsedTorrent, opts) {
self._rechokeIntervalId = null

self.ready = false
self.files = []
self.metadata = null
self.parsedTorrent = parsedTorrent
self.infoHash = parsedTorrent.infoHash
@@ -154,17 +153,11 @@ Torrent.prototype.attachStorage = function(storage) {
self.discovery.tracker.complete()

debug('torrent ' + self.infoHash + ' done')
self.emit('done')
})

self.storage.on('select', self.select.bind(self))
self.storage.on('deselect', self.deselect.bind(self))
self.storage.on('critical', self.critical.bind(self))

self.storage.files.forEach(function (file) {
self.files.push(file)
})

process.nextTick(self._onStorage.bind(self))
}

@@ -176,7 +169,11 @@ Torrent.prototype.updateMetadata = function(parsedTorrent) {
self.metadata = parseTorrent.toBuffer(parsedTorrent)

// update discovery module with full torrent metadata
self.discovery.setTorrent(self.parsedTorrent)
if(!!self.discovery) {
self.discovery.setTorrent(self.parsedTorrent)
} else {
self.on('listening', function() { self.discovery.setTorrent(self.parsedTorrent) });
}

self.rarityMap = new RarityMap(self.swarm, self.parsedTorrent.pieces.length)

@@ -913,33 +910,3 @@ function randomizedForEach (array, cb) {
cb(array[index], index, array)
})
}

/**
* Make http or https request, following redirects.
* @param {string} u
* @param {function}
* @param {number=} maxRedirects
* @return {http.ClientRequest}
*/
function httpGet (u, cb, maxRedirects) {
cb = once(cb)
if (!maxRedirects) maxRedirects = 5
if (maxRedirects === 0) return cb(new Error('too many redirects'))

hh.get(u, function (res) {
// Check for redirect
if (res.statusCode >= 300 && res.statusCode < 400 && 'location' in res.headers) {
var location = res.headers.location
if (!url.parse(location).host) {
// If relative redirect, prepend host of current url
var parsed = url.parse(u)
location = parsed.protocol + '//' + parsed.host + location
}
res.resume() // discard response
return httpGet(location, cb, --maxRedirects)
}
res.pipe(concat(function (data) {
cb(null, data)
}))
}).on('error', cb)
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.