/** * Generates and persists a random UUID suitable for use as MQTT client ID, * then returns this ID. MQTT clients in the same AWS account require a * unique client ID for each simulataneously connected client. This routine * generates UUIDs which can be considered "practically unique". See: * https://en.wikipedia.org/wiki/Universally_unique_identifier This routine * generates a UUID to be used as client ID and persists this value for * future use. Subsequent calls to this routine will return the same UUID * generated on the first call. * * @param filesDirectory directory to be used when persisting the client ID. * @return 36 character UUID string. * @throws IOException when error occurs accessing the filesystem. */ static synchronized String getPersistedClientId(String filesDirectory) throws IOException { File file = new File(filesDirectory, PERSISTENCE_FILE); if (clientId == null) { if (!file.exists()) { persistId(file, generateClientId()); } clientId = readPersistedId(file); } return clientId; }
@Test public void testGenerateClientId() throws Exception { String testId1 = AWSIotClientIdHelper.generateClientId(); String testId2 = AWSIotClientIdHelper.generateClientId(); String testId3 = AWSIotClientIdHelper.generateClientId(); // UUIDs are 36 character strings assertEquals(36, testId1.length()); assertEquals(36, testId2.length()); assertEquals(36, testId3.length()); // generate gets a new ID each call assertFalse(testId1.equals(testId2)); assertFalse(testId2.equals(testId3)); assertFalse(testId1.equals(testId3)); }
@Test public void testGetPersistedClientId() throws Exception { String testId = AWSIotClientIdHelper.getPersistedClientId(TEST_DIRECTORY); // UUIDs are 36 character strings assertEquals(36, testId.length()); // ensure same ID is returned 2nd time assertEquals(testId, AWSIotClientIdHelper.getPersistedClientId(TEST_DIRECTORY)); }
@Before public void setUp() throws Exception { // remove any existing file, clear the static class variable File persistenceFile = new File(TEST_DIRECTORY, PERSISTENCE_FILENAME); if (persistenceFile.exists()) { persistenceFile.delete(); } AWSIotClientIdHelper.reset(); }
@Test public void testGetPersistedClientIdFilePresent() throws Exception { File persistenceFile = new File(TEST_DIRECTORY, PERSISTENCE_FILENAME); FileOutputStream out = new FileOutputStream(persistenceFile); out.write("this-is-a-36-character-string-4-test".getBytes(StringUtils.UTF8)); out.close(); String testId = AWSIotClientIdHelper.getPersistedClientId(TEST_DIRECTORY); // UUIDs are 36 character strings assertEquals(36, testId.length()); // ensure same ID as create above is returned assertEquals("this-is-a-36-character-string-4-test", testId); } }
/** * Generates and persists a random UUID suitable for use as MQTT client ID, * then returns this ID. MQTT clients in the same AWS account require a * unique client ID for each simulataneously connected client. This routine * generates UUIDs which can be considered "practically unique". See: * https://en.wikipedia.org/wiki/Universally_unique_identifier This routine * generates a UUID to be used as client ID and persists this value for * future use. Subsequent calls to this routine will return the same UUID * generated on the first call. * * @param filesDirectory directory to be used when persisting the client ID. * @return 36 character UUID string. * @throws IOException when error occurs accessing the filesystem. */ static synchronized String getPersistedClientId(String filesDirectory) throws IOException { File file = new File(filesDirectory, PERSISTENCE_FILE); if (clientId == null) { if (!file.exists()) { persistId(file, generateClientId()); } clientId = readPersistedId(file); } return clientId; }