/** * Creates a device client that uses the provided security provider for authentication. * * @param uri The connection string for iot hub to connect to (format: "yourHubName.azure-devices.net") * @param deviceId The id for the device to use * @param securityProvider The security provider for the device * @param protocol The protocol the device shall use for communication to the IoT Hub * @return The created device client instance * @throws URISyntaxException If the provided connString could not be parsed. * @throws IOException If the SecurityProvider throws any exception while authenticating */ public static DeviceClient createFromSecurityProvider(String uri, String deviceId, SecurityProvider securityProvider, IotHubClientProtocol protocol) throws URISyntaxException, IOException { return new DeviceClient(uri, deviceId, securityProvider, protocol); }
public void openConnection() throws IOException, URISyntaxException, InterruptedException { client = new DeviceClient(connString, protocol); IotHubServicesCommon.openClientWithRetry(client); }
/** * @param props * @throws URISyntaxException * @throws IOException */ public AzureIoTHubLogger(final Map<String, Object> props) throws URISyntaxException, IOException { super(); this.deviceClient = new DeviceClient((String)props.get("connectionString"), IotHubClientProtocol.valueOf((String)props.get("protocol"))); this.deviceClient.open(); }
private void setUpDeviceClient(IotHubClientProtocol protocol) throws URISyntaxException, InterruptedException { deviceClient = new DeviceClient(DeviceConnectionString.get(iotHubConnectionString, scDevice), protocol); IotHubServicesCommon.openClientWithRetry(deviceClient); }
private void setUpX509DeviceClient(IotHubClientProtocol protocol) throws URISyntaxException, InterruptedException { deviceClient = new DeviceClient(DeviceConnectionString.get(iotHubConnectionString, scDevicex509), protocol, publicKeyCertificate, false, privateKeyCertificate, false); IotHubServicesCommon.openClientWithRetry(deviceClient); }
public static void setUp() throws IOException, IotHubException, InterruptedException, URISyntaxException { jobClient = JobClient.createFromConnectionString(iotHubConnectionString); registryManager = RegistryManager.createFromConnectionString(iotHubConnectionString); String uuid = UUID.randomUUID().toString(); for (int i = 0; i < MAX_DEVICES; i++) { testDevice = Tools.addDeviceWithRetry(registryManager, Device.createFromId(DEVICE_ID_NAME.concat("-" + i + "-" + uuid), DeviceStatus.Enabled, null)); DeviceTestManager testManager = new DeviceTestManager(new DeviceClient(registryManager.getDeviceConnectionString(testDevice), IotHubClientProtocol.AMQPS)); testManager.start(); devices.add(testManager); } }
@Test public void sendMessagesOverAmqpsWs() throws URISyntaxException, IOException, InterruptedException { TransportClient transportClient = new TransportClient(AMQPS_WS); ArrayList<InternalClient> clientArrayList = new ArrayList<>(); for (int i = 0; i < MAX_DEVICE_MULTIPLEX; i++) { clientArrayList.add(new DeviceClient(clientConnectionStringArrayList[i], transportClient)); } openTransportClientWithRetry(transportClient, clientArrayList); for (int i = 0; i < clientArrayList.size(); i++) { sendMessagesMultiplex(clientArrayList.get(i), IotHubClientProtocol.AMQPS_WS, NUM_MESSAGES_PER_CONNECTION, RETRY_MILLISECONDS, SEND_TIMEOUT_MILLISECONDS); } transportClient.closeNow(); }
@Test public void sendMessagesOverAmqps() throws URISyntaxException, IOException, InterruptedException { TransportClient transportClient = new TransportClient(AMQPS); ArrayList<InternalClient> clientArrayList = new ArrayList<>(); for (int i = 0; i < MAX_DEVICE_MULTIPLEX; i++) { clientArrayList.add(new DeviceClient(clientConnectionStringArrayList[i], transportClient)); } openTransportClientWithRetry(transportClient, clientArrayList); for (int i = 0; i < clientArrayList.size(); i++) { sendMessagesMultiplex(clientArrayList.get(i), IotHubClientProtocol.AMQPS, NUM_MESSAGES_PER_CONNECTION, RETRY_MILLISECONDS, SEND_TIMEOUT_MILLISECONDS); } transportClient.closeNow(); }
@Test public void sendMessagesOverAmqpsWsMultithreaded() throws InterruptedException, URISyntaxException, IOException { TransportClient transportClient = new TransportClient(AMQPS_WS); ArrayList<InternalClient> clientArrayList = new ArrayList<>(); for (int i = 0; i < MAX_DEVICE_MULTIPLEX; i++) { clientArrayList.add(new DeviceClient(clientConnectionStringArrayList[i], transportClient)); } CountDownLatch cdl = new CountDownLatch(clientArrayList.size()); IotHubServicesCommon.openTransportClientWithRetry(transportClient, clientArrayList); for (int i = 0; i < clientArrayList.size(); i++) { new Thread( new MultiplexRunnable( deviceListAmqps[i], clientArrayList.get(i), NUM_MESSAGES_PER_CONNECTION, NUM_KEYS_PER_MESSAGE, SEND_TIMEOUT_MILLISECONDS, cdl)) .start(); } cdl.await(); if(!succeed.get()) { Assert.fail(CorrelationDetailsLoggingAssert.buildExceptionMessage("Sending message over AMQP protocol in parallel failed", clientArrayList)); } transportClient.closeNow(); }
@Test public void sendMessagesOverAmqpsMultithreaded() throws InterruptedException, URISyntaxException, IOException { TransportClient transportClient = new TransportClient(AMQPS); ArrayList<InternalClient> clientArrayList = new ArrayList<>(); for (int i = 0; i < MAX_DEVICE_MULTIPLEX; i++) { clientArrayList.add(new DeviceClient(clientConnectionStringArrayList[i], transportClient)); } CountDownLatch cdl = new CountDownLatch(clientArrayList.size()); IotHubServicesCommon.openTransportClientWithRetry(transportClient, clientArrayList); for (int i = 0; i < clientArrayList.size(); i++) { new Thread( new MultiplexRunnable( deviceListAmqps[i], clientArrayList.get(i), NUM_MESSAGES_PER_CONNECTION, NUM_KEYS_PER_MESSAGE, SEND_TIMEOUT_MILLISECONDS, cdl)) .start(); } cdl.await(); if(!succeed.get()) { Assert.fail(CorrelationDetailsLoggingAssert.buildExceptionMessage("Sending message over AMQP protocol in parallel failed", clientArrayList)); } transportClient.closeNow(); }
@Test public void receiveMessagesOverAmqpsIncludingProperties() throws Exception { TransportClient transportClient = new TransportClient(AMQPS); final ArrayList<InternalClient> clientArrayList = new ArrayList<>(); for (int i = 0; i < MAX_DEVICE_MULTIPLEX; i++) { clientArrayList.add(new DeviceClient(clientConnectionStringArrayList[i], transportClient)); } IotHubServicesCommon.openTransportClientWithRetry(transportClient, clientArrayList); for (int i = 0; i < clientArrayList.size(); i++) { Success messageReceived = new Success(); com.microsoft.azure.sdk.iot.device.MessageCallback callback = new MessageCallback(); ((DeviceClient)clientArrayList.get(i)).setMessageCallback(callback, messageReceived); sendMessageToDevice(deviceListAmqps[i].getDeviceId(), "AMQPS"); waitForMessageToBeReceived(messageReceived, "AMQPS", clientArrayList.get(i)); Thread.sleep(200); } transportClient.closeNow(); }
@Test public void receiveMessagesOverAmqpWSIncludingProperties() throws Exception { TransportClient transportClient = new TransportClient(AMQPS); final ArrayList<InternalClient> clientArrayList = new ArrayList<>(); for (int i = 0; i < MAX_DEVICE_MULTIPLEX; i++) { clientArrayList.add(new DeviceClient(clientConnectionStringArrayList[i], transportClient)); } IotHubServicesCommon.openTransportClientWithRetry(transportClient, clientArrayList); for (int i = 0; i < clientArrayList.size(); i++) { Success messageReceived = new Success(); com.microsoft.azure.sdk.iot.device.MessageCallback callback = new MessageCallback(); ((DeviceClient)clientArrayList.get(i)).setMessageCallback(callback, messageReceived); sendMessageToDevice(deviceListAmqps[i].getDeviceId(), "AMQPS_WS"); waitForMessageToBeReceived(messageReceived, "AMQPS_WS", clientArrayList.get(i)); Thread.sleep(200); } transportClient.closeNow(); }
private void createDevice(IotHubClientProtocol protocol) throws IOException, URISyntaxException, InterruptedException { testDevice.deviceClient = new DeviceClient(DeviceConnectionString.get(iotHubConnectionString, deviceForRegistryManager), protocol); IotHubServicesCommon.openClientWithRetry(testDevice.deviceClient); testDevice.deviceClient.startDeviceTwin(new DeviceTwinStatusCallBack(), testDevice, new DeviceTwinPropertyCallback(), testDevice); }
@Test public void invokeMethodAMQPSWSSucceed() throws Exception { TransportClient transportClient = new TransportClient(AMQPS_WS); ArrayList<InternalClient> clientArrayList = new ArrayList<>(); for (int i = 0; i < MAX_DEVICE_MULTIPLEX; i++) { clientArrayList.add(new DeviceClient(clientConnectionStringArrayList[i], transportClient)); } IotHubServicesCommon.openTransportClientWithRetry(transportClient, clientArrayList); for (int i = 0; i < clientArrayList.size(); i++) { ((DeviceClient)clientArrayList.get(i)).subscribeToDeviceMethod(new SampleDeviceMethodCallback(), null, new DeviceMethodStatusCallBack(), null); CountDownLatch countDownLatch = new CountDownLatch(1); RunnableInvoke runnableInvoke = new RunnableInvoke(methodServiceClient, deviceListAmqps[i].getDeviceId(), METHOD_NAME, METHOD_PAYLOAD, countDownLatch); new Thread(runnableInvoke).start(); countDownLatch.await(); MethodResult result = runnableInvoke.getResult(); Assert.assertNotNull(buildExceptionMessage(runnableInvoke.getException() == null ? "Runnable returns null without exception information" : runnableInvoke.getException().getMessage(), clientArrayList.get(i)), result); Assert.assertEquals(buildExceptionMessage("result was not success, but was: " + result.getStatus(), clientArrayList.get(i)), (long)METHOD_SUCCESS,(long)result.getStatus()); Assert.assertEquals(buildExceptionMessage("Received unexpected payload", clientArrayList.get(i)), runnableInvoke.getExpectedPayload(), METHOD_NAME + ":" + result.getPayload().toString()); } transportClient.closeNow(); }
@Test public void invokeMethodAMQPSSucceed() throws Exception { TransportClient transportClient = new TransportClient(AMQPS); ArrayList<InternalClient> clientArrayList = new ArrayList<>(); for (int i = 0; i < MAX_DEVICE_MULTIPLEX; i++) { clientArrayList.add(new DeviceClient(clientConnectionStringArrayList[i], transportClient)); } IotHubServicesCommon.openTransportClientWithRetry(transportClient, clientArrayList); for (int i = 0; i < clientArrayList.size(); i++) { ((DeviceClient)clientArrayList.get(i)).subscribeToDeviceMethod(new SampleDeviceMethodCallback(), null, new DeviceMethodStatusCallBack(), null); CountDownLatch countDownLatch = new CountDownLatch(1); RunnableInvoke runnableInvoke = new RunnableInvoke(methodServiceClient, deviceListAmqps[i].getDeviceId(), METHOD_NAME, METHOD_PAYLOAD, countDownLatch); new Thread(runnableInvoke).start(); countDownLatch.await(); MethodResult result = runnableInvoke.getResult(); Assert.assertNotNull(buildExceptionMessage(runnableInvoke.getException() == null ? "Runnable returns null without exception information" : runnableInvoke.getException().getMessage(), clientArrayList.get(i)), result); Assert.assertEquals(buildExceptionMessage("result was not success, but was: " + result.getStatus(), clientArrayList.get(i)), (long)METHOD_SUCCESS,(long)result.getStatus()); Assert.assertEquals(buildExceptionMessage("Received unexpected payload", clientArrayList.get(i)), runnableInvoke.getExpectedPayload(), METHOD_NAME + ":" + result.getPayload().toString()); } transportClient.closeNow(); }
this.client = new DeviceClient(connectionString, protocol); this.client = new DeviceClient(connectionString, protocol, publicCert, false, privateKey, false);
clientArrayList.add(new DeviceClient(clientConnectionStringArrayList[i], transportClient));
clientArrayList.add(new DeviceClient(clientConnectionStringArrayList[i], transportClient));
@Test public void tokenExpiredAfterOpenButBeforeSendHttp() throws InvalidKeyException, IOException, InterruptedException, URISyntaxException { if (testInstance.protocol != HTTPS || testInstance.authenticationType != SAS) { //This scenario only applies to HTTP since MQTT and AMQP allow expired sas tokens for 30 minutes after open // as long as token did not expire before open. X509 doesn't apply either return; } String soonToBeExpiredSASToken = generateSasTokenForIotDevice(hostName, testInstance.identity.getDeviceId(), testInstance.identity.getPrimaryKey(), SECONDS_FOR_SAS_TOKEN_TO_LIVE); DeviceClient client = new DeviceClient(soonToBeExpiredSASToken, testInstance.protocol); IotHubServicesCommon.openClientWithRetry(client); //Force the SAS token to expire before sending messages Thread.sleep(MILLISECONDS_TO_WAIT_FOR_TOKEN_TO_EXPIRE); IotHubServicesCommon.sendMessagesExpectingSASTokenExpiration(client, testInstance.protocol.toString(), 1, RETRY_MILLISECONDS, SEND_TIMEOUT_MILLISECONDS, testInstance.authenticationType); client.closeNow(); }
deviceState.deviceClient = new DeviceClient(deviceState.connectionString, transportClient); devicesUnderTest[i].dCDeviceForTwin = new DeviceExtension(); clientArrayList.add(deviceState.deviceClient);