3 var config
= require('config')
4 var ipc
= require('node-ipc')
5 var pathUtils
= require('path')
6 var spawn
= require('electron-spawn')
8 var logger
= require('../helpers/logger')
10 var host
= config
.get('webserver.host')
11 var port
= config
.get('webserver.port')
12 var nodeKey
= 'webtorrentnode' + port
13 var processKey
= 'webtorrentprocess' + port
14 ipc
.config
.silent
= true
15 ipc
.config
.id
= nodeKey
19 app: null, // Pid of the app
23 silent: false // Useful for beautiful tests
26 function create (options
, callback
) {
27 if (typeof options
=== 'function') {
33 if (options
.host
) host
= options
.host
36 nodeKey
= 'webtorrentnode' + port
37 processKey
= 'webtorrentprocess' + port
38 ipc
.config
.id
= nodeKey
41 ipc
.serve(function () {
42 if (!webtorrent
.silent
) logger
.info('IPC server ready.')
44 // Run a timeout of 30s after which we exit the process
45 var timeout_webtorrent_process
= setTimeout(function () {
46 logger
.error('Timeout : cannot run the webtorrent process. Please ensure you have electron-prebuilt npm package installed with xvfb-run.')
50 ipc
.server
.on(processKey
+ '.ready', function () {
51 if (!webtorrent
.silent
) logger
.info('Webtorrent process ready.')
52 clearTimeout(timeout_webtorrent_process
)
56 ipc
.server
.on(processKey
+ '.exception', function (data
) {
57 logger
.error('Received exception error from webtorrent process.', { exception: data
.exception
})
61 var webtorrent_process
= spawn(pathUtils
.join(__dirname
, 'webtorrentProcess.js'), host
, port
, { detached: true })
62 webtorrent_process
.stderr
.on('data', function (data
) {
63 // logger.debug('Webtorrent process stderr: ', data.toString())
66 webtorrent_process
.stdout
.on('data', function (data
) {
67 // logger.debug('Webtorrent process:', data.toString())
70 webtorrent
.app
= webtorrent_process
76 function seed (path
, callback
) {
77 var extension
= pathUtils
.extname(path
)
78 var basename
= pathUtils
.basename(path
, extension
)
86 if (!webtorrent
.silent
) logger
.debug('Node wants to seed %s.', data
._id
)
89 var event_key
= nodeKey
+ '.seedDone.' + data
._id
90 ipc
.server
.on(event_key
, function listener (received
) {
91 if (!webtorrent
.silent
) logger
.debug('Process seeded torrent %s.', received
.magnetUri
)
93 // This is a fake object, we just use the magnetUri in this project
95 magnetURI: received
.magnetUri
98 ipc
.server
.off(event_key
)
102 ipc
.server
.broadcast(processKey
+ '.seed', data
)
105 function add (magnetUri
, callback
) {
113 if (!webtorrent
.silent
) logger
.debug('Node wants to add ' + data
._id
)
116 var event_key
= nodeKey
+ '.addDone.' + data
._id
117 ipc
.server
.on(event_key
, function (received
) {
118 if (!webtorrent
.silent
) logger
.debug('Process added torrent.')
120 // This is a fake object, we just use the magnetUri in this project
122 files: received
.files
125 ipc
.server
.off(event_key
)
129 ipc
.server
.broadcast(processKey
+ '.add', data
)
132 function remove (magnetUri
, callback
) {
140 if (!webtorrent
.silent
) logger
.debug('Node wants to stop seeding %s.', data
._id
)
143 var event_key
= nodeKey
+ '.removeDone.' + data
._id
144 ipc
.server
.on(event_key
, function (received
) {
145 if (!webtorrent
.silent
) logger
.debug('Process removed torrent %s.', data
._id
)
148 if (received
.err
) err
= received
.err
150 ipc
.server
.off(event_key
)
154 ipc
.server
.broadcast(processKey
+ '.remove', data
)
157 // ---------------------------------------------------------------------------
159 module
.exports
= webtorrent