public void registerMultiplexedDeviceClient()

in iothub/device/iot-device-client/src/main/java/com/microsoft/azure/sdk/iot/device/transport/IotHubTransport.java [1045:1109]


    public void registerMultiplexedDeviceClient(List<ClientConfiguration> configs, long timeoutMilliseconds) throws InterruptedException, IotHubClientException, MultiplexingClientRegistrationException
    {
        if (getProtocol() != IotHubClientProtocol.AMQPS && getProtocol() != IotHubClientProtocol.AMQPS_WS)
        {
            throw new UnsupportedOperationException("Cannot add a multiplexed device unless connection is over AMQPS or AMQPS_WS");
        }

        multiplexingDeviceRegistrationFailures.clear();

        for (ClientConfiguration configToRegister : configs)
        {
            this.deviceClientConfigs.put(configToRegister.getDeviceId(), configToRegister);
            this.multiplexedDeviceConnectionStates.put(configToRegister.getDeviceId(), new MultiplexedDeviceState(IotHubConnectionStatus.DISCONNECTED));
            if (this.iotHubTransportConnection != null)
            {
                // Safe cast since amqps and amqps_ws always use this transport connection type.
                ((AmqpsIotHubConnection) this.iotHubTransportConnection).registerMultiplexedDevice(configToRegister);
            }
        }

        // If the multiplexed connection is active, block until all the registered devices have been connected.
        long timeoutTime = System.currentTimeMillis() + timeoutMilliseconds;
        MultiplexingClientRegistrationException registrationException = null;
        if (this.connectionStatus != IotHubConnectionStatus.DISCONNECTED)
        {
            for (ClientConfiguration newlyRegisteredConfig : configs)
            {
                String deviceId = newlyRegisteredConfig.getDeviceId();
                boolean deviceIsNotConnected = multiplexedDeviceConnectionStates.get(deviceId).getConnectionStatus() != IotHubConnectionStatus.CONNECTED;
                Exception deviceRegistrationException = multiplexingDeviceRegistrationFailures.remove(deviceId);
                while (deviceIsNotConnected && deviceRegistrationException == null)
                {
                    Thread.sleep(100);

                    deviceIsNotConnected = multiplexedDeviceConnectionStates.get(deviceId).getConnectionStatus() != IotHubConnectionStatus.CONNECTED;
                    deviceRegistrationException = multiplexingDeviceRegistrationFailures.remove(deviceId);
                    boolean operationHasTimedOut = System.currentTimeMillis() >= timeoutTime;
                    if (operationHasTimedOut)
                    {
                        throw new IotHubClientException(DEVICE_OPERATION_TIMED_OUT, "Timed out waiting for all device registrations to finish.");
                    }
                }

                if (deviceRegistrationException != null)
                {
                    if (registrationException == null)
                    {
                        registrationException = new MultiplexingClientRegistrationException("Failed to register one or more devices to the multiplexed connection.");
                    }

                    registrationException.addRegistrationException(deviceId, deviceRegistrationException);

                    // Since the registration failed, need to remove the device from the list of multiplexed devices
                    ClientConfiguration configThatFailedToRegister = this.deviceClientConfigs.remove(deviceId);
                    this.multiplexedDeviceConnectionStates.remove(deviceId);
                    ((AmqpsIotHubConnection) this.iotHubTransportConnection).unregisterMultiplexedDevice(configThatFailedToRegister, false);
                }
            }

            if (registrationException != null)
            {
                throw registrationException;
            }
        }
    }