use of com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket in project azure-iot-sdk-java by Azure.
the class MqttTransport method sendMessages.
/**
* <p>
* Sends all messages on the transport queue, one at a time. If a previous
* send attempt had failed, the function will attempt to resend the messages
* in the previous attempt.
* </p>
* If one has not already been created, the function will initialize an
* MQTT connection with the IoT Hub specified in the configuration.
*
* @throws IllegalStateException if the transport has not been opened or is closed.
*/
public void sendMessages() throws IllegalStateException {
synchronized (sendMessagesLock) {
// the function shall throw an IllegalStateException.]
if (this.state == State.CLOSED) {
throw new IllegalStateException("MQTT transport is closed.");
}
if (this.waitingList.size() <= 0) {
return;
}
// on its waiting list, one at a time.]
while (!this.waitingList.isEmpty()) {
IotHubOutboundPacket packet = this.waitingList.remove();
try {
IotHubStatusCode status = this.mqttIotHubConnection.sendEvent(packet.getMessage());
// Codes_SRS_MQTTTRANSPORT_15_010: [For each message being sent, the function shall add
// the IoT Hub status code along with the callback and context to the callback list.]
IotHubCallbackPacket callbackPacket = new IotHubCallbackPacket(status, packet.getCallback(), packet.getContext());
this.callbackList.add(callbackPacket);
}// shall be buffered to be sent again next time.]
catch (IllegalStateException e) {
this.waitingList.add(packet);
}
}
}
}
use of com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket in project azure-iot-sdk-java by Azure.
the class IotHubCallbackPacketTest method getContextReturnsContext.
// Tests_SRS_IOTHUBCALLBACKPACKET_11_001: [The constructor shall save the status, eventCallback, and callback context.]
// Tests_SRS_IOTHUBCALLBACKPACKET_11_004: [The function shall return the callback context given in the constructor.]
@Test
public void getContextReturnsContext() {
final IotHubStatusCode status = IotHubStatusCode.OK;
final Map<String, Object> context = new HashMap<>();
final String key = "test-key";
final String value = "test-value";
context.put(key, value);
IotHubCallbackPacket packet = new IotHubCallbackPacket(status, mockEventCallback, context);
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)));
}
use of com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket in project azure-iot-sdk-java by Azure.
the class IotHubCallbackPacketTest method getResponseCallbackSetEventCallbackAndStatusAsNull.
// Tests_SRS_IOTHUBCALLBACKPACKET_21_009: [The constructor shall set status and eventCallback as null.]
@Test
public void getResponseCallbackSetEventCallbackAndStatusAsNull() {
// arrange
final Map<String, Object> context = new HashMap<>();
// act
IotHubCallbackPacket packet = new IotHubCallbackPacket(mockMessage, mockResponseCallback, context);
// assert
assertNull(packet.getCallback());
assertNull(packet.getStatus());
}
Aggregations