11var express = require ( 'express' ) ;
22var bodyParser = require ( 'body-parser' ) ;
3+ var timeout = require ( 'connect-timeout' )
34var log4js = require ( 'log4js' ) ;
45var cluster = require ( 'cluster' ) ;
56var http = require ( 'http' ) ;
@@ -20,9 +21,11 @@ String.prototype.replaceAll = function(search, replacement) {
2021var expectedDBNEnvVar = "DATABASE_NAME" ;
2122var expectedPORTEnvVar = "DATABASE_PORT" ;
2223var expectedAPIKeyEnvVar = "API_KEY" ;
24+ var expectedDebugKeyEnvVar = "DEBUG" ;
2325var dbMaster = null ;
2426var port = null ;
2527var APIKey = null ;
28+ var debug = null ;
2629
2730process . 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