forked from AraBlocks/ara-reward-dcdn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
140 lines (128 loc) · 2.88 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const extend = require('extend')
const DCDN = require('./src/dcdn')
const rc = require('ara-runtime-configuration')()
const debug = require('debug')('ard')
let instance = null
/**
* Start broadcasting services for an ara-network node for DCDN
* @public
* @param {Object} argv
* @return {Boolean}
*/
async function start(argv = {}) {
if (!instance) instance = new DCDN({ userId: argv.identity, password: argv.password })
const { did } = argv
if (did) {
await instance.join({
did: argv.did,
download: argv.download,
upload: argv.upload,
metaOnly: argv['meta-only'],
price: argv.reward,
maxPeers: argv.peers,
jobId: argv['job-id']
})
} else {
await instance.start()
}
process.on('SIGINT', async () => {
debug('process interrupted...')
await instance.stop()
process.exit()
})
return true
}
/**
* Stop the ara-network node of DCDN
* @public
* @return {null}
*/
async function stop(argv = {}) {
if (!instance) return false
const { did } = argv
if (did) {
await instance.unjoin({ did })
} else {
await instance.stop()
instance = null
}
return true
}
/**
* Configures the ara-network DCDN node
* @public
* @param {Object} opts
* @return Object
*/
async function configure(argv, program) {
if (program) {
const { argv: _argv } = program
.option('help', {
alias: 'h',
describe: 'Show this help message'
})
.option('debug', {
alias: 'D',
describe: 'Enable debug output'
})
.option('did', {
alias: 'd',
describe: 'The identity of the AFS of interest',
default: null
})
.option('reward', {
alias: 'r',
describe: '`The maximum reward for the AFS',
default: null
})
.option('peers', {
alias: 'p',
describe: 'The maximum number of simulataneous peers per AFS',
default: null
})
.option('job-id', {
describe: 'The job Id for the AFS',
default: null
})
.option('meta-only', {
describe: 'Whether to only upload/download the metadata',
default: false
})
.option('download', {
describe: 'Whether the node should download',
default: false
})
.option('upload', {
describe: 'Whether the node should upload',
default: false
})
.option('identity', {
alias: 'i',
describe: 'Your Ara identity',
default: rc.network.identity.whoami
})
// eslint-disable-next-line no-param-reassign
argv = extend(true, argv, _argv)
}
}
/**
* Gets the DCDN object
*
* @return {Object}
*/
async function getInstance() {
return instance
}
/**
* Sets the DCDN object
*/
async function setInstance(obj) {
if (obj instanceof DCDN) instance = obj
}
module.exports = {
getInstance,
setInstance,
configure,
start,
stop
}