use of com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket in project azure-iot-sdk-java by Azure.
the class AmqpsTransportTest method isEmptyReturnsFalseIfCallbackListIsNotEmpty.
// Tests_SRS_AMQPSTRANSPORT_15_035: [The function shall return true if the waiting list,
// in progress list and callback list are all empty, and false otherwise.]
@Test
public void isEmptyReturnsFalseIfCallbackListIsNotEmpty() throws IOException {
AmqpsTransport transport = new AmqpsTransport(mockConfig, false);
Queue<IotHubCallbackPacket> callbackList = new LinkedList<>();
callbackList.add(mockIotHubCallbackPacket);
Deencapsulation.setField(transport, "callbackList", callbackList);
Boolean isEmpty = transport.isEmpty();
Assert.assertFalse(isEmpty);
}
use of com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket in project azure-iot-sdk-java by Azure.
the class AmqpsTransportTest method messageSentBuffersPreviouslySentMessageIfNotSuccessfullyDelivered.
// Tests_SRS_AMQPSTRANSPORT_15_031: [If the message was not delivered successfully, it is buffered to be sent again.]
@Test
public void messageSentBuffersPreviouslySentMessageIfNotSuccessfullyDelivered() throws IOException {
new NonStrictExpectations() {
{
new AmqpsIotHubConnection(mockConfig, false);
result = mockConnection;
}
};
AmqpsTransport transport = new AmqpsTransport(mockConfig, false);
transport.open();
Map<Integer, IotHubOutboundPacket> inProgressMessages = new ConcurrentHashMap<>();
inProgressMessages.put(1, new IotHubOutboundPacket(new Message(), mockIotHubEventCallback, new Object()));
inProgressMessages.put(2, new IotHubOutboundPacket(new Message(), mockIotHubEventCallback, new Object()));
Deencapsulation.setField(transport, "inProgressMessages", inProgressMessages);
transport.messageSent(1, false);
new Verifications() {
{
new IotHubCallbackPacket(IotHubStatusCode.OK_EMPTY, (IotHubEventCallback) any, any);
times = 0;
}
};
Queue<IotHubOutboundPacket> waitingMessages = Deencapsulation.getField(transport, "waitingMessages");
Queue<IotHubCallbackPacket> callbackList = Deencapsulation.getField(transport, "callbackList");
Assert.assertTrue(inProgressMessages.size() == 1);
Assert.assertTrue(waitingMessages.size() == 1);
Assert.assertTrue(callbackList.size() == 0);
}
use of com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket 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);
}
}
}
use of com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket in project azure-iot-sdk-java by Azure.
the class AmqpsTransport method invokeCallbacks.
/**
* Invokes the callbacks for all completed requests.
*
* @throws IllegalStateException if the transport is closed.
*/
public void invokeCallbacks() throws IllegalStateException {
// Codes_SRS_AMQPSTRANSPORT_15_019: [If the transport closed, the function shall throw an IllegalStateException.]
if (this.state == State.CLOSED) {
logger.LogError("Cannot invoke callbacks when AMQPS transport is closed, method name is %s ", logger.getMethodName());
throw new IllegalStateException("Cannot invoke callbacks when AMQPS transport is closed.");
}
// Codes_SRS_AMQPSTRANSPORT_15_020: [The function shall invoke all the callbacks from the callback queue.]
while (!this.callbackList.isEmpty()) {
IotHubCallbackPacket packet = this.callbackList.remove();
IotHubStatusCode status = packet.getStatus();
IotHubEventCallback callback = packet.getCallback();
Object context = packet.getContext();
logger.LogInfo("Invoking the callback function for sent message, IoT Hub responded to message with status %s, method name is %s ", status.name(), logger.getMethodName());
callback.execute(status, context);
}
}
use of com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket 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);
}
Aggregations