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

check if download is complete and notify tracker #113

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var path = require('path');
var fs = require('fs');
var os = require('os');
var eos = require('end-of-stream');
var debounce = require('lodash.debounce');

var peerDiscovery = require('./lib/peer-discovery');
var blocklist = require('ip-set');
Expand All @@ -32,6 +33,8 @@ var BAD_PIECE_STRIKES_DURATION = 120000; // 2 minutes
var RECHOKE_INTERVAL = 10000;
var RECHOKE_OPTIMISTIC_DURATION = 2;

var COMPLETE_DEBOUNCE_DELAY = 250;

var TMP = fs.existsSync('/tmp') ? '/tmp' : os.tmpDir();

var noop = function() {};
Expand Down Expand Up @@ -719,6 +722,22 @@ var torrentStream = function(link, opts, cb) {
discovery.updatePort(engine.port);
};

// check if download is complete
engine.on('verify', debounce(function checkComplete () {
var bits = engine.torrent.pieces.length;
var bytes = bits / 8 | 0;
var rem = bits % 8;
var buffer = engine.bitfield.buffer;
for (var i = 0; i < bytes; i++) {
if (buffer[i] !== 255) return; // every byte must be full of ones
}
var mask = 256 - Math.pow(2, 8 - rem); // the last byte may be not full
if (rem === 0 || buffer[bytes] === mask) {
discovery.complete();
engine.emit('complete');
}
}, COMPLETE_DEBOUNCE_DELAY));

return engine;
};

Expand Down
11 changes: 10 additions & 1 deletion lib/peer-discovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ module.exports = function(torrent, opts) {
tr.on('peer', onpeer);
tr.on('error', function() { /* noop */ });

tr.start();
if (discovery.amSeeder) {
tr.complete();
} else {
tr.start();
}
return tr;
};

Expand All @@ -78,6 +82,11 @@ module.exports = function(torrent, opts) {
if (torrent) discovery.tracker = createTracker(torrent);
};

discovery.complete = function () {
discovery.amSeeder = true;
if (discovery.tracker) discovery.tracker.complete();
};

discovery.stop = function() {
if (discovery.tracker) discovery.tracker.stop();
if (discovery.dht) discovery.dht.destroy();
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"hat": "0.0.3",
"ip": "^0.3.0",
"ip-set": "^1.0.0",
"lodash.debounce": "^3.0.3",
"magnet-uri": "^2.0.1",
"mkdirp": "^0.3.5",
"parse-torrent": "^4.0.0",
Expand Down
7 changes: 7 additions & 0 deletions test/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ test('fixture can verify the torrent', function(t) {
});
});

test('fixture should report `complete`', function(t) {
t.plan(1);
fixture.once('complete', function() {
t.ok(true, 'should be complete');
});
});

test('fixture can read the file contents', function(t) {
t.equal(fixture.files.length, 1, 'should have one file');
var file = fixture.files[0];
Expand Down
7 changes: 5 additions & 2 deletions test/tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ server.on('error', function() {
});

test('seed should connect to the tracker', function(t) {
t.plan(3);
t.plan(4);

server.once('listening', function() {
t.ok(true, 'tracker should be listening');
Expand All @@ -28,7 +28,10 @@ test('seed should connect to the tracker', function(t) {
fixture.once('ready', t.ok.bind(t, true, 'should be ready'));
});
server.once('start', function(addr) {
t.equal(addr, '127.0.0.1:6882');
t.equal(addr, '127.0.0.1:6882', 'should report `start`');
});
server.once('complete', function(addr) {
t.equal(addr, '127.0.0.1:6882', 'should report `complete`');
});
server.listen(12345);
});
Expand Down