]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - src/webTorrentNode.js
4 var spawn
= require('electron-spawn')
5 var config
= require('config')
6 var ipc
= require('node-ipc')
7 var pathUtils
= require('path')
9 var logger
= require('./logger')
11 var host
= config
.get('webserver.host')
12 var port
= config
.get('webserver.port')
14 var nodeKey
= 'webtorrentnode' + port
15 var processKey
= 'webtorrent' + port
17 ipc
.config
.silent
= true
18 ipc
.config
.id
= nodeKey
20 var webtorrentnode
= {}
22 // Useful for beautiful tests
23 webtorrentnode
.silent
= false
26 webtorrentnode
.app
= null
28 webtorrentnode
.create = function (options
, callback
) {
29 if (typeof options
=== 'function') {
35 if (options
.host
) host
= options
.host
38 nodeKey
= 'webtorrentnode' + port
39 processKey
= 'webtorrent' + port
40 ipc
.config
.id
= nodeKey
43 ipc
.serve(function () {
44 if (!webtorrentnode
.silent
) logger
.info('IPC server ready.')
46 ipc
.server
.on(processKey
+ '.ready', function () {
47 if (!webtorrentnode
.silent
) logger
.info('Webtorrent process ready.')
51 var webtorrent_process
= spawn(__dirname
+ '/webtorrent.js', host
, port
, { detached: true })
52 webtorrent_process
.stderr
.on('data', function (data
) {
53 // logger.debug('Webtorrent process stderr: ', data.toString())
56 webtorrent_process
.stdout
.on('data', function (data
) {
57 // logger.debug('Webtorrent process:', data.toString())
60 function exitChildProcess () {
61 if (!webtorrentnode
.silent
) logger
.info('Gracefully exit child')
62 process
.kill(-webtorrent_process
.pid
)
66 process
.on('SIGINT', exitChildProcess
)
67 process
.on('SIGTERM', exitChildProcess
)
69 webtorrentnode
.app
= webtorrent_process
75 webtorrentnode
.seed = function (path
, callback
) {
76 var extension
= pathUtils
.extname(path
)
77 var basename
= pathUtils
.basename(path
, extension
)
85 if (!webtorrentnode
.silent
) logger
.debug('Node wants to seed ' + data
._id
)
88 ipc
.server
.on(nodeKey
+ '.seedDone.' + data
._id
, function (received
) {
89 if (!webtorrentnode
.silent
) logger
.debug('Process seeded torrent ' + received
.magnetUri
)
91 // This is a fake object, we just use the magnetUri in this project
93 magnetURI: received
.magnetUri
99 ipc
.server
.broadcast(processKey
+ '.seed', data
)
102 webtorrentnode
.add = function (magnetUri
, callback
) {
110 if (!webtorrentnode
.silent
) logger
.debug('Node wants to add ' + data
._id
)
113 ipc
.server
.on(nodeKey
+ '.addDone.' + data
._id
, function (received
) {
114 if (!webtorrentnode
.silent
) logger
.debug('Process added torrent')
116 // This is a fake object, we just use the magnetUri in this project
118 files: received
.files
124 ipc
.server
.broadcast(processKey
+ '.add', data
)
127 webtorrentnode
.remove = function (magnetUri
, callback
) {
135 if (!webtorrentnode
.silent
) logger
.debug('Node wants to stop seeding ' + data
._id
)
138 ipc
.server
.on(nodeKey
+ '.removeDone.' + data
._id
, function (received
) {
139 if (!webtorrentnode
.silent
) logger
.debug('Process removed torrent ' + data
._id
)
142 if (received
.err
) err
= received
.err
147 ipc
.server
.broadcast(processKey
+ '.remove', data
)
150 module
.exports
= webtorrentnode