]>
Commit | Line | Data |
---|---|---|
9f10b292 | 1 | 'use strict' |
c5a8be2b | 2 | |
f0f5567b C |
3 | const WebTorrent = require('webtorrent') |
4 | const ipc = require('node-ipc') | |
c5a8be2b | 5 | |
821b05ae | 6 | function webtorrent (args) { |
9f10b292 | 7 | if (args.length !== 3) { |
ac2f99eb | 8 | throw new Error('Wrong arguments number: ' + args.length + '/3') |
9f10b292 | 9 | } |
c5a8be2b | 10 | |
f0f5567b C |
11 | const host = args[1] |
12 | const port = args[2] | |
13 | const nodeKey = 'webtorrentnode' + port | |
14 | const processKey = 'webtorrentprocess' + port | |
c5a8be2b | 15 | |
9f10b292 C |
16 | ipc.config.silent = true |
17 | ipc.config.id = processKey | |
c5a8be2b | 18 | |
9f10b292 C |
19 | if (host === 'client' && port === '1') global.WEBTORRENT_ANNOUNCE = [] |
20 | else global.WEBTORRENT_ANNOUNCE = 'ws://' + host + ':' + port + '/tracker/socket' | |
f0f5567b | 21 | const wt = new WebTorrent({ dht: false }) |
c5a8be2b | 22 | |
9f10b292 | 23 | function seed (data) { |
f0f5567b C |
24 | const args = data.args |
25 | const path = args.path | |
26 | const _id = data._id | |
c5a8be2b | 27 | |
9f10b292 | 28 | wt.seed(path, { announceList: '' }, function (torrent) { |
bc503c2a | 29 | const toSend = { |
9f10b292 C |
30 | magnetUri: torrent.magnetURI |
31 | } | |
c5a8be2b | 32 | |
bc503c2a | 33 | ipc.of[nodeKey].emit(nodeKey + '.seedDone.' + _id, toSend) |
9f10b292 C |
34 | }) |
35 | } | |
c5a8be2b | 36 | |
9f10b292 | 37 | function add (data) { |
f0f5567b C |
38 | const args = data.args |
39 | const magnetUri = args.magnetUri | |
40 | const _id = data._id | |
c5a8be2b | 41 | |
9f10b292 | 42 | wt.add(magnetUri, function (torrent) { |
bc503c2a | 43 | const toSend = { |
9f10b292 C |
44 | files: [] |
45 | } | |
c5a8be2b | 46 | |
9f10b292 | 47 | torrent.files.forEach(function (file) { |
bc503c2a | 48 | toSend.files.push({ path: file.path }) |
c5a8be2b | 49 | }) |
c5a8be2b | 50 | |
bc503c2a | 51 | ipc.of[nodeKey].emit(nodeKey + '.addDone.' + _id, toSend) |
9f10b292 C |
52 | }) |
53 | } | |
c5a8be2b | 54 | |
9f10b292 | 55 | function remove (data) { |
f0f5567b C |
56 | const args = data.args |
57 | const magnetUri = args.magnetUri | |
58 | const _id = data._id | |
c5a8be2b | 59 | |
9f10b292 C |
60 | try { |
61 | wt.remove(magnetUri, callback) | |
62 | } catch (err) { | |
63 | console.log('Cannot remove the torrent from WebTorrent.') | |
64 | return callback(null) | |
65 | } | |
66 | ||
67 | function callback () { | |
bc503c2a C |
68 | const toSend = {} |
69 | ipc.of[nodeKey].emit(nodeKey + '.removeDone.' + _id, toSend) | |
c5a8be2b | 70 | } |
9f10b292 | 71 | } |
c5a8be2b | 72 | |
9f10b292 C |
73 | console.log('Configuration: ' + host + ':' + port) |
74 | console.log('Connecting to IPC...') | |
c5a8be2b | 75 | |
9f10b292 C |
76 | ipc.connectTo(nodeKey, function () { |
77 | ipc.of[nodeKey].on(processKey + '.seed', seed) | |
78 | ipc.of[nodeKey].on(processKey + '.add', add) | |
79 | ipc.of[nodeKey].on(processKey + '.remove', remove) | |
c5a8be2b | 80 | |
9f10b292 C |
81 | ipc.of[nodeKey].emit(processKey + '.ready') |
82 | console.log('Ready.') | |
83 | }) | |
c5a8be2b | 84 | |
9f10b292 | 85 | process.on('uncaughtException', function (e) { |
233d12d8 | 86 | ipc.of[nodeKey].emit(processKey + '.exception', { exception: e.toString() }) |
9f10b292 C |
87 | }) |
88 | } | |
c5a8be2b | 89 | |
9f10b292 | 90 | // --------------------------------------------------------------------------- |
c5a8be2b | 91 | |
9f10b292 | 92 | module.exports = webtorrent |