Stop parallel polling threads when we find a working address

This commit is contained in:
Cameron Gutman 2022-05-22 14:55:10 -05:00
parent 0a2117241f
commit 4901b0c78f

View File

@ -557,12 +557,19 @@ public class ComputerManagerService extends Service {
public ComputerDetails existingDetails; public ComputerDetails existingDetails;
public boolean complete; public boolean complete;
public Thread pollingThread;
public ComputerDetails returnedDetails; public ComputerDetails returnedDetails;
public ParallelPollTuple(String address, ComputerDetails existingDetails) { public ParallelPollTuple(String address, ComputerDetails existingDetails) {
this.address = address; this.address = address;
this.existingDetails = existingDetails; this.existingDetails = existingDetails;
} }
public void interrupt() {
if (pollingThread != null) {
pollingThread.interrupt();
}
}
} }
private void startParallelPollThread(ParallelPollTuple tuple, HashSet<String> uniqueAddresses) { private void startParallelPollThread(ParallelPollTuple tuple, HashSet<String> uniqueAddresses) {
@ -574,7 +581,7 @@ public class ComputerManagerService extends Service {
return; return;
} }
Thread t = new Thread() { tuple.pollingThread = new Thread() {
@Override @Override
public void run() { public void run() {
ComputerDetails details = tryPollIp(tuple.existingDetails, tuple.address); ComputerDetails details = tryPollIp(tuple.existingDetails, tuple.address);
@ -587,8 +594,8 @@ public class ComputerManagerService extends Service {
} }
} }
}; };
t.setName("Parallel Poll - "+tuple.address+" - "+tuple.existingDetails.name); tuple.pollingThread.setName("Parallel Poll - "+tuple.address+" - "+tuple.existingDetails.name);
t.start(); tuple.pollingThread.start();
} }
private ComputerDetails parallelPollPc(ComputerDetails details) throws InterruptedException { private ComputerDetails parallelPollPc(ComputerDetails details) throws InterruptedException {
@ -605,52 +612,61 @@ public class ComputerManagerService extends Service {
startParallelPollThread(remoteInfo, uniqueAddresses); startParallelPollThread(remoteInfo, uniqueAddresses);
startParallelPollThread(ipv6Info, uniqueAddresses); startParallelPollThread(ipv6Info, uniqueAddresses);
// Check local first try {
synchronized (localInfo) { // Check local first
while (!localInfo.complete) { synchronized (localInfo) {
localInfo.wait(500); while (!localInfo.complete) {
localInfo.wait();
}
if (localInfo.returnedDetails != null) {
localInfo.returnedDetails.activeAddress = localInfo.address;
return localInfo.returnedDetails;
}
} }
if (localInfo.returnedDetails != null) { // Now manual
localInfo.returnedDetails.activeAddress = localInfo.address; synchronized (manualInfo) {
return localInfo.returnedDetails; while (!manualInfo.complete) {
} manualInfo.wait();
} }
// Now manual if (manualInfo.returnedDetails != null) {
synchronized (manualInfo) { manualInfo.returnedDetails.activeAddress = manualInfo.address;
while (!manualInfo.complete) { return manualInfo.returnedDetails;
manualInfo.wait(500); }
} }
if (manualInfo.returnedDetails != null) { // Now remote IPv4
manualInfo.returnedDetails.activeAddress = manualInfo.address; synchronized (remoteInfo) {
return manualInfo.returnedDetails; while (!remoteInfo.complete) {
} remoteInfo.wait();
} }
// Now remote IPv4 if (remoteInfo.returnedDetails != null) {
synchronized (remoteInfo) { remoteInfo.returnedDetails.activeAddress = remoteInfo.address;
while (!remoteInfo.complete) { return remoteInfo.returnedDetails;
remoteInfo.wait(500); }
} }
if (remoteInfo.returnedDetails != null) { // Now global IPv6
remoteInfo.returnedDetails.activeAddress = remoteInfo.address; synchronized (ipv6Info) {
return remoteInfo.returnedDetails; while (!ipv6Info.complete) {
} ipv6Info.wait();
} }
// Now global IPv6 if (ipv6Info.returnedDetails != null) {
synchronized (ipv6Info) { ipv6Info.returnedDetails.activeAddress = ipv6Info.address;
while (!ipv6Info.complete) { return ipv6Info.returnedDetails;
ipv6Info.wait(500); }
}
if (ipv6Info.returnedDetails != null) {
ipv6Info.returnedDetails.activeAddress = ipv6Info.address;
return ipv6Info.returnedDetails;
} }
} finally {
// Stop any further polling if we've found a working address or we've been
// interrupted by an attempt to stop polling.
localInfo.interrupt();
manualInfo.interrupt();
remoteInfo.interrupt();
ipv6Info.interrupt();
} }
return null; return null;