Search in sources :

Example 31 with IotHubOutboundPacket

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

the class HttpsTransport 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 has not been opened or is
     * already closed.
     */
public void addMessage(Message message, IotHubEventCallback callback, Object callbackContext) {
    // Codes_SRS_HTTPSTRANSPORT_11_027: [If the transport is closed, the function shall throw an IllegalStateException.]
    if (this.state == HttpsTransportState.CLOSED) {
        throw new IllegalStateException("Cannot add a message to an HTTPS transport that is closed.");
    }
    // Codes_SRS_HTTPSTRANSPORT_11_003: [The function shall add a packet containing the message, callback, and callback context to the transport queue.]
    IotHubOutboundPacket packet = new IotHubOutboundPacket(message, callback, callbackContext);
    this.waitingList.add(packet);
}
Also used : IotHubOutboundPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket)

Example 32 with IotHubOutboundPacket

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

the class HttpsTransport method moveWaitingListToInProgressList.

/**
     * Moves as many messages as can be sent in one HTTPS request from the
     * waiting list to the in-progress list. If a single message is moved to the
     * in-progress list, this indicates that the message is to be sent in the
     * un-batched message format.
     */
private void moveWaitingListToInProgressList() {
    HttpsBatchMessage batch = new HttpsBatchMessage();
    while (!this.waitingList.isEmpty()) {
        IotHubOutboundPacket packet = this.waitingList.peek();
        try {
            HttpsSingleMessage httpsMsg = HttpsSingleMessage.parseHttpsMessage(packet.getMessage());
            batch.addMessage(httpsMsg);
        } catch (SizeLimitExceededException e) {
            break;
        }
        this.waitingList.remove();
        this.inProgressList.add(packet);
    }
    if (!this.waitingList.isEmpty() && batch.numMessages() <= 0) {
        IotHubOutboundPacket packet = this.waitingList.remove();
        this.inProgressList.add(packet);
    }
}
Also used : SizeLimitExceededException(javax.naming.SizeLimitExceededException) IotHubOutboundPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket)

Example 33 with IotHubOutboundPacket

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

the class HttpsTransport 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 has not been opened or is
     * already closed.
     */
public void addMessage(Message message, IotHubResponseCallback callback, Object callbackContext) {
    // Codes_SRS_HTTPSTRANSPORT_21_018: [If the transport is closed, the function shall throw an IllegalStateException.]
    if (this.state == HttpsTransportState.CLOSED) {
        throw new IllegalStateException("Cannot add a message to an HTTPS transport that is closed.");
    }
    // Codes_SRS_HTTPSTRANSPORT_21_017: [The function shall add a packet containing the message, callback, and callback context to the transport queue.]
    IotHubOutboundPacket packet = new IotHubOutboundPacket(message, callback, callbackContext);
    this.waitingList.add(packet);
}
Also used : IotHubOutboundPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket)

Example 34 with IotHubOutboundPacket

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

the class HttpsTransport 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 void close() throws IOException {
    // Codes_SRS_HTTPSTRANSPORT_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.waitingList.isEmpty()) {
        IotHubOutboundPacket packet = this.waitingList.remove();
        ResponseMessage responseMessage = new ResponseMessage(new byte[] {}, IotHubStatusCode.MESSAGE_CANCELLED_ONCLOSE);
        addOutboundPacketToCallbackList(packet, responseMessage);
    }
    while (!this.inProgressList.isEmpty()) {
        IotHubOutboundPacket packet = this.inProgressList.remove();
        ResponseMessage responseMessage = new ResponseMessage(new byte[] {}, IotHubStatusCode.MESSAGE_CANCELLED_ONCLOSE);
        addOutboundPacketToCallbackList(packet, responseMessage);
    }
    // Codes_SRS_HTTPSTRANSPORT_99_037: [The method will invoke all the callbacks]
    invokeCallbacks();
    // the HTTPS connection does not contain state
    // that needs to be explicitly destroyed.
    // Codes_SRS_HTTPSTRANSPORT_11_035: [The function shall mark the transport as being closed.]
    this.state = HttpsTransportState.CLOSED;
}
Also used : IotHubOutboundPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket)

Example 35 with IotHubOutboundPacket

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

the class MqttTransport 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.
     */
public void close() throws IOException {
    // Codes_SRS_MQTTTRANSPORT_15_006: [If the MQTT connection is closed, the function shall do nothing.]
    if (this.state == State.CLOSED) {
        return;
    }
    // Codes_SRS_MQTTTRANSPORT_99_020: [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.waitingList.isEmpty()) {
        IotHubOutboundPacket packet = this.waitingList.remove();
        IotHubCallbackPacket callbackPacket = new IotHubCallbackPacket(IotHubStatusCode.MESSAGE_CANCELLED_ONCLOSE, packet.getCallback(), packet.getContext());
        this.callbackList.add(callbackPacket);
    }
    // Codes_SRS_MQTTTRANSPORT_99_021: [The method will invoke the callback list]
    invokeCallbacks();
    // Codes_SRS_MQTTTRANSPORT_15_005: [The function shall close the MQTT connection
    // with the IoT Hub given in the configuration.]
    this.mqttIotHubConnection.close();
    this.state = State.CLOSED;
}
Also used : IotHubCallbackPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket) 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