org.eclipse.paho.client.mqttv3.IMqttActionListener Java Examples

The following examples show how to use org.eclipse.paho.client.mqttv3.IMqttActionListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConnectFactoryTest.java    From rxmqtt with Apache License 2.0 7 votes vote down vote up
@Test
public void whenCreateIsCalledThenAnObservableIsReturned()
        throws Exception {
    final IMqttAsyncClient client = Mockito.mock(IMqttAsyncClient.class);
    final MqttConnectOptions options = Mockito
            .mock(MqttConnectOptions.class);
    final ConnectFactory factory = new ConnectFactory(client, options);
    final ArgumentCaptor<IMqttActionListener> actionListener = ArgumentCaptor
            .forClass(IMqttActionListener.class);
    final Completable obs = factory.create();
    Assert.assertNotNull(obs);
    obs.subscribe();
    Mockito.verify(client).connect(Matchers.same(options),
            Matchers.isNull(), actionListener.capture());
    Assert.assertTrue(actionListener
            .getValue() instanceof ConnectFactory.ConnectActionListener);
}
 
Example #2
Source File: SubscribeFactoryTest.java    From rxmqtt with Apache License 2.0 6 votes vote down vote up
@Test
public void whenCreateIsCalledThenAnObservableIsReturned()
        throws Exception {
    final IMqttAsyncClient client = Mockito.mock(IMqttAsyncClient.class);
    final SubscribeFactory factory = new SubscribeFactory(client);
    final ArgumentCaptor<IMqttActionListener> actionListener = ArgumentCaptor
            .forClass(IMqttActionListener.class);
    final ArgumentCaptor<IMqttMessageListener[]> messageListener = ArgumentCaptor
            .forClass(IMqttMessageListener[].class);
    final String[] topics = new String[] { "topic1", "topic2" };
    final int[] qos = new int[] { 1, 2 };
    final Flowable<SubscribeMessage> obs = factory.create(topics, qos,
            BackpressureStrategy.ERROR);
    Assert.assertNotNull(obs);
    obs.subscribe();
    Mockito.verify(client).subscribe(Matchers.same(topics),
            Matchers.same(qos), Matchers.isNull(), actionListener.capture(),
            messageListener.capture());
    Assert.assertTrue(actionListener
            .getValue() instanceof SubscribeFactory.SubscribeActionListener);
    Assert.assertTrue(messageListener
            .getValue() instanceof SubscriberMqttMessageListener[]);
    Assert.assertEquals(2, messageListener.getValue().length);
}
 
Example #3
Source File: SubscribeFactoryTest.java    From rxmqtt with Apache License 2.0 6 votes vote down vote up
@Test
public void whenCreateIsCalledAndAnErrorOccursThenObserverOnErrorIsCalled()
        throws Throwable {
    this.expectedException.expectCause(isA(MqttException.class));
    final ArgumentCaptor<IMqttActionListener> actionListener = ArgumentCaptor
            .forClass(IMqttActionListener.class);
    final ArgumentCaptor<IMqttMessageListener[]> messageListener = ArgumentCaptor
            .forClass(IMqttMessageListener[].class);
    final String[] topics = new String[] { "topic1", "topic2" };
    final int[] qos = new int[] { 1, 2 };
    final IMqttAsyncClient client = Mockito.mock(IMqttAsyncClient.class);
    Mockito.when(client.subscribe(Matchers.same(topics), Matchers.same(qos),
            Matchers.isNull(), actionListener.capture(),
            messageListener.capture()))
            .thenThrow(new MqttException(
                    MqttException.REASON_CODE_CLIENT_CONNECTED));
    final SubscribeFactory factory = new SubscribeFactory(client);
    final Flowable<SubscribeMessage> obs = factory.create(topics, qos,
            BackpressureStrategy.ERROR);
    obs.blockingFirst();
}
 
