-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
82 lines (69 loc) · 2.27 KB
/
index.js
File metadata and controls
82 lines (69 loc) · 2.27 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
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
'use strict';
const express = require('express');
const io = require('socket.io');
const axios = require('axios');
const port = process.env[2] || 5000;
// Socket.io Implementation
const app = express();
const http = require('http').Server(app);
let socket = io(http);
// App
app.use(express.static(__dirname + "/public"));
let baseServerUrl = 'http://worldtimeapi.org/api/timezone/America/Bogota';
let nodes = [];
let clients, master_time, synchronized_time;
let allDifferencesSum, average_time = 0;
// Socket Listener
socket.on('connection', node => {
console.log('Node connected:', node.id);
node.join('time room');
clients = socket.sockets.adapter.rooms['time room'];
node.on('datetime', time => {
nodes.push({ id: node.id, time: new Date(time) });
if (nodes.length === clients.length) {
berkeleyAlgorithm();
node.emit('synchronize', synchronized_time);
node.broadcast.emit('synchronize', synchronized_time);
}
});
node.on('disconnect', () => {
node.leave('time room', '');
deleteDisconnectedNode(node);
});
});
async function getServerDate() {
const request = await axios.get(baseServerUrl);
const response = await request.data;
return response.datetime;
}
function berkeleyAlgorithm() {
nodes.forEach(slave => {
let time_difference = master_time.getTime() - slave.time.getTime();
allDifferencesSum += time_difference;
});
average_time = allDifferencesSum / nodes.length;
synchronized_time = master_time.getTime() + average_time;
console.log(`New server time: ${new Date(synchronized_time).toLocaleTimeString()}`);
}
function deleteDisconnectedNode(node) {
console.log('Node disconnected:', node.id);
let index = nodes.map(object => {
return object.id;
}).indexOf(node.id);
nodes.splice(index, 1);
}
function resetAlgorithmTime() {
nodes = [];
allDifferencesSum = 0;
average_time = 0;
}
setInterval(async () => {
await resetAlgorithmTime();
await getServerDate()
.then(datetime => master_time = new Date(datetime))
.catch(err => {console.log(err.Error)});
await socket.to('time room').emit('request_time');
}, 60000);
http.listen(port, () => {
console.log('Running on Port: ' + port);
});