-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathserver.js
221 lines (168 loc) · 5.99 KB
/
server.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
'use strict';
const express = require('express'),
app = express(),
server = require('http').createServer(app),
WebSocketServer = require('ws').Server,
request = require('request'),
async = require('async'),
zmq = require('zmq'),
log = require('npmlog');
// strongloop = require('strong-agent').profile();
app.use(express.static(__dirname + '/'));
app.use(express.json());
app.use(express.urlencoded());
app.enable('trust proxy');
log.level = process.env.LOGGING_LEVEL || 'verbose';
const port = process.env.PORT || 5000
server.listen(port, function() {
log.info('Server ' + process.pid + ' listening on', port);
});
var webSocketServer = new WebSocketServer({
server: server
});
/**
* Infrastructure settings and data models
*/
var resourceData = {}; // key = resourceId, value = data
var resourceObservers = {}; // key = resourceId, value = [connection1, conn2, ..]
/**
* Public Endpoints
*/
webSocketServer.on('connection', function (webSocketClient) {
handleClientConnected(webSocketClient);
});
function handleClientConnected(clientConnection) {
if (!isValidConnection(clientConnection)) {
clientConnection.disconnect();
}
var resourceId = getResourceId(clientConnection);
observeResource(clientConnection, resourceId);
var existingResourceData = resourceData.resourceId;
if (existingResourceData) {
sendResourceDataToObserver(clientConnection, resourceId);
} else {
requestResource(resourceId);
}
}
function observeResource(clientConnection, resourceId) {
var currentResourceObservers = resourceObservers[resourceId] || [];
currentResourceObservers.push(clientConnection);
resourceObservers[resourceId] = currentResourceObservers;
logNewObserver(clientConnection, resourceId);
}
// Publish a resource request for a resrouce that we don't have in memory (ie. in resourceData)
const resourceRequiredPusher = zmq.socket('push').bind('tcp://*:5432');
// Receive new resource data
const resourceUpdatedPuller = zmq.socket('pull').connect('tcp://localhost:5433');
resourceUpdatedPuller.on('message', function (data) {
handleResourceDataReceived(data);
});
function handleResourceDataReceived(data) {
var resource = JSON.parse(data);
log.verbose('Received resource data for resource id (' + resource.id + ')');
storeResourceData(resource);
notifyObservers(resource.id);
}
/**
* Implementation of public endpoints
*/
function sendResourceDataToObserver(clientConnection, resourceData) {
clientConnection.send(resourceData);
}
function requestResource(resourceId) {
log.silly('Requested resource (id: ' + resourceId + ') does not exist, sending a resource request');
resourceRequiredPusher.send(JSON.stringify({id: resourceId}));
}
function storeResourceData(resource) {
resourceData[resource.id] = resource.data;
logAllResources();
}
function notifyObservers(resourceId) {
var currentResourceObservers = resourceObservers[resourceId];
var data = resourceData[resourceId];
if (currentResourceObservers) {
async.forEach(currentResourceObservers, function(thisObserver){
if (thisObserver.readyState !== 3) {
sendResourceDataToObserver(thisObserver, data);
} else {
// We need to find the index ourselves, see https://github.com/caolan/async/issues/144
// Discussion: When a resource terminates, and all observers disconnect but
// currentResourceObservers will still be full.
var indexOfTheObserver = getIndexOfTheObserver(currentResourceObservers, thisObserver);
unobserveResource(currentResourceObservers, resourceId, indexOfTheObserver);
}
},
function(err){
log.error('Cant broadcast resource data to watching observer:', err);
});
} else {
log.verbose('No observers watching this resource: ' + resourceId);
}
}
function getIndexOfTheObserver(observersWatchingThisResource, observerToFind) {
for (var i = 0; i < observersWatchingThisResource.length; i++) {
var observer = observersWatchingThisResource[i];
if (observer === observerToFind) {
return i;
}
}
}
function unobserveResource(observersWatchingThisResource, resourceId, indexOfTheObserver) {
observersWatchingThisResource.splice(indexOfTheObserver, 1);
if (observersWatchingThisResource.length === 0) {
removeResource(resourceId);
}
logRemovedObserver();
}
function removeResource(resourceId) {
log.verbose('Removing resource ( ' + resourceId + ') from memory');
delete resourceObservers[resourceId];
delete resourceData[resourceId];
}
function getResourceId(clientConnection) {
return clientConnection.upgradeReq.url.substring(1);
}
function isValidConnection(clientConnection) {
var resourceId = getResourceId(clientConnection);
if (!resourceId) {
log.warn('Bad resource id (' + resourceId + ') is requested, closing the socket connection');
return false;
}
return true;
}
function closeAllConnections() {
resourceRequiredPusher.close();
resourceUpdatedPuller.close();
webSocketServer.close();
}
process.on('uncaughtException', function (err) {
log.error('Caught exception: ' + err.stack);
closeAllConnections();
process.exit(1);
});
process.on('SIGINT', function() {
closeAllConnections();
process.exit();
});
/**
* Logging
*/
function logNewObserver(clientConnection, resourceId) {
log.info('New connection for ' + resourceId + '. This resource\'s observers: ' +
resourceObservers[resourceId].length + ', Total observers : ', webSocketServer.clients.length);
}
function logRemovedObserver() {
log.verbose('Connection closed. Total connections: ', webSocketServer.clients.length);
logResourceObservers();
}
function logResourceObservers() {
for (var resourceId in resourceObservers) {
if (resourceObservers.hasOwnProperty(resourceId)) {
log.verbose(resourceObservers[resourceId].length + ' observers are watching ' + resourceId );
}
}
}
function logAllResources() {
log.silly('Total resources in memory: ' + Object.keys(resourceData).length);
// log.silly(JSON.stringify(resourceData, null, 4));
}