From f1787c43e5f8e323190816281c0e210b6e73a313 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Thu, 26 Feb 2015 18:27:50 -0500 Subject: [PATCH] Generalize the polling grace period to all users of CMS --- app/src/main/java/com/limelight/AppView.java | 27 +++++++------------ .../computers/ComputerManagerService.java | 25 +++++++++++++---- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/com/limelight/AppView.java b/app/src/main/java/com/limelight/AppView.java index 5ec10b46..05dcfddc 100644 --- a/app/src/main/java/com/limelight/AppView.java +++ b/app/src/main/java/com/limelight/AppView.java @@ -55,9 +55,6 @@ public class AppView extends Activity implements AdapterFragmentCallbacks { private SpinnerDialog blockingLoadSpinner; private String lastRawApplist; - private int consecutiveAppListFailures = 0; - private final static int CONSECUTIVE_FAILURE_LIMIT = 3; - private final static int START_OR_RESUME_ID = 1; private final static int QUIT_ID = 2; private final static int CANCEL_ID = 3; @@ -133,25 +130,19 @@ public class AppView extends Activity implements AdapterFragmentCallbacks { } if (details.state != ComputerDetails.State.ONLINE) { - consecutiveAppListFailures++; - - if (consecutiveAppListFailures >= CONSECUTIVE_FAILURE_LIMIT) { - // The PC is unreachable now - AppView.this.runOnUiThread(new Runnable() { - @Override - public void run() { - // Display a toast to the user and quit the activity - Toast.makeText(AppView.this, getResources().getText(R.string.lost_connection), Toast.LENGTH_SHORT).show(); - finish(); - } - }); - } + // The PC is unreachable now + AppView.this.runOnUiThread(new Runnable() { + @Override + public void run() { + // Display a toast to the user and quit the activity + Toast.makeText(AppView.this, getResources().getText(R.string.lost_connection), Toast.LENGTH_SHORT).show(); + finish(); + } + }); return; } - consecutiveAppListFailures = 0; - // App list is the same or empty; nothing to do if (details.rawAppList == null || details.rawAppList.equals(lastRawApplist)) { return; diff --git a/app/src/main/java/com/limelight/computers/ComputerManagerService.java b/app/src/main/java/com/limelight/computers/ComputerManagerService.java index 652d2c43..56e8d157 100644 --- a/app/src/main/java/com/limelight/computers/ComputerManagerService.java +++ b/app/src/main/java/com/limelight/computers/ComputerManagerService.java @@ -2,6 +2,7 @@ package com.limelight.computers; import java.io.FileOutputStream; import java.io.IOException; +import java.io.OutputStreamWriter; import java.io.StringReader; import java.net.InetAddress; import java.net.InetSocketAddress; @@ -34,6 +35,7 @@ public class ComputerManagerService extends Service { private static final int POLLING_PERIOD_MS = 3000; private static final int MDNS_QUERY_PERIOD_MS = 1000; private static final int FAST_POLL_TIMEOUT = 500; + private static final int OFFLINE_POLL_TRIES = 3; private final ComputerManagerBinder binder = new ComputerManagerBinder(); @@ -67,7 +69,7 @@ public class ComputerManagerService extends Service { }; // Returns true if the details object was modified - private boolean runPoll(ComputerDetails details, boolean newPc) throws InterruptedException { + private boolean runPoll(ComputerDetails details, boolean newPc, int offlineCount) throws InterruptedException { if (!getLocalDatabaseReference()) { return false; } @@ -77,6 +79,11 @@ public class ComputerManagerService extends Service { // Poll the machine try { if (!pollComputer(details)) { + if (!newPc && offlineCount < OFFLINE_POLL_TRIES) { + // Return without calling the listener + return false; + } + details.state = ComputerDetails.State.OFFLINE; details.reachability = ComputerDetails.Reachability.OFFLINE; } @@ -95,7 +102,7 @@ public class ComputerManagerService extends Service { if (dbManager.getComputerByName(details.name) == null) { // It's gone releaseLocalDatabaseReference(); - return true; + return false; } } @@ -115,13 +122,21 @@ public class ComputerManagerService extends Service { Thread t = new Thread() { @Override public void run() { + + int offlineCount = 0; while (!isInterrupted() && pollingActive) { try { // Check if this poll has modified the details - runPoll(details, false); + if (!runPoll(details, false, offlineCount)) { + LimeLog.warning(details.name + " is offline (try " + offlineCount + ")"); + offlineCount++; + } + else { + offlineCount = 0; + } // Wait until the next polling interval - Thread.sleep(POLLING_PERIOD_MS); + Thread.sleep(POLLING_PERIOD_MS / ((offlineCount > 0) ? 2 : 1)); } catch (InterruptedException e) { break; } @@ -293,7 +308,7 @@ public class ComputerManagerService extends Service { // Block while we try to fill the details try { - runPoll(fakeDetails, true); + runPoll(fakeDetails, true, 0); } catch (InterruptedException e) { return false; }