Search in sources :

Example 1 with AmqpsMessage

use of com.microsoft.azure.sdk.iot.deps.transport.amqp.AmqpsMessage in project azure-iot-sdk-java by Azure.

the class AmqpsMessageTest method acknowledgeTest.

@Test
public void acknowledgeTest() {
    // arrange
    AmqpsMessage message = new AmqpsMessage();
    message.setDelivery(mockDelivery);
    final DeliveryState expectedDisposition = Accepted.getInstance();
    // act
    message.acknowledge(expectedDisposition);
    // assert
    new Verifications() {

        {
            mockDelivery.disposition(expectedDisposition);
            mockDelivery.settle();
        }
    };
}
Also used : DeliveryState(org.apache.qpid.proton.amqp.transport.DeliveryState) Verifications(mockit.Verifications) AmqpsMessage(com.microsoft.azure.sdk.iot.deps.transport.amqp.AmqpsMessage) Test(org.junit.Test)

Example 2 with AmqpsMessage

use of com.microsoft.azure.sdk.iot.deps.transport.amqp.AmqpsMessage in project azure-iot-sdk-java by Azure.

the class AmqpsCbsReceiverLinkHandler method handleCBSResponseMessage.

private void handleCBSResponseMessage(Receiver receiver) {
    AmqpsMessage amqpsMessage = super.getMessageFromReceiverLink(receiver);
    if (amqpsMessage != null) {
        if (amqpsMessage.getApplicationProperties() != null && amqpsMessage.getProperties() != null) {
            Properties properties = amqpsMessage.getProperties();
            UUID correlationId = (UUID) properties.getCorrelationId();
            Map<String, Object> applicationProperties = amqpsMessage.getApplicationProperties().getValue();
            if (!this.correlationMap.containsKey(correlationId)) {
                log.error("Received cbs authentication message with no correlation id. Ignoring it...");
                amqpsMessage.acknowledge(Released.getInstance());
                return;
            }
            AuthenticationMessageCallback authenticationMessageCallback = this.correlationMap.remove(correlationId);
            for (Map.Entry<String, Object> entry : applicationProperties.entrySet()) {
                String propertyKey = entry.getKey();
                if (propertyKey.equals(APPLICATION_PROPERTY_STATUS_CODE) && entry.getValue() instanceof Integer) {
                    int authenticationResponseCode = (int) entry.getValue();
                    String statusDescription = "";
                    if (applicationProperties.containsKey(APPLICATION_PROPERTY_STATUS_DESCRIPTION)) {
                        statusDescription = (String) applicationProperties.get(APPLICATION_PROPERTY_STATUS_DESCRIPTION);
                    }
                    DeliveryState ackType = authenticationMessageCallback.handleAuthenticationResponseMessage(authenticationResponseCode, statusDescription, receiver.getSession().getConnection().getReactor());
                    amqpsMessage.acknowledge(ackType);
                    return;
                }
            }
        } else {
            log.warn("Could not handle authentication message because it had no application properties or had no system properties");
        }
    }
    // By default, we can't process the message
    if (amqpsMessage != null) {
        amqpsMessage.acknowledge(Released.getInstance());
    }
}
Also used : DeliveryState(org.apache.qpid.proton.amqp.transport.DeliveryState) Properties(org.apache.qpid.proton.amqp.messaging.Properties) UUID(java.util.UUID) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) AmqpsMessage(com.microsoft.azure.sdk.iot.deps.transport.amqp.AmqpsMessage)

Example 3 with AmqpsMessage

use of com.microsoft.azure.sdk.iot.deps.transport.amqp.AmqpsMessage in project azure-iot-sdk-java by Azure.

the class AmqpsReceiverLinkHandler method getMessageFromReceiverLink.

AmqpsMessage getMessageFromReceiverLink(Receiver receiver) {
    Delivery delivery = receiver.current();
    if (delivery == null) {
        return null;
    }
    if (delivery.isReadable() && !delivery.isPartial()) {
        int size = delivery.pending();
        byte[] buffer = new byte[size];
        int bytesRead = receiver.recv(buffer, 0, buffer.length);
        log.trace("read {} bytes from {} receiver link with address {} and link correlation id {}", bytesRead, getLinkInstanceType(), this.receiverLinkAddress, this.linkCorrelationId);
        boolean receiverLinkAdvanced = receiver.advance();
        if (!receiverLinkAdvanced) {
            log.warn("{} receiver link with link correlation id {} did not advance after bytes were read from it", getLinkInstanceType(), this.linkCorrelationId);
        }
        if (size != bytesRead) {
            log.warn("Amqp read operation on {} receiver link with link correlation id {} did not read the expected amount of bytes. Read {} but expected {}", getLinkInstanceType(), this.linkCorrelationId, bytesRead, size);
        }
        AmqpsMessage amqpsMessage = new AmqpsMessage();
        amqpsMessage.setDelivery(delivery);
        amqpsMessage.decode(buffer, 0, bytesRead);
        return amqpsMessage;
    }
    if (delivery.isPartial()) {
        log.trace("Partial delivery received on {} receiver link with address {} and link correlation id {}.", getLinkInstanceType(), this.receiverLinkAddress, this.linkCorrelationId);
    } else {
        // not partial, but not readable either
        log.warn("Unreadable delivery received on {} receiver link with address {} and link correlation id {}.", getLinkInstanceType(), this.receiverLinkAddress, this.linkCorrelationId);
    }
    return null;
}
Also used : AmqpsMessage(com.microsoft.azure.sdk.iot.deps.transport.amqp.AmqpsMessage)

