]>
Commit | Line | Data |
---|---|---|
9f10b292 C |
1 | 'use strict' |
2 | ||
f0f5567b C |
3 | const config = require('config') |
4 | const ipc = require('node-ipc') | |
5 | const pathUtils = require('path') | |
6 | const spawn = require('electron-spawn') | |
9f10b292 | 7 | |
f0f5567b | 8 | const logger = require('../helpers/logger') |
9f10b292 | 9 | |
f0f5567b C |
10 | let host = config.get('webserver.host') |
11 | let port = config.get('webserver.port') | |
12 | let nodeKey = 'webtorrentnode' + port | |
13 | let processKey = 'webtorrentprocess' + port | |
9f10b292 C |
14 | ipc.config.silent = true |
15 | ipc.config.id = nodeKey | |
16 | ||
f0f5567b | 17 | const webtorrent = { |
9f10b292 C |
18 | add: add, |
19 | app: null, // Pid of the app | |
20 | create: create, | |
21 | remove: remove, | |
22 | seed: seed, | |
23 | silent: false // Useful for beautiful tests | |
24 | } | |
25 | ||
26 | function create (options, callback) { | |
27 | if (typeof options === 'function') { | |
28 | callback = options | |
29 | options = {} | |
c5a8be2b | 30 | } |
8c308c2b | 31 | |
9f10b292 C |
32 | // Override options |
33 | if (options.host) host = options.host | |
34 | if (options.port) { | |
35 | port = options.port | |
36 | nodeKey = 'webtorrentnode' + port | |
37 | processKey = 'webtorrentprocess' + port | |
38 | ipc.config.id = nodeKey | |
39 | } | |
8c308c2b | 40 | |
9f10b292 C |
41 | ipc.serve(function () { |
42 | if (!webtorrent.silent) logger.info('IPC server ready.') | |
8c308c2b | 43 | |
9f10b292 | 44 | // Run a timeout of 30s after which we exit the process |
f0f5567b | 45 | const timeout_webtorrent_process = setTimeout(function () { |
ac2f99eb | 46 | throw new Error('Timeout : cannot run the webtorrent process. Please ensure you have electron-prebuilt npm package installed with xvfb-run.') |
9f10b292 | 47 | }, 30000) |
8c308c2b | 48 | |
9f10b292 C |
49 | ipc.server.on(processKey + '.ready', function () { |
50 | if (!webtorrent.silent) logger.info('Webtorrent process ready.') | |
51 | clearTimeout(timeout_webtorrent_process) | |
52 | callback() | |
53 | }) | |
8c308c2b | 54 | |
9f10b292 | 55 | ipc.server.on(processKey + '.exception', function (data) { |
233d12d8 | 56 | throw new Error('Received exception error from webtorrent process : ' + data.exception) |
9f10b292 | 57 | }) |
8c308c2b | 58 | |
f0f5567b | 59 | const webtorrent_process = spawn(pathUtils.join(__dirname, 'webtorrentProcess.js'), host, port, { detached: true }) |
9f10b292 C |
60 | webtorrent_process.stderr.on('data', function (data) { |
61 | // logger.debug('Webtorrent process stderr: ', data.toString()) | |
62 | }) | |
8c308c2b | 63 | |
9f10b292 C |
64 | webtorrent_process.stdout.on('data', function (data) { |
65 | // logger.debug('Webtorrent process:', data.toString()) | |
c5a8be2b | 66 | }) |
8c308c2b | 67 | |
9f10b292 C |
68 | webtorrent.app = webtorrent_process |
69 | }) | |
8c308c2b | 70 | |
9f10b292 C |
71 | ipc.server.start() |
72 | } | |
73 | ||
74 | function seed (path, callback) { | |
f0f5567b C |
75 | const extension = pathUtils.extname(path) |
76 | const basename = pathUtils.basename(path, extension) | |
77 | const data = { | |
9f10b292 C |
78 | _id: basename, |
79 | args: { | |
80 | path: path | |
8c308c2b | 81 | } |
9f10b292 | 82 | } |
8c308c2b | 83 | |
9f10b292 | 84 | if (!webtorrent.silent) logger.debug('Node wants to seed %s.', data._id) |
8c308c2b | 85 | |
9f10b292 | 86 | // Finish signal |
f0f5567b | 87 | const event_key = nodeKey + '.seedDone.' + data._id |
9f10b292 C |
88 | ipc.server.on(event_key, function listener (received) { |
89 | if (!webtorrent.silent) logger.debug('Process seeded torrent %s.', received.magnetUri) | |
c5a8be2b | 90 | |
9f10b292 | 91 | // This is a fake object, we just use the magnetUri in this project |
f0f5567b | 92 | const torrent = { |
9f10b292 C |
93 | magnetURI: received.magnetUri |
94 | } | |
8c308c2b | 95 | |
9f10b292 C |
96 | ipc.server.off(event_key) |
97 | callback(torrent) | |
98 | }) | |
c5a8be2b | 99 | |
9f10b292 C |
100 | ipc.server.broadcast(processKey + '.seed', data) |
101 | } | |
c5a8be2b | 102 | |
9f10b292 | 103 | function add (magnetUri, callback) { |
f0f5567b | 104 | const data = { |
9f10b292 C |
105 | _id: magnetUri, |
106 | args: { | |
107 | magnetUri: magnetUri | |
8c308c2b | 108 | } |
9f10b292 | 109 | } |
8c308c2b | 110 | |
9f10b292 | 111 | if (!webtorrent.silent) logger.debug('Node wants to add ' + data._id) |
8c308c2b | 112 | |
9f10b292 | 113 | // Finish signal |
f0f5567b | 114 | const event_key = nodeKey + '.addDone.' + data._id |
9f10b292 C |
115 | ipc.server.on(event_key, function (received) { |
116 | if (!webtorrent.silent) logger.debug('Process added torrent.') | |
c5a8be2b | 117 | |
9f10b292 | 118 | // This is a fake object, we just use the magnetUri in this project |
f0f5567b | 119 | const torrent = { |
9f10b292 C |
120 | files: received.files |
121 | } | |
8c308c2b | 122 | |
9f10b292 C |
123 | ipc.server.off(event_key) |
124 | callback(torrent) | |
125 | }) | |
0ae2e7f7 | 126 | |
9f10b292 C |
127 | ipc.server.broadcast(processKey + '.add', data) |
128 | } | |
c5a8be2b | 129 | |
9f10b292 | 130 | function remove (magnetUri, callback) { |
f0f5567b | 131 | const data = { |
9f10b292 C |
132 | _id: magnetUri, |
133 | args: { | |
134 | magnetUri: magnetUri | |
c5a8be2b | 135 | } |
9f10b292 | 136 | } |
c5a8be2b | 137 | |
9f10b292 | 138 | if (!webtorrent.silent) logger.debug('Node wants to stop seeding %s.', data._id) |
c5a8be2b | 139 | |
9f10b292 | 140 | // Finish signal |
f0f5567b | 141 | const event_key = nodeKey + '.removeDone.' + data._id |
9f10b292 C |
142 | ipc.server.on(event_key, function (received) { |
143 | if (!webtorrent.silent) logger.debug('Process removed torrent %s.', data._id) | |
c5a8be2b | 144 | |
f0f5567b | 145 | let err = null |
9f10b292 | 146 | if (received.err) err = received.err |
c5a8be2b | 147 | |
9f10b292 C |
148 | ipc.server.off(event_key) |
149 | callback(err) | |
150 | }) | |
c5a8be2b | 151 | |
9f10b292 C |
152 | ipc.server.broadcast(processKey + '.remove', data) |
153 | } | |
c45f7f84 | 154 | |
9f10b292 | 155 | // --------------------------------------------------------------------------- |
c45f7f84 | 156 | |
9f10b292 | 157 | module.exports = webtorrent |