aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/webTorrentNode.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2015-06-09 17:41:40 +0200
committerChocobozzz <florian.bigard@gmail.com>2015-10-29 23:14:54 +0100
commit8c308c2bf7f658945d80be9d5880361238635f5b (patch)
tree2130ae60af58e59dab3df07a5d5cdd5174f91ae8 /src/webTorrentNode.js
parent8cb4b4e00ee57eb98dfe1455b6d2de36fc561797 (diff)
downloadPeerTube-8c308c2bf7f658945d80be9d5880361238635f5b.tar.gz
PeerTube-8c308c2bf7f658945d80be9d5880361238635f5b.tar.zst
PeerTube-8c308c2bf7f658945d80be9d5880361238635f5b.zip
Spawn
Diffstat (limited to 'src/webTorrentNode.js')
-rw-r--r--src/webTorrentNode.js145
1 files changed, 145 insertions, 0 deletions
diff --git a/src/webTorrentNode.js b/src/webTorrentNode.js
new file mode 100644
index 000000000..03bff7803
--- /dev/null
+++ b/src/webTorrentNode.js
@@ -0,0 +1,145 @@
1;(function () {
2 'use strict'
3
4 var spawn = require('electron-spawn')
5 var config = require('config')
6 var ipc = require('node-ipc')
7 var pathUtils = require('path')
8
9 var logger = require('./logger')
10
11 var host
12 var port
13
14 try {
15 host = config.get('webserver.host')
16 port = config.get('webserver.port')
17 } catch (e) {
18 host = 'client'
19 port = 1
20 }
21
22 var nodeKey = 'webtorrentnode' + port
23 var processKey = 'webtorrent' + port
24
25 ipc.config.silent = true
26 ipc.config.id = nodeKey
27
28 var webtorrentnode = {}
29
30 // Useful for beautiful tests
31 webtorrentnode.silent = false
32
33 // Useful to kill it
34 webtorrentnode.app = null
35
36 webtorrentnode.create = function (callback) {
37 ipc.serve(function () {
38 if (!webtorrentnode.silent) logger.info('IPC server ready.')
39
40 ipc.server.on(processKey + '.ready', function () {
41 if (!webtorrentnode.silent) logger.info('Webtorrent process ready.')
42 callback()
43 })
44
45 var webtorrent_process = spawn(__dirname + '/webtorrent.js', host, port, { detached: true })
46 webtorrent_process.stderr.on('data', function (data) {
47 // logger.debug('Webtorrent process stderr: ', data.toString())
48 })
49
50 webtorrent_process.stdout.on('data', function (data) {
51 // logger.debug('Webtorrent process:', data.toString())
52 })
53
54 function exitChildProcess () {
55 if (!webtorrentnode.silent) logger.info('Gracefully exit child')
56 process.kill(-webtorrent_process.pid)
57 process.exit(0)
58 }
59
60 process.on('SIGINT', exitChildProcess)
61 process.on('SIGTERM', exitChildProcess)
62
63 webtorrentnode.app = webtorrent_process
64 })
65
66 ipc.server.start()
67 }
68
69 webtorrentnode.seed = function (path, callback) {
70 var extension = pathUtils.extname(path)
71 var basename = pathUtils.basename(path, extension)
72 var data = {
73 _id: basename,
74 args: {
75 path: path
76 }
77 }
78
79 if (!webtorrentnode.silent) logger.debug('Node wants to seed ' + data._id)
80
81 // Finish signal
82 ipc.server.on(nodeKey + '.seedDone.' + data._id, function (received) {
83 if (!webtorrentnode.silent) logger.debug('Process seeded torrent ' + received.magnetUri)
84
85 // This is a fake object, we just use the magnetUri in this project
86 var torrent = {
87 magnetURI: received.magnetUri
88 }
89
90 callback(torrent)
91 })
92
93 ipc.server.broadcast(processKey + '.seed', data)
94 }
95
96 webtorrentnode.add = function (magnetUri, callback) {
97 var data = {
98 _id: magnetUri,
99 args: {
100 magnetUri: magnetUri
101 }
102 }
103
104 if (!webtorrentnode.silent) logger.debug('Node wants to add ' + data._id)
105
106 // Finish signal
107 ipc.server.on(nodeKey + '.addDone.' + data._id, function (received) {
108 if (!webtorrentnode.silent) logger.debug('Process added torrent')
109
110 // This is a fake object, we just use the magnetUri in this project
111 var torrent = {
112 files: received.files
113 }
114
115 callback(torrent)
116 })
117
118 ipc.server.broadcast(processKey + '.add', data)
119 }
120
121 webtorrentnode.remove = function (magnetUri, callback) {
122 var data = {
123 _id: magnetUri,
124 args: {
125 magnetUri: magnetUri
126 }
127 }
128
129 if (!webtorrentnode.silent) logger.debug('Node wants to stop seeding ' + data._id)
130
131 // Finish signal
132 ipc.server.on(nodeKey + '.removeDone.' + data._id, function (received) {
133 if (!webtorrentnode.silent) logger.debug('Process removed torrent ' + data._id)
134
135 var err = null
136 if (received.err) err = received.err
137
138 callback(err)
139 })
140
141 ipc.server.broadcast(processKey + '.remove', data)
142 }
143
144 module.exports = webtorrentnode
145})()