Fix a bunch of bugs in the new (and old) computer manager service

This commit is contained in:
Cameron Gutman 2014-11-16 18:09:31 -08:00
parent a267cf59c7
commit 94ba7f8e45

View File

@ -32,6 +32,7 @@ public class ComputerManagerService extends Service {
private final LinkedList<PollingTuple> pollingTuples = new LinkedList<PollingTuple>(); private final LinkedList<PollingTuple> pollingTuples = new LinkedList<PollingTuple>();
private ComputerManagerListener listener = null; private ComputerManagerListener listener = null;
private AtomicInteger activePolls = new AtomicInteger(0); private AtomicInteger activePolls = new AtomicInteger(0);
private boolean pollingActive = false;
private DiscoveryService.DiscoveryBinder discoveryBinder; private DiscoveryService.DiscoveryBinder discoveryBinder;
private final ServiceConnection discoveryServiceConnection = new ServiceConnection() { private final ServiceConnection discoveryServiceConnection = new ServiceConnection() {
@ -100,7 +101,7 @@ public class ComputerManagerService extends Service {
Thread t = new Thread() { Thread t = new Thread() {
@Override @Override
public void run() { public void run() {
while (!isInterrupted()) { while (!isInterrupted() && pollingActive) {
// Check if this poll has modified the details // Check if this poll has modified the details
runPoll(details); runPoll(details);
@ -119,6 +120,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) {
// Polling is active
pollingActive = true;
// Set the listener // Set the listener
ComputerManagerService.this.listener = listener; ComputerManagerService.this.listener = listener;
@ -187,6 +191,7 @@ public class ComputerManagerService extends Service {
discoveryBinder.stopDiscovery(); discoveryBinder.stopDiscovery();
// Stop polling // Stop polling
pollingActive = false;
synchronized (pollingTuples) { synchronized (pollingTuples) {
for (PollingTuple tuple : pollingTuples) { for (PollingTuple tuple : pollingTuples) {
if (tuple.thread != null) { if (tuple.thread != null) {
@ -230,15 +235,21 @@ public class ComputerManagerService extends Service {
fakeDetails.localIp = addr; fakeDetails.localIp = addr;
fakeDetails.remoteIp = addr; fakeDetails.remoteIp = addr;
// Spawn a thread for this computer addTuple(fakeDetails);
}
private void addTuple(ComputerDetails details) {
synchronized (pollingTuples) { synchronized (pollingTuples) {
// This polling thread might already be there
for (PollingTuple tuple : pollingTuples) { for (PollingTuple tuple : pollingTuples) {
if (tuple.computer.localIp.equals(addr) || // Check if this is the same computer
tuple.computer.remoteIp.equals(addr)) { if (tuple.computer == details ||
// This is the same computer tuple.computer.localIp.equals(details.localIp) ||
if (tuple.thread == null) { tuple.computer.remoteIp.equals(details.remoteIp) ||
tuple.thread = createPollingThread(fakeDetails); tuple.computer.name.equals(details.name)) {
// Start a polling thread if polling is active
if (pollingActive && tuple.thread == null) {
tuple.thread = createPollingThread(details);
tuple.thread.start(); tuple.thread.start();
} }
@ -248,11 +259,13 @@ public class ComputerManagerService extends Service {
} }
// If we got here, we didn't find an entry // If we got here, we didn't find an entry
PollingTuple tuple = new PollingTuple(fakeDetails, createPollingThread(fakeDetails)); PollingTuple tuple = new PollingTuple(details, pollingActive ? createPollingThread(details) : null);
pollingTuples.add(tuple); pollingTuples.add(tuple);
if (tuple.thread != null) {
tuple.thread.start(); tuple.thread.start();
} }
} }
}
public boolean addComputerBlocking(InetAddress addr) { public boolean addComputerBlocking(InetAddress addr) {
// Setup a placeholder // Setup a placeholder
@ -264,7 +277,14 @@ public class ComputerManagerService extends Service {
runPoll(fakeDetails); runPoll(fakeDetails);
// If the machine is reachable, it was successful // If the machine is reachable, it was successful
return fakeDetails.state == ComputerDetails.State.ONLINE; if (fakeDetails.state == ComputerDetails.State.ONLINE) {
// Start a polling thread for this machine
addTuple(fakeDetails);
return true;
}
else {
return false;
}
} }
public void removeComputer(String name) { public void removeComputer(String name) {