-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.js
56 lines (44 loc) · 1.25 KB
/
client.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
const { Server } = require('./kademlia/network');
const { logger } = require('./logger');
class App {
constructor(SN, interval = 20000, debug = "hello world") {
if (!SN) {
logger.error('Device SN is NULL');
process.exit(0);
}
this.SN = SN;
this.interval = interval;
this.debug = debug;
this.node = new Server(8);
this.timer = null;
this.init();
}
stop() {
node.stop();
if (this.timer) {
clearTimeout(this.timer);
}
}
async init() {
try {
let ok = await this.node.listen(13001);
logger.info('node init %s', ok);
let router = await this.node.bootstrap([[ '0.0.0.0', 13001 ]]);
logger.info('node neighbors: %j', router);
this.keepalive();
} catch (err) {
logger.error(err);
this.stop();
}
}
async keepalive() {
let key = this.SN;
let val = Buffer.from(this.debug);
await this.node.set(key, val).catch(err => {
logger.error(err);
});
let f = this.keepalive.bind(this);
this.timer = setTimeout(f, this.interval);
}
}
new App(...process.argv.slice(2));