aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib/webtorrentProcess.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/webtorrentProcess.js')
-rw-r--r--lib/webtorrentProcess.js144
1 files changed, 71 insertions, 73 deletions
diff --git a/lib/webtorrentProcess.js b/lib/webtorrentProcess.js
index 7dc655f10..96ebf9d02 100644
--- a/lib/webtorrentProcess.js
+++ b/lib/webtorrentProcess.js
@@ -1,95 +1,93 @@
1;(function () { 1'use strict'
2 'use strict'
3 2
4 function webtorrent (args) { 3function webtorrent (args) {
5 var WebTorrent = require('webtorrent') 4 var WebTorrent = require('webtorrent')
6 var ipc = require('node-ipc') 5 var ipc = require('node-ipc')
7 6
8 if (args.length !== 3) { 7 if (args.length !== 3) {
9 console.log('Wrong arguments number: ' + args.length + '/3') 8 console.log('Wrong arguments number: ' + args.length + '/3')
10 process.exit(-1) 9 process.exit(-1)
11 } 10 }
12
13 var host = args[1]
14 var port = args[2]
15 var nodeKey = 'webtorrentnode' + port
16 var processKey = 'webtorrentprocess' + port
17 11
18 ipc.config.silent = true 12 var host = args[1]
19 ipc.config.id = processKey 13 var port = args[2]
14 var nodeKey = 'webtorrentnode' + port
15 var processKey = 'webtorrentprocess' + port
20 16
21 if (host === 'client' && port === '1') global.WEBTORRENT_ANNOUNCE = [] 17 ipc.config.silent = true
22 else global.WEBTORRENT_ANNOUNCE = 'ws://' + host + ':' + port + '/tracker/socket' 18 ipc.config.id = processKey
23 var wt = new WebTorrent({ dht: false })
24 19
25 function seed (data) { 20 if (host === 'client' && port === '1') global.WEBTORRENT_ANNOUNCE = []
26 var args = data.args 21 else global.WEBTORRENT_ANNOUNCE = 'ws://' + host + ':' + port + '/tracker/socket'
27 var path = args.path 22 var wt = new WebTorrent({ dht: false })
28 var _id = data._id
29 23
30 wt.seed(path, { announceList: '' }, function (torrent) { 24 function seed (data) {
31 var to_send = { 25 var args = data.args
32 magnetUri: torrent.magnetURI 26 var path = args.path
33 } 27 var _id = data._id
34 28
35 ipc.of[nodeKey].emit(nodeKey + '.seedDone.' + _id, to_send) 29 wt.seed(path, { announceList: '' }, function (torrent) {
36 }) 30 var to_send = {
37 } 31 magnetUri: torrent.magnetURI
32 }
38 33
39 function add (data) { 34 ipc.of[nodeKey].emit(nodeKey + '.seedDone.' + _id, to_send)
40 var args = data.args 35 })
41 var magnetUri = args.magnetUri 36 }
42 var _id = data._id
43 37
44 wt.add(magnetUri, function (torrent) { 38 function add (data) {
45 var to_send = { 39 var args = data.args
46 files: [] 40 var magnetUri = args.magnetUri
47 } 41 var _id = data._id
48 42
49 torrent.files.forEach(function (file) { 43 wt.add(magnetUri, function (torrent) {
50 to_send.files.push({ path: file.path }) 44 var to_send = {
51 }) 45 files: []
46 }
52 47
53 ipc.of[nodeKey].emit(nodeKey + '.addDone.' + _id, to_send) 48 torrent.files.forEach(function (file) {
49 to_send.files.push({ path: file.path })
54 }) 50 })
55 }
56 51
57 function remove (data) { 52 ipc.of[nodeKey].emit(nodeKey + '.addDone.' + _id, to_send)
58 var args = data.args 53 })
59 var magnetUri = args.magnetUri 54 }
60 var _id = data._id
61 55
62 try { 56 function remove (data) {
63 wt.remove(magnetUri, callback) 57 var args = data.args
64 } catch (err) { 58 var magnetUri = args.magnetUri
65 console.log('Cannot remove the torrent from WebTorrent.') 59 var _id = data._id
66 return callback(null)
67 }
68 60
69 function callback () { 61 try {
70 var to_send = {} 62 wt.remove(magnetUri, callback)
71 ipc.of[nodeKey].emit(nodeKey + '.removeDone.' + _id, to_send) 63 } catch (err) {
72 } 64 console.log('Cannot remove the torrent from WebTorrent.')
65 return callback(null)
66 }
67
68 function callback () {
69 var to_send = {}
70 ipc.of[nodeKey].emit(nodeKey + '.removeDone.' + _id, to_send)
73 } 71 }
72 }
74 73
75 console.log('Configuration: ' + host + ':' + port) 74 console.log('Configuration: ' + host + ':' + port)
76 console.log('Connecting to IPC...') 75 console.log('Connecting to IPC...')
77 76
78 ipc.connectTo(nodeKey, function () { 77 ipc.connectTo(nodeKey, function () {
79 ipc.of[nodeKey].on(processKey + '.seed', seed) 78 ipc.of[nodeKey].on(processKey + '.seed', seed)
80 ipc.of[nodeKey].on(processKey + '.add', add) 79 ipc.of[nodeKey].on(processKey + '.add', add)
81 ipc.of[nodeKey].on(processKey + '.remove', remove) 80 ipc.of[nodeKey].on(processKey + '.remove', remove)
82 81
83 ipc.of[nodeKey].emit(processKey + '.ready') 82 ipc.of[nodeKey].emit(processKey + '.ready')
84 console.log('Ready.') 83 console.log('Ready.')
85 }) 84 })
86 85
87 process.on('uncaughtException', function (e) { 86 process.on('uncaughtException', function (e) {
88 ipc.of[nodeKey].emit(processKey + '.exception', { exception: e }) 87 ipc.of[nodeKey].emit(processKey + '.exception', { exception: e })
89 }) 88 })
90 } 89}
91 90
92 // --------------------------------------------------------------------------- 91// ---------------------------------------------------------------------------
93 92
94 module.exports = webtorrent 93module.exports = webtorrent
95})()