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