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

Feature/continue seeding after closing player #607

Open
wants to merge 6 commits 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
26 changes: 14 additions & 12 deletions src/app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
<script id="keyboard-tpl" src="templates/keyboard.tpl" type="text/x-template"></script>
<script id="notification-tpl" src="templates/notification.tpl" type="text/x-template"></script>

<!-- Global variables -->
<script src="global.js"></script>
<!-- Global variables -->
<script src="global.js"></script>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still has whitespace issues

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you guys using tabs or spaces here? jshint doesn't seem to lint for me..


<!-- 3rd party Dependencies -->
<script src="vendor/jquery/dist/jquery.min.js"></script>
Expand Down Expand Up @@ -69,6 +69,7 @@
<script src="httpapi.js"></script>
<script src="language.js"></script>
<script src="lib/streamer.js"></script>
<script src="lib/seeder.js"></script>

<script src="lib/device/generic.js"></script>
<script src="lib/device/airplay.js"></script>
Expand All @@ -84,32 +85,32 @@
<script src="lib/models/lang.js"></script>
<script src="lib/models/content_item.js"></script>
<script src="lib/models/movie.js"></script>
<script src="lib/models/show.js"></script>
<script src="lib/models/show.js"></script>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still has whitespace issues

<script src="lib/models/generic_collection.js"></script>
<script src="lib/models/movie_collection.js"></script>
<script src="lib/models/stream_info.js"></script>
<script src="lib/models/show_collection.js"></script>
<script src="lib/models/anime_collection.js"></script>
<script src="lib/models/anime_collection.js"></script>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still has whitespace issues

<script src="lib/models/indie_collection.js"></script>
<script src="lib/models/favorite_collection.js"></script>
<script src="lib/models/watchlist_collection.js"></script>
<script src="lib/models/watchlist_collection.js"></script>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still has whitespace issues

<script src="lib/models/notification.js"></script>

<!-- Data Sources -->
<script src="lib/cache.js"></script>
<script src="lib/cachev2.js"></script>
<script src="lib/providers/generic.js"></script>
<!--
<script src="lib/providers/generic.js"></script>
<!--
<script src="lib/providers/cache_provider.js"></script>
<script src="lib/providers/cache_providerv2.js"></script>
<script src="lib/providers/trakttv.js"></script>
<script src="lib/providers/vodo.js"></script>
<script src="lib/providers/archive.js"></script>
<script src="lib/providers/favorites.js"></script>
<script src="lib/providers/watchlist.js"></script>
<script src="lib/providers/watchlist.js"></script>
<script src="lib/providers/opensubtitles.js"></script>
<script src="lib/providers/torrent_cache.js"></script>
-->
-->
<!-- Backbone Views and Controllers -->
<script src="lib/views/title_bar.js"></script>
<script src="lib/views/main_window.js"></script>
Expand All @@ -131,19 +132,20 @@
<script src="lib/views/player/loading.js"></script>
<script src="lib/views/player/player.js"></script>

<script src="lib/views/browser/generic_browser.js"></script>
<script src="lib/views/browser/generic_browser.js"></script>
<script src="lib/views/browser/movie_browser.js"></script>
<script src="lib/views/browser/filter_bar.js"></script>
<script src="lib/views/browser/item.js"></script>
<script src="lib/views/browser/list.js"></script>

<script src="lib/views/browser/show_browser.js"></script>
<script src="lib/views/browser/anime_browser.js"></script>
<script src="lib/views/browser/anime_browser.js"></script>
<script src="lib/views/browser/indie_browser.js"></script>

<script src="lib/views/browser/favorite_browser.js"></script>
<script src="lib/views/browser/watchlist_browser.js"></script>
<script src="lib/views/browser/watchlist_browser.js"></script>

<script src="bootstrap.js"></script>

