Skip to content

Commit

Permalink
changes to support resuming torrents via initial piece verification; …
Browse files Browse the repository at this point in the history
…also includes a slight optimization to separate opening an fs file for reading vs writing
  • Loading branch information
transitive-bullshit committed May 31, 2014
1 parent 32bac0c commit 831ede0
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 71 deletions.
152 changes: 82 additions & 70 deletions bin/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,78 +164,96 @@ client.on('addTorrent', function (torrent) {
}
})

function ontorrent (torrent) {
if (argv.list) {
torrent.files.forEach(function (file, i) {
clivas.line('{3+bold:'+i+'} : {magenta:'+file.name+'}')
})

process.exit(0)
}
function getRuntime () {
return Math.floor((Date.now() - started) / 1000)
}

var href = 'http://' + address() + ':' + client.server.address().port + '/'
function onTorrent (torrent) {
torrent.on('verifying', function (percent) {
if (!argv.quiet && !argv.list) {
clivas.clear()
clivas.line('{green:verifying existing torrent} {bold:'+percent.toFixed(2)+'%}')
}
})

if (argv.vlc && process.platform === 'win32') {
var registry = require('windows-no-runnable').registry
var key
if (process.arch === 'x64') {
try {
key = registry('HKLM/Software/Wow6432Node/VideoLAN/VLC')
} catch (e) {}
torrent.on('done', function () {
console.log('DONE')
if (argv.list) return
if (!argv.quiet) {
clivas.line('torrent downloaded {green:successfully} from {bold:'+torrent.swarm.wires.length+'} {green:peers} in {bold:'+getRuntime()+'s}!')
}
if (argv.remove) {
remove()
} else {
try {
key = registry('HKLM/Software/VideoLAN/VLC')
} catch (err) {}
process.exit(0)
}
})

if (key) {
var vlcPath = key['InstallDir'].value + path.sep + 'vlc'
VLC_ARGS = VLC_ARGS.split(' ')
VLC_ARGS.unshift(href)
cp.execFile(vlcPath, VLC_ARGS)
}
} else {
if (argv.vlc) {
var vlc = cp.exec('vlc '+href+' '+VLC_ARGS+' || /Applications/VLC.app/Contents/MacOS/VLC '+href+' '+VLC_ARGS, function (error) {
if (error) {
process.exit(1)
}
torrent.on('ready', function onTorrentReady () {
if (argv.list) {
torrent.files.forEach(function (file, i) {
clivas.line('{3+bold:'+i+'} : {magenta:'+file.name+'}')
})

vlc.on('exit', function () {
if (!argv['no-quit']) process.exit(0)
})
process.exit(0)
}
}

if (argv.omx) cp.exec(OMX_EXEC + ' ' + href)
if (argv.mplayer) cp.exec(MPLAYER_EXEC + ' ' + href)
//if (quiet) console.log('server is listening on', href)
var href = 'http://' + address() + ':' + client.server.address().port + '/'

if (argv.vlc && process.platform === 'win32') {
var registry = require('windows-no-runnable').registry
var key
if (process.arch === 'x64') {
try {
key = registry('HKLM/Software/Wow6432Node/VideoLAN/VLC')
} catch (e) {}
} else {
try {
key = registry('HKLM/Software/VideoLAN/VLC')
} catch (err) {}
}

var filename = torrent.name
//var filename = index.name.split('/').pop().replace(/\{|\}/g, '')
var swarm = torrent.swarm
var wires = swarm.wires
var hotswaps = 0
if (key) {
var vlcPath = key['InstallDir'].value + path.sep + 'vlc'
VLC_ARGS = VLC_ARGS.split(' ')
VLC_ARGS.unshift(href)
cp.execFile(vlcPath, VLC_ARGS)
}
} else {
if (argv.vlc) {
var vlc = cp.exec('vlc '+href+' '+VLC_ARGS+' || /Applications/VLC.app/Contents/MacOS/VLC '+href+' '+VLC_ARGS, function (error) {
if (error) {
process.exit(1)
}
})

vlc.on('exit', function () {
if (!argv['no-quit']) process.exit(0)
})
}
}

torrent.on('hotswap', function () {
hotswaps++
})
if (argv.omx) cp.exec(OMX_EXEC + ' ' + href)
if (argv.mplayer) cp.exec(MPLAYER_EXEC + ' ' + href)
//if (quiet) console.log('server is listening on', href)

function active (wire) {
return !wire.peerChoking
}
var filename = torrent.name
//var filename = index.name.split('/').pop().replace(/\{|\}/g, '')
var swarm = torrent.swarm
var wires = swarm.wires
var hotswaps = 0

function bytes (num) {
return numeral(num).format('0.0b')
}
torrent.on('hotswap', function () {
hotswaps++
})

function getRuntime () {
return Math.floor((Date.now() - started) / 1000)
}
function active (wire) {
return !wire.peerChoking
}

if (!argv.quiet) {
process.stdout.write(new Buffer('G1tIG1sySg==', 'base64')); // clear for drawing
function bytes (num) {
return numeral(num).format('0.0b')
}

function draw () {
var unchoked = swarm.wires.filter(active)
Expand Down Expand Up @@ -273,28 +291,22 @@ function ontorrent (torrent) {
clivas.flush()
}

setInterval(draw, 500)
draw()
}

torrent.on('done', function () {
if (!argv.quiet) {
clivas.line('torrent downloaded {green:successfully} from {bold:'+wires.length+'} {green:peers} in {bold:'+getRuntime()+'s}!')
}
if (argv.remove) {
remove()
} else {
process.exit(0)
process.stdout.write(new Buffer('G1tIG1sySg==', 'base64')) // clear for drawing

process.nextTick(function () {
setInterval(draw, 500)
})
}
})
}

client.on('torrent', function (torrent) {
if (listening) {
ontorrent(torrent)
onTorrent(torrent)
} else {
client.on('listening', function (torrent) {
ontorrent(torrent)
onTorrent(torrent)
})
}
})
8 changes: 7 additions & 1 deletion lib/fs_storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,16 @@ FSStorage.prototype._onPieceDone = function (piece) {
var end = targets.length
var i = 0

function cb () {
Storage.prototype._onPieceDone.call(self, piece)
}

if (!piece.buffer || self.readonly) return cb()

var writeToNextFile = function (err) {
if (err) return self.emit('error', err)
if (i >= end) {
return Storage.prototype._onPieceDone.call(self, piece)
return cb()
}

var target = targets[i++]
Expand Down

0 comments on commit 831ede0

Please sign in to comment.