Example #4
Source File: PublishFactoryTest.java    From rxmqtt with Apache License 2.0 6 votes vote down vote up
@Test
public void whenCreateIsCalledThenAnObservableIsReturned()
        throws Exception {
    // Given
    final IMqttAsyncClient client = Mockito.mock(IMqttAsyncClient.class);
    final PublishFactory factory = new PublishFactory(client);
    final String topic = "topic1";
    final PublishMessage msg = PublishMessage
            .create(new byte[] { 'a', 'b', 'c' }, 1, true);
    final ArgumentCaptor<IMqttActionListener> actionListener = ArgumentCaptor
            .forClass(IMqttActionListener.class);

    // When
    final Single<PublishToken> obs = factory.create(topic, msg);

    // Then
    Assert.assertNotNull(obs);
    obs.subscribe();
    Mockito.verify(client).publish(Matchers.same(topic),
            Matchers.same(msg.getPayload()), Matchers.anyInt(),
            Matchers.anyBoolean(), Matchers.any(),
            actionListener.capture());
    Assert.assertTrue(actionListener
            .getValue() instanceof PublishFactory.PublishActionListener);
}
 
Example #5
Source File: UnsubscribeFactoryTest.java    From rxmqtt with Apache License 2.0 6 votes vote down vote up
@Test
public void whenCreateIsCalledThenAnObservableIsReturned()
        throws Exception {
    // Given
    final IMqttAsyncClient client = Mockito.mock(IMqttAsyncClient.class);
    final UnsubscribeFactory factory = new UnsubscribeFactory(client);
    final String[] topics = new String[] { "topic1", "topic2" };
    final ArgumentCaptor<IMqttActionListener> actionListener = ArgumentCaptor
            .forClass(IMqttActionListener.class);

    // When
    final Completable obs = factory.create(topics);

    // Then
    Assert.assertNotNull(obs);
    obs.subscribe();
    Mockito.verify(client).unsubscribe(Matchers.same(topics),
            Matchers.isNull(), actionListener.capture());
    Assert.assertTrue(actionListener
            .getValue() instanceof UnsubscribeFactory.UnsubscribeActionListener);
}
 
Example #6
Source File: DisconnectFactoryTest.java    From rxmqtt with Apache License 2.0 6 votes vote down vote up
@Test
public void whenCreateIsCalledThenAnObservableIsReturned()
        throws Exception {
    // Given
    final IMqttAsyncClient client = Mockito.mock(IMqttAsyncClient.class);
    final DisconnectFactory factory = new DisconnectFactory(client);
    final ArgumentCaptor<IMqttActionListener> actionListener = ArgumentCaptor
            .forClass(IMqttActionListener.class);

    // When
    final Completable obs = factory.create();

    // Then
    Assert.assertNotNull(obs);
    obs.subscribe();
    Mockito.verify(client).disconnect(Matchers.isNull(),
            actionListener.capture());
    Assert.assertTrue(actionListener
            .getValue() instanceof DisconnectFactory.DisconnectActionListener);
}
 
Example #7
Source File: MqttAndroidClient.java    From Sparkplug with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Actually do the mqtt connect operation
 */
private void doConnect() {
	if (clientHandle == null) {
		clientHandle = mqttService.getClient(serverURI, clientId,myContext.getApplicationInfo().packageName,
				persistence);
	}
	mqttService.setTraceEnabled(traceEnabled);
	mqttService.setTraceCallbackId(clientHandle);
	
	String activityToken = storeToken(connectToken);
	try {
		mqttService.connect(clientHandle, connectOptions, null,
				activityToken);
	}
	catch (MqttException e) {
		IMqttActionListener listener = connectToken.getActionCallback();
		if (listener != null) {
			listener.onFailure(connectToken, e);
		}
	}
}
 
Example #8
Source File: MqttConnection.java    From Sparkplug with Eclipse Public License 1.0 6 votes vote down vote up
public void subscribe(String[] topicFilters, int[] qos, String invocationContext, String activityToken, IMqttMessageListener[] messageListeners) {
	service.traceDebug(TAG, "subscribe({" + Arrays.toString(topicFilters) + "}," + Arrays.toString(qos) + ",{"
			+ invocationContext + "}, {" + activityToken + "}");
	final Bundle resultBundle = new Bundle();
	resultBundle.putString(MqttServiceConstants.CALLBACK_ACTION, MqttServiceConstants.SUBSCRIBE_ACTION);
	resultBundle.putString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN, activityToken);
	resultBundle.putString(MqttServiceConstants.CALLBACK_INVOCATION_CONTEXT, invocationContext);
	if((myClient != null) && (myClient.isConnected())){
		IMqttActionListener listener = new MqttConnectionListener(resultBundle);
		try {

			myClient.subscribe(topicFilters, qos,messageListeners);
		} catch (Exception e){
			handleException(resultBundle, e);
		}
	} else {
		resultBundle.putString(MqttServiceConstants.CALLBACK_ERROR_MESSAGE, NOT_CONNECTED);
		service.traceError("subscribe", NOT_CONNECTED);
		service.callbackToActivity(clientHandle, Status.ERROR, resultBundle);
	}
}
 
