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,10 +612,11 @@ public class ComputerManagerService extends Service {
startParallelPollThread(remoteInfo, uniqueAddresses); startParallelPollThread(remoteInfo, uniqueAddresses);
startParallelPollThread(ipv6Info, uniqueAddresses); startParallelPollThread(ipv6Info, uniqueAddresses);
try {
// Check local first // Check local first
synchronized (localInfo) { synchronized (localInfo) {
while (!localInfo.complete) { while (!localInfo.complete) {
localInfo.wait(500); localInfo.wait();
} }
if (localInfo.returnedDetails != null) { if (localInfo.returnedDetails != null) {
@ -620,7 +628,7 @@ public class ComputerManagerService extends Service {
// Now manual // Now manual
synchronized (manualInfo) { synchronized (manualInfo) {
while (!manualInfo.complete) { while (!manualInfo.complete) {
manualInfo.wait(500); manualInfo.wait();
} }
if (manualInfo.returnedDetails != null) { if (manualInfo.returnedDetails != null) {
@ -632,7 +640,7 @@ public class ComputerManagerService extends Service {
// Now remote IPv4 // Now remote IPv4
synchronized (remoteInfo) { synchronized (remoteInfo) {
while (!remoteInfo.complete) { while (!remoteInfo.complete) {
remoteInfo.wait(500); remoteInfo.wait();
} }
if (remoteInfo.returnedDetails != null) { if (remoteInfo.returnedDetails != null) {
@ -644,7 +652,7 @@ public class ComputerManagerService extends Service {
// Now global IPv6 // Now global IPv6
synchronized (ipv6Info) { synchronized (ipv6Info) {
while (!ipv6Info.complete) { while (!ipv6Info.complete) {
ipv6Info.wait(500); ipv6Info.wait();
} }
if (ipv6Info.returnedDetails != null) { if (ipv6Info.returnedDetails != null) {
@ -652,6 +660,14 @@ public class ComputerManagerService extends Service {
return ipv6Info.returnedDetails; 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;
} }