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

View File

@ -156,7 +156,7 @@ public class PcView extends Activity {
} }
} }
private void stopComputerUpdates() { private void stopComputerUpdates(boolean wait) {
if (managerBinder != null) { if (managerBinder != null) {
if (!runningPolling) { if (!runningPolling) {
return; return;
@ -165,6 +165,11 @@ public class PcView extends Activity {
freezeUpdates = true; freezeUpdates = true;
managerBinder.stopPolling(); managerBinder.stopPolling();
if (wait) {
managerBinder.waitForPollingStopped();
}
runningPolling = false; runningPolling = false;
} }
} }
@ -189,7 +194,7 @@ public class PcView extends Activity {
protected void onPause() { protected void onPause() {
super.onPause(); super.onPause();
stopComputerUpdates(); stopComputerUpdates(false);
} }
@Override @Override
@ -201,7 +206,7 @@ public class PcView extends Activity {
@Override @Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
stopComputerUpdates(); stopComputerUpdates(false);
// Call superclass // Call superclass
super.onCreateContextMenu(menu, v, menuInfo); super.onCreateContextMenu(menu, v, menuInfo);
@ -258,6 +263,9 @@ public class PcView extends Activity {
NvHTTP httpConn; NvHTTP httpConn;
String message; String message;
try { try {
// Stop updates and wait while pairing
stopComputerUpdates(true);
InetAddress addr = null; InetAddress addr = null;
if (computer.reachability == ComputerDetails.Reachability.LOCAL) { if (computer.reachability == ComputerDetails.Reachability.LOCAL) {
addr = computer.localIp; addr = computer.localIp;
@ -312,6 +320,9 @@ public class PcView extends Activity {
Toast.makeText(PcView.this, toastMessage, Toast.LENGTH_LONG).show(); Toast.makeText(PcView.this, toastMessage, Toast.LENGTH_LONG).show();
} }
}); });
// Start polling again
startComputerUpdates();
} }
}).start(); }).start();
} }

View File

@ -46,6 +46,8 @@ public class ComputerManagerService extends Service {
private ThreadPoolExecutor pollingPool; private ThreadPoolExecutor pollingPool;
private Timer pollingTimer; private Timer pollingTimer;
private ComputerManagerListener listener = null; private ComputerManagerListener listener = null;
private AtomicInteger activePolls = new AtomicInteger(0);
private boolean stopped;
private DiscoveryService.DiscoveryBinder discoveryBinder; private DiscoveryService.DiscoveryBinder discoveryBinder;
private ServiceConnection discoveryServiceConnection = new ServiceConnection() { private ServiceConnection discoveryServiceConnection = new ServiceConnection() {
@ -69,6 +71,9 @@ public class ComputerManagerService extends Service {
public class ComputerManagerBinder extends Binder { public class ComputerManagerBinder extends Binder {
public void startPolling(ComputerManagerListener listener) { public void startPolling(ComputerManagerListener listener) {
// Not stopped
stopped = false;
// Set the listener // Set the listener
ComputerManagerService.this.listener = 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) { public boolean addComputerBlocking(InetAddress addr) {
return ComputerManagerService.this.addComputerBlocking(addr); return ComputerManagerService.this.addComputerBlocking(addr);
} }
@ -116,6 +129,9 @@ public class ComputerManagerService extends Service {
@Override @Override
public boolean onUnbind(Intent intent) { public boolean onUnbind(Intent intent) {
// Stopped now
stopped = true;
// Stop mDNS autodiscovery // Stop mDNS autodiscovery
discoveryBinder.stopDiscovery(); discoveryBinder.stopDiscovery();
@ -385,15 +401,23 @@ public class ComputerManagerService extends Service {
public void run() { public void run() {
boolean newPc = (details.name == null); boolean newPc = (details.name == null);
if (stopped) {
return;
}
if (!getLocalDatabaseReference()) { if (!getLocalDatabaseReference()) {
return; return;
} }
activePolls.incrementAndGet();
// Poll the machine // Poll the machine
if (!doPollMachine(details)) { if (!doPollMachine(details)) {
details.state = ComputerDetails.State.OFFLINE; details.state = ComputerDetails.State.OFFLINE;
details.reachability = ComputerDetails.Reachability.OFFLINE; details.reachability = ComputerDetails.Reachability.OFFLINE;
} }
activePolls.decrementAndGet();
// If it's online, update our persistent state // If it's online, update our persistent state
if (details.state == ComputerDetails.State.ONLINE) { if (details.state == ComputerDetails.State.ONLINE) {