Skip to content

Commit

Permalink
Merge pull request #53 from janantala/develop
Browse files Browse the repository at this point in the history
Remove old device registration on the new registration
  • Loading branch information
dmoranj committed May 7, 2015
2 parents 3075dac + 1c00674 commit 6e870bc
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 49 deletions.
1 change: 1 addition & 0 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
- Fix MongoDB did not recover information at starting (#38).
- 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)
75 changes: 45 additions & 30 deletions lib/services/server/inMemoryDeviceRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,25 @@ var registry = {},
_ = require('underscore');

/**
* Inserts the given object in the registry. The generated ID is returned through the callback.
* Gets the device that has the device name passed as a parameter (should be unique) or return a DeviceNotFound error
* in case none exist.
*
* @param {Object} object Object to insert into the registry.
* @param {String} deviceName Name of the device to retrieve.
*/
function register(object, callback) {
var id = idCounter++;
registry[id] = object;
registry[id].id = id;
callback(null, id);
function getByName(deviceName, callback) {
var result;

for(var key in registry) {
if (registry[key] && registry[key].name === deviceName) {
result = registry[key];
}
}

if (result) {
callback(null, result);
} else {
callback(new errors.DeviceNotFound(deviceName));
}
}

/**
Expand All @@ -58,6 +68,33 @@ function unregister(id, callback) {
}
}

/**
* Inserts the given object in the registry and removes the old registration.
* The generated ID is returned through the callback.
*
* @param {Object} object Object to insert into the registry.
*/
function register(object, callback) {

function save(cb) {
var id = idCounter++;
registry[id] = object;
registry[id].id = id;
return cb(null, id);
}

getByName(object.name, function(error, result){
if (!error && result) {
unregister(result.id, function(err){
return save(callback);
});
}
else {
return save(callback);
}
});
}

/**
* Remove all the objects from the registry.
*/
Expand Down Expand Up @@ -110,28 +147,6 @@ function list(callback) {
callback(null, result);
}

/**
* Gets the device that has the device name passed as a parameter (should be unique) or return a DeviceNotFound error
* in case none exist.
*
* @param {String} deviceName Name of the device to retrieve.
*/
function getByName(deviceName, callback) {
var result;

for(var key in registry) {
if (registry[key] && registry[key].name === deviceName) {
result = registry[key];
}
}

if (result) {
callback(null, result);
} else {
callback(new errors.DeviceNotFound(deviceName));
}
}

/**
* Initializes the device registry based on the parameter found in the configuration. For this in memory registry this
* function doesn't do anything.
Expand All @@ -149,4 +164,4 @@ exports.update = update;
exports.clean = clean;
exports.list = list;
exports.getByName = getByName;
exports.init = init;
exports.init = init;
48 changes: 29 additions & 19 deletions lib/services/server/mongodbDeviceRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,26 @@ function getByName(deviceName, callback) {
}

/**
* Inserts the given object in the registry. The generated ID is returned through the callback.
* Removes the object identified by this id from the registry. The removed object is passed as the first callback
* parameter.
*
* @param {Integer} id Identifier of the object to be removed.
*/
function unregister(id, callback) {
Device.model.findOneAndRemove({ id: id }, function(error, device) {
if (error) {
callback(errors.InternalDbError(error));
} else if (device) {
callback(null, device.toObject());
} else {
callback(new errors.DeviceNotFound(id));
}
});
}

/**
* Inserts the given object in the registry and removes the old registration.
* The generated ID is returned through the callback.
*
* @param {Object} object Object to insert into the registry.
*/
Expand All @@ -114,23 +133,14 @@ function register(object, callback) {
deviceObj.save(innerCb);
}

mongoStore(saveDeviceHandler);
}

/**
* Removes the object identified by this id from the registry. The removed object is passed as the first callback
* parameter.
*
* @param {Integer} id Identifier of the object to be removed.
*/
function unregister(id, callback) {
Device.model.findOneAndRemove({ id: id }, function(error, device) {
if (error) {
callback(errors.InternalDbError(error));
} else if (device) {
callback(null, device.toObject());
} else {
callback(new errors.DeviceNotFound(id));
getByName(object.name, function(error, result){
if (!error && result) {
unregister(result.id, function(err){
return mongoStore(saveDeviceHandler);
});
}
else {
return mongoStore(saveDeviceHandler);
}
});
}
Expand Down Expand Up @@ -198,4 +208,4 @@ exports.update = update;
exports.clean = clean;
exports.list = list;
exports.getByName = getByName;
exports.init = init;
exports.init = init;

0 comments on commit 6e870bc

Please sign in to comment.