Avoid nulling activeAddress during polling

This commit is contained in:
Cameron Gutman 2018-10-27 23:38:11 -07:00
parent cac2bdbb81
commit d4072eb295

View File

@ -5,6 +5,7 @@ import java.io.OutputStream;
import java.io.StringReader; import java.io.StringReader;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.Socket; import java.net.Socket;
import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
@ -530,26 +531,37 @@ public class ComputerManagerService extends Service {
private boolean pollComputer(ComputerDetails details) throws InterruptedException { private boolean pollComputer(ComputerDetails details) throws InterruptedException {
ComputerDetails polledDetails; ComputerDetails polledDetails;
// Do a TCP-level connection to the HTTP server to see if it's listening // Do a TCP-level connection to the HTTP server to see if it's listening.
// Do not write this address to details.activeAddress because:
// a) it's only a candidate and may be wrong (multiple PCs behind a single router)
// b) if it's null, it will be unexpectedly nulling the activeAddress of a possibly online PC
LimeLog.info("Starting fast poll for "+details.name+" ("+details.localAddress +", "+details.remoteAddress +", "+details.manualAddress +")"); LimeLog.info("Starting fast poll for "+details.name+" ("+details.localAddress +", "+details.remoteAddress +", "+details.manualAddress +")");
details.activeAddress = fastPollPc(details.localAddress, details.remoteAddress, details.manualAddress); String candidateAddress = fastPollPc(details.localAddress, details.remoteAddress, details.manualAddress);
LimeLog.info("Fast poll for "+details.name+" returned active address: "+details.activeAddress); LimeLog.info("Fast poll for "+details.name+" returned active address: "+details.activeAddress);
// If no connection could be established to either IP address, there's nothing we can do // If no connection could be established to either IP address, there's nothing we can do
if (details.activeAddress == null) { if (candidateAddress == null) {
return false; return false;
} }
// Try using the active address from fast-poll // Try using the active address from fast-poll
polledDetails = tryPollIp(details, details.activeAddress); polledDetails = tryPollIp(details, candidateAddress);
if (polledDetails == null && details.localAddress != null && !details.localAddress.equals(details.activeAddress)) { if (polledDetails == null) {
polledDetails = tryPollIp(details, details.localAddress); // If that failed, try all unique addresses except what we've
} // already tried
if (polledDetails == null && details.manualAddress != null && !details.manualAddress.equals(details.activeAddress)) { HashSet<String> uniqueAddresses = new HashSet<>();
polledDetails = tryPollIp(details, details.manualAddress); uniqueAddresses.add(details.localAddress);
} uniqueAddresses.add(details.remoteAddress);
if (polledDetails == null && details.remoteAddress != null && !details.remoteAddress.equals(details.activeAddress)) { uniqueAddresses.add(details.manualAddress);
polledDetails = tryPollIp(details, details.remoteAddress); for (String addr : uniqueAddresses) {
if (addr == null || addr.equals(candidateAddress)) {
continue;
}
polledDetails = tryPollIp(details, addr);
if (polledDetails != null) {
break;
}
}
} }
if (polledDetails != null) { if (polledDetails != null) {