From 6ba9cf6e058c959614bd7f1f4148e8fa39ef1681 Mon Sep 17 00:00:00 2001 From: oteffahi <70609372+oteffahi@users.noreply.github.com> Date: Thu, 18 Apr 2024 16:42:07 +0200 Subject: [PATCH] Align examples and remove reading from stdin (#47) * Remove reading from stdin * Add queryable keyexpr to stdout * Add index to ZPub payload --- examples/src/main/kotlin/io.zenoh/ZPub.kt | 12 +++++++----- examples/src/main/kotlin/io.zenoh/ZPubThr.kt | 3 +-- examples/src/main/kotlin/io.zenoh/ZQueryable.kt | 3 ++- examples/src/main/kotlin/io.zenoh/ZSub.kt | 1 + examples/src/main/kotlin/io.zenoh/ZSubThr.kt | 2 ++ 5 files changed, 13 insertions(+), 8 deletions(-) diff --git a/examples/src/main/kotlin/io.zenoh/ZPub.kt b/examples/src/main/kotlin/io.zenoh/ZPub.kt index 69e8144f..ac0dc009 100644 --- a/examples/src/main/kotlin/io.zenoh/ZPub.kt +++ b/examples/src/main/kotlin/io.zenoh/ZPub.kt @@ -62,18 +62,20 @@ class ZPub(private val emptyArgs: Boolean) : CliktCommand( println("Declaring publisher on '$keyExpr'...") session.declarePublisher(keyExpr).res().onSuccess { pub -> pub.use { + println("Press CTRL-C to quit...") val attachment = attachment?.let { decodeAttachment(it) } var idx = 0 while (true) { Thread.sleep(1000) - println( - "Putting Data ('$keyExpr': '[${ + val payload = "[${ idx.toString().padStart(4, ' ') - }] $value')..." + }] $value" + println( + "Putting Data ('$keyExpr': '$payload')..." ) attachment?.let { - pub.put(value).withAttachment(attachment).res() - } ?: let { pub.put(value).res() } + pub.put(payload).withAttachment(attachment).res() + } ?: let { pub.put(payload).res() } idx++ } } diff --git a/examples/src/main/kotlin/io.zenoh/ZPubThr.kt b/examples/src/main/kotlin/io.zenoh/ZPubThr.kt index 6e28947d..f031b7ad 100644 --- a/examples/src/main/kotlin/io.zenoh/ZPubThr.kt +++ b/examples/src/main/kotlin/io.zenoh/ZPubThr.kt @@ -87,9 +87,9 @@ class ZPubThr(private val emptyArgs: Boolean) : CliktCommand( var count: Long = 0 var start = System.currentTimeMillis() val number = number.toLong() + println("Press CTRL-C to quit...") while (true) { pub.put(value).res().getOrThrow() - if (statsPrint) { if (count < number) { count++ @@ -100,7 +100,6 @@ class ZPubThr(private val emptyArgs: Boolean) : CliktCommand( start = System.currentTimeMillis() } } - } } } diff --git a/examples/src/main/kotlin/io.zenoh/ZQueryable.kt b/examples/src/main/kotlin/io.zenoh/ZQueryable.kt index dc6a4964..a11967a7 100644 --- a/examples/src/main/kotlin/io.zenoh/ZQueryable.kt +++ b/examples/src/main/kotlin/io.zenoh/ZQueryable.kt @@ -61,9 +61,10 @@ class ZQueryable(private val emptyArgs: Boolean) : CliktCommand( session.use { key.intoKeyExpr().onSuccess { keyExpr -> keyExpr.use { - println("Declaring Queryable") + println("Declaring Queryable on " + key + "...") session.declareQueryable(keyExpr).res().onSuccess { queryable -> queryable.use { + println("Press CTRL-C to quit...") queryable.receiver?.let { receiverChannel -> // The default receiver is a Channel we can process on a coroutine. runBlocking { handleRequests(receiverChannel, keyExpr) diff --git a/examples/src/main/kotlin/io.zenoh/ZSub.kt b/examples/src/main/kotlin/io.zenoh/ZSub.kt index e29eb764..46f724b8 100644 --- a/examples/src/main/kotlin/io.zenoh/ZSub.kt +++ b/examples/src/main/kotlin/io.zenoh/ZSub.kt @@ -54,6 +54,7 @@ class ZSub(private val emptyArgs: Boolean) : CliktCommand( println("Declaring Subscriber on '$keyExpr'...") session.declareSubscriber(keyExpr).bestEffort().res().onSuccess { subscriber -> subscriber.use { + println("Press CTRL-C to quit...") runBlocking { val receiver = subscriber.receiver!! val iterator = receiver.iterator() diff --git a/examples/src/main/kotlin/io.zenoh/ZSubThr.kt b/examples/src/main/kotlin/io.zenoh/ZSubThr.kt index 4e42be48..e1fea119 100644 --- a/examples/src/main/kotlin/io.zenoh/ZSubThr.kt +++ b/examples/src/main/kotlin/io.zenoh/ZSubThr.kt @@ -104,9 +104,11 @@ class ZSubThr(private val emptyArgs: Boolean) : CliktCommand( println("Opening Session") Session.open(config).onSuccess { it.use { session -> + println("Press CTRL-C to quit...") subscriber = session.declareSubscriber(keyExpr).reliable().with { listener(number) }.res().getOrThrow() while (subscriber.isValid()) {/* Keep alive the subscriber until the test is done. */ + Thread.sleep(1000) } } }