Skip to content

Commit

Permalink
resume the swarm and restart peer discovery on interested
Browse files Browse the repository at this point in the history
  • Loading branch information
asapach committed May 9, 2015
1 parent 2c2095e commit 37115da
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
15 changes: 9 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,15 @@ var torrentStream = function(link, opts, cb) {
});

if (prev === engine.amInterested) return;
if (engine.amInterested) engine.emit('interested');
else engine.emit('uninterested');
if (engine.amInterested) {
engine.emit('interested');
if (swarm.paused) {
swarm.resume();

This comment has been minimized.

Copy link
@jaruba

jaruba May 9, 2015

Contributor

Why use swarm.resume() here? From what I can tell peer-wire-swarm resumes anyway.

This comment has been minimized.

Copy link
@asapach

asapach May 9, 2015

Author Collaborator

If you explicitly .pause() it, it doesn't resume automatically. You need to .resume() it.

This comment has been minimized.

Copy link
@jaruba

jaruba May 9, 2015

Contributor

Just realized I'm using peerflix too, not only torrent-stream, and peerflix has this line:
https://github.com/mafintosh/peerflix/blob/master/index.js#L197

I guess this needs to be removed in peerflix as torrent-stream will do it anyway to not execute the same method multiple times.

discovery.restart();
}
} else {
engine.emit('uninterested');
}
};

var gc = function() {
Expand Down Expand Up @@ -581,15 +588,11 @@ var torrentStream = function(link, opts, cb) {
if (engine.bitfield) wire.bitfield(engine.bitfield);
});

swarm.pause();

if (link.files && engine.metadata) {
swarm.resume();
ontorrent(link);
} else {
fs.readFile(torrentPath, function(_, buf) {
if (destroyed) return;
swarm.resume();

This comment has been minimized.

Copy link
@jaruba

jaruba May 9, 2015

Contributor

swarm.resume() should be kept in both cases, only swarm.pause() should be removed.

When I removed swarm.resume() in my tests I got some issues with torrent-stream. Although I didn't get any JS errors, I think the "ready" state didn't emit or something of the sort but my app didn't start the videos anymore.

This comment has been minimized.

Copy link
@asapach

asapach May 9, 2015

Author Collaborator

It seems to be working fine for me. Can you give an example?

This comment has been minimized.

Copy link
@asapach

asapach May 9, 2015

Author Collaborator

This comment has been minimized.

Copy link
@jaruba

jaruba May 9, 2015

Contributor

Works fine now, must of been something else I changed in the code then. :)


// We know only infoHash here, not full infoDictionary.
// But infoHash is enough to connect to trackers and get peers.
Expand Down
22 changes: 13 additions & 9 deletions lib/peer-discovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@ var tracker = require('bittorrent-tracker');

var DEFAULT_PORT = 6881;

module.exports = function(torrent, opts) {
if (typeof opts !== 'object') {
opts = torrent;
torrent = null;
}

module.exports = function(opts) {
var torrent;
var port = opts.port || DEFAULT_PORT;

var discovery = new events.EventEmitter();
Expand Down Expand Up @@ -74,16 +70,24 @@ module.exports = function(torrent, opts) {
if (port === p) return;
port = p;
if (discovery.tracker) discovery.tracker.stop();
if (discovery.dht) discovery.dht.announce(torrent.infoHash, port);
if (torrent) discovery.tracker = createTracker(torrent);
if (torrent) {
if (discovery.dht) discovery.dht.announce(torrent.infoHash, port);
discovery.tracker = createTracker(torrent);
}
};

discovery.stop = function() {
if (discovery.tracker) discovery.tracker.stop();
if (discovery.dht) discovery.dht.destroy();
};

if (torrent) discovery.setTorrent(torrent);
discovery.restart = function() {
if (!torrent) return;
if (discovery.tracker) discovery.tracker.stop();
if (discovery.dht) discovery.dht.destroy();
discovery.tracker = createTracker(torrent);
discovery.dht = createDHT(torrent.infoHash);
};

return discovery;
};

0 comments on commit 37115da

Please sign in to comment.