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.

This commit is contained in:
Cameron Gutman
2014-09-19 22:23:06 -07:00
parent 62ecb1af50
commit 201704dc9d
2 changed files with 38 additions and 3 deletions
@@ -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) {