/** * Process notification of a message's arrival * * @param data */ private void messageArrivedAction(Bundle data) { if (callback != null) { String messageId = data .getString(MqttServiceConstants.CALLBACK_MESSAGE_ID); String destinationName = data .getString(MqttServiceConstants.CALLBACK_DESTINATION_NAME); ParcelableMqttMessage message = data .getParcelable(MqttServiceConstants.CALLBACK_MESSAGE_PARCEL); try { if (messageAck == Ack.AUTO_ACK) { callback.messageArrived(destinationName, message); mqttService.acknowledgeMessageArrival(clientHandle, messageId); } else { message.messageId = messageId; callback.messageArrived(destinationName, message); } // let the service discard the saved message details } catch (Exception e) { // Swallow the exception } } }
/** * Process a Connection Lost notification * * @param data */ private void connectionLostAction(Bundle data) { if (callback != null) { Exception reason = (Exception) data .getSerializable(MqttServiceConstants.CALLBACK_EXCEPTION); callback.connectionLost(reason); } }
/** * Process notification of a published message having been delivered * * @param data */ private void messageDeliveredAction(Bundle data) { IMqttToken token = removeMqttToken(data); if (token != null) { if (callback != null) { Status status = (Status) data .getSerializable(MqttServiceConstants.CALLBACK_STATUS); if (status == Status.OK && token instanceof IMqttDeliveryToken) { callback.deliveryComplete((IMqttDeliveryToken) token); } } } }
protected boolean deliverMessage(String topicName, int messageId, MqttMessage aMessage) throws Exception { boolean delivered = false; Enumeration keys = callbacks.keys(); while (keys.hasMoreElements()) { String topicFilter = (String)keys.nextElement(); if (MqttTopic.isMatched(topicFilter, topicName)) { aMessage.setId(messageId); ((IMqttMessageListener)(callbacks.get(topicFilter))).messageArrived(topicName, aMessage); delivered = true; } } /* if the message hasn't been delivered to a per subscription handler, give it to the default handler */ if (mqttCallback != null && !delivered) { aMessage.setId(messageId); mqttCallback.messageArrived(topicName, aMessage); delivered = true; } return delivered; }
public void mockDisconnect() { isConnected = false; mockCallback.connectionLost(new Exception("disconnect")); } }
@Override public void deliveryComplete(IMqttDeliveryToken token) { if (publisherKey != null) { injector.getInstance(publisherKey).deliveryComplete(token); } }
@Test public void testUnSubscribeTopic() throws Exception { MockMqttClient mockClient = new MockMqttClient(); AWSIotMqttManager testClient = new AWSIotMqttManager("test-client", Region.getRegion(Regions.US_EAST_1), TEST_ENDPOINT_PREFIX); testClient.setMqttClient(mockClient); KeyStore testKeystore = AWSIotKeystoreHelper.getIotKeystore(CERT_ID, KEYSTORE_PATH, KEYSTORE_NAME, KEYSTORE_PASSWORD); testClient.connect(testKeystore, null); TestNewMessageCallback mcb = new TestNewMessageCallback(); testClient.subscribeToTopic("unit/test/topic", AWSIotMqttQos.QOS0, mcb); assertEquals(1, mockClient.subscribeCalls); assertTrue(mockClient.mockSubscriptions.containsKey("unit/test/topic")); assertEquals((Integer) 0, mockClient.mockSubscriptions.get("unit/test/topic")); MqttMessage msg = new MqttMessage(); msg.setPayload("test payload".getBytes(StringUtils.UTF8)); mockClient.mockCallback.messageArrived("unit/test/topic", msg); assertEquals(1, mcb.receivedMessages.size()); // received first one assertEquals("unit/test/topic" + "test payload", mcb.receivedMessages.get(0)); testClient.unsubscribeTopic("unit/test/topic"); assertEquals(1, mockClient.unsubscribeCalls); assertFalse(mockClient.mockSubscriptions.containsKey("unit/test/topic")); msg.setPayload("test payload".getBytes(StringUtils.UTF8)); mockClient.mockCallback.messageArrived("unit/test/topic", msg); assertEquals(1, mcb.receivedMessages.size()); // didn't receive this one }
/** * Process a notification that we have disconnected * * @param data */ private void disconnected(Bundle data) { clientHandle = null; // avoid reuse! IMqttToken token = removeMqttToken(data); if (token != null) { ((MqttTokenAndroid) token).notifyComplete(); } if (callback != null) { callback.connectionLost(null); } }
&& token instanceof MqttDeliveryToken && token.isComplete()) { mqttCallback.deliveryComplete((MqttDeliveryToken) token);
@Test public void testSubscribeToTopic() throws Exception { MockMqttClient mockClient = new MockMqttClient(); AWSIotMqttManager testClient = new AWSIotMqttManager("test-client", Region.getRegion(Regions.US_EAST_1), TEST_ENDPOINT_PREFIX); testClient.setMqttClient(mockClient); KeyStore testKeystore = AWSIotKeystoreHelper.getIotKeystore(CERT_ID, KEYSTORE_PATH, KEYSTORE_NAME, KEYSTORE_PASSWORD); testClient.connect(testKeystore, null); TestNewMessageCallback mcb = new TestNewMessageCallback(); testClient.subscribeToTopic("unit/test/topic", AWSIotMqttQos.QOS0, mcb); assertEquals(1, mockClient.subscribeCalls); assertTrue(mockClient.mockSubscriptions.containsKey("unit/test/topic")); assertEquals((Integer) 0, mockClient.mockSubscriptions.get("unit/test/topic")); MqttMessage msg = new MqttMessage(); msg.setPayload("test payload".getBytes(StringUtils.UTF8)); mockClient.mockCallback.messageArrived("unit/test/topic", msg); assertEquals(1, mcb.receivedMessages.size()); assertEquals("unit/test/topic" + "test payload", mcb.receivedMessages.get(0)); }
/** * This method is called when the connection to the server is lost. If there * is no cause then it was a clean disconnect. The connectionLost callback * will be invoked if registered and run on the thread that requested * shutdown e.g. receiver or sender thread. If the request was a user * initiated disconnect then the disconnect token will be notified. * * @param cause the reason behind the loss of connection. */ public void connectionLost(MqttException cause) { final String methodName = "connectionLost"; // If there was a problem and a client callback has been set inform // the connection lost listener of the problem. try { if (mqttCallback != null && cause != null) { // @TRACE 708=call connectionLost log.fine(CLASS_NAME, methodName, "708", new Object[] { cause }); mqttCallback.connectionLost(cause); } if(reconnectInternalCallback != null && cause != null){ reconnectInternalCallback.connectionLost(cause); } } catch (java.lang.Throwable t) { // Just log the fact that a throwable has caught connection lost // is called during shutdown processing so no need to do anything else // @TRACE 720=exception from connectionLost {0} log.fine(CLASS_NAME, methodName, "720", new Object[] { t }); } }
@Test public void testPublishStringQos1WithCallback() throws Exception { MockMqttClient mockClient = new MockMqttClient(); AWSIotMqttManager testClient = new AWSIotMqttManager("test-client", Region.getRegion(Regions.US_EAST_1), TEST_ENDPOINT_PREFIX); testClient.setMqttClient(mockClient); TestClientStatusCallback csb = new TestClientStatusCallback(); TestMessageDeliveryCallback mdcb = new TestMessageDeliveryCallback(); KeyStore testKeystore = AWSIotKeystoreHelper.getIotKeystore(CERT_ID, KEYSTORE_PATH, KEYSTORE_NAME, KEYSTORE_PASSWORD); testClient.connect(testKeystore, csb); mockClient.mockConnectSuccess(); testClient.publishString("test1", "unit/test/topic", AWSIotMqttQos.QOS1, mdcb, "TEST_TOKEN"); assertEquals(1, mockClient.connectCalls); assertEquals(1, mockClient.publishCalls); assertEquals(1, mockClient.mostRecentPublishQoS); assertEquals(false, mockClient.mostRecentPublishRetained); assertEquals("test1", new String(mockClient.mostRecentPublishPayload, StringUtils.UTF8)); assertEquals(0, mdcb.statuses.size()); // callback hasn't been called yet MockDeliveryToken testDeliveryToken = new MockDeliveryToken(); testDeliveryToken.setUserContext(mockClient.mostRecentPublishUserContext); mockClient.mockCallback.deliveryComplete(testDeliveryToken); assertEquals(1, mdcb.statuses.size()); // now it has assertEquals(AWSIotMqttMessageDeliveryCallback.MessageDeliveryStatus.Success, mdcb.statuses.get(0)); assertEquals(1, mdcb.userDatas.size()); assertEquals("TEST_TOKEN", mdcb.userDatas.get(0)); }
@Test public void testMessageArrivedOne() throws Exception { MockMqttClient mockClient = new MockMqttClient(); AWSIotMqttManager testClient = new AWSIotMqttManager("test-client", Region.getRegion(Regions.US_EAST_1), TEST_ENDPOINT_PREFIX); testClient.setMqttClient(mockClient); KeyStore testKeystore = AWSIotKeystoreHelper.getIotKeystore(CERT_ID, KEYSTORE_PATH, KEYSTORE_NAME, KEYSTORE_PASSWORD); testClient.connect(testKeystore, null); TestNewMessageCallback mcb = new TestNewMessageCallback(); testClient.subscribeToTopic("unit/test/topic", AWSIotMqttQos.QOS0, mcb); MqttMessage msg = new MqttMessage(); msg.setPayload("test payload".getBytes(StringUtils.UTF8)); mockClient.mockCallback.messageArrived("unit/test/topic", msg); assertEquals(1, mcb.receivedMessages.size()); assertEquals("unit/test/topic" + "test payload", mcb.receivedMessages.get(0)); }
/** * Process a Connection Lost notification * * @param data */ private void connectionLostAction(Bundle data) { if (callback != null) { Exception reason = (Exception) data .getSerializable(MqttServiceConstants.CALLBACK_EXCEPTION); callback.connectionLost(reason); } }
@Test public void testPublishStringQos0WithCallback() throws Exception { MockMqttClient mockClient = new MockMqttClient(); AWSIotMqttManager testClient = new AWSIotMqttManager("test-client", Region.getRegion(Regions.US_EAST_1), TEST_ENDPOINT_PREFIX); testClient.setMqttClient(mockClient); TestClientStatusCallback csb = new TestClientStatusCallback(); TestMessageDeliveryCallback mdcb = new TestMessageDeliveryCallback(); KeyStore testKeystore = AWSIotKeystoreHelper.getIotKeystore(CERT_ID, KEYSTORE_PATH, KEYSTORE_NAME, KEYSTORE_PASSWORD); testClient.connect(testKeystore, csb); mockClient.mockConnectSuccess(); testClient.publishString("test1", "unit/test/topic", AWSIotMqttQos.QOS0, mdcb, "TEST_TOKEN"); assertEquals(1, mockClient.connectCalls); assertEquals(1, mockClient.publishCalls); assertEquals(0, mockClient.mostRecentPublishQoS); assertEquals(false, mockClient.mostRecentPublishRetained); assertEquals("test1", new String(mockClient.mostRecentPublishPayload, StringUtils.UTF8)); assertEquals(0, mdcb.statuses.size()); // callback hasn't been called yet MockDeliveryToken testDeliveryToken = new MockDeliveryToken(); testDeliveryToken.setUserContext(mockClient.mostRecentPublishUserContext); mockClient.mockCallback.deliveryComplete(testDeliveryToken); assertEquals(1, mdcb.statuses.size()); // now it has assertEquals(AWSIotMqttMessageDeliveryCallback.MessageDeliveryStatus.Success, mdcb.statuses.get(0)); assertEquals(1, mdcb.userDatas.size()); assertEquals("TEST_TOKEN", mdcb.userDatas.get(0)); }
@Test public void testMessageArrivedTwo() throws Exception { MockMqttClient mockClient = new MockMqttClient(); AWSIotMqttManager testClient = new AWSIotMqttManager("test-client", Region.getRegion(Regions.US_EAST_1), TEST_ENDPOINT_PREFIX); testClient.setMqttClient(mockClient); KeyStore testKeystore = AWSIotKeystoreHelper.getIotKeystore(CERT_ID, KEYSTORE_PATH, KEYSTORE_NAME, KEYSTORE_PASSWORD); testClient.connect(testKeystore, null); TestNewMessageCallback mcb = new TestNewMessageCallback(); testClient.subscribeToTopic("unit/test/topic", AWSIotMqttQos.QOS0, mcb); testClient.subscribeToTopic("unit/test/+", AWSIotMqttQos.QOS0, mcb); assertEquals(2, mockClient.subscribeCalls); assertEquals(2, mockClient.mockSubscriptions.size()); MqttMessage msg = new MqttMessage(); msg.setPayload("test payload".getBytes(StringUtils.UTF8)); mockClient.mockCallback.messageArrived("unit/test/topic", msg); assertEquals(2, mcb.receivedMessages.size()); assertEquals("unit/test/topic" + "test payload", mcb.receivedMessages.get(0)); assertEquals("unit/test/topic" + "test payload", mcb.receivedMessages.get(1)); }
/** * Process a Connection Lost notification * * @param data */ private void connectionLostAction(Bundle data) { if (callback != null) { Exception reason = (Exception) data .getSerializable(MqttServiceConstants.CALLBACK_EXCEPTION); callback.connectionLost(reason); } }
/** * Process notification of a published message having been delivered * * @param data */ private void messageDeliveredAction(Bundle data) { IMqttToken token = removeMqttToken(data); if (token != null) { if (callback != null) { Status status = (Status) data .getSerializable(MqttServiceConstants.CALLBACK_STATUS); if (status == Status.OK && token instanceof IMqttDeliveryToken) { callback.deliveryComplete((IMqttDeliveryToken) token); } } } }
@Test public void testMessageArrivedTooShallow() throws Exception { MockMqttClient mockClient = new MockMqttClient(); AWSIotMqttManager testClient = new AWSIotMqttManager("test-client", Region.getRegion(Regions.US_EAST_1), TEST_ENDPOINT_PREFIX); testClient.setMqttClient(mockClient); KeyStore testKeystore = AWSIotKeystoreHelper.getIotKeystore(CERT_ID, KEYSTORE_PATH, KEYSTORE_NAME, KEYSTORE_PASSWORD); testClient.connect(testKeystore, null); TestNewMessageCallback mcb = new TestNewMessageCallback(); testClient.subscribeToTopic("unit/test/topic", AWSIotMqttQos.QOS0, mcb); MqttMessage msg = new MqttMessage(); msg.setPayload("test payload".getBytes(StringUtils.UTF8)); mockClient.mockCallback.messageArrived("unit/test", msg); assertEquals(0, mcb.receivedMessages.size()); }
@Test public void testLostConnectUserDisconnect() throws Exception { MockMqttClient mockClient = new MockMqttClient(); AWSIotMqttManager testClient = new AWSIotMqttManager("test-client", Region.getRegion(Regions.US_EAST_1), TEST_ENDPOINT_PREFIX); testClient.setMqttClient(mockClient); TestClientStatusCallback csb = new TestClientStatusCallback(); KeyStore testKeystore = AWSIotKeystoreHelper.getIotKeystore(CERT_ID, KEYSTORE_PATH, KEYSTORE_NAME, KEYSTORE_PASSWORD); testClient.connect(testKeystore, csb); mockClient.mockConnectSuccess(); assertEquals(1, mockClient.connectCalls); assertTrue(mockClient.mostRecentOptions.isCleanSession()); testClient.disconnect(); mockClient.mockCallback.connectionLost(new Exception("test")); assertEquals(1, mockClient.connectCalls); } /*