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

Added pause/resume, refactored code, improved command line interface, etc #514

Closed
wants to merge 21 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

added nice cli for seed quitting interface

  • Loading branch information
whitef0x0 committed Nov 27, 2015
commit cd1d84ce23c71f0c2fece2186b805208650dfabe
@@ -345,6 +345,7 @@ function runDownload (torrentId) {
return a.length > b.length ? a : b
}))


if (argv.select) {
var interactive = process.stdin.isTTY && !!process.stdin.setRawMode
if (interactive) {
@@ -493,118 +494,182 @@ function runSeed (input) {
}

var drawInterval
var commandMode = false
var lastInput = ''
var blockDraw = false
var cliInput = false


function drawTorrent (torrent) {

process.stdin.on('data', function(chunk) {
blockDraw = true

if (chunk === 'q' || chunk === 's') {
cliInput = true
process.stdin.setRawMode(false)
process.stdin.pause();
var cli = inquirer.prompt([{
type: 'input',
name: 'shouldQuit',
validate: function(input) {
if (input === 'y' || input === 'n') {
// Pass the return value in the done callback
return true
}else{
return "Incorrect input. Please enter 'y' or 'n'"
}
},
filter: function( input ) {
if(input === 'y') return true
else if(input === 'n') return false
},
message: 'Do you wish to stop seeding?',
}], function (answers) {
if(answers.shouldQuit){
clearInterval(drawInterval)
drawInterval.unref()
gracefulExit()
}else{
process.stdin.setRawMode(true)
blockDraw = false
cliInput = false
}
})

cli.rl.on('SIGINT', function () {
return gracefulExit()
})
}else if(!cliInput){
setTimeout(function(){
blockDraw = false
draw()
},100)
}
});

if (!argv.quiet) {

process.stdout.write(new Buffer('G1tIG1sySg==', 'base64')) // clear for drawing
process.stdin.setRawMode(true)
process.stdin.resume()
process.stdin.setEncoding( 'utf8' );
drawInterval = setInterval(draw, 500)
drawInterval.unref()

}

function draw () {
var hotswaps = 0
torrent.on('hotswap', function () {
hotswaps += 1
})

var unchoked = torrent.swarm.wires.filter(function (wire) {
return !wire.peerChoking
})
var linesRemaining = clivas.height
var peerslisted = 0
var speed = torrent.downloadSpeed()
var estimate = moment.duration(torrent.timeRemaining / 1000, 'seconds').humanize()

clivas.clear()

if (playerName) {
clivas.line('{green:Streaming to} {bold:' + playerName + '}')
linesRemaining -= 1
}
if(!blockDraw){
var hotswaps = 0
torrent.on('hotswap', function () {
hotswaps += 1
})

if (server) {
clivas.line('{green:server running at} {bold:' + href + '}')
linesRemaining -= 1
}
var unchoked = torrent.swarm.wires.filter(function (wire) {
return !wire.peerChoking
})
var linesRemaining = clivas.height
var peerslisted = 0
var speed = torrent.downloadSpeed()
var estimate = moment.duration(torrent.timeRemaining / 1000, 'seconds').humanize()

if (argv.out) {
clivas.line('{green:downloading to} {bold:' + argv.out + '}')
linesRemaining -= 1
}
clivas.clear()

var seeding = torrent.done
if (playerName) {
clivas.line('{green:Streaming to} {bold:' + playerName + '}')
linesRemaining -= 1
}

if (!seeding) clivas.line('')
clivas.line(
'{green:' + (seeding ? 'seeding' : 'downloading') + ':} ' +
'{bold:' + torrent.name + '}'
)
if (seeding) {
clivas.line('{green:info hash:} ' + torrent.infoHash)
linesRemaining -= 1
}
clivas.line(
'{green:speed: }{bold:' + prettyBytes(speed) + '/s} ' +
'{green:downloaded:} {bold:' + prettyBytes(torrent.downloaded) + '}' +
'/{bold:' + prettyBytes(torrent.length) + '} ' +
'{green:uploaded:} {bold:' + prettyBytes(torrent.uploaded) + '} ' +
'{green:peers:} {bold:' + unchoked.length + '/' + torrent.swarm.wires.length + '} ' +
'{green:hotswaps:} {bold:' + hotswaps + '}'
)
clivas.line(
'{green:time remaining:} {bold:' + estimate + ' remaining} ' +
'{green:total time:} {bold:' + getRuntime() + 's} ' +
'{green:queued peers:} {bold:' + torrent.swarm.numQueued + '} ' +
'{green:blocked:} {bold:' + torrent.numBlockedPeers + '}'
)
clivas.line('{80:}')
linesRemaining -= 5

torrent.swarm.wires.every(function (wire) {
var progress = '?'
if (torrent.length) {
var bits = 0
var piececount = Math.ceil(torrent.length / torrent.pieceLength)
for (var i = 0; i < piececount; i++) {
if (wire.peerPieces.get(i)) {
bits++
}
}
progress = bits === piececount ? 'S' : Math.floor(100 * bits / piececount) + '%'
if (server) {
clivas.line('{green:server running at} {bold:' + href + '}')
linesRemaining -= 1
}
var tags = []

if (wire.peerChoking) tags.push('choked')
if (wire.requests.length > 0) tags.push(wire.requests.length + ' reqs')
if (argv.out) {
clivas.line('{green:downloading to} {bold:' + argv.out + '}')
linesRemaining -= 1
}

var reqStats = argv.verbose
? wire.requests.map(function (req) { return req.piece })
: []
var seeding = torrent.done

if (!seeding) clivas.line('')
clivas.line(
'{3:%s} {25+magenta:%s} {10:%s} {12+cyan:%s/s} {12+red:%s/s} {15+grey:%s}' +
'{10+grey:%s}',
progress,
wire.remoteAddress
? (wire.remoteAddress + ':' + wire.remotePort)
: 'Unknown',
prettyBytes(wire.downloaded),
prettyBytes(wire.downloadSpeed()),
prettyBytes(wire.uploadSpeed()),
tags.join(', '),
reqStats.join(' ')
'{green:' + (seeding ? 'seeding' : 'downloading') + ':} ' +
'{bold:' + torrent.name + '}'
)
peerslisted++
return linesRemaining - peerslisted > 4
})
linesRemaining -= peerslisted
if (seeding) {
clivas.line('{green:info hash:} ' + torrent.infoHash)
linesRemaining -= 1
}
clivas.line(
'{green:speed: }{bold:' + prettyBytes(speed) + '/s} ' +
'{green:downloaded:} {bold:' + prettyBytes(torrent.downloaded) + '}' +
'/{bold:' + prettyBytes(torrent.length) + '} ' +
'{green:uploaded:} {bold:' + prettyBytes(torrent.uploaded) + '} ' +
'{green:peers:} {bold:' + unchoked.length + '/' + torrent.swarm.wires.length + '} ' +
'{green:hotswaps:} {bold:' + hotswaps + '}'
)
clivas.line(
'{green:time remaining:} {bold:' + estimate + ' remaining} ' +
'{green:total time:} {bold:' + getRuntime() + 's} ' +
'{green:queued peers:} {bold:' + torrent.swarm.numQueued + '} ' +
'{green:blocked:} {bold:' + torrent.numBlockedPeers + '}'
)
clivas.line('{80:}')
linesRemaining -= 5

torrent.swarm.wires.every(function (wire) {
var progress = '?'
if (torrent.length) {
var bits = 0
var piececount = Math.ceil(torrent.length / torrent.pieceLength)
for (var i = 0; i < piececount; i++) {
if (wire.peerPieces.get(i)) {
bits++
}
}
progress = bits === piececount ? 'S' : Math.floor(100 * bits / piececount) + '%'
}
var tags = []

if (wire.peerChoking) tags.push('choked')
if (wire.requests.length > 0) tags.push(wire.requests.length + ' reqs')

var reqStats = argv.verbose
? wire.requests.map(function (req) { return req.piece })
: []

clivas.line(
'{3:%s} {25+magenta:%s} {10:%s} {12+cyan:%s/s} {12+red:%s/s} {15+grey:%s}' +
'{10+grey:%s}',
progress,
wire.remoteAddress
? (wire.remoteAddress + ':' + wire.remotePort)
: 'Unknown',
prettyBytes(wire.downloaded),
prettyBytes(wire.downloadSpeed()),
prettyBytes(wire.uploadSpeed()),
tags.join(', '),
reqStats.join(' ')
)
peerslisted++
return linesRemaining - peerslisted > 4
})
linesRemaining -= peerslisted

if (torrent.swarm.wires.length > peerslisted) {
clivas.line('{80:}')
clivas.line('... and %s more', torrent.swarm.wires.length - peerslisted)
}

if (torrent.swarm.wires.length > peerslisted) {
clivas.line('{80:}')
clivas.line('... and %s more', torrent.swarm.wires.length - peerslisted)
}

clivas.line('{80:}')
clivas.flush(true)
if(commandMode){
clivas.line('{green:command :}')
}
clivas.flush(true)
}
}
}

@@ -223,6 +223,7 @@ Torrent.prototype._onParsedTorrent = function (parsedTorrent) {
})

self.swarm.on('upload', function (uploaded) {
self.emit('seed request')
self.client.uploadSpeed(uploaded) // update overall client stats
self.client.emit('upload', uploaded)
self.emit('upload', uploaded)
@@ -235,6 +236,7 @@ Torrent.prototype._onParsedTorrent = function (parsedTorrent) {


process.nextTick(function () {
self.emit('seed request')
if (self.destroyed || self.paused) return
self.emit('infoHash', self.infoHash)
})
@@ -407,18 +409,13 @@ Torrent.prototype._onMetadata = function (metadata) {

self._onWireWithMetadata(wire)
})
//console.log('initial progress: '+self.progress);
}else{
//console.log('RESUMING')
//console.log('resumed progress: '+self.progress);
}

self.files = self.files.map(function (file) {
return new File(self, file)
})

debug('verifying existing torrent data')
self.emit('verifying')
parallel(self.pieces.map(function (piece, index) {
return function (cb) {
self.store.get(index, function (err, buf) {
@@ -660,6 +657,7 @@ Torrent.prototype.critical = function (start, end) {
Torrent.prototype._onWire = function (wire, addr) {
var self = this
debug('got wire (%s)', addr || 'Unknown')
self.emit('seed request')

//console.log('got wire (%s)', addr || 'Unknown')

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.