4 var config = require('config')
5 var ipc = require('node-ipc')
6 var pathUtils = require('path')
7 var spawn = require('electron-spawn')
9 var logger = require('../helpers/logger')
11 var host = config.get('webserver.host')
12 var port = config.get('webserver.port')
13 var nodeKey = 'webtorrentnode' + port
14 var processKey = 'webtorrent' + port
15 ipc.config.silent = true
16 ipc.config.id = nodeKey
18 var webtorrentnode = {
20 app: null, // Pid of the app
24 silent: false // Useful for beautiful tests
27 function create (options, callback) {
28 if (typeof options === 'function') {
34 if (options.host) host = options.host
37 nodeKey = 'webtorrentnode' + port
38 processKey = 'webtorrent' + port
39 ipc.config.id = nodeKey
42 ipc.serve(function () {
43 if (!webtorrentnode.silent) logger.info('IPC server ready.')
45 // Run a timeout of 30s after which we exit the process
46 var timeout_webtorrent_process = setTimeout(function () {
47 logger.error('Timeout : cannot run the webtorrent process. Please ensure you have electron-prebuilt npm package installed with xvfb-run.')
51 ipc.server.on(processKey + '.ready', function () {
52 if (!webtorrentnode.silent) logger.info('Webtorrent process ready.')
53 clearTimeout(timeout_webtorrent_process)
57 ipc.server.on(processKey + '.exception', function (data) {
58 logger.error('Received exception error from webtorrent process.', { exception: data.exception })
62 var webtorrent_process = spawn(__dirname + '/webtorrent.js', host, port, { detached: true })
63 webtorrent_process.stderr.on('data', function (data) {
64 // logger.debug('Webtorrent process stderr: ', data.toString())
67 webtorrent_process.stdout.on('data', function (data) {
68 // logger.debug('Webtorrent process:', data.toString())
71 webtorrentnode.app = webtorrent_process
77 function seed (path, callback) {
78 var extension = pathUtils.extname(path)
79 var basename = pathUtils.basename(path, extension)
87 if (!webtorrentnode.silent) logger.debug('Node wants to seed %s.', data._id)
90 var event_key = nodeKey + '.seedDone.' + data._id
91 ipc.server.on(event_key, function listener (received) {
92 if (!webtorrentnode.silent) logger.debug('Process seeded torrent %s.', received.magnetUri)
94 // This is a fake object, we just use the magnetUri in this project
96 magnetURI: received.magnetUri
99 ipc.server.off(event_key)
103 ipc.server.broadcast(processKey + '.seed', data)
106 function add (magnetUri, callback) {
114 if (!webtorrentnode.silent) logger.debug('Node wants to add ' + data._id)
117 var event_key = nodeKey + '.addDone.' + data._id
118 ipc.server.on(event_key, function (received) {
119 if (!webtorrentnode.silent) logger.debug('Process added torrent.')
121 // This is a fake object, we just use the magnetUri in this project
123 files: received.files
126 ipc.server.off(event_key)
130 ipc.server.broadcast(processKey + '.add', data)
133 function remove (magnetUri, callback) {
141 if (!webtorrentnode.silent) logger.debug('Node wants to stop seeding %s.', data._id)
144 var event_key = nodeKey + '.removeDone.' + data._id
145 ipc.server.on(event_key, function (received) {
146 if (!webtorrentnode.silent) logger.debug('Process removed torrent %s.', data._id)
149 if (received.err) err = received.err
151 ipc.server.off(event_key)
155 ipc.server.broadcast(processKey + '.remove', data)
158 // ---------------------------------------------------------------------------
160 module.exports = webtorrentnode