From f890fb0c2935006071544c488f21514e9fa72636 Mon Sep 17 00:00:00 2001 From: vanny96 Date: Wed, 15 May 2024 15:07:13 +0200 Subject: [PATCH] Use for loop for kotlin Channels in examples (#76) Co-authored-by: Giovanni Barbaro --- examples/src/main/kotlin/io.zenoh/ZGet.kt | 14 +++++--------- examples/src/main/kotlin/io.zenoh/ZQueryable.kt | 15 ++++++--------- examples/src/main/kotlin/io.zenoh/ZSub.kt | 5 +---- examples/src/main/kotlin/io.zenoh/ZSubThr.kt | 8 ++++---- 4 files changed, 16 insertions(+), 26 deletions(-) diff --git a/examples/src/main/kotlin/io.zenoh/ZGet.kt b/examples/src/main/kotlin/io.zenoh/ZGet.kt index 36724b98..b5c14803 100644 --- a/examples/src/main/kotlin/io.zenoh/ZGet.kt +++ b/examples/src/main/kotlin/io.zenoh/ZGet.kt @@ -89,16 +89,12 @@ class ZGet(private val emptyArgs: Boolean) : CliktCommand( } } .res() - .onSuccess { + .onSuccess { receiver -> runBlocking { - val iterator = it!!.iterator() - while (iterator.hasNext()) { - val reply = iterator.next() - if (reply is Reply.Success) { - println("Received ('${reply.sample.keyExpr}': '${reply.sample.value}')") - } else { - reply as Reply.Error - println("Received (ERROR: '${reply.error}')") + for (reply in receiver!!) { + when (reply) { + is Reply.Success -> {println("Received ('${reply.sample.keyExpr}': '${reply.sample.value}')")} + is Reply.Error -> println("Received (ERROR: '${reply.error}')") } } } diff --git a/examples/src/main/kotlin/io.zenoh/ZQueryable.kt b/examples/src/main/kotlin/io.zenoh/ZQueryable.kt index a11967a7..fda23397 100644 --- a/examples/src/main/kotlin/io.zenoh/ZQueryable.kt +++ b/examples/src/main/kotlin/io.zenoh/ZQueryable.kt @@ -55,7 +55,7 @@ class ZQueryable(private val emptyArgs: Boolean) : CliktCommand( ).flag(default = false) override fun run() { - val config = loadConfig(emptyArgs, configFile, connect, listen, noMulticastScouting,mode) + val config = loadConfig(emptyArgs, configFile, connect, listen, noMulticastScouting, mode) Session.open(config).onSuccess { session -> session.use { @@ -81,14 +81,11 @@ class ZQueryable(private val emptyArgs: Boolean) : CliktCommand( private suspend fun handleRequests( receiverChannel: Channel, keyExpr: KeyExpr ) { - val iterator = receiverChannel.iterator() - while (iterator.hasNext()) { - iterator.next().use { query -> - val valueInfo = query.value?.let { value -> " with value '$value'" } ?: "" - println(">> [Queryable] Received Query '${query.selector}' $valueInfo") - query.reply(keyExpr).success(value).withKind(SampleKind.PUT).withTimeStamp(TimeStamp.getCurrentTime()) - .res().onFailure { println(">> [Queryable ] Error sending reply: $it") } - } + for (query in receiverChannel) { + val valueInfo = query.value?.let { value -> " with value '$value'" } ?: "" + println(">> [Queryable] Received Query '${query.selector}' $valueInfo") + query.reply(keyExpr).success(value).withKind(SampleKind.PUT).withTimeStamp(TimeStamp.getCurrentTime()) + .res().onFailure { println(">> [Queryable ] Error sending reply: $it") } } } } diff --git a/examples/src/main/kotlin/io.zenoh/ZSub.kt b/examples/src/main/kotlin/io.zenoh/ZSub.kt index 46f724b8..b7b60609 100644 --- a/examples/src/main/kotlin/io.zenoh/ZSub.kt +++ b/examples/src/main/kotlin/io.zenoh/ZSub.kt @@ -56,10 +56,7 @@ class ZSub(private val emptyArgs: Boolean) : CliktCommand( subscriber.use { println("Press CTRL-C to quit...") runBlocking { - val receiver = subscriber.receiver!! - val iterator = receiver.iterator() - while (iterator.hasNext()) { - val sample = iterator.next() + for (sample in subscriber.receiver!!) { println(">> [Subscriber] Received ${sample.kind} ('${sample.keyExpr}': '${sample.value}'" + "${ sample.attachment?.let { ", with attachment: " + "${ diff --git a/examples/src/main/kotlin/io.zenoh/ZSubThr.kt b/examples/src/main/kotlin/io.zenoh/ZSubThr.kt index e1fea119..f0f22bf8 100644 --- a/examples/src/main/kotlin/io.zenoh/ZSubThr.kt +++ b/examples/src/main/kotlin/io.zenoh/ZSubThr.kt @@ -99,11 +99,11 @@ class ZSubThr(private val emptyArgs: Boolean) : CliktCommand( override fun run() { val config = loadConfig(emptyArgs, configFile, connect, listen, noMulticastScouting, mode) - "test/thr".intoKeyExpr().onSuccess { - it.use { keyExpr -> + "test/thr".intoKeyExpr().onSuccess { keyExpr -> + keyExpr.use { println("Opening Session") - Session.open(config).onSuccess { - it.use { session -> + Session.open(config).onSuccess { session -> + session.use { println("Press CTRL-C to quit...") subscriber = session.declareSubscriber(keyExpr).reliable().with { listener(number) }.res().getOrThrow()