Search in sources :

Example 21 with IotHubCallbackPacket

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

the class MqttTransport method sendMessages.

/**
     * <p>
     * Sends all messages on the transport queue, one at a time. If a previous
     * send attempt had failed, the function will attempt to resend the messages
     * in the previous attempt.
     * </p>
     * If one has not already been created, the function will initialize an
     * MQTT connection with the IoT Hub specified in the configuration.
     *
     * @throws IllegalStateException if the transport has not been opened or is closed.
     */
public void sendMessages() throws IllegalStateException {
    synchronized (sendMessagesLock) {
        // the function shall throw an IllegalStateException.]
        if (this.state == State.CLOSED) {
            throw new IllegalStateException("MQTT transport is closed.");
        }
        if (this.waitingList.size() <= 0) {
            return;
        }
        // on its waiting list, one at a time.]
        while (!this.waitingList.isEmpty()) {
            IotHubOutboundPacket packet = this.waitingList.remove();
            try {
                IotHubStatusCode status = this.mqttIotHubConnection.sendEvent(packet.getMessage());
                // Codes_SRS_MQTTTRANSPORT_15_010: [For each message being sent, the function shall add
                // the IoT Hub status code along with the callback and context to the callback list.]
                IotHubCallbackPacket callbackPacket = new IotHubCallbackPacket(status, packet.getCallback(), packet.getContext());
                this.callbackList.add(callbackPacket);
            }// shall be buffered to be sent again next time.]
             catch (IllegalStateException e) {
                this.waitingList.add(packet);
            }
        }
    }
}
Also used : IotHubCallbackPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket) IotHubOutboundPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket)

Example 22 with IotHubCallbackPacket

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

the class IotHubCallbackPacketTest method getContextReturnsContext.

// Tests_SRS_IOTHUBCALLBACKPACKET_11_001: [The constructor shall save the status, eventCallback, and callback context.]
// Tests_SRS_IOTHUBCALLBACKPACKET_11_004: [The function shall return the callback context given in the constructor.]
@Test
public void getContextReturnsContext() {
    final IotHubStatusCode status = IotHubStatusCode.OK;
    final Map<String, Object> context = new HashMap<>();
    final String key = "test-key";
    final String value = "test-value";
    context.put(key, value);
    IotHubCallbackPacket packet = new IotHubCallbackPacket(status, mockEventCallback, 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 : IotHubStatusCode(com.microsoft.azure.sdk.iot.device.IotHubStatusCode) HashMap(java.util.HashMap) IotHubCallbackPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 23 with IotHubCallbackPacket

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

the class IotHubCallbackPacketTest method getResponseCallbackSetEventCallbackAndStatusAsNull.

// Tests_SRS_IOTHUBCALLBACKPACKET_21_009: [The constructor shall set status and eventCallback as null.]
@Test
public void getResponseCallbackSetEventCallbackAndStatusAsNull() {
    // arrange
    final Map<String, Object> context = new HashMap<>();
    // act
    IotHubCallbackPacket packet = new IotHubCallbackPacket(mockMessage, mockResponseCallback, context);
    // assert
    assertNull(packet.getCallback());
    assertNull(packet.getStatus());
}
Also used : HashMap(java.util.HashMap) IotHubCallbackPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket) Test(org.junit.Test)

Aggregations

IotHubCallbackPacket (com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket)23 Test (org.junit.Test)14 IotHubOutboundPacket (com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket)10 HashMap (java.util.HashMap)9 AmqpsTransport (com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsTransport)6 AmqpsIotHubConnection (com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsIotHubConnection)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 IotHubStatusCode (com.microsoft.azure.sdk.iot.device.IotHubStatusCode)4 IotHubResponseCallback (com.microsoft.azure.sdk.iot.device.IotHubResponseCallback)2 ResponseMessage (com.microsoft.azure.sdk.iot.device.ResponseMessage)2 AmqpsMessage (com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsMessage)2 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 Queue (java.util.Queue)2 IotHubEventCallback (com.microsoft.azure.sdk.iot.device.IotHubEventCallback)1 MqttIotHubConnection (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection)1 MqttTransport (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttTransport)1 MessageImpl (org.apache.qpid.proton.message.impl.MessageImpl)1