Search in sources :

Example 6 with IotHubOutboundPacket

use of com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket in project azure-iot-sdk-java by Azure.

the class IotHubOutboundPacketTest method getContextReturnsContext.

// Tests_SRS_IOTHUBOUTBOUNDPACKET_11_001: [The constructor shall save the message, callback, and callback context.]
// Tests_SRS_IOTHUBOUTBOUNDPACKET_11_004: [The function shall return the callback context given in the constructor.] 
@Test
public void getContextReturnsContext() {
    final Map<String, Object> context = new HashMap<>();
    final String key = "test-key";
    final String value = "test-value";
    context.put(key, value);
    IotHubOutboundPacket packet = new IotHubOutboundPacket(mockMsg, mockCallback, context);
    Map<String, Object> testContext = (Map<String, Object>) packet.getContext();
    Set<Map.Entry<String, Object>> testEntrySet = testContext.entrySet();
    final Set<Map.Entry<String, Object>> expectedEntrySet = context.entrySet();
    assertThat(testEntrySet, everyItem(isIn(expectedEntrySet)));
}
Also used : HashMap(java.util.HashMap) IotHubOutboundPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 7 with IotHubOutboundPacket

use of com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket in project azure-iot-sdk-java by Azure.

the class AmqpsTransport method messageSent.

/**
     * When a message is acknowledged by IoTHub, it is removed from the list of in progress messages and its callback
     * is added to the list of callbacks to be executed. If the message was not successfully delivered, it is buffered
     * to be sent again.
     * @param messageHash The hash of the message.
     * @param deliveryState The state of the delivery.
     */
public void messageSent(Integer messageHash, Boolean deliveryState) {
    // Codes_SRS_AMQPSTRANSPORT_15_029: [If the hash cannot be found in the list of keys for the messages in progress, the method returns.]
    if (inProgressMessages.containsKey(messageHash)) {
        IotHubOutboundPacket packet = inProgressMessages.remove(messageHash);
        if (deliveryState) {
            logger.LogInfo("Message with messageid %s has been successfully delivered to IoTHub, adding a callback to callbacklist with IotHubStatusCode.OK_EMPTY, method name is %s ", packet.getMessage().getMessageId(), logger.getMethodName());
            // Codes_SRS_AMQPSTRANSPORT_15_030: [If the message was successfully delivered,
            // its callback is added to the list of callbacks to be executed.]
            IotHubCallbackPacket callbackPacket = new IotHubCallbackPacket(IotHubStatusCode.OK_EMPTY, packet.getCallback(), packet.getContext());
            this.callbackList.add(callbackPacket);
        } else {
            logger.LogInfo("Message with messageid %s was not delivered to IoTHub, it is buffered to be sent again, method name is %s ", packet.getMessage().getMessageId(), logger.getMethodName());
            // Codes_SRS_AMQPSTRANSPORT_15_031: [If the message was not delivered successfully, it is buffered to be sent again.]
            waitingMessages.add(packet);
        }
    }
}
Also used : IotHubCallbackPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket) IotHubOutboundPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket)

Example 8 with IotHubOutboundPacket

use of com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket in project azure-iot-sdk-java by Azure.

the class AmqpsTransport method sendMessages.

/**
     * <p>
     * Sends all messages from the waiting list, one at a time. If a previous
     * send attempt had failed, the function will attempt to resend the messages
     * in the previous attempt.
     * </p>
     *
     * @throws IOException if the server could not be reached.
     * @throws IllegalStateException if the transport has not been opened or is
     * already closed.
     */
public void sendMessages() throws IOException, IllegalStateException {
    // Codes_SRS_AMQPSTRANSPORT_15_012: [If the AMQPS session is closed, the function shall throw an IllegalStateException.]
    if (this.state == State.CLOSED) {
        logger.LogError("Cannot send messages when the AMQPS transport is closed, method name is %s ", logger.getMethodName());
        throw new IllegalStateException("Cannot send messages when the AMQPS transport is closed.");
    }
    // Codes_SRS_AMQPSTRANSPORT_15_013: [If there are no messages in the waiting list, the function shall return.]
    if (this.waitingMessages.size() <= 0) {
        return;
    }
    Collection<IotHubOutboundPacket> failedMessages = new ArrayList<>();
    // Codes_SRS_AMQPSTRANSPORT_15_014: [The function shall attempt to send every message on its waiting list, one at a time.]
    while (!this.waitingMessages.isEmpty()) {
        logger.LogInfo("Get the message from waiting message queue to be sent to IoT Hub, method name is %s ", logger.getMethodName());
        IotHubOutboundPacket packet = this.waitingMessages.remove();
        Message message = packet.getMessage();
        // Codes_SRS_AMQPSTRANSPORT_15_015: [The function shall skip messages with null or empty body.]
        if (message != null && message.getBytes().length > 0) {
            // with the MESSAGE_EXPIRED status and add it to the callback list.]
            if (message.isExpired()) {
                logger.LogInfo("Creating a callback for the expired message with MESSAGE_EXPIRED status, method name is %s ", logger.getMethodName());
                IotHubCallbackPacket callbackPacket = new IotHubCallbackPacket(IotHubStatusCode.MESSAGE_EXPIRED, packet.getCallback(), packet.getContext());
                this.callbackList.add(callbackPacket);
            } else {
                logger.LogInfo("Converting the IoT Hub message into AmqpsMessage, method name is %s ", logger.getMethodName());
                // Codes_SRS_AMQPSTRANSPORT_15_036: [The function shall create a new Proton message from the IoTHub message.]
                MessageImpl protonMessage = iotHubMessageToProtonMessage(message);
                // Codes_SRS_AMQPSTRANSPORT_15_037: [The function shall attempt to send the Proton message to IoTHub using the underlying AMQPS connection.]
                Integer sendHash = connection.sendMessage(protonMessage);
                // Codes_SRS_AMQPSTRANSPORT_15_016: [If the sent message hash is valid, it shall be added to the in progress map.]
                if (sendHash != -1) {
                    this.inProgressMessages.put(sendHash, packet);
                } else // Codes_SRS_AMQPSTRANSPORT_15_017: [If the sent message hash is not valid, it shall be buffered to be sent in a subsequent attempt.]
                {
                    failedMessages.add(packet);
                }
            }
        }
    }
    this.waitingMessages.addAll(failedMessages);
}
Also used : IotHubCallbackPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket) IotHubOutboundPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket) MessageImpl(org.apache.qpid.proton.message.impl.MessageImpl)

