Skip to content

Commit

Permalink
Use for loop for kotlin Channels in examples (#76)
Browse files Browse the repository at this point in the history
Co-authored-by: Giovanni Barbaro <[email protected]>
  • Loading branch information
vanny96 and vanny96 authored May 15, 2024
1 parent ea3987e commit f890fb0
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 26 deletions.
14 changes: 5 additions & 9 deletions examples/src/main/kotlin/io.zenoh/ZGet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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}')")
}
}
}
Expand Down
15 changes: 6 additions & 9 deletions examples/src/main/kotlin/io.zenoh/ZQueryable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -81,14 +81,11 @@ class ZQueryable(private val emptyArgs: Boolean) : CliktCommand(
private suspend fun handleRequests(
receiverChannel: Channel<Query>, 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") }
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions examples/src/main/kotlin/io.zenoh/ZSub.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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: " + "${
Expand Down
8 changes: 4 additions & 4 deletions examples/src/main/kotlin/io.zenoh/ZSubThr.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit f890fb0

Please sign in to comment.