forked from Spacebrew/spacebrew
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spacebrew_live_persist.js
484 lines (422 loc) · 15.7 KB
/
spacebrew_live_persist.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/**
* Spacebrew Live Persist Module
* -----------------------------
*
* This module persists all routes added via a standard spacebrew admin. To
* run this module a standalone app you should use the node_persisten_live.js
* script.
*
* Latest Updates:
* - checks if data/routes/live folder already exists, and if not, it creates folder
* - changed error message when script attempts to load a live routes file that
* does not exist
*
* @author: Julio Terra
* @filename: node_server.js
* @date: June 1st, 2013
* @updated with version: 0.3.1
*
*/
var fs = require("fs") // fs used for file read/write
, WebSocketClient = require('ws') // websocket used for conection to spacebrew
, logger = require('./logger') // logger used to log messages
, livePersister = exports // set livePersister to be a node module
, WS_OPEN = 1 // websockets library open state constant
;
/**
* Live Persister module that makes sure that all routes connected via an standard
* Admin remain connected if an app disconnects and then reconnects, or
* if the server fails.
*
* @param {Object} opts Object that holds the following optional settings for
* the livePersister:
* * port: port number of the Spacebrew server
* * host: host of the spacebrew server
* * autosave: saves routes to file
* * load: loads routes from file on startup
* * loadFile: speficies name of file to load
* * logLevel: sets the log level for the app
*
*/
livePersister.persistRoutes = function( opts ){
// Import Modules and Configure Input and Output streams
var reconnect = undefined // holds timer that maintains server connection
;
var port = opts.port || 9000
, host = opts.host || "localhost"
, autosave = opts.autosave || true
, load = opts.load || true
, configFile = opts.loadFile || "live_persist_config.json"
;
var clients = []
, routes = []
;
logger.debugLevel = opts.logLevel || "error";
/**
* check if data/routes directory already exists, and if not, then create it.
*/
var setupLogDirectory = function () {
var data_dir = __dirname + "/data"
, routes_dir = __dirname + "/data/routes"
, live_dir = __dirname + "/data/routes/live"
;
// check if data folder exists
try {
fs.statSync(data_dir);
}
catch (e) {
fs.mkdir(data_dir, err => { if (err) console.log(err) });
logger.log("info", "creating data directory");
}
// check if data/routes folder exists
try {
fs.statSync(routes_dir);
}
catch (e) {
fs.mkdir(routes_dir, err => { if (err) console.log(err) });
logger.log("info", "creating data/routes directory");
}
// check if data/routes folder exists
try {
fs.statSync(live_dir);
}
catch (e) {
fs.mkdir(live_dir, err => { if (err) console.log(err) });
logger.log("info", "creating data/routes/live directory");
}
}
var setupWSClient = function(){
// create the wsclient to connect to spacebrew
wsc = new WebSocketClient("ws://" + host + ":" + port);
// configure the spacebrew admin client once connection stablished
wsc.on("open", function(conn){
logger.log("info", "[ws.open] connected to spacebrew \n");
// send the admin configuration message to spacebrew server
var adminMsg = { "admin": [
{
"admin": true
, "no_msgs" : true
}
]};
wsc.send(JSON.stringify(adminMsg));
// if the reconnect timer is activated then stop it
if (reconnect) {
reconnect = clearInterval(reconnect);
reconnect = undefined;
}
// load the routes that were saved
if (load) loadRoutes();
});
// handle messages
wsc.on("message", receivedMessage);
// handle websocket error events
wsc.on("error", function(){
logger.log("error", "[ws.onerror] spacebrew Connection Error");
logger.log("error", arguments);
if (wsc.readyState != WS_OPEN) reestablishConnection();
});
// handle websocket close events
wsc.on("close", function(){
logger.log("info", "[on.close] spacebrew Connection Closed");
logger.log("info", arguments);
reestablishConnection();
});
}
/**
* Walks all the clients and all the persistent routes, and sends a route Add message for each
* route that should exist.
*/
var ensureConnected = function(){
//for each publisher, if that publisher is in the persistent routes
// for each subscriber, if that subscriber is the other end of that persistent route
// send the add route message
//for each publisher
for (var i = 0; i < clients.length; i++){
for (var j = 0; j < clients[i].publish.messages.length; j++){
// for each persistent route
for (var k = 0; k < routes.length; k++){
var currRoute = routes[k];
// if the publisher is in a persistent route
// ---- TO DO: check if the client remoteAddress also matches
if (currRoute.publisher.clientName === clients[i].name &&
currRoute.publisher.remoteAddress === clients[i].remoteAddress &&
currRoute.publisher.name === clients[i].publish.messages[j].name){
// loop through all connected clients to find a subscriber
for (var m = 0; m < clients.length; m++){
// loop through subscribers for each client
for (var n = 0; n < clients[m].subscribe.messages.length; n++){
// if a client/subscriber match is found then add route
// ---- TO DO: check if the client remoteAddress also matches
if (currRoute.subscriber.clientName === clients[m].name &&
currRoute.subscriber.remoteAddress === clients[m].remoteAddress &&
currRoute.subscriber.name === clients[m].subscribe.messages[n].name){
//if the pub/sub pair match the persistent route
//send route message
wsc.send(JSON.stringify({
route:{
type:'add'
, publisher: {
clientName:clients[i].name
, name:clients[i].publish.messages[j].name
, type:clients[i].publish.messages[j].type
, remoteAddress:clients[i].remoteAddress
}
, subscriber:{
clientName:clients[m].name
, name:clients[m].subscribe.messages[n].name
, type:clients[m].subscribe.messages[n].type
, remoteAddress:clients[m].remoteAddress
}
}
}));
}
}
}
}
}
}
}
};
/**
* Called when we receive a message from the Server.
* @param {websocket message} data The websocket message from the Server
*/
var receivedMessage = function(data){
logger.log("info", "[receivedMessage] received new message from spacebrew server: ")
logger.log("info", data);
if (data){
var json = JSON.parse(data)
, status = handleMessage(json)
;
if (status > 0){
for(var i = 0, end = json.length; i < end; i++){
handleMessage(json[i]);
}
}
else if (status < 0) {
logger.log("info", "[receivedMessage] message is not valid: ");
logger.log("info", data);
}
}
};
/**
* Handle the json data from the Server and forward it to the appropriate function
* @param {json} json The message sent from the Server
* @return {boolean} True iff the message was a recognized type
*/
var handleMessage = function(json){
if (json.message || json.admin){
//do nothing
}
else if (json.config){
logger.log("info", "[handleMessage] add client request received: ");
logger.log("info", json);
handleConfigMessage(json);
}
else if (json.route){
logger.log("info", "[handleMessage] route request received: ");
logger.log("info", json);
if (json.route.type === 'remove'){
handleRouteRemoveMessage(json);
}
if (json.route.type === 'add'){
handleRouteAddMessage(json);
}
}
else if (json.remove){
handleClientRemoveMessage(json);
}
// return read array status
else if (json instanceof Array) {
return 1;
}
// return failure status
else {
return -1;
}
// return success status
return 0;
};
/**
* Handles removes routes from the spacebrew server
* @param {json} msg The route remove message from the Server
*/
var handleRouteRemoveMessage = function(msg){
// if the route remove message was associated to client disconnecting, then don't
// delete from the persistent route list.
if ( msg.route.client_disconnect ) return;
logger.log("info", "[handleRouteRemoveMessage] request received ");
logger.log("info", msg);
for (var i = routes.length - 1; i >= 0; i--) {
var currRoute = routes[i];
if (currRoute.publisher.clientName === msg.route.publisher.clientName &&
currRoute.publisher.name === msg.route.publisher.name &&
currRoute.publisher.remoteAddress === msg.route.publisher.remoteAddress &&
currRoute.subscriber.clientName == msg.route.subscriber.clientName &&
currRoute.subscriber.name === msg.route.subscriber.name &&
currRoute.subscriber.remoteAddress === msg.route.subscriber.remoteAddress){
// remove this route
routes.splice(i,1);
logger.log("info", "[handleRouteRemoveMessage] removed route at index " + i);
}
};
saveRoutes();
};
/**
* Handles add routes from the spacebrew
* @param {json} msg The route remove message from the Server
*/
var handleRouteAddMessage = function(msg){
logger.log("info", "[handleRouteAddMessage] request received ");
logger.log("info", msg);
//go through all persistent routes and see if this one exists already
for (var i = routes.length - 1; i >= 0; i--) {
var currRoute = routes[i];
if (currRoute.publisher.clientName === msg.route.publisher.clientName &&
currRoute.publisher.name === msg.route.publisher.name &&
currRoute.publisher.remoteAddress === msg.route.publisher.remoteAddress &&
currRoute.subscriber.clientName == msg.route.publisher.clientName &&
currRoute.subscriber.name === msg.route.publisher.name &&
currRoute.subscriber.remoteAddress === msg.route.publisher.remoteAddress){
//this route already exists, abort
return;
}
};
//add the route to the list
var newRoute = {
publisher: msg.route.publisher,
subscriber: msg.route.subscriber
};
routes.push(newRoute);
saveRoutes();
logger.log("info", "[handleRouteAddMessage] new route: ");
logger.log("info", newRoute);
}
/**
* Handles a remove message from the Server when a Client disconnects.
* This function cleans up the appropriate data structures
* @param {json} msg The message from the Server
*/
var handleClientRemoveMessage = function(msg){
for (var j = msg.remove.length-1; j >= 0; j--){
for (var i = clients.length - 1; i >= 0; i--){
if (areClientsEqual(clients[i], msg.remove[j])){
clients.splice(i, 1);
logger.log("info", "[handleClientRemoveMessage] removed a client");
break;
}
}
}
};
/**
* handles a new Config message from a Client. Will connect the new Client to
* all the necessary persistent routes.
* @param {json} msg The Config message from the Server from a Client
*/
var handleConfigMessage = function(msg){
var existing = false;
// see if we are updating a current client
for (var i = clients.length-1; i >= 0; i--){
if (areClientsEqual(clients[i], msg.config)){
//we are updating an existing client
logger.log("info", "[handleConfigMessage] updating a client");
clients[i] = msg.config;
existing = true;
}
}
// otherwise add client to array
if (!existing){
logger.log("info", "[handleConfigMessage] adding a new client");
clients.push(msg.config);
}
ensureConnected();
};
/**
* Sends an 'add route' command to the Server.
* @param {Client obj} pubClient The client of the publisher involved in the new route
* @param {Pub obj} pub The particular publisher exposed by pubClient involved in the new route
* @param {Client obj} subClient The client of the subscriber involved in the new route
* @param {Pub obj} sub The particular subscriber exposed by subClient involved in the new route
*/
var addRoute = function(pubClient, pub, subClient, sub){
if (pub.type != sub.type){
return;
}
wsc.send(JSON.stringify({
route:{type:'add',
publisher:{clientName:pubClient.name,
name:pub.name,
type:pub.type,
remoteAddress:pubClient.remoteAddress},
subscriber:{clientName:subClient.name,
name:sub.name,
type:sub.type,
remoteAddress:subClient.remoteAddress}}
}));
};
/**
* Save routes to the file
* @return {[type]} [description]
*/
var saveRoutes = function() {
fs.writeFile('./data/routes/live/' + configFile, JSON.stringify(routes), function(err){
if (err){
logger.log("warn", "[saveRoutes] error saving route model to live_persist_config.json");
logger.log("warn", err);
} else {
logger.log("info", "[saveRoutes] route model was saved to live_persist_config.json");
}
});
}
/**
* Loads routes from the specified file
*
* @param {String} filename Name of file that should be loaded
* @return {Boolean} Returns true if data was properly loaded
*/
var loadRoutes = function(){
var raw_data;
// open the raw_data file
try{
raw_data = fs.readFileSync("./data/routes/live/" + configFile);
} catch(err){
logger.log("warn", "[loadRoutes] live routes config file does not exist " + configFile);
return false;
}
// parse the file contents
try{
routes = JSON.parse(raw_data);
ensureConnected();
}catch(err){
logger.log("warn", "[loadRoutes] unable to parse content from file ");
return false;
}
return true;
};
/**
* Sets a timer for attempting to reconnect to the Spacbrew server again. Method is
* called when connection to Spacebrew server is closed.
*/
var reestablishConnection = function() {
if (!reconnect) {
reconnect = setInterval(function() {
if (wsc.readyState !== WS_OPEN) {
logger.log("info", "[reestablishConnection] attempting to reconnect");
wsc.terminate();
setupWSClient();
}
}, 5000);
}
}
/**
* Utility function for helping determine if two config objects refer to the same Client
* @param {Client config} A
* @param {Client config} B
* @return {boolean} true iff the names and remote addresses match
*/
var areClientsEqual = function(A, B){
return A.name === B.name && A.remoteAddress === B.remoteAddress;
};
setupLogDirectory();
setupWSClient();
}