Example #9
Source File: MqttAsyncClientEx.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public IMqttToken subscribe(String topic, int qos, Object userContext, IMqttActionListener callback)
        throws MqttException {
    if (connection.publishSuccess) {
        callback.onSuccess(getToken(userContext, callback, topic));
    } else {
        callback.onFailure(getToken(userContext, callback, topic), new MqttException(0));
    }
    return getToken(userContext, callback, topic);
}
 
Example #10
Source File: MqttConnection.java    From Sparkplug with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Disconnect from the server
 * 
 * @param invocationContext
 *            arbitrary data to be passed back to the application
 * @param activityToken
 *            arbitrary string to be passed back to the activity
 */
void disconnect(String invocationContext, String activityToken) {
	service.traceDebug(TAG, "disconnect()");
	disconnected = true;
	final Bundle resultBundle = new Bundle();
	resultBundle.putString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN,
			activityToken);
	resultBundle.putString(
			MqttServiceConstants.CALLBACK_INVOCATION_CONTEXT,
			invocationContext);
	resultBundle.putString(MqttServiceConstants.CALLBACK_ACTION,
			MqttServiceConstants.DISCONNECT_ACTION);
	if ((myClient != null) && (myClient.isConnected())) {
		IMqttActionListener listener = new MqttConnectionListener(
				resultBundle);
		try {
			myClient.disconnect(invocationContext, listener);
		} catch (Exception e) {
			handleException(resultBundle, e);
		}
	} else {
		resultBundle.putString(MqttServiceConstants.CALLBACK_ERROR_MESSAGE,
				NOT_CONNECTED);
		service.traceError(MqttServiceConstants.DISCONNECT_ACTION,
				NOT_CONNECTED);
		service.callbackToActivity(clientHandle, Status.ERROR, resultBundle);
	}

	if (connectOptions != null && connectOptions.isCleanSession()) {
		// assume we'll clear the stored messages at this point
		service.messageStore.clearArrivedMessages(clientHandle);
	}
	releaseWakeLock();
}
 
Example #11
Source File: MqttAsyncClientEx.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public IMqttDeliveryToken publish(String topic, byte[] payload, int qos, boolean retained, Object userContext,
        IMqttActionListener callback) throws MqttException, MqttPersistenceException {

    if (connection.publishSuccess) {
        callback.onSuccess(getToken(userContext, callback, topic));
    } else {
        callback.onFailure(getToken(userContext, callback, topic), new MqttException(0));
    }
    return getDeliveryToken(userContext, callback, topic);
}
 
Example #12
Source File: MqttAsyncClientEx.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
IMqttDeliveryToken getDeliveryToken(Object userContext, IMqttActionListener callback, String topic) {
    IMqttDeliveryToken t = mock(IMqttDeliveryToken.class);
    doReturn(userContext).when(t).getUserContext();
    doReturn(true).when(t).isComplete();
    doReturn(Collections.singletonList(topic).toArray(new String[1])).when(t).getTopics();
    doReturn(MqttAsyncClientEx.this).when(t).getClient();
    doReturn(callback).when(t).getActionCallback();
    doReturn(null).when(t).getException();
    return t;
}
 
Example #13
Source File: MqttAsyncClientEx.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
IMqttToken getToken(Object userContext, IMqttActionListener callback, String topic) {
    IMqttToken t = mock(IMqttToken.class);
    doReturn(userContext).when(t).getUserContext();
    doReturn(true).when(t).isComplete();
    doReturn(Collections.singletonList(topic).toArray(new String[1])).when(t).getTopics();
    doReturn(MqttAsyncClientEx.this).when(t).getClient();
    doReturn(callback).when(t).getActionCallback();
    doReturn(null).when(t).getException();
    return t;
}
 
