Skip to content

Commit 09c48df

Browse files
committed
Merge pull request #4 from netzme/refactor_kick_process
add interface for Auth Fail handler.
2 parents 5c73e22 + f076f97 commit 09c48df

File tree

4 files changed

+46
-28
lines changed

4 files changed

+46
-28
lines changed

org.eclipse.paho.android.service/org.eclipse.paho.android.service/src/org/eclipse/paho/android/service/MqttAndroidClient.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ public void onServiceDisconnected(ComponentName name) {
149149

150150
private volatile boolean registerReceiver = false;
151151
private volatile boolean bindedService = false;
152+
153+
private MqttAuthenticationFailureHandler authFailHandler;
152154

153155
/**
154156
* Constructor - create an MqttAndroidClient that can be used to communicate with an MQTT server on android
@@ -163,8 +165,8 @@ public void onServiceDisconnected(ComponentName name) {
163165
* identified to the server
164166
*/
165167
public MqttAndroidClient(Context context, String serverURI,
166-
String clientId) {
167-
this(context, serverURI, clientId, null, Ack.AUTO_ACK);
168+
String clientId, MqttAuthenticationFailureHandler authFailHandler) {
169+
this(context, serverURI, clientId, null, Ack.AUTO_ACK, authFailHandler);
168170
}
169171

170172
/**
@@ -183,8 +185,9 @@ public MqttAndroidClient(Context context, String serverURI,
183185
* how the application wishes to acknowledge a message has been
184186
* processed
185187
*/
186-
public MqttAndroidClient(Context ctx, String serverURI, String clientId, Ack ackType) {
187-
this(ctx, serverURI, clientId, null, ackType);
188+
public MqttAndroidClient(Context ctx, String serverURI, String clientId, Ack ackType,
189+
MqttAuthenticationFailureHandler authFailHandler) {
190+
this(ctx, serverURI, clientId, null, ackType, authFailHandler);
188191
}
189192

190193
/**
@@ -202,8 +205,9 @@ public MqttAndroidClient(Context ctx, String serverURI, String clientId, Ack ack
202205
* @param persistence
203206
* The object to use to store persisted data
204207
*/
205-
public MqttAndroidClient(Context ctx, String serverURI, String clientId, MqttClientPersistence persistence) {
206-
this(ctx, serverURI, clientId, persistence, Ack.AUTO_ACK);
208+
public MqttAndroidClient(Context ctx, String serverURI, String clientId, MqttClientPersistence persistence,
209+
MqttAuthenticationFailureHandler authFailHandler) {
210+
this(ctx, serverURI, clientId, persistence, Ack.AUTO_ACK, authFailHandler);
207211
}
208212

209213
/**
@@ -226,12 +230,14 @@ public MqttAndroidClient(Context ctx, String serverURI, String clientId, MqttCli
226230
* processed.
227231
*/
228232
public MqttAndroidClient(Context context, String serverURI,
229-
String clientId, MqttClientPersistence persistence, Ack ackType) {
233+
String clientId, MqttClientPersistence persistence, Ack ackType,
234+
MqttAuthenticationFailureHandler authFailHandler) {
230235
myContext = context;
231236
this.serverURI = serverURI;
232237
this.clientId = clientId;
233238
this.persistence = persistence;
234239
messageAck = ackType;
240+
this.authFailHandler = authFailHandler;
235241
}
236242

237243
/**
@@ -286,7 +292,7 @@ public String getServerURI() {
286292
@Override
287293
public void close() {
288294
if (clientHandle == null) {
289-
clientHandle = mqttService.getClient(serverURI, clientId, myContext.getApplicationInfo().packageName,persistence);
295+
clientHandle = mqttService.getClient(serverURI, clientId, myContext.getApplicationInfo().packageName,persistence, authFailHandler);
290296
}
291297
mqttService.close(clientHandle);
292298
}
@@ -452,7 +458,7 @@ private void registerReceiver(BroadcastReceiver receiver) {
452458
private void doConnect() {
453459
if (clientHandle == null) {
454460
clientHandle = mqttService.getClient(serverURI, clientId,myContext.getApplicationInfo().packageName,
455-
persistence);
461+
persistence, authFailHandler);
456462
}
457463
mqttService.setTraceEnabled(traceEnabled);
458464
mqttService.setTraceCallbackId(clientHandle);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.eclipse.paho.android.service;
2+
3+
public interface MqttAuthenticationFailureHandler {
4+
boolean isApplicationAuthenticationFailure();
5+
6+
void onApplicationAuthenticationFailure();
7+
}

org.eclipse.paho.android.service/org.eclipse.paho.android.service/src/org/eclipse/paho/android/service/MqttConnection.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ public void setClientHandle(String clientHandle) {
136136

137137
private WakeLock wakelock = null;
138138
private String wakeLockTag = null;
139+
140+
private MqttAuthenticationFailureHandler authFailHandler;
139141

140142
/**
141143
* Constructor - create an MqttConnection to communicate with MQTT server
@@ -154,13 +156,15 @@ public void setClientHandle(String clientHandle) {
154156
* the "handle" by which the activity will identify us
155157
*/
156158
MqttConnection(MqttService service, String serverURI, String clientId,
157-
MqttClientPersistence persistence, String clientHandle) {
159+
MqttClientPersistence persistence, String clientHandle,
160+
MqttAuthenticationFailureHandler authFailHandler) {
158161
this.serverURI = serverURI.toString();
159162
this.service = service;
160163
this.clientId = clientId;
161164
this.persistence = persistence;
162165
this.clientHandle = clientHandle;
163-
166+
this.authFailHandler = authFailHandler;
167+
164168
StringBuffer buff = new StringBuffer(this.getClass().getCanonicalName());
165169
buff.append(" ");
166170
buff.append(clientId);
@@ -260,7 +264,11 @@ public void onFailure(IMqttToken asyncActionToken,
260264

261265
// if connect fail ,try reconnect.
262266
if(service.isOnline()){
263-
connect(connectOptions, internel_invocationContext,connectActivityToken);
267+
if(isAuthenticationFailure()) {
268+
authFailHandler.onApplicationAuthenticationFailure();
269+
} else {
270+
connect(connectOptions, internel_invocationContext, connectActivityToken);
271+
}
264272
}
265273

266274
}
@@ -984,7 +992,7 @@ synchronized void reconnect() {
984992
resultBundle.putString(
985993
MqttServiceConstants.CALLBACK_INVOCATION_CONTEXT, null);
986994
resultBundle.putString(MqttServiceConstants.CALLBACK_ACTION,
987-
MqttServiceConstants.CONNECT_ACTION);
995+
MqttServiceConstants.CONNECT_ACTION);
988996

989997
try {
990998

@@ -1019,20 +1027,11 @@ public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
10191027
} catch(InterruptedException e) {
10201028
}
10211029

1022-
if(isNotAuthenticationFailed(exception))
1023-
{
1030+
if (isAuthenticationFailure()) {
1031+
authFailHandler.onApplicationAuthenticationFailure();
1032+
} else {
10241033
reconnect();
10251034
}
1026-
1027-
}
1028-
1029-
private boolean isNotAuthenticationFailed(Throwable throwable) {
1030-
int reasonCode = -1;
1031-
if(throwable instanceof MqttException) {
1032-
reasonCode = ((MqttException) throwable).getReasonCode();
1033-
}
1034-
1035-
return reasonCode != MqttException.REASON_CODE_FAILED_AUTHENTICATION;
10361035
}
10371036
};
10381037

@@ -1053,4 +1052,9 @@ private boolean isNotAuthenticationFailed(Throwable throwable) {
10531052
synchronized void setConnectingState(boolean isConnecting){
10541053
this.isConnecting = isConnecting;
10551054
}
1055+
1056+
private boolean isAuthenticationFailure() {
1057+
return (authFailHandler != null) &&
1058+
authFailHandler.isApplicationAuthenticationFailure();
1059+
}
10561060
}

org.eclipse.paho.android.service/org.eclipse.paho.android.service/src/org/eclipse/paho/android/service/MqttService.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,12 @@ void callbackToActivity(String clientHandle, Status status,
296296
* @return a string to be used by the Activity as a "handle" for this
297297
* MqttConnection
298298
*/
299-
public String getClient(String serverURI, String clientId, String contextId,MqttClientPersistence persistence) {
299+
public String getClient(String serverURI, String clientId, String contextId,
300+
MqttClientPersistence persistence, MqttAuthenticationFailureHandler authFailHandler) {
300301
String clientHandle = serverURI + ":" + clientId+":"+contextId;
301302
if (!connections.containsKey(clientHandle)) {
302-
MqttConnection client = new MqttConnection(this, serverURI,
303-
clientId, persistence, clientHandle);
303+
MqttConnection client = new MqttConnection(this, serverURI,
304+
clientId, persistence, clientHandle, authFailHandler);
304305
connections.put(clientHandle, client);
305306
}
306307
return clientHandle;

0 commit comments

Comments
 (0)