aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/webTorrentNode.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/webTorrentNode.js')
-rw-r--r--src/webTorrentNode.js160
1 files changed, 0 insertions, 160 deletions
diff --git a/src/webTorrentNode.js b/src/webTorrentNode.js
deleted file mode 100644
index d6801d0fb..000000000
--- a/src/webTorrentNode.js
+++ /dev/null
@@ -1,160 +0,0 @@
1;(function () {
2 'use strict'
3
4 var config = require('config')
5 var ipc = require('node-ipc')
6 var pathUtils = require('path')
7 var spawn = require('electron-spawn')
8
9 var logger = require('./logger')
10
11 var host = config.get('webserver.host')
12 var port = config.get('webserver.port')
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
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
43 ipc.serve(function () {
44 if (!webtorrentnode.silent) logger.info('IPC server ready.')
45
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
52 ipc.server.on(processKey + '.ready', function () {
53 if (!webtorrentnode.silent) logger.info('Webtorrent process ready.')
54 clearTimeout(timeout_webtorrent_process)
55 callback()
56 })
57
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
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
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
88 if (!webtorrentnode.silent) logger.debug('Node wants to seed %s.', data._id)
89
90 // Finish signal
91 var event_key = nodeKey + '.seedDone.' + data._id
92 ipc.server.on(event_key, function listener (received) {
93 if (!webtorrentnode.silent) logger.debug('Process seeded torrent %s.', received.magnetUri)
94
95 // This is a fake object, we just use the magnetUri in this project
96 var torrent = {
97 magnetURI: received.magnetUri
98 }
99
100 ipc.server.off(event_key)
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
118 var event_key = nodeKey + '.addDone.' + data._id
119 ipc.server.on(event_key, function (received) {
120 if (!webtorrentnode.silent) logger.debug('Process added torrent.')
121
122 // This is a fake object, we just use the magnetUri in this project
123 var torrent = {
124 files: received.files
125 }
126
127 ipc.server.off(event_key)
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
142 if (!webtorrentnode.silent) logger.debug('Node wants to stop seeding %s.', data._id)
143
144 // Finish signal
145 var event_key = nodeKey + '.removeDone.' + data._id
146 ipc.server.on(event_key, function (received) {
147 if (!webtorrentnode.silent) logger.debug('Process removed torrent %s.', data._id)
148
149 var err = null
150 if (received.err) err = received.err
151
152 ipc.server.off(event_key)
153 callback(err)
154 })
155
156 ipc.server.broadcast(processKey + '.remove', data)
157 }
158
159 module.exports = webtorrentnode
160})()