-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Session info: creating SessionInfo, implementing peersZid() and routersZid() * Session info: adding id() function, returning Results. * Session info: adding kdocs + cargo clippy * Session info: renaming functions
- Loading branch information
Showing
7 changed files
with
333 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ tasks { | |
"ZBytes", | ||
"ZDelete", | ||
"ZGet", | ||
"ZInfo", | ||
"ZPub", | ||
"ZPubThr", | ||
"ZPut", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// | ||
// Copyright (c) 2023 ZettaScale Technology | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Eclipse Public License 2.0 which is available at | ||
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 | ||
// which is available at https://www.apache.org/licenses/LICENSE-2.0. | ||
// | ||
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 | ||
// | ||
// Contributors: | ||
// ZettaScale Zenoh Team, <[email protected]> | ||
// | ||
|
||
package io.zenoh | ||
|
||
import com.github.ajalt.clikt.core.CliktCommand | ||
import com.github.ajalt.clikt.parameters.options.* | ||
|
||
class ZInfo(private val emptyArgs: Boolean) : CliktCommand( | ||
help = "Zenoh Info example" | ||
) { | ||
override fun run() { | ||
val config = loadConfig(emptyArgs, configFile, connect, listen, noMulticastScouting, mode) | ||
|
||
Zenoh.initLogFromEnvOr("error") | ||
|
||
println("Opening session...") | ||
Zenoh.open(config).onSuccess { session -> | ||
session.use { | ||
val info = session.info() | ||
println("zid: ${info.zid().getOrThrow()}") | ||
|
||
println("routers zid: ${info.routersZid().getOrThrow()}") | ||
|
||
println("peers zid: ${info.peersZid().getOrThrow()}") | ||
} | ||
}.onFailure { exception -> println(exception.message) } | ||
} | ||
|
||
|
||
private val configFile by option("-c", "--config", help = "A configuration file.", metavar = "config") | ||
private val connect: List<String> by option( | ||
"-e", "--connect", help = "Endpoints to connect to.", metavar = "connect" | ||
).multiple() | ||
private val listen: List<String> by option( | ||
"-l", "--listen", help = "Endpoints to listen on.", metavar = "listen" | ||
).multiple() | ||
private val mode by option( | ||
"-m", | ||
"--mode", | ||
help = "The session mode. Default: peer. Possible values: [peer, client, router]", | ||
metavar = "mode" | ||
).default("peer") | ||
private val noMulticastScouting: Boolean by option( | ||
"--no-multicast-scouting", help = "Disable the multicast-based scouting mechanism." | ||
).flag(default = false) | ||
} | ||
|
||
fun main(args: Array<String>) = ZInfo(args.isEmpty()).main(args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
zenoh-kotlin/src/commonMain/kotlin/io/zenoh/SessionInfo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// | ||
// Copyright (c) 2023 ZettaScale Technology | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Eclipse Public License 2.0 which is available at | ||
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 | ||
// which is available at https://www.apache.org/licenses/LICENSE-2.0. | ||
// | ||
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 | ||
// | ||
// Contributors: | ||
// ZettaScale Zenoh Team, <[email protected]> | ||
// | ||
|
||
package io.zenoh | ||
|
||
import io.zenoh.protocol.ZenohID | ||
|
||
/** | ||
* Class allowing to obtain the information of a [Session]. | ||
*/ | ||
class SessionInfo(private val session: Session) { | ||
|
||
/** | ||
* Return the [ZenohID] of the current Zenoh [Session] | ||
*/ | ||
fun zid(): Result<ZenohID> { | ||
return session.zid() | ||
} | ||
|
||
/** | ||
* Return the [ZenohID] of the zenoh peers the session is currently connected to. | ||
*/ | ||
fun peersZid(): Result<List<ZenohID>> { | ||
return session.getPeersId() | ||
} | ||
|
||
/** | ||
* Return the [ZenohID] of the zenoh routers the session is currently connected to. | ||
*/ | ||
fun routersZid(): Result<List<ZenohID>> { | ||
return session.getRoutersId() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
zenoh-kotlin/src/commonTest/kotlin/io/zenoh/SessionInfoTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// | ||
// Copyright (c) 2023 ZettaScale Technology | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Eclipse Public License 2.0 which is available at | ||
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 | ||
// which is available at https://www.apache.org/licenses/LICENSE-2.0. | ||
// | ||
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 | ||
// | ||
// Contributors: | ||
// ZettaScale Zenoh Team, <[email protected]> | ||
// | ||
|
||
package io.zenoh | ||
|
||
import org.junit.jupiter.api.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertTrue | ||
|
||
class SessionInfoTest { | ||
|
||
@Test | ||
fun `peersZid test`() { | ||
val jsonConfig = """ | ||
{ | ||
mode: "peer", | ||
connect: { | ||
endpoints: ["tcp/localhost:7450"], | ||
}, | ||
} | ||
""".trimIndent() | ||
|
||
val listenConfig = Config.fromJson(""" | ||
{ | ||
mode: "peer", | ||
listen: { | ||
endpoints: ["tcp/localhost:7450"], | ||
}, | ||
} | ||
""".trimIndent()).getOrThrow() | ||
|
||
val sessionC = Zenoh.open(listenConfig).getOrThrow() | ||
val sessionA = Zenoh.open(Config.fromJson(jsonConfig).getOrThrow()).getOrThrow() | ||
val sessionB = Zenoh.open(Config.fromJson(jsonConfig).getOrThrow()).getOrThrow() | ||
|
||
val idA = sessionA.info().zid().getOrThrow() | ||
val idB = sessionB.info().zid().getOrThrow() | ||
val peers = sessionC.info().peersZid().getOrThrow() | ||
assertTrue(peers.contains(idA)) | ||
assertTrue(peers.contains(idB)) | ||
|
||
sessionA.close() | ||
sessionB.close() | ||
sessionC.close() | ||
} | ||
|
||
|
||
@Test | ||
fun `routersZid test`() { | ||
val jsonConfig = """ | ||
{ | ||
mode: "router", | ||
connect: { | ||
endpoints: ["tcp/localhost:7450"], | ||
}, | ||
listen: { | ||
endpoints: ["tcp/localhost:7452"], | ||
}, | ||
} | ||
""".trimIndent() | ||
|
||
val listenConfig = Config.fromJson(""" | ||
{ | ||
mode: "router", | ||
listen: { | ||
endpoints: ["tcp/localhost:7450"], | ||
}, | ||
} | ||
""".trimIndent()).getOrThrow() | ||
|
||
val sessionC = Zenoh.open(listenConfig).getOrThrow() | ||
val sessionA = Zenoh.open(Config.fromJson(jsonConfig).getOrThrow()).getOrThrow() | ||
val sessionB = Zenoh.open(Config.fromJson(jsonConfig).getOrThrow()).getOrThrow() | ||
|
||
val idA = sessionA.info().zid().getOrThrow() | ||
val idB = sessionB.info().zid().getOrThrow() | ||
val routers = sessionC.info().routersZid().getOrThrow() | ||
assertTrue(routers.contains(idA)) | ||
assertTrue(routers.contains(idB)) | ||
|
||
sessionA.close() | ||
sessionB.close() | ||
sessionC.close() | ||
} | ||
|
||
@Test | ||
fun `zid test`() { | ||
val jsonConfig = """ | ||
{ | ||
id: "123456", | ||
} | ||
""".trimIndent() | ||
|
||
val session = Zenoh.open(Config.fromJson(jsonConfig).getOrThrow()).getOrThrow() | ||
assertEquals("123456", session.info().zid().getOrThrow().toString()) | ||
session.close() | ||
} | ||
} |