</body>
</html>
61 changes: 61 additions & 0 deletions src/app/lib/seeder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
(function (App) {

var Seeder = function () {
this.worker = null;
};

Seeder.prototype = {
start: function() {
this.getWorkerInstance().send({action: 'start'});
},

stop: function() {
if (this.getWorkerInstance()) {
this.getWorkerInstance().send({action: 'stop'});
this.worker = null;
}
},

append: function(torrent) {
this.getWorkerInstance().send({action: 'append', payload: torrent.name});
},

save: function(torrent) {
var targetFile = path.join(App.settings.tmpLocation, 'TorrentCache', Common.md5(torrent.name) + '.torrent');
var wstream = fs.createWriteStream(targetFile);

wstream.write(torrent.torrentFile);
wstream.end();
},

getWorkerInstance: function () {
if (this.worker === null) {
var taskFile = 'src/app/lib/workers/seederTask.js';
// TODO: AdvSettings here should be trigger creation of worker instance
var args = JSON.stringify({
name: 'BackgroundSeeder',
connectionLimit: Settings.connectionLimit,
trackerAnnouncement: Settings.trackers.forced,
tmpLocation: App.settings.tmpLocation,
seedLimit: Settings.seedLimit
});

this.worker = child.fork(taskFile, [args], {silent: true, execPath:'node'});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'node' will fail on machines that don't have nodejs installed (we bundle)


this.worker.on('message', function(msg) {
win.info(msg);
});
}

return this.worker;
}
};

var seeder = new Seeder();

App.vent.on('seed:start', seeder.start.bind(seeder));
App.vent.on('seed:stop', seeder.stop.bind(seeder));
App.vent.on('seed:save', seeder.save.bind(seeder));
App.vent.on('seed:append', seeder.append.bind(seeder));

})(window.App);
7 changes: 7 additions & 0 deletions src/app/lib/streamer.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
this.setModels(model);

