use of com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket 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());
}
use of com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket in project azure-iot-sdk-java by Azure.
the class HttpsTransport method invokeCallbacks.
/**
* Invokes the callbacks for all completed requests.
*
* @throws IllegalStateException if the transport has not been opened or is
* already closed.
*/
public void invokeCallbacks() {
// Codes_SRS_HTTPSTRANSPORT_11_031: [If the transport is closed, the function shall throw an IllegalStateException.]
if (this.state == HttpsTransportState.CLOSED) {
throw new IllegalStateException("Cannot invoke callbacks from " + "an HTTPS transport that is closed.");
}
// Codes_SRS_HTTPSTRANSPORT_11_007: [The function shall invoke all callbacks on the callback queue.]
while (!this.callbackList.isEmpty()) {
// Codes_SRS_HTTPSTRANSPORT_11_016: [If an exception is thrown during the callback, the function shall drop the callback from the queue.]
IotHubCallbackPacket packet = this.callbackList.remove();
Object context = packet.getContext();
IotHubEventCallback eventCallback = packet.getCallback();
if (eventCallback != null) {
eventCallback.execute(packet.getStatus(), context);
}
IotHubResponseCallback responseCallback = packet.getResponseCallback();
if (responseCallback != null) {
responseCallback.execute(packet.getResponseMessage(), context);
}
}
}
use of com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket in project azure-iot-sdk-java by Azure.
the class AmqpsTransportTest method sendMessagesAddsExpiredMessagesToCallbackListWithCorrectCode.
// Tests_SRS_AMQPSTRANSPORT_15_039: [If the message is expired, the function shall create a callback
// with the MESSAGE_EXPIRED status and add it to the callback list.]
@Test
public void sendMessagesAddsExpiredMessagesToCallbackListWithCorrectCode(@Mocked final Message mockMsg, @Mocked final IotHubEventCallback mockCallback, @Mocked final IotHubOutboundPacket mockPacket) throws IOException {
final Map<String, Object> context = new HashMap<>();
final byte[] messageBytes = new byte[] { 1, 2 };
new NonStrictExpectations() {
{
new AmqpsIotHubConnection(mockConfig, false);
result = mockConnection;
new IotHubOutboundPacket(mockMsg, mockCallback, context);
result = mockPacket;
mockPacket.getMessage();
result = mockMsg;
mockMsg.getBytes();
result = messageBytes;
mockMsg.isExpired();
returns(true, false);
mockConnection.sendMessage((org.apache.qpid.proton.message.Message) any);
result = 1;
}
};
AmqpsTransport transport = new AmqpsTransport(mockConfig, false);
transport.open();
transport.addMessage(mockMsg, mockCallback, context);
transport.addMessage(mockMsg, mockCallback, context);
transport.sendMessages();
Map<Integer, IotHubOutboundPacket> inProgressMessages = Deencapsulation.getField(transport, "inProgressMessages");
Assert.assertEquals(1, inProgressMessages.size());
Queue<IotHubOutboundPacket> waitingMessages = Deencapsulation.getField(transport, "waitingMessages");
Assert.assertEquals(0, waitingMessages.size());
Queue<IotHubCallbackPacket> callbackList = Deencapsulation.getField(transport, "callbackList");
Assert.assertEquals(1, callbackList.size());
new Verifications() {
{
new IotHubOutboundPacket(mockMsg, mockCallback, context);
times = 2;
mockPacket.getMessage();
times = 2;
mockConnection.sendMessage((org.apache.qpid.proton.message.Message) any);
times = 1;
new IotHubCallbackPacket(IotHubStatusCode.MESSAGE_EXPIRED, (IotHubEventCallback) any, any);
times = 1;
}
};
}
use of com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket in project azure-iot-sdk-java by Azure.
the class AmqpsTransportTest method invokeCallbacksInvokesAllCallbacksFromQueue.
// Tests_SRS_AMQPSTRANSPORT_15_020: [The function shall invoke all the callbacks from the callback queue.]
@Test
public void invokeCallbacksInvokesAllCallbacksFromQueue() throws IOException {
final Integer context = 24;
new NonStrictExpectations() {
{
new AmqpsIotHubConnection(mockConfig, false);
result = mockConnection;
mockIotHubCallbackPacket.getCallback();
result = mockIotHubEventCallback;
mockIotHubCallbackPacket.getStatus();
result = IotHubStatusCode.OK_EMPTY;
mockIotHubCallbackPacket.getContext();
result = context;
}
};
AmqpsTransport transport = new AmqpsTransport(mockConfig, false);
transport.open();
Queue<IotHubCallbackPacket> callbackList = new LinkedList<>();
callbackList.add(mockIotHubCallbackPacket);
callbackList.add(mockIotHubCallbackPacket);
Deencapsulation.setField(transport, "callbackList", callbackList);
transport.invokeCallbacks();
new Verifications() {
{
mockIotHubCallbackPacket.getStatus();
times = 2;
mockIotHubCallbackPacket.getCallback();
times = 2;
mockIotHubCallbackPacket.getCallback();
times = 2;
mockIotHubEventCallback.execute(IotHubStatusCode.OK_EMPTY, context);
times = 2;
}
};
}
use of com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket in project azure-iot-sdk-java by Azure.
the class AmqpsTransportTest method messageSentRemovesSuccessfullyDeliveredMessageFromInProgressMap.
// Tests_SRS_AMQPSTRANSPORT_15_030: [If the message was successfully delivered,
// its callback is added to the list of callbacks to be executed.]
@Test
public void messageSentRemovesSuccessfullyDeliveredMessageFromInProgressMap() 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, true);
new Verifications() {
{
new IotHubCallbackPacket(IotHubStatusCode.OK_EMPTY, (IotHubEventCallback) any, any);
times = 1;
}
};
Queue<IotHubOutboundPacket> waitingMessages = Deencapsulation.getField(transport, "waitingMessages");
Queue<IotHubCallbackPacket> callbackList = Deencapsulation.getField(transport, "callbackList");
Assert.assertTrue(inProgressMessages.size() == 1);
Assert.assertTrue(waitingMessages.size() == 0);
Assert.assertTrue(callbackList.size() == 1);
}
Aggregations