From 0cf0d0a57382e4153363493c9428296e3e6c2430 Mon Sep 17 00:00:00 2001 From: Jan Antala Date: Thu, 7 May 2015 14:18:57 +0200 Subject: [PATCH 1/5] FIX Remove old registration in memory registry --- lib/services/server/inMemoryDeviceRegistry.js | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/lib/services/server/inMemoryDeviceRegistry.js b/lib/services/server/inMemoryDeviceRegistry.js index aaa113b..d6463d6 100644 --- a/lib/services/server/inMemoryDeviceRegistry.js +++ b/lib/services/server/inMemoryDeviceRegistry.js @@ -29,15 +29,29 @@ var registry = {}, _ = require('underscore'); /** - * Inserts the given object in the registry. The generated ID is returned through the 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) { - var id = idCounter++; - registry[id] = object; - registry[id].id = id; - callback(null, id); + + 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); + } + }); } /** @@ -149,4 +163,4 @@ exports.update = update; exports.clean = clean; exports.list = list; exports.getByName = getByName; -exports.init = init; \ No newline at end of file +exports.init = init; From d820fc317c2d320fa8b7cfdda1d4df339c394f1b Mon Sep 17 00:00:00 2001 From: Jan Antala Date: Thu, 7 May 2015 14:20:40 +0200 Subject: [PATCH 2/5] FIX Remove old registration in mongodb registry --- lib/services/server/mongodbDeviceRegistry.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/services/server/mongodbDeviceRegistry.js b/lib/services/server/mongodbDeviceRegistry.js index 14a9800..4692512 100644 --- a/lib/services/server/mongodbDeviceRegistry.js +++ b/lib/services/server/mongodbDeviceRegistry.js @@ -88,7 +88,7 @@ function getByName(deviceName, callback) { } /** - * Inserts the given object in the registry. The generated ID is returned through the 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. */ @@ -114,7 +114,16 @@ function register(object, callback) { deviceObj.save(innerCb); } - mongoStore(saveDeviceHandler); + getByName(object.name, function(error, result){ + if (!error && result) { + unregister(result.id, function(err){ + return mongoStore(saveDeviceHandler); + }) + } + else { + return mongoStore(saveDeviceHandler); + } + }); } /** @@ -198,4 +207,4 @@ exports.update = update; exports.clean = clean; exports.list = list; exports.getByName = getByName; -exports.init = init; \ No newline at end of file +exports.init = init; From 0ca85a756b089b4b0f5f6ec79b1587f85837515a Mon Sep 17 00:00:00 2001 From: Jan Antala Date: Thu, 7 May 2015 14:27:57 +0200 Subject: [PATCH 3/5] ADD Changes to changelog --- CHANGES_NEXT_RELEASE | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index 58ce372..a2aa346 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -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) From 45bdd90b63624f4d941609b2dc48c1bc1c264484 Mon Sep 17 00:00:00 2001 From: Jan Antala Date: Thu, 7 May 2015 15:43:24 +0200 Subject: [PATCH 4/5] FIX lint errors --- lib/services/server/inMemoryDeviceRegistry.js | 81 ++++++++++--------- 1 file changed, 41 insertions(+), 40 deletions(-) diff --git a/lib/services/server/inMemoryDeviceRegistry.js b/lib/services/server/inMemoryDeviceRegistry.js index d6463d6..fd6e832 100644 --- a/lib/services/server/inMemoryDeviceRegistry.js +++ b/lib/services/server/inMemoryDeviceRegistry.js @@ -29,29 +29,25 @@ var registry = {}, _ = require('underscore'); /** - * Inserts the given object in the registry and removes the old registration. 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) { +function getByName(deviceName, callback) { + var result; - function save(cb) { - var id = idCounter++; - registry[id] = object; - registry[id].id = id; - return cb(null, id); + for(var key in registry) { + if (registry[key] && registry[key].name === deviceName) { + result = registry[key]; + } } - getByName(object.name, function(error, result){ - if (!error && result) { - unregister(result.id, function(err){ - return save(callback); - }) - } - else { - return save(callback); - } - }); + if (result) { + callback(null, result); + } else { + callback(new errors.DeviceNotFound(deviceName)); + } } /** @@ -72,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. */ @@ -124,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. From 1c00674b08ccf10f57d2bba1f5fe87100608088c Mon Sep 17 00:00:00 2001 From: Jan Antala Date: Thu, 7 May 2015 15:43:28 +0200 Subject: [PATCH 5/5] FIX lint errors --- lib/services/server/mongodbDeviceRegistry.js | 41 ++++++++++---------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/lib/services/server/mongodbDeviceRegistry.js b/lib/services/server/mongodbDeviceRegistry.js index 4692512..9f17dd7 100644 --- a/lib/services/server/mongodbDeviceRegistry.js +++ b/lib/services/server/mongodbDeviceRegistry.js @@ -88,7 +88,26 @@ function getByName(deviceName, callback) { } /** - * Inserts the given object in the registry and removes the old registration. 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. */ @@ -118,7 +137,7 @@ function register(object, callback) { if (!error && result) { unregister(result.id, function(err){ return mongoStore(saveDeviceHandler); - }) + }); } else { return mongoStore(saveDeviceHandler); @@ -126,24 +145,6 @@ function register(object, 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)); - } - }); -} - /** * Remove all the objects from the registry. */