Example #14
Source File: MqttConnection.java    From Sparkplug with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Unsubscribe from one or more topics
 * 
 * @param topic
 *            a list of possibly wildcarded topic names
 * @param invocationContext
 *            arbitrary data to be passed back to the application
 * @param activityToken
 *            arbitrary identifier to be passed back to the Activity
 */
void unsubscribe(final String[] topic, String invocationContext,
		String activityToken) {
	service.traceDebug(TAG, "unsubscribe({" + Arrays.toString(topic) + "},{"
			+ invocationContext + "}, {" + activityToken + "})");
	final Bundle resultBundle = new Bundle();
	resultBundle.putString(MqttServiceConstants.CALLBACK_ACTION,
			MqttServiceConstants.UNSUBSCRIBE_ACTION);
	resultBundle.putString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN,
			activityToken);
	resultBundle.putString(
			MqttServiceConstants.CALLBACK_INVOCATION_CONTEXT,
			invocationContext);
	if ((myClient != null) && (myClient.isConnected())) {
		IMqttActionListener listener = new MqttConnectionListener(
				resultBundle);
		try {
			myClient.unsubscribe(topic, invocationContext, listener);
		} catch (Exception e) {
			handleException(resultBundle, e);
		}
	} else {
		resultBundle.putString(MqttServiceConstants.CALLBACK_ERROR_MESSAGE,
				NOT_CONNECTED);

		service.traceError("subscribe", NOT_CONNECTED);
		service.callbackToActivity(clientHandle, Status.ERROR, resultBundle);
	}
}
 
Example #15
Source File: MqttConnection.java    From Sparkplug with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Subscribe to one or more topics
 * 
 * @param topic
 *            a list of possibly wildcarded topic names
 * @param qos
 *            requested quality of service for each topic
 * @param invocationContext
 *            arbitrary data to be passed back to the application
 * @param activityToken
 *            arbitrary identifier to be passed back to the Activity
 */
public void subscribe(final String[] topic, final int[] qos,
		String invocationContext, String activityToken) {
	service.traceDebug(TAG, "subscribe({" + Arrays.toString(topic) + "}," + Arrays.toString(qos) + ",{"
			+ invocationContext + "}, {" + activityToken + "}");
	final Bundle resultBundle = new Bundle();
	resultBundle.putString(MqttServiceConstants.CALLBACK_ACTION,
			MqttServiceConstants.SUBSCRIBE_ACTION);
	resultBundle.putString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN,
			activityToken);
	resultBundle.putString(
			MqttServiceConstants.CALLBACK_INVOCATION_CONTEXT,
			invocationContext);

	if ((myClient != null) && (myClient.isConnected())) {
		IMqttActionListener listener = new MqttConnectionListener(
				resultBundle);
		try {
			myClient.subscribe(topic, qos, invocationContext, listener);
		} catch (Exception e) {
			handleException(resultBundle, e);
		}
	} else {
		resultBundle.putString(MqttServiceConstants.CALLBACK_ERROR_MESSAGE,
				NOT_CONNECTED);
		service.traceError("subscribe", NOT_CONNECTED);
		service.callbackToActivity(clientHandle, Status.ERROR, resultBundle);
	}
}
 
Example #16
Source File: MqttConnection.java    From Sparkplug with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Subscribe to a topic
 * 
 * @param topic
 *            a possibly wildcarded topic name
 * @param qos
 *            requested quality of service for the topic
 * @param invocationContext
 *            arbitrary data to be passed back to the application
 * @param activityToken
 *            arbitrary identifier to be passed back to the Activity
 */
