diff --git a/bundles/org.openhab.binding.network/src/main/java/org/openhab/binding/network/internal/discovery/NetworkDiscoveryService.java b/bundles/org.openhab.binding.network/src/main/java/org/openhab/binding/network/internal/discovery/NetworkDiscoveryService.java index 78728a0ffd9..5f29b772d7f 100644 --- a/bundles/org.openhab.binding.network/src/main/java/org/openhab/binding/network/internal/discovery/NetworkDiscoveryService.java +++ b/bundles/org.openhab.binding.network/src/main/java/org/openhab/binding/network/internal/discovery/NetworkDiscoveryService.java @@ -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 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 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(); } }); } diff --git a/bundles/org.openhab.binding.network/src/main/java/org/openhab/binding/network/internal/utils/LatencyParser.java b/bundles/org.openhab.binding.network/src/main/java/org/openhab/binding/network/internal/utils/LatencyParser.java index 6217739efbd..0cf6db14307 100644 --- a/bundles/org.openhab.binding.network/src/main/java/org/openhab/binding/network/internal/utils/LatencyParser.java +++ b/bundles/org.openhab.binding.network/src/main/java/org/openhab/binding/network/internal/utils/LatencyParser.java @@ -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 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))); } diff --git a/bundles/org.openhab.binding.network/src/test/java/org/openhab/binding/network/internal/utils/LatencyParserTest.java b/bundles/org.openhab.binding.network/src/test/java/org/openhab/binding/network/internal/utils/LatencyParserTest.java index 141f6644f94..faf8b4dc3d1 100644 --- a/bundles/org.openhab.binding.network/src/test/java/org/openhab/binding/network/internal/utils/LatencyParserTest.java +++ b/bundles/org.openhab.binding.network/src/test/java/org/openhab/binding/network/internal/utils/LatencyParserTest.java @@ -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 resultLatency = latencyParser.parseLatency(input); + + // Assert + assertTrue(resultLatency.isPresent()); + assertEquals(2, resultLatency.get(), 0); + } }