Skip to content

Commit

Permalink
Merge pull request #57 from subteca/feature/lifetime
Browse files Browse the repository at this point in the history
Check lifetime and unregister devices
  • Loading branch information
dmoranj committed May 11, 2015
2 parents df9a6b0 + 1ed12ef commit 2430e68
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 9 deletions.
3 changes: 2 additions & 1 deletion CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
- Fix command line break when executing command before starting the server (#47).
- 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 Travis CI files and changes to enable it in the package.json (#55).
- Add Check lifetime and unregister devices (#56)
1 change: 1 addition & 0 deletions config-mongo.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var config = {};
//--------------------------------------------------
config.server = {
port: 5683, // Port where the server will be listening
lifetimeCheckInterval: 1000, // Minimum interval between lifetime checks in ms
udpWindow: 100,
defaultType: 'Device',
logLevel: 'FATAL',
Expand Down
1 change: 1 addition & 0 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var config = {};
//--------------------------------------------------
config.server = {
port: 5683, // Port where the server will be listening
lifetimeCheckInterval: 1000, // Minimum interval between lifetime checks in ms
udpWindow: 100,
defaultType: 'Device',
logLevel: 'FATAL',
Expand Down
1 change: 1 addition & 0 deletions lib/lwm2m-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ function start(config, startCallback) {
status = 'ERROR';
} else {
status = 'RUNNING';
registry.checkLifetime(config.lifetimeCheckInterval);
}

startCallback(error, results);
Expand Down
53 changes: 52 additions & 1 deletion lib/services/server/inMemoryDeviceRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@
var registry = {},
idCounter = 1,
errors = require('../../errors'),
_ = require('underscore');
_ = require('underscore'),
logger = require('logops'),
context = {
op: 'LWM2MLib.MemoryDeviceRegistry'
},
checkLifetimeInterval;

/**
* Gets the device that has the device name passed as a parameter (should be unique) or return a DeviceNotFound error
Expand Down Expand Up @@ -147,13 +152,57 @@ function list(callback) {
callback(null, result);
}


/**
* 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
*/
function checkLifetime(lifetimeCheckInterval) {
checkLifetimeInterval = setInterval(function(){
list(function(error, deviceList){
if (!error && deviceList) {
deviceList.forEach(function(device){
if (device.lifetime &&
new Date() - new Date(device.creationDate) > Number(device.lifetime) * 1000) {
unregister(device.id, function(err, obj){
if (err) {
logger.debug(context,
'Lifetime unregistration for device [%s] ended up in error [%s] with code [%s]',
obj.name, err.name, err.code);
}
else {
logger.debug(context,
'Lifetime unregistration for device [%s] ended successfully',
obj.name);
}
});
}
});
}
});
}, 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.
*
* @param {Object} config Configuration object.
*/
function init(config, callback) {
if (config.logLevel) {
logger.setLevel(config.logLevel);
}
callback(null);
}

Expand All @@ -163,5 +212,7 @@ exports.get = getObject;
exports.update = update;
exports.clean = clean;
exports.list = list;
exports.checkLifetime = checkLifetime;
exports.stopLifetimeCheck = stopLifetimeCheck;
exports.getByName = getByName;
exports.init = init;
5 changes: 5 additions & 0 deletions lib/services/server/informationReporting.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ function clean(callback) {
}

subscriptions = {};

if (registry){
registry.stopLifetimeCheck();
}

callback();
}

Expand Down
66 changes: 61 additions & 5 deletions lib/services/server/mongodbDeviceRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@

var errors = require('../../errors'),
dbService = require('../model/dbConn'),
Device = require('../model/Device');
Device = require('../model/Device'),
logger = require('logops'),
context = {
op: 'LWM2MLib.MomgodbDeviceRegistry'
},
checkLifetimeInterval;

/**
* Generic function to retrieve a device based on a parameter value. This is an auxiliary function meant to abstract
Expand Down Expand Up @@ -129,6 +134,7 @@ function register(object, callback) {
deviceObj.lifetime = object.lifetime;
deviceObj.name = object.name;
deviceObj.type = object.type;
deviceObj.creationDate = object.creationDate;

deviceObj.save(innerCb);
}
Expand Down Expand Up @@ -170,7 +176,13 @@ function update(id, obj, callback) {
callback(error);
} else {
objDAO.id = obj.id;
objDAO.type = objDAO.type;
objDAO.address = obj.address;
objDAO.port = obj.port;
objDAO.path = obj.path;
objDAO.lifetime = obj.lifetime;
objDAO.name = obj.name;
objDAO.type = obj.type;
objDAO.creationDate = obj.creationDate;
objDAO.save(toObject(callback));
}
});
Expand All @@ -188,6 +200,46 @@ function list(callback) {
query.exec(callback);
}

/**
* 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
*/
function checkLifetime(lifetimeCheckInterval) {
checkLifetimeInterval = setInterval(function(){
list(function(error, deviceList){
if (!error && deviceList) {
deviceList.forEach(function(device){
if (device.lifetime &&
new Date() - new Date(device.creationDate) > Number(device.lifetime) * 1000) {
unregister(device.id, function(err, obj){
if (err) {
logger.debug(context,
'Lifetime unregistration for device [%s] ended up in error [%s] with code [%s]',
obj.name, err.name, err.code);
}
else {
logger.debug(context,
'Lifetime unregistration for device [%s] ended successfully',
obj.name);
}
});
}
});
}
});
}, 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 All @@ -196,9 +248,11 @@ function list(callback) {
*
* @param {Object} config Configuration object containing a deviceRegistry attribute with the info.
*/

function init(newConfig, callback) {
dbService.init(newConfig.deviceRegistry.host, newConfig.deviceRegistry.db, callback);
function init(config, callback) {
if (config.logLevel) {
logger.setLevel(config.logLevel);
}
dbService.init(config.deviceRegistry.host, config.deviceRegistry.db, callback);
}

exports.register = register;
Expand All @@ -207,5 +261,7 @@ exports.get = getObject;
exports.update = update;
exports.clean = clean;
exports.list = list;
exports.checkLifetime = checkLifetime;
exports.stopLifetimeCheck = stopLifetimeCheck;
exports.getByName = getByName;
exports.init = init;
3 changes: 2 additions & 1 deletion lib/services/server/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ function storeDevice(queryParams, req, callback) {
name: queryParams.ep,
lifetime: queryParams.lt,
address: req.rsinfo.address,
port: req.rsinfo.port
port: req.rsinfo.port,
creationDate: new Date()
};

logger.debug(context, 'Storing the following device in the db:\n%s', JSON.stringify(device, null, 4));
Expand Down
3 changes: 2 additions & 1 deletion lib/services/server/updateRegistration.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ function endUpdate(req, res) {
function updateRegister(req, queryParams, obj, callback) {
logger.debug(context, 'Updating device register with lifetime [%s] and address [%s].',
queryParams.lt, req.rsinfo.address);
obj.lifetime = queryParams.lt;
obj.lifetime = queryParams.lt || obj.lifetime;
obj.address = req.rsinfo.address;
obj.port = req.rsinfo.port;
obj.creationDate = new Date();
callback(null, obj);
}

Expand Down

0 comments on commit 2430e68

Please sign in to comment.