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

adding bittorrent-peerid to README #63

Merged
merged 5 commits into from Jun 3, 2014

changes to support resuming torrents via initial piece verification; …

…also includes a slight optimization to separate opening an fs file for reading vs writing
  • Loading branch information
transitive-bullshit committed May 31, 2014
commit 831ede0b798da55047fb10b13b11fdc8fc531160
@@ -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)
@@ -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)
})
}
})
@@ -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++]
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.