Skip to content

Commit

Permalink
[50076] Added timer to recheck connection every 3 seconds when discon…
Browse files Browse the repository at this point in the history
…nected from the RVI server.
  • Loading branch information
lillialexis committed Nov 12, 2015
1 parent 46fa56f commit 7c07adb
Showing 1 changed file with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.util.Log;
import com.google.gson.Gson;
Expand Down Expand Up @@ -155,12 +156,16 @@ public void onServiceInvoked(ServiceBundle serviceBundle, String serviceIdentifi
public void nodeDidConnect() {
Log.d(TAG, "Connected to RVI provisioning server!");
connectionStatus = ConnectionStatus.CONNECTED;

stopRepeatingTask();
}

@Override
public void nodeDidFailToConnect(Throwable trigger) {
Log.d(TAG, "Failed to connect to RVI provisioning server!");
connectionStatus = ConnectionStatus.DISCONNECTED;

//startRepeatingTask();
}

@Override
Expand All @@ -169,7 +174,7 @@ public void nodeDidDisconnect(Throwable trigger) {
connectionStatus = ConnectionStatus.DISCONNECTED;

/* Try and reconnect */
connect();
startRepeatingTask();
}
};

Expand All @@ -183,7 +188,32 @@ public void nodeDidDisconnect(Throwable trigger) {
rviNode.addBundle(reportingServiceBundle);
}


Handler timerHandler = new Handler();
Runnable timerRunnable = new Runnable()
{
@Override
public void run() {
if (connectionStatus == ConnectionStatus.DISCONNECTED) connect();

timerHandler.postDelayed(this, 3000);
}
};


void startRepeatingTask() {
timerHandler.postDelayed(timerRunnable, 0);
}

void stopRepeatingTask() {
timerHandler.removeCallbacks(timerRunnable);
}



public static void connect() {
Log.d(TAG, "Attempting to connect to RVI provisioning server.");

rviNode.setServerUrl(preferences.getString("pref_rvi_server", "38.129.64.40"));
rviNode.setServerPort(Integer.parseInt(preferences.getString("pref_rvi_server_port", "8807")));

Expand Down Expand Up @@ -271,7 +301,9 @@ public static Collection<UserCredentials> getRemoteCredentialsList() {
if (credListStr == null) return null;

Collection<UserCredentials> credsList = null;
Type collectionType = new TypeToken<Collection<UserCredentials>>() {}.getType();
Type collectionType = new TypeToken<Collection<UserCredentials>>()
{
}.getType();

try {
credsList = gson.fromJson(credListStr, collectionType);
Expand Down

0 comments on commit 7c07adb

Please sign in to comment.