Java Code Examples for org.eclipse.paho.client.mqttv3.IMqttActionListener#onFailure()

The following examples show how to use org.eclipse.paho.client.mqttv3.IMqttActionListener#onFailure() . 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: 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 2
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 3
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 4
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 5
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 6
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 7
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 8
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 9
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 10
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 11
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 12
Source File: MqttAndroidClient.java    From Sparkplug with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Connects to an MQTT server using the specified options.
 * <p>
 * The server to connect to is specified on the constructor. It is
 * recommended to call {@link #setCallback(MqttCallback)} prior to
 * connecting in order that messages destined for the client can be accepted
 * as soon as the client is connected.
 * </p>
 *
 * <p>
 * The method returns control before the connect completes. Completion can
 * be tracked by:
    * </p>
 * <ul>
 * <li>Waiting on the returned token {@link IMqttToken#waitForCompletion()}
 * or</li>
 * <li>Passing in a callback {@link IMqttActionListener}</li>
 * </ul>
 *
 * 
 * @param options
 *            a set of connection parameters that override the defaults.
 * @param userContext
 *            optional object for used to pass context to the callback. Use
 *            null if not required.
 * @param callback
 *            optional listener that will be notified when the connect
 *            completes. Use null if not required.
 * @return token used to track and wait for the connect to complete. The
 *         token will be passed to any callback that has been set.
 * @throws MqttException
 *             for any connected problems, including communication errors
 */

@Override
public IMqttToken connect(MqttConnectOptions options, Object userContext,
		IMqttActionListener callback) throws MqttException {

	IMqttToken token = new MqttTokenAndroid(this, userContext,
			callback);

	connectOptions = options;
	connectToken = token;

	/*
	 * The actual connection depends on the service, which we start and bind
	 * to here, but which we can't actually use until the serviceConnection
	 * onServiceConnected() method has run (asynchronously), so the
	 * connection itself takes place in the onServiceConnected() method
	 */
	if (mqttService == null) { // First time - must bind to the service
		Intent serviceStartIntent = new Intent();
		serviceStartIntent.setClassName(myContext, SERVICE_NAME);
		Object service = myContext.startService(serviceStartIntent);
		if (service == null) {
			IMqttActionListener listener = token.getActionCallback();
			if (listener != null) {
				listener.onFailure(token, new RuntimeException(
						"cannot start service " + SERVICE_NAME));
			}
		}

		// We bind with BIND_SERVICE_FLAG (0), leaving us the manage the lifecycle
		// until the last time it is stopped by a call to stopService()
		myContext.bindService(serviceStartIntent, serviceConnection,
				Context.BIND_AUTO_CREATE);

		if (!receiverRegistered) registerReceiver(this);
	}
	else {
		pool.execute(new Runnable() {

			@Override
			public void run() {
				doConnect();
				
				//Register receiver to show shoulder tap.
				if (!receiverRegistered) registerReceiver(MqttAndroidClient.this);
			}

		});
	}

	return token;
}