]>
Commit | Line | Data |
---|---|---|
9f10b292 C |
1 | 'use strict' |
2 | ||
3 | var config = require('config') | |
4 | var ipc = require('node-ipc') | |
5 | var pathUtils = require('path') | |
6 | var spawn = require('electron-spawn') | |
7 | ||
8 | var logger = require('../helpers/logger') | |
9 | ||
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 | |
16 | ||
17 | var webtorrent = { | |
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 C |
44 | // Run a timeout of 30s after which we exit the process |
45 | var 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) { |
ac2f99eb | 56 | throw new Error('Received exception error from webtorrent process.' + data.exception) |
9f10b292 | 57 | }) |
8c308c2b | 58 | |
9f10b292 C |
59 | var webtorrent_process = spawn(pathUtils.join(__dirname, 'webtorrentProcess.js'), host, port, { detached: true }) |
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) { | |
75 | var extension = pathUtils.extname(path) | |
76 | var basename = pathUtils.basename(path, extension) | |
77 | var data = { | |
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 C |
86 | // Finish signal |
87 | var event_key = nodeKey + '.seedDone.' + data._id | |
88 | ipc.server.on(event_key, function listener (received) { | |
89 | if (!webtorrent.silent) logger.debug('Process seeded torrent %s.', received.magnetUri) | |
c5a8be2b | 90 | |
9f10b292 C |
91 | // This is a fake object, we just use the magnetUri in this project |
92 | var torrent = { | |
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 C |
103 | function add (magnetUri, callback) { |
104 | var data = { | |
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 C |
113 | // Finish signal |
114 | var event_key = nodeKey + '.addDone.' + data._id | |
115 | ipc.server.on(event_key, function (received) { | |
116 | if (!webtorrent.silent) logger.debug('Process added torrent.') | |
c5a8be2b | 117 | |
9f10b292 C |
118 | // This is a fake object, we just use the magnetUri in this project |
119 | var torrent = { | |
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 C |
130 | function remove (magnetUri, callback) { |
131 | var data = { | |
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 C |
140 | // Finish signal |
141 | var event_key = nodeKey + '.removeDone.' + data._id | |
142 | ipc.server.on(event_key, function (received) { | |
143 | if (!webtorrent.silent) logger.debug('Process removed torrent %s.', data._id) | |
c5a8be2b | 144 | |
9f10b292 C |
145 | var err = null |
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 |