4 function webtorrent (args) {
5 var WebTorrent = require('webtorrent')
6 var ipc = require('node-ipc')
8 if (args.length !== 3) {
9 console.log('Wrong arguments number: ' + args.length + '/3')
15 var nodeKey = 'webtorrentnode' + port
16 var processKey = 'webtorrent' + port
18 ipc.config.silent = true
19 ipc.config.id = processKey
21 if (host === 'client' && port === '1') global.WEBTORRENT_ANNOUNCE = []
22 else global.WEBTORRENT_ANNOUNCE = 'ws://' + host + ':' + port + '/tracker/socket'
23 var wt = new WebTorrent({ dht: false })
25 function seed (data) {
30 wt.seed(path, { announceList: '' }, function (torrent) {
32 magnetUri: torrent.magnetURI
35 ipc.of[nodeKey].emit(nodeKey + '.seedDone.' + _id, to_send)
41 var magnetUri = args.magnetUri
44 wt.add(magnetUri, function (torrent) {
49 torrent.files.forEach(function (file) {
50 to_send.files.push({ path: file.path })
53 ipc.of[nodeKey].emit(nodeKey + '.addDone.' + _id, to_send)
57 function remove (data) {
59 var magnetUri = args.magnetUri
63 wt.remove(magnetUri, callback)
65 console.log('Cannot remove the torrent from WebTorrent', { err: err })
69 function callback () {
71 ipc.of[nodeKey].emit(nodeKey + '.removeDone.' + _id, to_send)
75 console.log('Configuration: ' + host + ':' + port)
76 console.log('Connecting to IPC...')
78 ipc.connectTo(nodeKey, function () {
79 ipc.of[nodeKey].on(processKey + '.seed', seed)
80 ipc.of[nodeKey].on(processKey + '.add', add)
81 ipc.of[nodeKey].on(processKey + '.remove', remove)
83 ipc.of[nodeKey].emit(processKey + '.ready')
87 process.on('uncaughtException', function (e) {
88 ipc.of[nodeKey].emit(processKey + '.exception', { exception: e })
92 // ---------------------------------------------------------------------------
94 module.exports = webtorrent