public void subscribe(final String topic, final int qos,
		String invocationContext, String activityToken) {
	service.traceDebug(TAG, "subscribe({" + topic + "}," + qos + ",{"
			+ invocationContext + "}, {" + activityToken + "}");
	final Bundle resultBundle = new Bundle();
	resultBundle.putString(MqttServiceConstants.CALLBACK_ACTION,
			MqttServiceConstants.SUBSCRIBE_ACTION);
	resultBundle.putString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN,
			activityToken);
	resultBundle.putString(
			MqttServiceConstants.CALLBACK_INVOCATION_CONTEXT,
			invocationContext);

	if ((myClient != null) && (myClient.isConnected())) {
		IMqttActionListener listener = new MqttConnectionListener(
				resultBundle);
		try {
			myClient.subscribe(topic, qos, invocationContext, listener);
		} catch (Exception e) {
			handleException(resultBundle, e);
		}
	} else {
		resultBundle.putString(MqttServiceConstants.CALLBACK_ERROR_MESSAGE,
				NOT_CONNECTED);
		service.traceError("subscribe", NOT_CONNECTED);
		service.callbackToActivity(clientHandle, Status.ERROR, resultBundle);
	}
}
 
Example #17
Source File: MqttConnection.java    From Sparkplug with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Publish a message on a topic
 * 
 * @param topic
 *            the topic on which to publish - represented as a string, not
 *            an MqttTopic object
 * @param payload
 *            the content of the message to publish
 * @param qos
 *            the quality of service requested
 * @param retained
 *            whether the MQTT server should retain this message
 * @param invocationContext
 *            arbitrary data to be passed back to the application
 * @param activityToken
 *            arbitrary string to be passed back to the activity
 * @return token for tracking the operation
 */
public IMqttDeliveryToken publish(String topic, byte[] payload, int qos,
		boolean retained, String invocationContext, String activityToken) {
	final Bundle resultBundle = new Bundle();
	resultBundle.putString(MqttServiceConstants.CALLBACK_ACTION,
			MqttServiceConstants.SEND_ACTION);
	resultBundle.putString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN,
			activityToken);
	resultBundle.putString(
			MqttServiceConstants.CALLBACK_INVOCATION_CONTEXT,
			invocationContext);

	IMqttDeliveryToken sendToken = null;

	if ((myClient != null) && (myClient.isConnected())) {
		IMqttActionListener listener = new MqttConnectionListener(
				resultBundle);
		try {
			MqttMessage message = new MqttMessage(payload);
			message.setQos(qos);
			message.setRetained(retained);
			sendToken = myClient.publish(topic, payload, qos, retained,
					invocationContext, listener);
			storeSendDetails(topic, message, sendToken, invocationContext,
					activityToken);
		} catch (Exception e) {
			handleException(resultBundle, e);
		}
	} else {
		resultBundle.putString(MqttServiceConstants.CALLBACK_ERROR_MESSAGE,
				NOT_CONNECTED);
		service.traceError(MqttServiceConstants.SEND_ACTION, NOT_CONNECTED);
		service.callbackToActivity(clientHandle, Status.ERROR, resultBundle);
	}

	return sendToken;
}
 
Example #18
Source File: MqttConnection.java    From Sparkplug with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Disconnect from the server
 * 
 * @param quiesceTimeout
 *            in milliseconds
 * @param invocationContext
 *            arbitrary data to be passed back to the application
 * @param activityToken
 *            arbitrary string to be passed back to the activity
 */
void disconnect(long quiesceTimeout, String invocationContext,
		String activityToken) {
	service.traceDebug(TAG, "disconnect()");
	disconnected = true;
	final Bundle resultBundle = new Bundle();
	resultBundle.putString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN,
			activityToken);
	resultBundle.putString(
			MqttServiceConstants.CALLBACK_INVOCATION_CONTEXT,
			invocationContext);
	resultBundle.putString(MqttServiceConstants.CALLBACK_ACTION,
			MqttServiceConstants.DISCONNECT_ACTION);
	if ((myClient != null) && (myClient.isConnected())) {
		IMqttActionListener listener = new MqttConnectionListener(
				resultBundle);
		try {
			myClient.disconnect(quiesceTimeout, invocationContext, listener);
		} catch (Exception e) {
			handleException(resultBundle, e);
		}
	} else {
		resultBundle.putString(MqttServiceConstants.CALLBACK_ERROR_MESSAGE,
				NOT_CONNECTED);
		service.traceError(MqttServiceConstants.DISCONNECT_ACTION,
				NOT_CONNECTED);
		service.callbackToActivity(clientHandle, Status.ERROR, resultBundle);
	}

	if (connectOptions != null && connectOptions.isCleanSession()) {
		// assume we'll clear the stored messages at this point
		service.messageStore.clearArrivedMessages(clientHandle);
	}

	releaseWakeLock();
}
 
