From db637e6aa9488f1c34ce23c18492d72edd38ee1c Mon Sep 17 00:00:00 2001 From: Darius Maitia Date: Fri, 29 Nov 2024 18:00:20 -0300 Subject: [PATCH] Examples - using countdown latch on ZPong --- examples/src/main/kotlin/io.zenoh/ZPong.kt | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/examples/src/main/kotlin/io.zenoh/ZPong.kt b/examples/src/main/kotlin/io.zenoh/ZPong.kt index fbd73f5fc..9525c362f 100644 --- a/examples/src/main/kotlin/io.zenoh/ZPong.kt +++ b/examples/src/main/kotlin/io.zenoh/ZPong.kt @@ -20,10 +20,13 @@ import io.zenoh.keyexpr.intoKeyExpr import io.zenoh.qos.CongestionControl import io.zenoh.qos.QoS import io.zenoh.sample.Sample +import java.util.concurrent.CountDownLatch class ZPong(private val emptyArgs: Boolean) : CliktCommand( help = "Zenoh ZPong example" ) { + val latch = CountDownLatch(1) + override fun run() { val config = loadConfig(emptyArgs, configFile, connect, listen, noMulticastScouting, mode) @@ -37,7 +40,7 @@ class ZPong(private val emptyArgs: Boolean) : CliktCommand( val publisher = session.declarePublisher(keyExprPong, qos = QoS(CongestionControl.BLOCK, express = !noExpress)).getOrThrow() session.declareSubscriber(keyExprPing, callback = { sample: Sample -> publisher.put(sample.payload).getOrThrow() }).getOrThrow() - while (true) { Thread.sleep(1000)} + latch.await() } @@ -63,4 +66,7 @@ class ZPong(private val emptyArgs: Boolean) : CliktCommand( ).flag(default = false) } -fun main(args: Array) = ZPong(args.isEmpty()).main(args) +fun main(args: Array) { + val zPong = ZPong(args.isEmpty()) + zPong.main(args) +}