2015-06-09 10:41:40 -05:00
|
|
|
;(function () {
|
|
|
|
'use strict'
|
|
|
|
|
|
|
|
var config = require('config')
|
|
|
|
var ipc = require('node-ipc')
|
|
|
|
var pathUtils = require('path')
|
2015-11-16 06:54:00 -06:00
|
|
|
var spawn = require('electron-spawn')
|
2015-06-09 10:41:40 -05:00
|
|
|
|
2016-01-30 10:05:22 -06:00
|
|
|
var logger = require('../helpers/logger')
|
2015-06-09 10:41:40 -05:00
|
|
|
|
2015-10-30 12:34:37 -05:00
|
|
|
var host = config.get('webserver.host')
|
|
|
|
var port = config.get('webserver.port')
|
2015-06-09 10:41:40 -05:00
|
|
|
var nodeKey = 'webtorrentnode' + port
|
|
|
|
var processKey = 'webtorrent' + port
|
|
|
|
ipc.config.silent = true
|
|
|
|
ipc.config.id = nodeKey
|
|
|
|
|
2016-01-31 04:23:52 -06:00
|
|
|
var webtorrentnode = {
|
|
|
|
add: add,
|
|
|
|
app: null, // Pid of the app
|
|
|
|
create: create,
|
|
|
|
remove: remove,
|
|
|
|
seed: seed,
|
|
|
|
silent: false // Useful for beautiful tests
|
|
|
|
}
|
2015-06-09 10:41:40 -05:00
|
|
|
|
2016-01-31 04:23:52 -06:00
|
|
|
function create (options, callback) {
|
2015-10-30 12:34:37 -05:00
|
|
|
if (typeof options === 'function') {
|
|
|
|
callback = options
|
|
|
|
options = {}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Override options
|
|
|
|
if (options.host) host = options.host
|
|
|
|
if (options.port) {
|
|
|
|
port = options.port
|
|
|
|
nodeKey = 'webtorrentnode' + port
|
|
|
|
processKey = 'webtorrent' + port
|
|
|
|
ipc.config.id = nodeKey
|
|
|
|
}
|
|
|
|
|
2015-06-09 10:41:40 -05:00
|
|
|
ipc.serve(function () {
|
|
|
|
if (!webtorrentnode.silent) logger.info('IPC server ready.')
|
|
|
|
|
2015-11-02 15:19:39 -06:00
|
|
|
// Run a timeout of 30s after which we exit the process
|
|
|
|
var timeout_webtorrent_process = setTimeout(function () {
|
|
|
|
logger.error('Timeout : cannot run the webtorrent process. Please ensure you have electron-prebuilt npm package installed with xvfb-run.')
|
|
|
|
process.exit()
|
|
|
|
}, 30000)
|
|
|
|
|
2015-06-09 10:41:40 -05:00
|
|
|
ipc.server.on(processKey + '.ready', function () {
|
|
|
|
if (!webtorrentnode.silent) logger.info('Webtorrent process ready.')
|
2015-11-02 15:19:39 -06:00
|
|
|
clearTimeout(timeout_webtorrent_process)
|
2015-06-09 10:41:40 -05:00
|
|
|
callback()
|
|
|
|
})
|
|
|
|
|
2015-11-02 15:19:39 -06:00
|
|
|
ipc.server.on(processKey + '.exception', function (data) {
|
|
|
|
logger.error('Received exception error from webtorrent process.', { exception: data.exception })
|
|
|
|
process.exit()
|
|
|
|
})
|
|
|
|
|
2015-06-09 10:41:40 -05:00
|
|
|
var webtorrent_process = spawn(__dirname + '/webtorrent.js', host, port, { detached: true })
|
|
|
|
webtorrent_process.stderr.on('data', function (data) {
|
|
|
|
// logger.debug('Webtorrent process stderr: ', data.toString())
|
|
|
|
})
|
|
|
|
|
|
|
|
webtorrent_process.stdout.on('data', function (data) {
|
|
|
|
// logger.debug('Webtorrent process:', data.toString())
|
|
|
|
})
|
|
|
|
|
|
|
|
webtorrentnode.app = webtorrent_process
|
|
|
|
})
|
|
|
|
|
|
|
|
ipc.server.start()
|
|
|
|
}
|
|
|
|
|
2016-01-31 04:23:52 -06:00
|
|
|
function seed (path, callback) {
|
2015-06-09 10:41:40 -05:00
|
|
|
var extension = pathUtils.extname(path)
|
|
|
|
var basename = pathUtils.basename(path, extension)
|
|
|
|
var data = {
|
|
|
|
_id: basename,
|
|
|
|
args: {
|
|
|
|
path: path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-16 06:54:00 -06:00
|
|
|
if (!webtorrentnode.silent) logger.debug('Node wants to seed %s.', data._id)
|
2015-06-09 10:41:40 -05:00
|
|
|
|
|
|
|
// Finish signal
|
2015-11-02 15:42:27 -06:00
|
|
|
var event_key = nodeKey + '.seedDone.' + data._id
|
|
|
|
ipc.server.on(event_key, function listener (received) {
|
2015-11-16 06:54:00 -06:00
|
|
|
if (!webtorrentnode.silent) logger.debug('Process seeded torrent %s.', received.magnetUri)
|
2015-06-09 10:41:40 -05:00
|
|
|
|
|
|
|
// This is a fake object, we just use the magnetUri in this project
|
|
|
|
var torrent = {
|
|
|
|
magnetURI: received.magnetUri
|
|
|
|
}
|
|
|
|
|
2015-11-02 15:42:27 -06:00
|
|
|
ipc.server.off(event_key)
|
2015-06-09 10:41:40 -05:00
|
|
|
callback(torrent)
|
|
|
|
})
|
|
|
|
|
|
|
|
ipc.server.broadcast(processKey + '.seed', data)
|
|
|
|
}
|
|
|
|
|
2016-01-31 04:23:52 -06:00
|
|
|
function add (magnetUri, callback) {
|
2015-06-09 10:41:40 -05:00
|
|
|
var data = {
|
|
|
|
_id: magnetUri,
|
|
|
|
args: {
|
|
|
|
magnetUri: magnetUri
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!webtorrentnode.silent) logger.debug('Node wants to add ' + data._id)
|
|
|
|
|
|
|
|
// Finish signal
|
2015-11-02 15:42:27 -06:00
|
|
|
var event_key = nodeKey + '.addDone.' + data._id
|
|
|
|
ipc.server.on(event_key, function (received) {
|
2015-11-16 06:54:00 -06:00
|
|
|
if (!webtorrentnode.silent) logger.debug('Process added torrent.')
|
2015-06-09 10:41:40 -05:00
|
|
|
|
|
|
|
// This is a fake object, we just use the magnetUri in this project
|
|
|
|
var torrent = {
|
|
|
|
files: received.files
|
|
|
|
}
|
|
|
|
|
2015-11-02 15:42:27 -06:00
|
|
|
ipc.server.off(event_key)
|
2015-06-09 10:41:40 -05:00
|
|
|
callback(torrent)
|
|
|
|
})
|
|
|
|
|
|
|
|
ipc.server.broadcast(processKey + '.add', data)
|
|
|
|
}
|
|
|
|
|
2016-01-31 04:23:52 -06:00
|
|
|
function remove (magnetUri, callback) {
|
2015-06-09 10:41:40 -05:00
|
|
|
var data = {
|
|
|
|
_id: magnetUri,
|
|
|
|
args: {
|
|
|
|
magnetUri: magnetUri
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-16 06:54:00 -06:00
|
|
|
if (!webtorrentnode.silent) logger.debug('Node wants to stop seeding %s.', data._id)
|
2015-06-09 10:41:40 -05:00
|
|
|
|
|
|
|
// Finish signal
|
2015-11-02 15:42:27 -06:00
|
|
|
var event_key = nodeKey + '.removeDone.' + data._id
|
|
|
|
ipc.server.on(event_key, function (received) {
|
2015-11-16 06:54:00 -06:00
|
|
|
if (!webtorrentnode.silent) logger.debug('Process removed torrent %s.', data._id)
|
2015-06-09 10:41:40 -05:00
|
|
|
|
|
|
|
var err = null
|
|
|
|
if (received.err) err = received.err
|
|
|
|
|
2015-11-02 15:42:27 -06:00
|
|
|
ipc.server.off(event_key)
|
2015-06-09 10:41:40 -05:00
|
|
|
callback(err)
|
|
|
|
})
|
|
|
|
|
|
|
|
ipc.server.broadcast(processKey + '.remove', data)
|
|
|
|
}
|
|
|
|
|
2016-01-31 04:23:52 -06:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2015-06-09 10:41:40 -05:00
|
|
|
module.exports = webtorrentnode
|
|
|
|
})()
|