From 201704dc9dcaf0c80ea7fb39236b3481227b3714 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Fri, 19 Sep 2014 22:23:06 -0700 Subject: [PATCH] Finally fix the random pairing failure. It turns out that it was causing by background querying for serverinfo during the pairing process. Now we stop polling computers while pairing is in progress. --- src/com/limelight/PcView.java | 17 ++++++++++--- .../computers/ComputerManagerService.java | 24 +++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/com/limelight/PcView.java b/src/com/limelight/PcView.java index 660c22d7..ae22092a 100644 --- a/src/com/limelight/PcView.java +++ b/src/com/limelight/PcView.java @@ -156,7 +156,7 @@ public class PcView extends Activity { } } - private void stopComputerUpdates() { + private void stopComputerUpdates(boolean wait) { if (managerBinder != null) { if (!runningPolling) { return; @@ -165,6 +165,11 @@ public class PcView extends Activity { freezeUpdates = true; managerBinder.stopPolling(); + + if (wait) { + managerBinder.waitForPollingStopped(); + } + runningPolling = false; } } @@ -189,7 +194,7 @@ public class PcView extends Activity { protected void onPause() { super.onPause(); - stopComputerUpdates(); + stopComputerUpdates(false); } @Override @@ -201,7 +206,7 @@ public class PcView extends Activity { @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { - stopComputerUpdates(); + stopComputerUpdates(false); // Call superclass super.onCreateContextMenu(menu, v, menuInfo); @@ -258,6 +263,9 @@ public class PcView extends Activity { NvHTTP httpConn; String message; try { + // Stop updates and wait while pairing + stopComputerUpdates(true); + InetAddress addr = null; if (computer.reachability == ComputerDetails.Reachability.LOCAL) { addr = computer.localIp; @@ -312,6 +320,9 @@ public class PcView extends Activity { Toast.makeText(PcView.this, toastMessage, Toast.LENGTH_LONG).show(); } }); + + // Start polling again + startComputerUpdates(); } }).start(); } diff --git a/src/com/limelight/computers/ComputerManagerService.java b/src/com/limelight/computers/ComputerManagerService.java index ddc07131..c82596c0 100644 --- a/src/com/limelight/computers/ComputerManagerService.java +++ b/src/com/limelight/computers/ComputerManagerService.java @@ -46,6 +46,8 @@ public class ComputerManagerService extends Service { private ThreadPoolExecutor pollingPool; private Timer pollingTimer; private ComputerManagerListener listener = null; + private AtomicInteger activePolls = new AtomicInteger(0); + private boolean stopped; private DiscoveryService.DiscoveryBinder discoveryBinder; private ServiceConnection discoveryServiceConnection = new ServiceConnection() { @@ -69,6 +71,9 @@ public class ComputerManagerService extends Service { public class ComputerManagerBinder extends Binder { public void startPolling(ComputerManagerListener listener) { + // Not stopped + stopped = false; + // Set the listener ComputerManagerService.this.listener = listener; @@ -92,6 +97,14 @@ public class ComputerManagerService extends Service { } } + public void waitForPollingStopped() { + while (activePolls.get() != 0) { + try { + Thread.sleep(250); + } catch (InterruptedException e) {} + } + } + public boolean addComputerBlocking(InetAddress addr) { return ComputerManagerService.this.addComputerBlocking(addr); } @@ -116,6 +129,9 @@ public class ComputerManagerService extends Service { @Override public boolean onUnbind(Intent intent) { + // Stopped now + stopped = true; + // Stop mDNS autodiscovery discoveryBinder.stopDiscovery(); @@ -385,15 +401,23 @@ public class ComputerManagerService extends Service { public void run() { boolean newPc = (details.name == null); + if (stopped) { + return; + } + if (!getLocalDatabaseReference()) { return; } + activePolls.incrementAndGet(); + // Poll the machine if (!doPollMachine(details)) { details.state = ComputerDetails.State.OFFLINE; details.reachability = ComputerDetails.Reachability.OFFLINE; } + + activePolls.decrementAndGet(); // If it's online, update our persistent state if (details.state == ComputerDetails.State.ONLINE) {