-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathheartbeat.js
More file actions
29 lines (28 loc) · 1.09 KB
/
heartbeat.js
File metadata and controls
29 lines (28 loc) · 1.09 KB
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
const {check} = require('./utils');
const fs = require('fs');
module.exports.heartbeat = function (id, address, res) {
console.log(`[HEARTBEAT] New node tried to connect: ${address} [${id}]`);
check(address, function (status, host, e=null){
console.log(status, host);
if(status==1) {
fs.readFile('./nodes.txt', 'utf8' , (err, data) => {
if (err) {console.error(err);return}
if(data.includes(host)) {return;} else {
fs.writeFile('./nodes.txt', `${host}\n`, { flag: 'a+' }, err => {
if (err) {console.error(err);return}
})
}
})
res.send({
'type': 'ok',
'message': `Node ID:${id} with address ${address} added to queue`
})
} else {
res.send({
'type': 'error',
'message': `Node ID:${id} with address ${address} not respond to head correctly`,
'debug': JSON.stringify(e)
})
}
});
}