use of com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket in project azure-iot-sdk-java by Azure.
the class HttpsTransportTest method addMessageAddsToTransportQueue.
// Tests_SRS_HTTPSTRANSPORT_11_003: [The function shall add a packet containing the message, callback, and callback context to the transport queue.]
@Test
public <T extends Queue> void addMessageAddsToTransportQueue(@Mocked final Message mockMsg, @Mocked final IotHubEventCallback mockCallback, @Mocked final IotHubOutboundPacket mockPacket) throws IOException {
final Queue mockQueue = new MockUp<T>() {
}.getMockInstance();
final Map<String, Object> context = new HashMap<>();
HttpsTransport transport = new HttpsTransport(mockConfig);
transport.open();
transport.addMessage(mockMsg, mockCallback, context);
new VerificationsInOrder() {
{
new IotHubOutboundPacket(mockMsg, mockCallback, context);
mockQueue.add(mockPacket);
}
};
}
use of com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket in project azure-iot-sdk-java by Azure.
the class HttpsTransportTest method addMessageWithResponseAddsToTransportQueue.
// Tests_SRS_HTTPSTRANSPORT_21_017: [The function shall add a packet containing the message, callback, and callback context to the transport queue.]
@Test
public <T extends Queue> void addMessageWithResponseAddsToTransportQueue(@Mocked final Message mockMsg, @Mocked final IotHubResponseCallback mockCallback, @Mocked final IotHubOutboundPacket mockPacket) throws IOException {
final Queue mockQueue = new MockUp<T>() {
}.getMockInstance();
final Map<String, Object> context = new HashMap<>();
HttpsTransport transport = new HttpsTransport(mockConfig);
transport.open();
transport.addMessage(mockMsg, mockCallback, context);
new VerificationsInOrder() {
{
new IotHubOutboundPacket(mockMsg, mockCallback, context);
mockQueue.add(mockPacket);
}
};
}
use of com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket 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.IotHubOutboundPacket 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);
}
use of com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket in project azure-iot-sdk-java by Azure.
the class AmqpsTransport 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 synchronized void close() throws IOException {
// Codes_SRS_AMQPSTRANSPORT_15_007: [If the AMQPS connection is closed, the function shall do nothing.]
if (this.state == State.CLOSED) {
logger.LogInfo("The connection is already in closed state, method name is %s ", logger.getMethodName());
return;
}
// Codes_SRS_AMQPSTRANSPORT_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.waitingMessages.isEmpty()) {
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) {
IotHubCallbackPacket callbackPacket = new IotHubCallbackPacket(IotHubStatusCode.MESSAGE_CANCELLED_ONCLOSE, packet.getCallback(), packet.getContext());
this.callbackList.add(callbackPacket);
}
}
for (Map.Entry<Integer, IotHubOutboundPacket> entry : inProgressMessages.entrySet()) {
IotHubOutboundPacket packet = entry.getValue();
IotHubCallbackPacket callbackPacket = new IotHubCallbackPacket(IotHubStatusCode.MESSAGE_CANCELLED_ONCLOSE, packet.getCallback(), packet.getContext());
this.callbackList.add(callbackPacket);
}
// Codes_SRS_AMQPSTRANSPORT_99_037: [The method will invoke all the callbacks..]
invokeCallbacks();
// Codes_SRS_AMQPSTRANSPORT_15_033: [The map of messages in progress is cleared.]
inProgressMessages.clear();
logger.LogInfo("Starting to close the connection..., method name is %s ", logger.getMethodName());
// Codes_SRS_AMQPSTRANSPORT_15_008: [The function shall close an AMQPS connection with the IoT Hub given in the configuration.]
this.connection.close();
// Codes_SRS_AMQPSTRANSPORT_15_009: [The function shall set the transport state to CLOSED.]
this.state = State.CLOSED;
logger.LogInfo("Connection has been closed, method name is %s ", logger.getMethodName());
}
Aggregations