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;
// 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();
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);
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();
}
});
}
*/
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:
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)));
}
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);
+ }
}