this.fetchTorrent(this.torrentModel.get('torrent')).then(function (torrent) {
if (AdvSettings.get('autoSeed')) {
App.vent.trigger('seed:save', torrent);
}
this.handleTorrent(torrent);
this.watchState();
this.handleStreamInfo();
Expand All @@ -63,6 +66,10 @@

// kill the streamer
stop: function() {
if (AdvSettings.get('autoSeed')) {
App.vent.trigger('seed:append', this.torrentModel.get('torrent'));
}

if (this.webtorrent) {
// update ratio
AdvSettings.set('totalDownloaded', Settings.totalDownloaded + this.torrentModel.get('torrent').downloaded);
Expand Down
6 changes: 4 additions & 2 deletions src/app/lib/views/main_window.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@
$('.events').css('display', 'block');
}

if (AdvSettings.get('autoSeed')) {
App.vent.trigger('seed:start');
}

// set player from settings
var players = App.Device.Collection.models;
for (var i in players) {
Expand All @@ -240,7 +244,6 @@

// Focus the window when the app opens
win.focus();

});

// Cancel all new windows (Middle clicks / New Tab)
Expand All @@ -250,7 +253,6 @@

App.vent.trigger('updatePostersSizeStylesheet');
App.vent.trigger('main:ready');

},

movieTabShow: function (e) {
Expand Down
8 changes: 8 additions & 0 deletions src/app/lib/views/settings_container.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@
case 'opensubtitlesAutoUpload':
case 'subtitles_bold':
case 'rememberFilters':
case 'autoSeed':
value = field.is(':checked');
break;
case 'httpApiUsername':
Expand Down Expand Up @@ -292,6 +293,13 @@

syncSetting: function (setting, value) {
switch (setting) {
case 'autoSeed':
if (value) {
App.vent.trigger('seed:start');
} else {
App.vent.trigger('seed:stop');
}
break;
case 'coversShowRating':
if (value) {
$('.rating').show();
Expand Down
116 changes: 116 additions & 0 deletions src/app/lib/workers/seederTask.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
'use strict';

var _ = require('underscore');
var fs = require('fs');
var path = require('path');
var WebTorrent = require('webtorrent');
var child = require('child_process');
var crypt = require('crypto');

var SeederTask = function (opt) {
this.webtorrent = null;
this.torrentFiles = null;
this.name = opt.name;
this.tmpLocation = opt.tmpLocation;
this.seedLimit = opt.seedLimit;
this.connectionLimit = opt.connectionLimit;
this.trackerAnnouncement = opt.trackerAnnouncement;
this.torrentDir = path.join(this.tmpLocation, 'TorrentCache');
};

SeederTask.prototype = {
start: function() {
if (this.webtorrent) {
this.stop();
}

process.send('Seeding started');

var seedTorrentFiles = this.seedLimit ?
_.sample(this.getTorrentFiles(), this.seedLimit) : this.getTorrentFiles();

seedTorrentFiles.forEach(this.joinSwarm.bind(this));
},

stop: function() {
if (this.webtorrent) {
this.webtorrent.destroy();
}

this.webtorrent = null;
this.torrentFiles = [];

process.send('Seeding stopped');
process.kill();
},

getTorrentFiles: function() {
if (this.torrentFiles === null) {
var regexp = /\.torrent$/i;
var files = fs.readdirSync(this.torrentDir);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of calling fs here, i think i'd rather maintain a registery of things that have been 'added/removed' to this module.


this.torrentFiles = files.filter(function(val) {
return regexp.test(val);
}).map(function(filename) {
return path.join(this.torrentDir, filename);
}, this);
}

return this.torrentFiles;
},

joinSwarm: function (torrentId) {
var client = this.getWebTorrentInstance();

if (!client.get(torrentId)) {
var torrent = client.add(torrentId, {
path: this.tmpLocation
});

torrent.on('ready', function () {
process.send(`Seeding ${torrent.name} --- ${torrentId}`);
});
}
},

append: function(name) {
var targetFile = path.join(this.torrentDir, this._md5(name) + '.torrent');

this.torrentFiles.push(targetFile);
this.seedLimit += 1;
this.joinSwarm(targetFile);
},

_md5: function (arg) {
return crypt.createHash('md5').update(arg).digest('hex');
},

getWebTorrentInstance: function() {
if (this.webtorrent === null) {
this.webtorrent = new WebTorrent({
maxConns: parseInt(this.connectionLimit, 10) || 55,
tracker: {
wrtc: false,
announce: this.trackerAnnouncement
}
});

this.webtorrent.on('error', function (error) {
process.send('WebTorrent fatal error', error);
});
}

return this.webtorrent;
}
};

var args = JSON.parse(process.argv[2]);
var seederTask = new SeederTask(args);

process.on('message', function (params) {
if (params.action && seederTask[params.action]) {
seederTask[params.action].call(seederTask, params.payload);
} else {
process.send(`Invalid seederTask function`);
}
});
2 changes: 2 additions & 0 deletions src/app/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ Settings.automaticUpdating = true;
Settings.events = true;
Settings.minimizeToTray = false;
Settings.bigPicture = false;
Settings.autoSeed = false;
Settings.seedLimit = 2;

// Features
Settings.activateTorrentCollection = false;
Expand Down
4 changes: 4 additions & 0 deletions src/app/templates/settings-container.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,10 @@
<input class="settings-checkbox" name="deleteTmpOnClose" id="cb2" type="checkbox" <%=(Settings.deleteTmpOnClose? "checked='checked'":"")%>>
<label class="settings-label" for="cb2"><%= i18n.__("Clear Tmp Folder after closing app?") %></label>
</span>
<span>
<input class="settings-checkbox" name="autoSeed" id="cb8" type="checkbox" <%=(Settings.autoSeed? "checked='checked'":"")%>>
<label class="settings-label" for="cb8"><%= i18n.__("Continue seeding from Tmp Folder") %></label>
</span>
</div>
</section>

Expand Down