Example 4 with AmqpsMessage

use of com.microsoft.azure.sdk.iot.deps.transport.amqp.AmqpsMessage in project azure-iot-sdk-java by Azure.

the class AmqpsReceiverLinkHandler method onDelivery.

@Override
public void onDelivery(Event event) {
    // Safe to cast as receiver here since this event only fires when a message is ready to be received over this receiver link
    Receiver receiverLink = (Receiver) event.getLink();
    AmqpsMessage amqpsMessage = this.getMessageFromReceiverLink(receiverLink);
    if (amqpsMessage == null) {
        // continue to be called until it is fully formed.
        return;
    }
    IotHubTransportMessage iotHubMessage = this.protonMessageToIoTHubMessage(amqpsMessage);
    this.receivedMessagesMap.put(iotHubMessage, amqpsMessage);
    this.amqpsLinkStateCallback.onMessageReceived(iotHubMessage);
    log.trace("Current link credit on {} receiver link with address {} and link correlation id {} is {}", this.getLinkInstanceType(), this.receiverLinkAddress, this.linkCorrelationId, receiverLink.getCredit());
}
Also used : IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) AmqpsMessage(com.microsoft.azure.sdk.iot.deps.transport.amqp.AmqpsMessage)

Example 5 with AmqpsMessage

use of com.microsoft.azure.sdk.iot.deps.transport.amqp.AmqpsMessage in project azure-iot-sdk-java by Azure.

the class CbsReceiverLinkHandler method handleCBSResponseMessage.

private void handleCBSResponseMessage() {
    AmqpsMessage amqpsMessage = super.getMessageFromReceiverLink();
    if (amqpsMessage != null) {
        if (amqpsMessage.getApplicationProperties() != null && amqpsMessage.getProperties() != null) {
            Properties properties = amqpsMessage.getProperties();
            UUID correlationId = (UUID) properties.getCorrelationId();
            Map<String, Object> applicationProperties = amqpsMessage.getApplicationProperties().getValue();
            if (!this.authenticationMessageCorrelationId.equals(correlationId)) {
                log.error("Received cbs authentication message with unexpected correlation id. Ignoring it...");
                amqpsMessage.acknowledge(Released.getInstance());
                return;
            }
            for (Map.Entry<String, Object> entry : applicationProperties.entrySet()) {
                String propertyKey = entry.getKey();
                if (propertyKey.equals(APPLICATION_PROPERTY_STATUS_CODE_TAG) && entry.getValue() instanceof Integer) {
                    int authenticationResponseCode = (int) entry.getValue();
                    String statusDescription = "";
                    if (applicationProperties.containsKey(APPLICATION_PROPERTY_STATUS_DESCRIPTION_TAG)) {
                        statusDescription = (String) applicationProperties.get(APPLICATION_PROPERTY_STATUS_DESCRIPTION_TAG);
                    }
                    DeliveryState ackType = authenticationMessageCallback.handleAuthenticationResponseMessage(authenticationResponseCode, statusDescription);
                    amqpsMessage.acknowledge(ackType);
                    return;
                }
            }
        } else {
            log.warn("Could not handle authentication message because it had no application properties or system properties");
        }
    } else {
        log.warn("Failed to read the message on the CBS receiver link");
    }
    // By default, we can't process the message
    if (amqpsMessage != null) {
        amqpsMessage.acknowledge(Released.getInstance());
    }
}
Also used : DeliveryState(org.apache.qpid.proton.amqp.transport.DeliveryState) Properties(org.apache.qpid.proton.amqp.messaging.Properties) UUID(java.util.UUID) Map(java.util.Map) AmqpsMessage(com.microsoft.azure.sdk.iot.deps.transport.amqp.AmqpsMessage)

Aggregations

AmqpsMessage (com.microsoft.azure.sdk.iot.deps.transport.amqp.AmqpsMessage)6 DeliveryState (org.apache.qpid.proton.amqp.transport.DeliveryState)3 Map (java.util.Map)2 UUID (java.util.UUID)2 Properties (org.apache.qpid.proton.amqp.messaging.Properties)2 IotHubTransportMessage (com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Verifications (mockit.Verifications)1 Delivery (org.apache.qpid.proton.engine.Delivery)1 Test (org.junit.Test)1