Search in sources :

Example 1 with IotHubCallbackPacket

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

the class MqttTransport method invokeCallbacks.

/**
     * Invokes the callbacks for all completed requests.
     *
     * @throws IllegalStateException if the transport has not been opened or is closed.
     */
public void invokeCallbacks() throws IllegalStateException {
    synchronized (sendMessagesLock) {
        // the function shall throw an IllegalStateException.]
        if (this.state == State.CLOSED) {
            throw new IllegalStateException("MQTT transport is closed.");
        }
        // Codes_SRS_MQTTTRANSPORT_15_013: [The function shall invoke all callbacks on the callback queue.]
        while (!this.callbackList.isEmpty()) {
            // Codes_SRS_MQTTTRANSPORT_15_015: [If an exception is thrown during the callback,
            // the function shall drop the callback from the queue.]
            IotHubCallbackPacket packet = this.callbackList.remove();
            IotHubStatusCode status = packet.getStatus();
            IotHubEventCallback callback = packet.getCallback();
            Object context = packet.getContext();
            callback.execute(status, context);
        }
    }
}
Also used : IotHubCallbackPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket)

Example 2 with IotHubCallbackPacket

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

the class IotHubCallbackPacketTest method getResponseCallbackReturnsNull.

// Tests_SRS_IOTHUBCALLBACKPACKET_11_001: [The constructor shall save the status, eventCallback, and callback context.]
// Tests_SRS_IOTHUBCALLBACKPACKET_21_007: [The constructor shall set message and responseCallback as null.]
@Test
public void getResponseCallbackReturnsNull() {
    final IotHubStatusCode status = IotHubStatusCode.HUB_OR_DEVICE_ID_NOT_FOUND;
    final Map<String, Object> context = new HashMap<>();
    IotHubCallbackPacket packet = new IotHubCallbackPacket(status, mockEventCallback, context);
    IotHubResponseCallback testCallback = packet.getResponseCallback();
    ResponseMessage testMessage = packet.getResponseMessage();
    assertNull(testCallback);
    assertNull(testMessage);
}
Also used : IotHubStatusCode(com.microsoft.azure.sdk.iot.device.IotHubStatusCode) HashMap(java.util.HashMap) IotHubCallbackPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket) IotHubResponseCallback(com.microsoft.azure.sdk.iot.device.IotHubResponseCallback) ResponseMessage(com.microsoft.azure.sdk.iot.device.ResponseMessage) Test(org.junit.Test)

Example 3 with IotHubCallbackPacket

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

the class IotHubCallbackPacketTest method getStatusReturnsStatus.

// Tests_SRS_IOTHUBCALLBACKPACKET_11_001: [The constructor shall save the status, eventCallback, and callback context.]
// Tests_SRS_IOTHUBCALLBACKPACKET_11_002: [The function shall return the status given in the constructor.]
@Test
public void getStatusReturnsStatus() {
    final IotHubStatusCode status = IotHubStatusCode.BAD_FORMAT;
    final Map<String, Object> context = new HashMap<>();
    IotHubCallbackPacket packet = new IotHubCallbackPacket(status, mockEventCallback, context);
    IotHubStatusCode testStatus = packet.getStatus();
    final IotHubStatusCode expectedStatus = status;
    assertThat(testStatus, is(expectedStatus));
}
Also used : IotHubStatusCode(com.microsoft.azure.sdk.iot.device.IotHubStatusCode) HashMap(java.util.HashMap) IotHubCallbackPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket) Test(org.junit.Test)

Example 4 with IotHubCallbackPacket

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

the class IotHubCallbackPacketTest method getResponseCallbackReturnsCallback.

// Tests_SRS_IOTHUBCALLBACKPACKET_21_006: [The constructor shall save the responseMessage, responseCallback, and callback context.]
// Tests_SRS_IOTHUBCALLBACKPACKET_21_005: [The getResponseCallback shall return the responseCallback given in the constructor.]
@Test
public void getResponseCallbackReturnsCallback() {
    // arrange
    final Map<String, Object> context = new HashMap<>();
    // act
    IotHubCallbackPacket packet = new IotHubCallbackPacket(mockMessage, mockResponseCallback, context);
    // assert
    IotHubResponseCallback testCallback = packet.getResponseCallback();
    assertThat(testCallback, is(mockResponseCallback));
    ResponseMessage testMessage = packet.getResponseMessage();
    assertThat(testMessage, is(mockMessage));
    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) IotHubCallbackPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket) IotHubResponseCallback(com.microsoft.azure.sdk.iot.device.IotHubResponseCallback) ResponseMessage(com.microsoft.azure.sdk.iot.device.ResponseMessage) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 5 with IotHubCallbackPacket

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

the class IotHubCallbackPacketTest method getEventCallbackReturnsCallback.

// Tests_SRS_IOTHUBCALLBACKPACKET_11_001: [The constructor shall save the status, eventCallback, and callback context.]
// Tests_SRS_IOTHUBCALLBACKPACKET_11_003: [The function shall return the eventCallback given in the constructor.]
@Test
public void getEventCallbackReturnsCallback() {
    final IotHubStatusCode status = IotHubStatusCode.HUB_OR_DEVICE_ID_NOT_FOUND;
    final Map<String, Object> context = new HashMap<>();
    IotHubCallbackPacket packet = new IotHubCallbackPacket(status, mockEventCallback, context);
    IotHubEventCallback testCallback = packet.getCallback();
    assertThat(testCallback, is(mockEventCallback));
}
Also used : IotHubEventCallback(com.microsoft.azure.sdk.iot.device.IotHubEventCallback) IotHubStatusCode(com.microsoft.azure.sdk.iot.device.IotHubStatusCode) 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