From 1e3ec46ddb561f307818b0cacfe2eddb55fcb48f Mon Sep 17 00:00:00 2001 From: fros4943 Date: Wed, 19 Mar 2008 09:41:03 +0000 Subject: [PATCH] added help method for generating unique mote type identifiers --- .../cooja/contikimote/ContikiMoteType.java | 52 ++++++++++++++++++- .../contikimote/ContikiMoteTypeDialog.java | 33 ++---------- 2 files changed, 54 insertions(+), 31 deletions(-) diff --git a/tools/cooja/java/se/sics/cooja/contikimote/ContikiMoteType.java b/tools/cooja/java/se/sics/cooja/contikimote/ContikiMoteType.java index cfe0dfbc4..215a0e2ac 100644 --- a/tools/cooja/java/se/sics/cooja/contikimote/ContikiMoteType.java +++ b/tools/cooja/java/se/sics/cooja/contikimote/ContikiMoteType.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: ContikiMoteType.java,v 1.25 2008/02/12 15:04:43 fros4943 Exp $ + * $Id: ContikiMoteType.java,v 1.26 2008/03/19 09:41:03 fros4943 Exp $ */ package se.sics.cooja.contikimote; @@ -1158,6 +1158,56 @@ public class ContikiMoteType implements MoteType { return messageDigest.digest(); } + /** + * Generates a unique Contiki mote type ID. + * + * @param existingTypes Already existing mote types, may be null + * @param reservedIdentifiers Already reserved identifiers, may be null + * @return Unique mote type ID. + */ + public static String generateUniqueMoteTypeID(Collection existingTypes, Collection reservedIdentifiers) { + int counter = 0; + String testID = ""; + boolean okID = false; + + while (!okID) { + counter++; + testID = ContikiMoteTypeDialog.ID_PREFIX + counter; + okID = true; + + // Check if identifier is reserved + if (reservedIdentifiers != null && reservedIdentifiers.contains(testID)) { + okID = false; + } + + if (!okID) { + continue; + } + + // Check if identifier is used + if (existingTypes != null) { + for (MoteType existingMoteType : existingTypes) { + if (existingMoteType.getIdentifier().equals(testID)) { + okID = false; + break; + } + } + } + + if (!okID) { + continue; + } + + // Check if identifier library has been loaded + File libraryFile = new File(ContikiMoteType.tempOutputDirectory, testID + ContikiMoteType.librarySuffix); + if (CoreComm.hasLibraryFileBeenLoaded(libraryFile)) { + okID = false; + } + } + + return testID; + } + /** * Returns a panel with interesting data for this mote type. * diff --git a/tools/cooja/java/se/sics/cooja/contikimote/ContikiMoteTypeDialog.java b/tools/cooja/java/se/sics/cooja/contikimote/ContikiMoteTypeDialog.java index eaada2a52..5ef12ecdc 100644 --- a/tools/cooja/java/se/sics/cooja/contikimote/ContikiMoteTypeDialog.java +++ b/tools/cooja/java/se/sics/cooja/contikimote/ContikiMoteTypeDialog.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: ContikiMoteTypeDialog.java,v 1.41 2008/02/12 15:25:41 fros4943 Exp $ + * $Id: ContikiMoteTypeDialog.java,v 1.42 2008/03/19 09:41:03 fros4943 Exp $ */ package se.sics.cooja.contikimote; @@ -157,35 +157,8 @@ public class ContikiMoteTypeDialog extends JDialog { myDialog.setTitle("Recompile Mote Type"); } else { // Suggest new identifier - int counter = 0; - String testIdentifier = ""; - boolean identifierOK = false; - while (!identifierOK) { - counter++; - testIdentifier = ID_PREFIX + counter; - identifierOK = true; - - // Check if identifier is already used by some other type - for (MoteType existingMoteType : myDialog.allOtherTypes) { - if (existingMoteType != myDialog.myMoteType - && existingMoteType.getIdentifier().equals(testIdentifier)) { - identifierOK = false; - break; - } - } - - // Check if library file with given identifier has already been loaded - if (identifierOK - && CoreComm.hasLibraryFileBeenLoaded(new File( - ContikiMoteType.tempOutputDirectory, - testIdentifier - + ContikiMoteType.librarySuffix))) { - identifierOK = false; - } - } - - myDialog.textID.setText(testIdentifier); - + String suggestedID = ContikiMoteType.generateUniqueMoteTypeID(myDialog.allOtherTypes, null); + myDialog.textID.setText(suggestedID); } // Set preset description of mote type