Fix thread spawning issue and remove some dead code

This commit is contained in:
Cameron Gutman 2014-11-02 21:16:09 -08:00
parent 1a71dda243
commit 78d213d686

View File

@ -38,7 +38,6 @@ public class ComputerManagerService extends Service {
private HashMap<ComputerDetails, Thread> pollingThreads; private HashMap<ComputerDetails, Thread> pollingThreads;
private ComputerManagerListener listener = null; private ComputerManagerListener listener = null;
private AtomicInteger activePolls = new AtomicInteger(0); private AtomicInteger activePolls = new AtomicInteger(0);
private boolean stopped;
private DiscoveryService.DiscoveryBinder discoveryBinder; private DiscoveryService.DiscoveryBinder discoveryBinder;
private final ServiceConnection discoveryServiceConnection = new ServiceConnection() { private final ServiceConnection discoveryServiceConnection = new ServiceConnection() {
@ -65,15 +64,6 @@ public class ComputerManagerService extends Service {
{ {
boolean newPc = (details.name == null); boolean newPc = (details.name == null);
// This is called from addComputerManually() where we don't
// want to block the initial poll if polling is disabled, so
// we explicitly let this through if we've never seen this
// PC before. This path won't be triggered normally when polling
// is disabled because the mDNS discovery is stopped.
if (stopped && !newPc) {
return false;
}
if (!getLocalDatabaseReference()) { if (!getLocalDatabaseReference()) {
return false; return false;
} }
@ -132,18 +122,18 @@ public class ComputerManagerService extends Service {
// Wait until the next polling interval // Wait until the next polling interval
try { try {
Thread.sleep(POLLING_PERIOD_MS); Thread.sleep(POLLING_PERIOD_MS);
} catch (InterruptedException e) {} } catch (InterruptedException e) {
break;
}
} }
} }
}; };
t.setName("Polling thread for "+details.localIp.getHostAddress());
return t; return t;
} }
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;
@ -159,9 +149,12 @@ public class ComputerManagerService extends Service {
synchronized (pollingThreads) { synchronized (pollingThreads) {
for (ComputerDetails computer : computerList) { for (ComputerDetails computer : computerList) {
Thread t = createPollingThread(computer); // This polling thread might already be there
pollingThreads.put(computer, t); if (!pollingThreads.containsKey(computer)) {
t.start(); Thread t = createPollingThread(computer);
pollingThreads.put(computer, t);
t.start();
}
} }
} }
} }
@ -210,9 +203,6 @@ 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();
@ -221,6 +211,7 @@ public class ComputerManagerService extends Service {
for (Thread t : pollingThreads.values()) { for (Thread t : pollingThreads.values()) {
t.interrupt(); t.interrupt();
} }
pollingThreads.clear();
} }
// Remove the listener // Remove the listener
@ -258,9 +249,12 @@ public class ComputerManagerService extends Service {
// Spawn a thread for this computer // Spawn a thread for this computer
synchronized (pollingThreads) { synchronized (pollingThreads) {
Thread t = createPollingThread(fakeDetails); // This polling thread might already be there
pollingThreads.put(fakeDetails, t); if (!pollingThreads.containsKey(fakeDetails)) {
t.start(); Thread t = createPollingThread(fakeDetails);
pollingThreads.put(fakeDetails, t);
t.start();
}
} }
} }