Skip to content

Commit e24382c

Browse files
authored
Merge pull request #12 from flamebase/develop
last change control, remove listeners, bugfixes
2 parents 3337e86 + e40b43f commit e24382c

File tree

7 files changed

+112
-66
lines changed

7 files changed

+112
-66
lines changed

Diff for: README.md

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[ ![flamebase/flamebase-database-server-cluster](https://d25lcipzij17d.cloudfront.net/badge.svg?id=js&type=6&v=1.2.1&x2=0)](https://www.npmjs.com/package/flamebase-database-server-cluster)
1+
[ ![flamebase/flamebase-database-server-cluster](https://d25lcipzij17d.cloudfront.net/badge.svg?id=js&type=6&v=1.3.0&x2=0)](https://www.npmjs.com/package/flamebase-database-server-cluster)
22

33
# :fire: flamebase-database-server-cluster
44
Server cluster to hold a realtime JSON database.
@@ -24,12 +24,18 @@ var FlamebaseDatabaseCluster = require('flamebase-database-server-cluster');
2424

2525
var serverKey = 'AIzafvwsdfmvsdfv-fdvwsdfgweFGfdAsfwefgw-z1JI';
2626

27-
var FSC = new FlamebaseDatabaseCluster("myDatabase", 1507, serverKey);
28-
FSC.initCluster({
27+
/**
28+
* - database name
29+
* - clusters port
30+
* - server key for push messages (fcm)
31+
* - debug logs
32+
*/
33+
var FDC = new FlamebaseDatabaseCluster("myDatabase", 1507, serverKey, true);
34+
FDC.initCluster({
2935
start: function () {
3036
console.log("start!!")
3137
}
32-
}, null);
38+
});
3339
```
3440
Alternatively you can start the server cluster by cloning this repo and launch on terminal:
3541
```bash
@@ -54,7 +60,7 @@ Client options to connect with server cluster.
5460

5561
- [Android](https://github.com/flamebase/flamebase-database-android)
5662
```groovy
57-
compile 'com.flamebase:database:1.2.0'
63+
compile 'com.flamebase:database:1.3.0'
5864
```
5965

6066
- [Node](https://github.com/flamebase/flamebase-database-node)

Diff for: index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var log4js = require('log4js');
44
const TAG = "Flamebase Database";
55
var logger = log4js.getLogger(TAG);
66

7-
function FlamebaseDatabaseCluster(database, port, APIKey) {
7+
function FlamebaseDatabaseCluster(database, port, APIKey, debug) {
88

99
this.initCluster = function (callback) {
1010
var child = forever.start('./server.js', {
@@ -19,7 +19,7 @@ function FlamebaseDatabaseCluster(database, port, APIKey) {
1919

2020
sourceDir: __dirname,
2121

22-
args: ['DATABASE_NAME=' + database, 'DATABASE_PORT=' + port, 'API_KEY=' + APIKey],
22+
args: ['DATABASE_NAME=' + database, 'DATABASE_PORT=' + port, 'API_KEY=' + APIKey, 'DEBUG=' + debug.toString()],
2323

2424
watch: false,
2525
watchIgnoreDotFiles: null,

Diff for: launcher.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
var FlamebaseDatabaseCluster = require('./index.js');
1+
var FlamebaseDatabaseCluster = require('flamebase-database-server-cluster');
22

3-
var FDC = new FlamebaseDatabaseCluster("draco", 1507);
3+
var serverKey = "...";
4+
var FDC = new FlamebaseDatabaseCluster("myDatabase", 1507, serverKey, true);
45
FDC.initCluster({
56
start: function () {
67
console.log("start!!")
78
}
8-
}, null);
9+
});
910

Diff for: model/path.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var log4js = require('log4js');
44
var TAG = "PATH CLUSTER";
55
var logger = log4js.getLogger(TAG);
66

7-
function Path(APIKey, databasePath, database, path, pid) {
7+
function Path(APIKey, databasePath, database, path, pid, dbg) {
88

99
// object reference
1010
var object = this;
@@ -16,9 +16,6 @@ function Path(APIKey, databasePath, database, path, pid) {
1616

1717
this.FD.syncFromDatabase();
1818

19-
// logger.debug("created reference listener for " + this.database + " - " + this.path + ": " + JSON.stringify(this.FD.ref));
20-
21-
2219
var config = {};
2320

2421
/**
@@ -70,10 +67,10 @@ function Path(APIKey, databasePath, database, path, pid) {
7067
};
7168

7269
this.FD.setSyncConfig(config);
73-
this.FD.debug(true);
70+
this.FD.debug(dbg === "true" ? true : false);
7471

75-
this.sendUpdateFor = function (before, device) {
76-
this.FD.sendDifferencesForClient(before, device);
72+
this.sendUpdateFor = function (before, device, callback) {
73+
this.FD.sendDifferencesForClient(before, device, callback);
7774
}
7875

7976
}

Diff for: package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "flamebase-database-server-cluster",
3-
"version": "1.2.1",
3+
"version": "1.3.0",
44
"description": "Server cluster to hold a realtime JSON database",
55
"main": "index.js",
66
"scripts": {
@@ -13,7 +13,7 @@
1313
"dependencies": {
1414
"body-parser": "latest",
1515
"express": "latest",
16-
"flamebase-database-node": "^1.2.0",
16+
"flamebase-database-node": "^1.3.0",
1717
"forever-monitor": "^1.7.1",
1818
"json-align": "^0.1.0",
1919
"log4js": "latest",

Diff for: paths.json

-2
This file was deleted.

Diff for: server.js

+89-45
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var express = require('express');
22
var bodyParser = require('body-parser');
3+
var timeout = require('connect-timeout')
34
var log4js = require('log4js');
45
var cluster = require('cluster');
56
var http = require('http');
@@ -20,9 +21,11 @@ String.prototype.replaceAll = function(search, replacement) {
2021
var expectedDBNEnvVar = "DATABASE_NAME";
2122
var expectedPORTEnvVar = "DATABASE_PORT";
2223
var expectedAPIKeyEnvVar = "API_KEY";
24+
var expectedDebugKeyEnvVar = "DEBUG";
2325
var dbMaster = null;
2426
var port = null;
2527
var APIKey = null;
28+
var debug = null;
2629

2730
process.argv.forEach(function (val, index, array) {
2831
if (val.indexOf(expectedDBNEnvVar) > -1) {
@@ -31,6 +34,9 @@ process.argv.forEach(function (val, index, array) {
3134
if (val.indexOf(expectedPORTEnvVar) > -1) {
3235
port = val.replaceAll(expectedPORTEnvVar + "=", "");
3336
}
37+
if (val.indexOf(expectedDebugKeyEnvVar) > -1) {
38+
debug = val.replaceAll(expectedDebugKeyEnvVar + "=", "") === "true" ? true : false;
39+
}
3440
if (val.indexOf(expectedAPIKeyEnvVar) > -1) {
3541
APIKey = val.replaceAll(expectedAPIKeyEnvVar + "=", "");
3642
}
@@ -62,24 +68,17 @@ if (cluster.isMaster) {
6268
} else {
6369

6470
var action = {
65-
response: function (connection, data, error, pId) {
71+
response: function (connection, data, error, pId) {
6672
var result = {status: (data === null || error !== null ? "KO" : "OK"),
6773
data: (data === null ? {} : data), error: error};
6874
logger.info("worker: " + pId);
6975
logger.info("response: " + JSON.stringify(result));
7076
connection.response.contentType('application/json');
7177
connection.response.send(result);
7278
},
73-
addSingleListener: function (connection, pId) {
74-
this.addGreatListener(connection, pId);
75-
},
76-
addGreatListener: function (connection, pId) {
79+
addListener: function (connection, pId) {
7780
var paths = new FlamebaseDatabase(dbPaths, "/");
7881
paths.syncFromDatabase();
79-
logger.debug(JSON.stringifyAligned(paths.ref));
80-
paths.syncFromDatabase();
81-
logger.debug("getting");
82-
logger.debug(JSON.stringifyAligned(paths.ref));
8382

8483
if (paths.ref === undefined) {
8584
paths.ref = {}
@@ -115,10 +114,10 @@ if (cluster.isMaster) {
115114
}
116115
}
117116

118-
paths.ref[key].tokens[connection.token].time = new Date().getTime();
119-
120117
paths.syncToDatabase();
121118

119+
this.updateTime(connection);
120+
122121
var equals = this.verifyLenght(connection, pId);
123122

124123
var object = this.getReference(connection, pId);
@@ -135,7 +134,7 @@ if (cluster.isMaster) {
135134
var data = {};
136135
data.len = len;
137136

138-
if (lastToken === connection.token && equals) {
137+
if (lastToken === connection.token) {
139138
data.info = "listener_up_to_date";
140139
} else {
141140
data.info = "listener_ready_for_refresh_client";
@@ -147,22 +146,46 @@ if (cluster.isMaster) {
147146
}
148147

149148
},
150-
verifyLenght: function (connection, pId) {
149+
removeListener: function (connection, pId) {
151150
var paths = new FlamebaseDatabase(dbPaths, "/");
152151
paths.syncFromDatabase();
152+
153+
if (connection.path.indexOf("\.") === -1 && connection.path.indexOf("/") === 0) {
154+
var key = connection.path.replaceAll("/", "\.");
155+
key = key.substr(1, key.length - 1);
156+
157+
if (paths.ref[key] !== undefined && paths.ref[key].tokens !== undefined && paths.ref[key].tokens[connection.token] !== undefined) {
158+
delete paths.ref[key].tokens[connection.token];
159+
160+
paths.syncToDatabase();
161+
162+
var data = {};
163+
data.info = "listener_removed";
164+
165+
this.response(connection, data, null, pId);
166+
} else {
167+
if (paths.ref[key] === undefined) {
168+
this.response(connection, null, "path_not_found", pId);
169+
} else {
170+
this.response(connection, null, "token_not_found", pId);
171+
}
172+
}
173+
} else {
174+
this.response(connection, null, "path_contains_dots", pId);
175+
}
176+
177+
},
178+
verifyLenght: function (connection, pId) {
153179
var object = this.getReference(connection, pId);
154-
// var len = JSON.stringify(object.FD.ref).length;
155180
logger.debug(sha1(JSON.stringify(object.FD.ref)).toUpperCase());
156181
logger.debug(connection.sha1);
157182

158183
var hash = sha1(JSON.stringify(object.FD.ref)).toUpperCase();
159184
return hash === connection.sha1;
160185
},
161-
getUpdatesFrom: function (connection, pId) {
186+
getUpdatesFrom: function (connection, pId) {
162187
var paths = new FlamebaseDatabase(dbPaths, "/");
163188
paths.syncFromDatabase();
164-
logger.debug("getting");
165-
logger.debug(JSON.stringifyAligned(paths.ref));
166189
var object = this.getReference(connection, pId);
167190
if (typeof object === "string") {
168191
this.response(connection, null, object, pId);
@@ -171,18 +194,35 @@ if (cluster.isMaster) {
171194
token: connection.token,
172195
os: connection.os
173196
}
174-
object.sendUpdateFor(connection.content, device)
175-
var data = {};
176-
data.info = "updates_sent";
177-
data.len = JSON.stringify(object.FD.ref).length;
178-
this.response(connection, data, null, pId);
197+
object.sendUpdateFor(connection.content, device, function() {
198+
var data = {};
199+
data.info = "updates_sent";
200+
data.len = JSON.stringify(object.FD.ref).length;
201+
action.response(connection, data, null, pId);
202+
});
179203
}
180204
},
181-
updateData: function (connection, pId) {
182-
var paths = new FlamebaseDatabase(dbPaths, "/");
183-
paths.syncFromDatabase();
184-
logger.debug("getting");
185-
logger.debug(JSON.stringifyAligned(paths.ref));
205+
updateTime: function (connection) {
206+
if (connection.path.indexOf("\.") === -1 && connection.path.indexOf("/") === 0) {
207+
var key = connection.path.replaceAll("/", "\.");
208+
key = key.substr(1, key.length - 1);
209+
210+
var paths = new FlamebaseDatabase(dbPaths, "/" + key);
211+
paths.syncFromDatabase();
212+
213+
if (paths.ref === undefined) {
214+
paths.ref = {};
215+
paths.ref.path = connection.path;
216+
}
217+
218+
if (paths.ref.tokens === undefined) {
219+
paths.ref.tokens = {};
220+
}
221+
paths.ref.tokens[connection.token].time = new Date().getTime();
222+
paths.syncToDatabase();
223+
}
224+
},
225+
updateData: function (connection, pId) {
186226
var object = this.getReference(connection, pId);
187227
if (typeof object === "string") {
188228
this.response(connection, null, object, pId);
@@ -192,22 +232,23 @@ if (cluster.isMaster) {
192232
var differences = connection.differences;
193233

194234
if (differences !== undefined) {
195-
logger.debug(JSON.stringify(differences));
196235
apply(object.FD.ref, JSON.parse(differences));
236+
this.updateTime(connection);
237+
238+
object.FD.syncToDatabase(false, function() {
239+
if (JSON.stringify(object.FD.ref).length !== connection.len) {
240+
action.response(connection, null, "data_updated_with_differences", pId);
241+
} else {
242+
action.response(connection, "data_updated", null, pId);
243+
}
244+
});
197245

198-
if (JSON.stringify(object.FD.ref).length !== connection.len) {
199-
object.FD.syncToDatabase();
200-
this.response(connection, null, "data_updated_with_differences", pId);
201-
} else {
202-
object.FD.syncToDatabase();
203-
this.response(connection, "data_updated", null, pId);
204-
}
205246
} else {
206247
this.response(connection, "no_diff_updated", null, pId);
207248
}
208249
}
209250
},
210-
getReference: function (connection, pId) {
251+
getReference: function (connection, pId) {
211252
var paths = new FlamebaseDatabase(dbPaths, "/");
212253
paths.syncFromDatabase();
213254
var error = null;
@@ -217,7 +258,7 @@ if (cluster.isMaster) {
217258
var key = connection.path.replaceAll("/", "\.");
218259
key = key.substr(1, key.length - 1);
219260
if (paths.ref[key] !== undefined) {
220-
return new Path(APIKey,paths.ref[key], dbMaster, connection.path, pId);
261+
return new Path(APIKey, paths.ref[key], dbMaster, connection.path, pId, debug.toString());
221262
} else {
222263
error = "holder_not_found";
223264
}
@@ -233,7 +274,7 @@ if (cluster.isMaster) {
233274
logger.error(error);
234275
return error;
235276
},
236-
parseRequest: function (req, res, worker) {
277+
parseRequest: function (req, res, worker) {
237278
var response = res;
238279

239280
try {
@@ -314,21 +355,23 @@ if (cluster.isMaster) {
314355
connection.response = response;
315356

316357
switch (connection.method) {
317-
case "single_listener":
358+
359+
case "create_listener":
318360
try {
319-
this.addSingleListener(connection, worker);
361+
this.addListener(connection, worker);
320362
} catch (e) {
321-
logger.error("there was an error parsing request from addSingleListener: " + e.toString());
322-
this.response(connection, null, "cluster_" + worker + "_error_adding_single", worker);
363+
logger.error("there was an error parsing request from addGreatListener: " + e.toString());
364+
this.response(connection, null, "cluster_" + worker + "_error_creating", worker);
323365
}
324366
break;
325367

326-
case "great_listener":
368+
369+
case "remove_listener":
327370
try {
328-
this.addGreatListener(connection, worker);
371+
this.removeListener(connection, worker);
329372
} catch (e) {
330373
logger.error("there was an error parsing request from addGreatListener: " + e.toString());
331-
this.response(connection, null, "cluster_" + worker + "_error_adding_great", worker);
374+
this.response(connection, null, "cluster_" + worker + "_error_removing_listener", worker);
332375
}
333376
break;
334377

@@ -374,6 +417,7 @@ if (cluster.isMaster) {
374417
}));
375418

376419
app.use(bodyParser.json({limit: '50mb'}));
420+
app.use(timeout('60s'))
377421

378422
app.route('/')
379423
.get(function (req, res) {

0 commit comments

Comments
 (0)