Example #19
Source File: MqttConnection.java    From Sparkplug with Eclipse Public License 1.0 5 votes vote down vote up
/**
        * Unsubscribe from a topic
        *
        * @param topic
        *            a possibly wildcarded topic name
        * @param invocationContext
        *            arbitrary data to be passed back to the application
        * @param activityToken
        *            arbitrary identifier to be passed back to the Activity
        */
void unsubscribe(final String topic, String invocationContext,
		String activityToken) {
	service.traceDebug(TAG, "unsubscribe({" + topic + "},{"
			+ invocationContext + "}, {" + activityToken + "})");
	final Bundle resultBundle = new Bundle();
	resultBundle.putString(MqttServiceConstants.CALLBACK_ACTION,
			MqttServiceConstants.UNSUBSCRIBE_ACTION);
	resultBundle.putString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN,
			activityToken);
	resultBundle.putString(
			MqttServiceConstants.CALLBACK_INVOCATION_CONTEXT,
			invocationContext);
	if ((myClient != null) && (myClient.isConnected())) {
		IMqttActionListener listener = new MqttConnectionListener(
				resultBundle);
		try {
			myClient.unsubscribe(topic, invocationContext, listener);
		} catch (Exception e) {
			handleException(resultBundle, e);
		}
	} else {
		resultBundle.putString(MqttServiceConstants.CALLBACK_ERROR_MESSAGE,
				NOT_CONNECTED);

		service.traceError("subscribe", NOT_CONNECTED);
		service.callbackToActivity(clientHandle, Status.ERROR, resultBundle);
	}
}
 
Example #20
Source File: MqttAsyncClientEx.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public IMqttToken unsubscribe(String topic, Object userContext, IMqttActionListener callback) throws MqttException {
    if (connection.unsubscribeSuccess) {
        callback.onSuccess(getToken(userContext, callback, topic));
    } else {
        callback.onFailure(getToken(userContext, callback, topic), new MqttException(0));
    }
    return getToken(userContext, callback, topic);
}
 
Example #21
Source File: MqttAsyncClientEx.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public IMqttToken disconnect(long quiesceTimeout, Object userContext, IMqttActionListener callback)
        throws MqttException {
    connection.connectionStateOverwrite = MqttConnectionState.DISCONNECTED;
    if (connection.disconnectSuccess) {
        callback.onSuccess(getToken(userContext, callback, null));
    } else {
        callback.onFailure(getToken(userContext, callback, null), new MqttException(0));
    }
    return getToken(userContext, callback, null);
}
 
Example #22
Source File: MqttAsyncClientEx.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public IMqttToken connect(MqttConnectOptions options, Object userContext, IMqttActionListener callback)
        throws MqttException, MqttSecurityException {
    if (!connection.connectTimeout) {
        connection.connectionStateOverwrite = MqttConnectionState.CONNECTED;
        if (connection.connectSuccess) {
            callback.onSuccess(getToken(userContext, callback, null));
        } else {
            callback.onFailure(getToken(userContext, callback, null), new MqttException(0));
        }
    } else {
        connection.connectionStateOverwrite = MqttConnectionState.DISCONNECTED;
    }
    return getToken(userContext, callback, null);
}
 
Example #23
Source File: MqttAsyncClientEx.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
IMqttToken getToken(Object userContext, IMqttActionListener callback, String topic) {
    IMqttToken t = mock(IMqttToken.class);
    doReturn(userContext).when(t).getUserContext();
    doReturn(true).when(t).isComplete();
    doReturn(Collections.singletonList(topic).toArray(new String[1])).when(t).getTopics();
    doReturn(MqttAsyncClientEx.this).when(t).getClient();
    doReturn(callback).when(t).getActionCallback();
    doReturn(null).when(t).getException();
    return t;
}
 