Example 9 with IotHubOutboundPacket

use of com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket in project azure-iot-sdk-java by Azure.

the class AmqpsTransport method close.

/**
     * Closes all resources used to communicate with an IoT Hub. Once {@code close()} is
     * called, the transport is no longer usable. If the transport is already
     * closed, the function shall do nothing.
     *
     * @throws IOException if an error occurs in closing the transport.
     */
public synchronized void close() throws IOException {
    // Codes_SRS_AMQPSTRANSPORT_15_007: [If the AMQPS connection is closed, the function shall do nothing.]
    if (this.state == State.CLOSED) {
        logger.LogInfo("The connection is already in closed state, method name is %s ", logger.getMethodName());
        return;
    }
    // Codes_SRS_AMQPSTRANSPORT_99_036: [The method will remove all the messages which are in progress or waiting to be sent and add them to the callback list.]
    while (!this.waitingMessages.isEmpty()) {
        IotHubOutboundPacket packet = this.waitingMessages.remove();
        Message message = packet.getMessage();
        // Codes_SRS_AMQPSTRANSPORT_15_015: [The function shall skip messages with null or empty body.]
        if (message != null && message.getBytes().length > 0) {
            IotHubCallbackPacket callbackPacket = new IotHubCallbackPacket(IotHubStatusCode.MESSAGE_CANCELLED_ONCLOSE, packet.getCallback(), packet.getContext());
            this.callbackList.add(callbackPacket);
        }
    }
    for (Map.Entry<Integer, IotHubOutboundPacket> entry : inProgressMessages.entrySet()) {
        IotHubOutboundPacket packet = entry.getValue();
        IotHubCallbackPacket callbackPacket = new IotHubCallbackPacket(IotHubStatusCode.MESSAGE_CANCELLED_ONCLOSE, packet.getCallback(), packet.getContext());
        this.callbackList.add(callbackPacket);
    }
    // Codes_SRS_AMQPSTRANSPORT_99_037: [The method will invoke all the callbacks..]
    invokeCallbacks();
    // Codes_SRS_AMQPSTRANSPORT_15_033: [The map of messages in progress is cleared.]
    inProgressMessages.clear();
    logger.LogInfo("Starting to close the connection..., method name is %s ", logger.getMethodName());
    // Codes_SRS_AMQPSTRANSPORT_15_008: [The function shall close an AMQPS connection with the IoT Hub given in the configuration.]
    this.connection.close();
    // Codes_SRS_AMQPSTRANSPORT_15_009: [The function shall set the transport state to CLOSED.]
    this.state = State.CLOSED;
    logger.LogInfo("Connection has been closed, method name is %s ", logger.getMethodName());
}
Also used : IotHubCallbackPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket) IotHubOutboundPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 10 with IotHubOutboundPacket

use of com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket in project azure-iot-sdk-java by Azure.

the class AmqpsTransport method addMessage.

/**
     * Adds a message to the transport queue.
     *
     * @param message the message to be sent.
     * @param callback the callback to be invoked when a response for the message is received.
     * @param callbackContext the context to be passed in when the callback is invoked.
     *
     * @throws IllegalStateException if the transport is closed.
     */
public void addMessage(Message message, IotHubEventCallback callback, Object callbackContext) throws IllegalStateException {
    // Codes_SRS_AMQPSTRANSPORT_15_010: [If the AMQPS session is closed, the function shall throw an IllegalStateException.]
    if (this.state == State.CLOSED) {
        logger.LogError("Cannot add a message when the AMQPS transport is closed, method name is %s ", logger.getMethodName());
        throw new IllegalStateException("Cannot add a message when the AMQPS transport is closed.");
    }
    // Codes_SRS_AMQPSTRANSPORT_15_011: [The function shall add a packet containing the message, callback, and callback context to the queue of messages waiting to be sent.]
    IotHubOutboundPacket packet = new IotHubOutboundPacket(message, callback, callbackContext);
    this.waitingMessages.add(packet);
}
Also used : IotHubOutboundPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket)

Aggregations

IotHubOutboundPacket (com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket)37 Test (org.junit.Test)25 HashMap (java.util.HashMap)17 AmqpsTransport (com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsTransport)15 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)14 AmqpsIotHubConnection (com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsIotHubConnection)12 IotHubCallbackPacket (com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket)10 AmqpsMessage (com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsMessage)5 Queue (java.util.Queue)5 MqttTransport (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttTransport)4 MqttIotHubConnection (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection)3 IotHubEventCallback (com.microsoft.azure.sdk.iot.device.IotHubEventCallback)2 IotHubResponseCallback (com.microsoft.azure.sdk.iot.device.IotHubResponseCallback)2 LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)2 MessageImpl (org.apache.qpid.proton.message.impl.MessageImpl)2 Message (com.microsoft.azure.sdk.iot.device.Message)1 Map (java.util.Map)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 SizeLimitExceededException (javax.naming.SizeLimitExceededException)1 ApplicationProperties (org.apache.qpid.proton.amqp.messaging.ApplicationProperties)1