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