Example #24
Source File: MqttAsyncClientEx.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
IMqttDeliveryToken getDeliveryToken(Object userContext, IMqttActionListener callback, String topic) {
    IMqttDeliveryToken t = mock(IMqttDeliveryToken.class);
    doReturn(userContext).when(t).getUserContext();
    doReturn(true).when(t).isComplete();
    doReturn(Collections.singletonList(topic).toArray(new String[1])).when(t).getTopics();
    doReturn(MqttAsyncClientEx.this).when(t).getClient();
    doReturn(callback).when(t).getActionCallback();
    doReturn(null).when(t).getException();
    return t;
}
 
Example #25
Source File: MqttAsyncClientEx.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public IMqttDeliveryToken publish(String topic, byte[] payload, int qos, boolean retained, Object userContext,
        IMqttActionListener callback) throws MqttException, MqttPersistenceException {

    if (connection.publishSuccess) {
        callback.onSuccess(getToken(userContext, callback, topic));
    } else {
        callback.onFailure(getToken(userContext, callback, topic), new MqttException(0));
    }
    return getDeliveryToken(userContext, callback, topic);
}
 
Example #26
Source File: MqttAsyncClientEx.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public IMqttToken subscribe(String topic, int qos, Object userContext, IMqttActionListener callback)
        throws MqttException {
    if (connection.publishSuccess) {
        callback.onSuccess(getToken(userContext, callback, topic));
    } else {
        callback.onFailure(getToken(userContext, callback, topic), new MqttException(0));
    }
    return getToken(userContext, callback, topic);
}
 
Example #27
Source File: MqttAsyncClientEx.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public IMqttToken unsubscribe(String topic, Object userContext, IMqttActionListener callback) throws MqttException {
    if (connection.unsubscribeSuccess) {
        callback.onSuccess(getToken(userContext, callback, topic));
    } else {
        callback.onFailure(getToken(userContext, callback, topic), new MqttException(0));
    }
    return getToken(userContext, callback, topic);
}
 
Example #28
Source File: MqttAsyncClientEx.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public IMqttToken disconnect(long quiesceTimeout, Object userContext, IMqttActionListener callback)
        throws MqttException {
    connection.connectionStateOverwrite = MqttConnectionState.DISCONNECTED;
    if (connection.disconnectSuccess) {
        callback.onSuccess(getToken(userContext, callback, null));
    } else {
        callback.onFailure(getToken(userContext, callback, null), new MqttException(0));
    }
    return getToken(userContext, callback, null);
}
 
Example #29
Source File: MqttAsyncClientEx.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public IMqttToken connect(MqttConnectOptions options, Object userContext, IMqttActionListener callback)
        throws MqttException, MqttSecurityException {
    if (!connection.connectTimeout) {
        connection.connectionStateOverwrite = MqttConnectionState.CONNECTED;
        if (connection.connectSuccess) {
            callback.onSuccess(getToken(userContext, callback, null));
        } else {
            callback.onFailure(getToken(userContext, callback, null), new MqttException(0));
        }
    } else {
        connection.connectionStateOverwrite = MqttConnectionState.DISCONNECTED;
    }
    return getToken(userContext, callback, null);
}
 
Example #30
Source File: MqttConnection.java    From bce-sdk-java with Apache License 2.0 5 votes vote down vote up
public MqttConnection(String serverURI, String clientId, String userName, String
        password, SocketFactory socketFactory, MqttCallback mqttCallbackListener,
                      IMqttActionListener mqttMessageListener) throws MqttException {

    if (serverURI == null || mqttCallbackListener == null || mqttMessageListener == null) {
        throw new IllegalArgumentException("serverURI, mqttCallbackListener, mqttMessageListener can't be null!");
    }
    this.mqttAsyncClient = new MqttAsyncClient(serverURI, clientId, new MemoryPersistence());
    this.mqttAsyncClient.setManualAcks(true);
    this.connectionOptions = new MqttConnectOptions();
    this.initOptions(userName, password, socketFactory);
    this.mqttMessageListener = mqttMessageListener;
    this.mqttAsyncClient.setCallback(mqttCallbackListener);
}