[network] Fix latency parsing on windows 10 (#8781)

Fixes #8776

Signed-off-by: Connor Petty <mistercpp2000+gitsignoff@gmail.com>
(cherry picked from commit ac1e4d8a83)
This commit is contained in:
Connor Petty 2020-10-18 11:43:09 -07:00 committed by Fabian Wolter
parent 8934554dfd
commit 895455b40f
3 changed files with 23 additions and 10 deletions

View File

@ -22,6 +22,7 @@ import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@ -65,7 +66,7 @@ public class NetworkDiscoveryService extends AbstractDiscoveryService implements
// TCP port 1025 (Xbox / MS-RPC)
private Set<Integer> tcpServicePorts = Collections
.unmodifiableSet(Stream.of(80, 548, 554, 1025).collect(Collectors.toSet()));
private Integer scannedIPcount = 0;
private AtomicInteger scannedIPcount = new AtomicInteger(0);
private @Nullable ExecutorService executorService = null;
private final NetworkBindingConfiguration configuration = new NetworkBindingConfiguration();
private final NetworkUtils networkUtils = new NetworkUtils();
@ -135,7 +136,7 @@ public class NetworkDiscoveryService extends AbstractDiscoveryService implements
logger.trace("Starting Network Device Discovery");
final Set<String> networkIPs = networkUtils.getNetworkIPs(MAXIMUM_IPS_PER_INTERFACE);
scannedIPcount = 0;
scannedIPcount.set(0);
for (String ip : networkIPs) {
final PresenceDetection s = new PresenceDetection(this, 2000);
@ -152,12 +153,10 @@ public class NetworkDiscoveryService extends AbstractDiscoveryService implements
service.execute(() -> {
Thread.currentThread().setName("Discovery thread " + ip);
s.performPresenceDetection(true);
synchronized (scannedIPcount) {
scannedIPcount += 1;
if (scannedIPcount == networkIPs.size()) {
logger.trace("Scan of {} IPs successful", scannedIPcount);
stopScan();
}
int count = scannedIPcount.incrementAndGet();
if (count == networkIPs.size()) {
logger.trace("Scan of {} IPs successful", scannedIPcount);
stopScan();
}
});
}

View File

@ -26,6 +26,7 @@ import org.slf4j.LoggerFactory;
*/
public class LatencyParser {
private static Pattern LATENCY_PATTERN = Pattern.compile(".*time=(.*) ?ms");
private final Logger logger = LoggerFactory.getLogger(LatencyParser.class);
// This is how the input looks like on Mac and Linux:
@ -47,8 +48,7 @@ public class LatencyParser {
public Optional<Double> parseLatency(String inputLine) {
logger.debug("Parsing latency from input {}", inputLine);
String pattern = ".*time=(.*) ms";
Matcher m = Pattern.compile(pattern).matcher(inputLine);
Matcher m = LATENCY_PATTERN.matcher(inputLine);
if (m.find() && m.groupCount() == 1) {
return Optional.of(Double.parseDouble(m.group(1)));
}

View File

@ -57,4 +57,18 @@ public class LatencyParserTest {
Assert.assertFalse(resultLatency.isPresent());
}
}
@Test
public void parseWindows10ResultFoundTest() {
// Arrange
LatencyParser latencyParser = new LatencyParser();
String input = "Reply from 192.168.178.207: bytes=32 time=2ms TTL=64";
// Act
Optional<Double> resultLatency = latencyParser.parseLatency(input);
// Assert
assertTrue(resultLatency.isPresent());
assertEquals(2, resultLatency.get(), 0);
}
}