diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-07-26 22:30:46 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-07-27 21:21:57 +0200 |
commit | 052937db8a8d282eccdbdf38d487ed8d85d3c0a7 (patch) | |
tree | da84e08fbe3569c2122c9b733e22768980750744 /server/lib/webtorrent-process.js | |
parent | 71d3476b825571376b06498326d138685493fb62 (diff) | |
download | PeerTube-052937db8a8d282eccdbdf38d487ed8d85d3c0a7.tar.gz PeerTube-052937db8a8d282eccdbdf38d487ed8d85d3c0a7.tar.zst PeerTube-052937db8a8d282eccdbdf38d487ed8d85d3c0a7.zip |
First draft using only webseed for server
Diffstat (limited to 'server/lib/webtorrent-process.js')
-rw-r--r-- | server/lib/webtorrent-process.js | 92 |
1 files changed, 0 insertions, 92 deletions
diff --git a/server/lib/webtorrent-process.js b/server/lib/webtorrent-process.js deleted file mode 100644 index be7ac5bb4..000000000 --- a/server/lib/webtorrent-process.js +++ /dev/null | |||
@@ -1,92 +0,0 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const WebTorrent = require('webtorrent') | ||
4 | const ipc = require('node-ipc') | ||
5 | |||
6 | function webtorrent (args) { | ||
7 | if (args.length !== 3) { | ||
8 | throw new Error('Wrong arguments number: ' + args.length + '/3') | ||
9 | } | ||
10 | |||
11 | const host = args[1] | ||
12 | const port = args[2] | ||
13 | const nodeKey = 'webtorrentnode' + port | ||
14 | const processKey = 'webtorrentprocess' + port | ||
15 | |||
16 | ipc.config.silent = true | ||
17 | ipc.config.id = processKey | ||
18 | |||
19 | if (host === 'client' && port === '1') global.WEBTORRENT_ANNOUNCE = [] | ||
20 | else global.WEBTORRENT_ANNOUNCE = 'ws://' + host + ':' + port + '/tracker/socket' | ||
21 | const wt = new WebTorrent({ dht: false }) | ||
22 | |||
23 | function seed (data) { | ||
24 | const args = data.args | ||
25 | const path = args.path | ||
26 | const _id = data._id | ||
27 | |||
28 | wt.seed(path, { announceList: '' }, function (torrent) { | ||
29 | const toSend = { | ||
30 | magnetUri: torrent.magnetURI | ||
31 | } | ||
32 | |||
33 | ipc.of[nodeKey].emit(nodeKey + '.seedDone.' + _id, toSend) | ||
34 | }) | ||
35 | } | ||
36 | |||
37 | function add (data) { | ||
38 | const args = data.args | ||
39 | const magnetUri = args.magnetUri | ||
40 | const _id = data._id | ||
41 | |||
42 | wt.add(magnetUri, function (torrent) { | ||
43 | const toSend = { | ||
44 | files: [] | ||
45 | } | ||
46 | |||
47 | torrent.files.forEach(function (file) { | ||
48 | toSend.files.push({ path: file.path }) | ||
49 | }) | ||
50 | |||
51 | ipc.of[nodeKey].emit(nodeKey + '.addDone.' + _id, toSend) | ||
52 | }) | ||
53 | } | ||
54 | |||
55 | function remove (data) { | ||
56 | const args = data.args | ||
57 | const magnetUri = args.magnetUri | ||
58 | const _id = data._id | ||
59 | |||
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 () { | ||
68 | const toSend = {} | ||
69 | ipc.of[nodeKey].emit(nodeKey + '.removeDone.' + _id, toSend) | ||
70 | } | ||
71 | } | ||
72 | |||
73 | console.log('Configuration: ' + host + ':' + port) | ||
74 | console.log('Connecting to IPC...') | ||
75 | |||
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) | ||
80 | |||
81 | ipc.of[nodeKey].emit(processKey + '.ready') | ||
82 | console.log('Ready.') | ||
83 | }) | ||
84 | |||
85 | process.on('uncaughtException', function (e) { | ||
86 | ipc.of[nodeKey].emit(processKey + '.exception', { exception: e.toString() }) | ||
87 | }) | ||
88 | } | ||
89 | |||
90 | // --------------------------------------------------------------------------- | ||
91 | |||
92 | module.exports = webtorrent | ||