Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ ext{
sampleVersion = '1.1.1'

serviceArchivesBaseName = 'in.mohalla.paho.android.service'
serviceVersion = '1.1.1.6-timer-ping-sender'
serviceVersion = '1.1.1.7-handle-suback-failure'

clientVersion = '1.1.0'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

/**
* <p>
* MqttConnection holds a MqttAsyncClient {host,port,clientId} instance to perform
* MqttConnection holds a MqttAsyncClient {host,port,clientId} instance to perform
* MQTT operations to MQTT broker.
* </p>
* <p>
Expand Down Expand Up @@ -149,7 +149,7 @@ public void setClientHandle(String clientHandle) {

/**
* Constructor - create an MqttConnection to communicate with MQTT server
*
*
* @param service
* our "parent" service - we make callbacks to it
* @param serverURI
Expand Down Expand Up @@ -183,7 +183,7 @@ public void setClientHandle(String clientHandle) {
// The major API implementation follows
/**
* Connect to the server specified when we were instantiated
*
*
* @param options
* timeout, etc
* @param invocationContext
Expand All @@ -193,7 +193,7 @@ public void setClientHandle(String clientHandle) {
*/
public void connect(MqttConnectOptions options, String invocationContext,
String activityToken) {

connectOptions = options;
reconnectActivityToken = activityToken;

Expand All @@ -215,8 +215,8 @@ public void connect(MqttConnectOptions options, String invocationContext,
invocationContext);
resultBundle.putString(MqttServiceConstants.CALLBACK_ACTION,
MqttServiceConstants.CONNECT_ACTION);


try {
if (persistence == null) {
// ask Android where we can put files
Expand All @@ -225,7 +225,7 @@ public void connect(MqttConnectOptions options, String invocationContext,
if (myDir == null) {
// No external storage, use internal storage instead.
myDir = service.getDir(TAG, Context.MODE_PRIVATE);

if(myDir == null){
//Shouldn't happen.
resultBundle.putString(
Expand All @@ -243,7 +243,7 @@ public void connect(MqttConnectOptions options, String invocationContext,
persistence = new MqttDefaultFilePersistence(
myDir.getAbsolutePath());
}

IMqttActionListener listener = new MqttConnectionListener(
resultBundle) {

Expand Down Expand Up @@ -638,7 +638,12 @@ public void subscribe(final String topic, final int qos,
IMqttActionListener listener = new MqttConnectionListener(
resultBundle);
try {
myClient.subscribe(topic, qos, invocationContext, listener);
IMqttToken token = myClient.subscribe(topic, qos, invocationContext, listener);
token.waitForCompletion();
int[] grantedQos = token.getGrantedQos();
if (grantedQos.length == 1 && grantedQos[0] == 0x80) {
throw new MqttException(MqttException.REASON_CODE_SUBSCRIBE_FAILED);
}
} catch (Exception e) {
handleException(resultBundle, e);
}
Expand All @@ -652,7 +657,7 @@ public void subscribe(final String topic, final int qos,

/**
* Subscribe to one or more topics
*
*
* @param topic
* a list of possibly wildcarded topic names
* @param qos
Expand All @@ -679,7 +684,13 @@ public void subscribe(final String[] topic, final int[] qos,
IMqttActionListener listener = new MqttConnectionListener(
resultBundle);
try {
myClient.subscribe(topic, qos, invocationContext, listener);
IMqttToken token = myClient.subscribe(topic, qos, invocationContext, listener);
token.waitForCompletion();
int[] grantedQos = token.getGrantedQos();
System.arraycopy(grantedQos, 0, qos, 0, grantedQos.length);
if (grantedQos.length == 1 && qos[0] == 0x80) {
throw new MqttException(MqttException.REASON_CODE_SUBSCRIBE_FAILED);
}
} catch (Exception e) {
handleException(resultBundle, e);
}
Expand All @@ -701,8 +712,13 @@ public void subscribe(String[] topicFilters, int[] qos, String invocationContext
if((myClient != null) && (myClient.isConnected())){
IMqttActionListener listener = new MqttConnectionListener(resultBundle);
try {

myClient.subscribe(topicFilters, qos,messageListeners);
IMqttToken token = myClient.subscribe(topicFilters, qos,messageListeners);
token.waitForCompletion();
int[] grantedQos = token.getGrantedQos();
System.arraycopy(grantedQos, 0, qos, 0, grantedQos.length);
if (grantedQos.length == 1 && qos[0] == 0x80) {
throw new MqttException(MqttException.REASON_CODE_SUBSCRIBE_FAILED);
}
} catch (Exception e){
handleException(resultBundle, e);
}
Expand Down Expand Up @@ -754,7 +770,7 @@ void unsubscribe(final String topic, String invocationContext,

/**
* Unsubscribe from one or more topics
*
*
* @param topic
* a list of possibly wildcarded topic names
* @param invocationContext
Expand Down Expand Up @@ -793,7 +809,7 @@ void unsubscribe(final String[] topic, String invocationContext,

/**
* Get tokens for all outstanding deliveries for a client
*
*
* @return an array (possibly empty) of tokens
*/
public IMqttDeliveryToken[] getPendingDeliveryTokens() {
Expand All @@ -803,7 +819,7 @@ public IMqttDeliveryToken[] getPendingDeliveryTokens() {
// Implement MqttCallback
/**
* Callback for connectionLost
*
*
* @param why
* the exeception causing the break in communications
*/
Expand Down Expand Up @@ -858,7 +874,7 @@ public void onFailure(IMqttToken asyncActionToken,
/**
* Callback to indicate a message has been delivered (the exact meaning of
* "has been delivered" is dependent on the QOS value)
*
*
* @param messageToken
* the messge token provided when the message was originally sent
*/
Expand All @@ -885,7 +901,7 @@ public void deliveryComplete(IMqttDeliveryToken messageToken) {
resultBundle.putString(
MqttServiceConstants.CALLBACK_INVOCATION_CONTEXT,
invocationContext);

service.callbackToActivity(clientHandle, Status.OK,
resultBundle);
}
Expand All @@ -899,7 +915,7 @@ public void deliveryComplete(IMqttDeliveryToken messageToken) {

/**
* Callback when a message is received
*
*
* @param topic
* the topic on which the message was received
* @param message
Expand All @@ -914,22 +930,22 @@ public void messageArrived(String topic, MqttMessage message)

String messageId = service.messageStore.storeArrived(clientHandle,
topic, message);

Bundle resultBundle = messageToBundle(messageId, topic, message);
resultBundle.putString(MqttServiceConstants.CALLBACK_ACTION,
MqttServiceConstants.MESSAGE_ARRIVED_ACTION);
resultBundle.putString(MqttServiceConstants.CALLBACK_MESSAGE_ID,
messageId);
service.callbackToActivity(clientHandle, Status.OK, resultBundle);

}



/**
* Store details of sent messages so we can handle "deliveryComplete"
* callbacks from the mqttClient
*
*
* @param topic
* @param msg
* @param messageToken
Expand Down Expand Up @@ -975,7 +991,7 @@ private void releaseWakeLock() {
* <p>
* Simply handles the basic success/failure cases for operations which don't
* return results
*
*
*/
private class MqttConnectionListener implements IMqttActionListener {

Expand Down Expand Up @@ -1007,18 +1023,18 @@ public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
* if cleanSession is true, we need to regard this as a disconnection
*/
void offline() {

if (!disconnected && !cleanSession) {
Exception e = new Exception("Android offline");
connectionLost(e);
}
}

/**
* Reconnect<br>
* Only appropriate if cleanSession is false and we were connected.
* Declare as synchronized to avoid multiple calls to this method to send connect
* multiple times
* Declare as synchronized to avoid multiple calls to this method to send connect
* multiple times
*/
synchronized void reconnect() {

Expand All @@ -1031,7 +1047,7 @@ synchronized void reconnect() {
service.traceDebug(TAG, "The client is connecting. Reconnect return directly.");
return ;
}

if(!service.isOnline()){
service.traceDebug(TAG,
"The network is not reachable. Will not do reconnect");
Expand Down Expand Up @@ -1067,9 +1083,9 @@ synchronized void reconnect() {
MqttServiceConstants.CALLBACK_INVOCATION_CONTEXT, null);
resultBundle.putString(MqttServiceConstants.CALLBACK_ACTION,
MqttServiceConstants.CONNECT_ACTION);

try {

IMqttActionListener listener = new MqttConnectionListener(resultBundle) {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
Expand All @@ -1079,7 +1095,7 @@ public void onSuccess(IMqttToken asyncActionToken) {
service.traceDebug(TAG,"DeliverBacklog when reconnect.");
doAfterConnectSuccess(resultBundle);
}

@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
resultBundle.putString(
Expand All @@ -1092,10 +1108,10 @@ public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
resultBundle);

doAfterConnectFail(resultBundle);

}
};

myClient.connect(connectOptions, null, listener);
setConnectingState(true);
} catch (MqttException e) {
Expand All @@ -1116,13 +1132,13 @@ public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
}
}
}

/**
*
*
* @param isConnecting
*/
private synchronized void setConnectingState(boolean isConnecting){
this.isConnecting = isConnecting;
this.isConnecting = isConnecting;
}

/**
Expand Down