Skip to content

Commit

Permalink
Merge pull request #60 from subteca/feature/lifetime-unregistration-h…
Browse files Browse the repository at this point in the history
…andler

ADD Lifetime unregistration handler
  • Loading branch information
dmoranj committed May 14, 2015
2 parents 2430e68 + 1c66434 commit cddedde
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 23 deletions.
3 changes: 2 additions & 1 deletion CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
- FIX Client handler after connection update (#51)
- FIX Remove old device registration on the new registration (#52)
- Add Travis CI files and changes to enable it in the package.json (#55).
- Add Check lifetime and unregister devices (#56)
- Add Check lifetime and unregister devices (#56)
- Add Lifetime unregistration handler (#59)
24 changes: 22 additions & 2 deletions lib/lwm2m-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var coapRouter = require('./services/coapRouter'),
op: 'LWM2MLib.Server'
},
apply = async.apply,
config,
status = 'STOPPED';

/**
Expand Down Expand Up @@ -103,13 +104,14 @@ function validateTypes(config, callback) {
callback(error);
}

function start(config, startCallback) {
function start(serverConfig, startCallback) {
function loadDefaults(serverInfo, callback) {
loadRoutes(serverInfo);
loadDefaultHandlers(serverInfo, config);
callback(null, serverInfo);
}

config = serverConfig;
if (config.logLevel) {
logger.setLevel(config.logLevel);
}
Expand Down Expand Up @@ -165,8 +167,26 @@ function isRunning() {
return status === 'RUNNING';
}

/**
* Sets the handler callback for a given type of operation.
*
* The signature of the handler will depend on the operation being handled. The complete list of operations and the
* signature of its handlers can be found in the online documentation.
*
* @param {Object} serverInfo Object containing all the information of the current server.
* @param {String} type Name of the operation to be handled.
* @param {Function} handler Operation handler.
*/
function setHandler(serverInfo, type, handler) {
coapRouter.setHandler(serverInfo, type, handler);

if (type === 'unregistration') {
registry.checkLifetime(config.lifetimeCheckInterval, handler);
}
}

exports.start = start;
exports.setHandler = coapRouter.setHandler;
exports.setHandler = setHandler;
exports.stop = stop;
exports.read = deviceManagement.read;
exports.write = deviceManagement.write;
Expand Down
29 changes: 19 additions & 10 deletions lib/services/server/inMemoryDeviceRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,24 @@ function list(callback) {
}


/**
* Stops checking for device lifetime.
*/
function stopLifetimeCheck() {
clearInterval(checkLifetimeInterval);
checkLifetimeInterval = null;
}

/**
* If Lifetime Resource exists, then the registration SHOULD be removed by the Server if a new registration or update
* is not received within this lifetime.
*
* @param {Object} lifetimeCheckInterval Minimum interval between lifetime checks in ms
* @param {Object} lifetimeCheckInterval Minimum interval between lifetime checks in ms
* @param {Function} unregistrationHandler Unregistration device handler
*/
function checkLifetime(lifetimeCheckInterval) {
function checkLifetime(lifetimeCheckInterval, unregistrationHandler) {
stopLifetimeCheck();

checkLifetimeInterval = setInterval(function(){
list(function(error, deviceList){
if (!error && deviceList) {
Expand All @@ -176,6 +187,12 @@ function checkLifetime(lifetimeCheckInterval) {
logger.debug(context,
'Lifetime unregistration for device [%s] ended successfully',
obj.name);

if (unregistrationHandler) {
unregistrationHandler(obj, function(){

});
}
}
});
}
Expand All @@ -185,14 +202,6 @@ function checkLifetime(lifetimeCheckInterval) {
}, lifetimeCheckInterval);
}

/**
* Stops checking for device lifetime.
*/
function stopLifetimeCheck() {
clearInterval(checkLifetimeInterval);
checkLifetimeInterval = null;
}

/**
* Initializes the device registry based on the parameter found in the configuration. For this in memory registry this
* function doesn't do anything.
Expand Down
30 changes: 20 additions & 10 deletions lib/services/server/mongodbDeviceRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,25 @@ function list(callback) {
query.exec(callback);
}


/**
* Stops checking for device lifetime.
*/
function stopLifetimeCheck() {
clearInterval(checkLifetimeInterval);
checkLifetimeInterval = null;
}

/**
* If Lifetime Resource exists, then the registration SHOULD be removed by the Server if a new registration or update
* is not received within this lifetime.
*
* @param {Object} lifetimeCheckInterval Minimum interval between lifetime checks in ms
* @param {Object} lifetimeCheckInterval Minimum interval between lifetime checks in ms
* @param {Function} unregistrationHandler Unregistration device handler
*/
function checkLifetime(lifetimeCheckInterval) {
function checkLifetime(lifetimeCheckInterval, unregistrationHandler) {
stopLifetimeCheck();

checkLifetimeInterval = setInterval(function(){
list(function(error, deviceList){
if (!error && deviceList) {
Expand All @@ -223,6 +235,12 @@ function checkLifetime(lifetimeCheckInterval) {
logger.debug(context,
'Lifetime unregistration for device [%s] ended successfully',
obj.name);

if (unregistrationHandler) {
unregistrationHandler(obj, function(){

});
}
}
});
}
Expand All @@ -232,14 +250,6 @@ function checkLifetime(lifetimeCheckInterval) {
}, lifetimeCheckInterval);
}

/**
* Stops checking for device lifetime.
*/
function stopLifetimeCheck() {
clearInterval(checkLifetimeInterval);
checkLifetimeInterval = null;
}

/**
* Initializes the device registry based on the parameter found in the configuration. The MongoDB config object should
* contain at least the host string needed to connect to MongoDB and the database name where to store the device info.
Expand Down

0 comments on commit cddedde

Please sign in to comment.