/
index.js
157 lines (140 loc) · 5.28 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const colors = require('colors');
const socketServer = require('socket.io');
class MonitoringHub {
constructor(config) {
this.hubConfig = Object.assign({
hubPort: 8082,
}, config);
}
log(message, attributes, isError) {
const logTag = 'HUB';
let text = colors.yellow(`[${logTag}]`);
if (isError) {
text += ` ${colors.yellow('[ERROR]')}`;
}
text += ` ${message}`;
if (attributes) {
text += ` ${colors.green(JSON.stringify(attributes))}`;
}
console.log(text);
}
start() {
const observers = [];
const metrics = [];
// hub server
this.log('Starting hub');
const hubServer = socketServer.listen(this.hubConfig.hubPort, {
log: false,
});
hubServer.on('connection', (socket) => {
let connectionInfo = {
id: socket.id,
address: socket.handshake.address.replace('::1', '127.0.0.1').replace('::ffff:', ''),
};
let connection = {
metrics: [],
observers: [],
};
this.log('New connection', connectionInfo);
socket.on('registerMetric', (metricDescriptor) => {
if (!metrics[metricDescriptor.metricInfo.metricUid]) {
metricDescriptor.sensorInfo.sensorId = connectionInfo.id;
metricDescriptor.sensorInfo.sensorLocation = connectionInfo.address;
let metric = {
socket: socket,
metricDescriptor: metricDescriptor,
};
this.log('New connection is metric', {
metricUid: metric.metricDescriptor.metricInfo.metricUid,
});
metrics[metric.metricDescriptor.metricInfo.metricUid] = metric;
connection.metrics.push(metric);
socket.emit('metricRegistered', metric.metricDescriptor);
this.log('Metric registered', metric.metricDescriptor);
this.log('Sending metric info to observers', metric.metricDescriptor);
for (let observerId in observers) {
let observer = observers[observerId];
this.log('Sending metric info to observer', {
observerId: observer.observerInfo.observerId,
metricUid: metric.metricDescriptor.metricInfo.metricUid,
});
observer.socket.emit('registerMetric', metric.metricDescriptor);
}
}
});
socket.on('registerObserver', () => {
let observerInfo = {};
if (!observers[connectionInfo.id]) {
observerInfo.observerId = connectionInfo.id;
observerInfo.observerLocation = connectionInfo.address;
let observer = {
socket: socket,
observerInfo: observerInfo,
};
this.log('New connection is observer', {
observerId: observer.observerInfo.observerId,
});
observers[connectionInfo.id] = observer;
socket.emit('observerRegistered', {
observerInfo: observerInfo,
});
this.log('Observer registered', observer.observerInfo);
for (let metricUid in metrics) {
let metric = metrics[metricUid];
this.log('Sending metric info to observer', {
observerId: observer.observerInfo.observerId,
metricUid: metric.metricDescriptor.metricInfo.metricUid,
});
socket.emit('registerMetric', metric.metricDescriptor);
}
for (let metricUid in metrics) {
let metric = metrics[metricUid];
if (metric.metricData) {
this.log('Sending metric data to observer', {
observerId: observer.observerInfo.observerId,
metricUid: metric.metricDescriptor.metricInfo.metricUid,
});
socket.emit('metricData', metric.metricData);
}
}
}
});
socket.on('metricData', (data) => {
let metric = metrics[data.metricUid];
if (metric) {
metric.metricData = data.metricData;
for (let observerId in observers) {
let observer = observers[observerId];
observer.socket.emit('metricData', data);
}
}
});
socket.on('disconnect', () => {
this.log('Disconnection', connectionInfo);
connection.metrics.map((metric) => {
delete metrics[metric.metricDescriptor.metricInfo.metricUid];
this.log('Disconnection of metric', metric.metricDescriptor);
this.log('Informing observers about metric disconnection', metric.metricDescriptor);
for (let observerId in observers) {
let observer = observers[observerId];
this.log('Informing observer about metric disconnection', {
observerId: observer.observerInfo.observerId,
metricUid: metric.metricDescriptor.metricInfo.metricUid,
});
observer.socket.emit('unregisterMetric', metric.metricDescriptor);
}
this.log('Metric disconnected', metric.metricDescriptor);
});
let observer = observers[connectionInfo.id];
delete observers[connectionInfo.id];
if (observer) {
this.log('Disconnection of observer', {
observerId: observer.observerInfo.observerId,
});
}
});
});
this.log(`Listening on port ${this.hubConfig.hubPort}`);
}
}
module.exports = MonitoringHub;