Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -15,6 +15,7 @@
import org.eclipse.paho.client.mqttv3.IMqttActionListener;
import org.eclipse.paho.client.mqttv3.IMqttToken;
import org.eclipse.paho.client.mqttv3.MqttPingSender;
import org.eclipse.paho.client.mqttv3.MqttToken;
import org.eclipse.paho.client.mqttv3.internal.ClientComms;

import android.app.AlarmManager;
Expand Down Expand Up @@ -126,47 +127,57 @@ public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Ping " + count + " times.");

Log.d(TAG, "Check time :" + System.currentTimeMillis());
IMqttToken token = comms.checkForActivity();

// No ping has been sent.
if (token == null) {
return;
}

// Assign new callback to token to execute code after PingResq
// arrives. Get another wakelock even receiver already has one,
// release it until ping response returns.
if (wakelock == null) {
PowerManager pm = (PowerManager) service
.getSystemService(Service.POWER_SERVICE);
wakelock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
wakeLockTag);
wakelock.setReferenceCounted(false);
}
wakelock.acquire();
token.setActionCallback(new IMqttActionListener() {
IMqttActionListener actionListener = new IMqttActionListener() {

@Override
public void onSuccess(IMqttToken asyncActionToken) {
Log.d(TAG, "Success. Release lock(" + wakeLockTag + "):"
+ System.currentTimeMillis());
//Release wakelock when it is done.
if(wakelock != null && wakelock.isHeld()){
wakelock.release();
}
releaseWakeLock();
}

@Override
public void onFailure(IMqttToken asyncActionToken,
Throwable exception) {
Throwable exception) {
Log.d(TAG, "Failure. Release lock(" + wakeLockTag + "):"
+ System.currentTimeMillis());
//Release wakelock when it is done.
if(wakelock != null && wakelock.isHeld()){
wakelock.release();
}
releaseWakeLock();
}
});
};

// Assign new callback to token to execute code after PingResq
// arrives. Get another wakelock even receiver already has one,
// release it until ping response returns.
if (wakelock == null) {
PowerManager pm = (PowerManager) service
.getSystemService(Service.POWER_SERVICE);
wakelock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
wakeLockTag);
wakelock.setReferenceCounted(false);
}
wakelock.acquire();
Log.d(TAG, "hold wakelock(" + wakeLockTag + "):"
+ System.currentTimeMillis());

//action listener ditambahkan supaya wakelock dilepas setelah send ping.
IMqttToken token = comms.checkForActivity(actionListener);

// No ping has been sent.
if (token == null) {
Log.d(TAG, "no ping sent");
releaseWakeLock();
return;
}
}

private void releaseWakeLock() {
if(wakelock != null && wakelock.isHeld()){
wakelock.release();
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Properties;
import java.util.Vector;

import org.eclipse.paho.client.mqttv3.IMqttActionListener;
import org.eclipse.paho.client.mqttv3.IMqttAsyncClient;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClientPersistence;
Expand Down Expand Up @@ -657,14 +658,26 @@ public void run() {
public MqttToken checkForActivity(){
MqttToken token = null;
try{
token = clientState.checkForActivity();
token = clientState.checkForActivity(null);
}catch(MqttException e){
handleRunException(e);
}catch(Exception e){
handleRunException(e);
}
return token;
}
}

public MqttToken checkForActivity(IMqttActionListener actionListener) {
MqttToken token = null;
try{
token = clientState.checkForActivity(actionListener);
}catch(MqttException e){
handleRunException(e);
}catch(Exception e){
handleRunException(e);
}
return token;
}

private void handleRunException(Exception ex) {
final String methodName = "handleRunException";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public class MqttToken implements IMqttToken {
* MqttToken. MQTT application programs must not use the internal class.
*/
public Token internalTok = null;

/*
* callback ketika selesai kirim ping
*/
private IMqttActionListener pingSentCallback;

public MqttToken() {
}
Expand Down Expand Up @@ -98,4 +103,16 @@ public boolean getSessionPresent() {
public MqttWireMessage getResponse() {
return internalTok.getResponse();
}

/*
* callback untuk ping event
*/
public void setPingCallback(IMqttActionListener callback) {
internalTok.setActionCallback(callback);
this.pingSentCallback = callback;
}

public IMqttActionListener getPingSentCallback() {
return pingSentCallback;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Properties;
import java.util.Vector;

import org.eclipse.paho.client.mqttv3.IMqttActionListener;
import org.eclipse.paho.client.mqttv3.MqttClientPersistence;
import org.eclipse.paho.client.mqttv3.MqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttException;
Expand Down Expand Up @@ -526,7 +527,7 @@ protected void undo(MqttPublish message) throws MqttPersistenceException {
*
* @return token of ping command, null if no ping command has been sent.
*/
public MqttToken checkForActivity() throws MqttException {
public MqttToken checkForActivity(IMqttActionListener actionListener) throws MqttException {
final String methodName = "checkForActivity";
//@TRACE 616=checkForActivity entered
log.fine(CLASS_NAME,methodName,"616", new Object[]{});
Expand Down Expand Up @@ -593,7 +594,13 @@ public MqttToken checkForActivity() throws MqttException {
// pingOutstanding++; // it will be set after the ping has been written on the wire
// lastPing = time; // it will be set after the ping has been written on the wire
token = new MqttToken(clientComms.getClient().getClientId());
tokenStore.saveToken(token, pingCommand);

//set callback di pindah dari ping sender untuk memastikan callback selalu terpanggil
if(actionListener != null) {
token.setPingCallback(actionListener);
}

tokenStore.saveToken(token, pingCommand);
pendingFlows.insertElementAt(pingCommand, 0);

nextPingTime = getKeepAlive();
Expand Down Expand Up @@ -771,7 +778,12 @@ protected void notifySent(MqttWireMessage message) {
pingOutstanding++;
}
//@TRACE 635=ping sent. pingOutstanding: {0}
log.fine(CLASS_NAME,methodName,"635",new Object[]{ new Integer(pingOutstanding)});
log.fine(CLASS_NAME, methodName, "635", new Object[]{new Integer(pingOutstanding)});

IMqttActionListener callback = token.getPingSentCallback();
if (callback != null) {
callback.onSuccess(token);
Copy link

Choose a reason for hiding this comment

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

Menurut saya, ini sebaiknya jangan menggunakan onSuccess, supaya tidak rancu antara sukses menerima ping response (behaviour terdahulu) dengan sukses mengirim ping (behaviour sekarang). Lebih baik membuat interface baru yang extends IMqttActionListener, lalu ada method onPingSent.

}
}
}
else if (message instanceof MqttPublish) {
Expand Down