Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ public void onServiceDisconnected(ComponentName name) {

private volatile boolean registerReceiver = false;
private volatile boolean bindedService = false;

private MqttAuthenticationFailureHandler authFailHandler;

/**
* Constructor - create an MqttAndroidClient that can be used to communicate with an MQTT server on android
Expand All @@ -163,8 +165,8 @@ public void onServiceDisconnected(ComponentName name) {
* identified to the server
*/
public MqttAndroidClient(Context context, String serverURI,
String clientId) {
this(context, serverURI, clientId, null, Ack.AUTO_ACK);
String clientId, MqttAuthenticationFailureHandler authFailHandler) {
this(context, serverURI, clientId, null, Ack.AUTO_ACK, authFailHandler);
}

/**
Expand All @@ -183,8 +185,9 @@ public MqttAndroidClient(Context context, String serverURI,
* how the application wishes to acknowledge a message has been
* processed
*/
public MqttAndroidClient(Context ctx, String serverURI, String clientId, Ack ackType) {
this(ctx, serverURI, clientId, null, ackType);
public MqttAndroidClient(Context ctx, String serverURI, String clientId, Ack ackType,
MqttAuthenticationFailureHandler authFailHandler) {
this(ctx, serverURI, clientId, null, ackType, authFailHandler);
}

/**
Expand All @@ -202,8 +205,9 @@ public MqttAndroidClient(Context ctx, String serverURI, String clientId, Ack ack
* @param persistence
* The object to use to store persisted data
*/
public MqttAndroidClient(Context ctx, String serverURI, String clientId, MqttClientPersistence persistence) {
this(ctx, serverURI, clientId, persistence, Ack.AUTO_ACK);
public MqttAndroidClient(Context ctx, String serverURI, String clientId, MqttClientPersistence persistence,
MqttAuthenticationFailureHandler authFailHandler) {
this(ctx, serverURI, clientId, persistence, Ack.AUTO_ACK, authFailHandler);
}

/**
Expand All @@ -226,12 +230,14 @@ public MqttAndroidClient(Context ctx, String serverURI, String clientId, MqttCli
* processed.
*/
public MqttAndroidClient(Context context, String serverURI,
String clientId, MqttClientPersistence persistence, Ack ackType) {
String clientId, MqttClientPersistence persistence, Ack ackType,
MqttAuthenticationFailureHandler authFailHandler) {
myContext = context;
this.serverURI = serverURI;
this.clientId = clientId;
this.persistence = persistence;
messageAck = ackType;
this.authFailHandler = authFailHandler;
}

/**
Expand Down Expand Up @@ -286,7 +292,7 @@ public String getServerURI() {
@Override
public void close() {
if (clientHandle == null) {
clientHandle = mqttService.getClient(serverURI, clientId, myContext.getApplicationInfo().packageName,persistence);
clientHandle = mqttService.getClient(serverURI, clientId, myContext.getApplicationInfo().packageName,persistence, authFailHandler);
}
mqttService.close(clientHandle);
}
Expand Down Expand Up @@ -452,7 +458,7 @@ private void registerReceiver(BroadcastReceiver receiver) {
private void doConnect() {
if (clientHandle == null) {
clientHandle = mqttService.getClient(serverURI, clientId,myContext.getApplicationInfo().packageName,
persistence);
persistence, authFailHandler);
}
mqttService.setTraceEnabled(traceEnabled);
mqttService.setTraceCallbackId(clientHandle);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.eclipse.paho.android.service;

public interface MqttAuthenticationFailureHandler {
boolean isApplicationAuthenticationFailure();

void onApplicationAuthenticationFailure();
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ public void setClientHandle(String clientHandle) {

private WakeLock wakelock = null;
private String wakeLockTag = null;

private MqttAuthenticationFailureHandler authFailHandler;

/**
* Constructor - create an MqttConnection to communicate with MQTT server
Expand All @@ -154,13 +156,15 @@ public void setClientHandle(String clientHandle) {
* the "handle" by which the activity will identify us
*/
MqttConnection(MqttService service, String serverURI, String clientId,
MqttClientPersistence persistence, String clientHandle) {
MqttClientPersistence persistence, String clientHandle,
MqttAuthenticationFailureHandler authFailHandler) {
this.serverURI = serverURI.toString();
this.service = service;
this.clientId = clientId;
this.persistence = persistence;
this.clientHandle = clientHandle;

this.authFailHandler = authFailHandler;

StringBuffer buff = new StringBuffer(this.getClass().getCanonicalName());
buff.append(" ");
buff.append(clientId);
Expand Down Expand Up @@ -260,7 +264,11 @@ public void onFailure(IMqttToken asyncActionToken,

// if connect fail ,try reconnect.
if(service.isOnline()){
connect(connectOptions, internel_invocationContext,connectActivityToken);
if(authFailHandler.isApplicationAuthenticationFailure()) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. bagian clause if bisa ekstrak ke private method untuk menghindari duplikasi.
  2. Sebaiknya ditambahkan juga handling untuk null, karena bisa saja jika ke depannya kita tidak mau menggunakan handler sendiri.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sudah di perbaiki pada f076f97

authFailHandler.onApplicationAuthenticationFailure();
} else {
connect(connectOptions, internel_invocationContext, connectActivityToken);
}
}

}
Expand Down Expand Up @@ -1019,20 +1027,11 @@ public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
} catch(InterruptedException e) {
}

if(isNotAuthenticationFailed(exception))
{
if (authFailHandler.isApplicationAuthenticationFailure()) {
authFailHandler.onApplicationAuthenticationFailure();
} else {
reconnect();
}

}

private boolean isNotAuthenticationFailed(Throwable throwable) {
int reasonCode = -1;
if(throwable instanceof MqttException) {
reasonCode = ((MqttException) throwable).getReasonCode();
}

return reasonCode != MqttException.REASON_CODE_FAILED_AUTHENTICATION;
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,12 @@ void callbackToActivity(String clientHandle, Status status,
* @return a string to be used by the Activity as a "handle" for this
* MqttConnection
*/
public String getClient(String serverURI, String clientId, String contextId,MqttClientPersistence persistence) {
public String getClient(String serverURI, String clientId, String contextId,
MqttClientPersistence persistence, MqttAuthenticationFailureHandler authFailHandler) {
String clientHandle = serverURI + ":" + clientId+":"+contextId;
if (!connections.containsKey(clientHandle)) {
MqttConnection client = new MqttConnection(this, serverURI,
clientId, persistence, clientHandle);
MqttConnection client = new MqttConnection(this, serverURI,
clientId, persistence, clientHandle, authFailHandler);
connections.put(clientHandle, client);
}
return clientHandle;
Expand Down