Skip to content

Commit

Permalink
feature(scouting): enabling configuration parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
DariusIMP committed Aug 29, 2024
1 parent 0e8949a commit 33d4883
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 15 deletions.
2 changes: 1 addition & 1 deletion zenoh-jni/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ macro_rules! session_error {
$crate::errors::Error::Session($arg.to_string())
};
($fmt:expr, $($arg:tt)*) => {
Error::Session(format!($fmt, $($arg)*))
$crate::errors::Error::Session(format!($fmt, $($arg)*))
};

}
Expand Down
56 changes: 53 additions & 3 deletions zenoh-jni/src/scouting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,76 @@ use jni::{
use zenoh::{config::WhatAmIMatcher, prelude::Wait};
use zenoh::{scouting::Scout, Config};

use crate::{errors::Result, throw_exception};
use crate::{errors::Result, throw_exception, utils::decode_string};
use crate::{
session_error,
utils::{get_callback_global_ref, get_java_vm},
};

/// Start a scout.
///
/// # Params
/// - `whatAmI`: Ordinal value of the WhatAmI enum.
/// - `callback`: Callback to be executed whenever a hello message is received.
/// - `config_string`: Optional embedded configuration as a string.
/// - `format`: format of the `config_string` param.
/// - `config_path`: Optional path to a config file.
///
/// Note: Either the config_string or the config_path or None can be provided.
/// If none is provided, then the default configuration is loaded. Otherwise
/// it's the config_string or the config_path that are loaded. This consistency
/// logic is granted by the kotlin layer.
///
/// Returns a pointer to the scout, which must be freed afterwards.
/// If starting the scout fails, an exception is thrown on the JVM, and a null pointer is returned.
///
#[no_mangle]
#[allow(non_snake_case)]
pub unsafe extern "C" fn Java_io_zenoh_jni_JNIScout_00024Companion_scoutViaJNI(
mut env: JNIEnv,
_class: JClass,
whatAmI: jint,
callback: JObject,
_config: JString,
config_string: /*nullable=*/ JString,
format: jint,
config_path: /*nullable=*/ JString,
) -> *const Scout<()> {
|| -> Result<*const Scout<()>> {
let callback_global_ref = get_callback_global_ref(&mut env, callback)?;
let java_vm = Arc::new(get_java_vm(&mut env)?);
let whatAmIMatcher: WhatAmIMatcher = (whatAmI as u8).try_into().unwrap(); // The validity of the operation is guaranteed on the kotlin layer.
zenoh::scout(whatAmIMatcher, Config::default())

let config = if config_string.is_null() && config_path.is_null() {
Config::default()
} else if !config_string.is_null() {
let string_config = decode_string(&mut env, &config_string)?;
match format {
0 /*YAML*/ => {
let deserializer = serde_yaml::Deserializer::from_str(&string_config);
Config::from_deserializer(deserializer).map_err(|err| match err {
Ok(c) => session_error!("Invalid configuration: {}", c),
Err(e) => session_error!("YAML error: {}", e),
})?
}
1 | 2 /*JSON | JSON5*/ => {
let mut deserializer =
json5::Deserializer::from_str(&string_config).map_err(|err| session_error!(err))?;
Config::from_deserializer(&mut deserializer).map_err(|err| match err {
Ok(c) => session_error!("Invalid configuration: {}", c),
Err(e) => session_error!("JSON error: {}", e),
})?
}
_ => {
// This can never happen unless the Config.Format enum on Kotlin is wrongly modified!
Err(session_error!("Unexpected error: attempting to decode a config with a format other than Json,
Json5 or Yaml. Check Config.Format for eventual modifications..."))?
}
}
} else {
let config_file_path = decode_string(&mut env, &config_path)?;
Config::from_file(config_file_path).map_err(|err| session_error!(err))?
};
zenoh::scout(whatAmIMatcher, config)
.callback(move |hello| {
tracing::debug!("Received hello: {hello}");
let _ = || -> jni::errors::Result<()> {
Expand Down
4 changes: 2 additions & 2 deletions zenoh-jni/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// ZettaScale Zenoh Team, <[email protected]>
//

use crate::errors::{Error, Result};
use crate::errors::{Result};
use crate::key_expr::process_kotlin_key_expr;
use crate::{jni_error, utils::*};
use crate::{session_error, throw_exception};
Expand Down Expand Up @@ -165,7 +165,7 @@ fn open_session_with_yaml_config(env: &mut JNIEnv, yaml_config: JString) -> Resu
let deserializer = serde_yaml::Deserializer::from_str(&yaml_config);
let config = Config::from_deserializer(deserializer).map_err(|err| match err {
Ok(c) => session_error!("Invalid configuration: {}", c),
Err(e) => session_error!("JSON error: {}", e),
Err(e) => session_error!("YAML error: {}", e),
})?;
zenoh::open(config)
.wait()
Expand Down
2 changes: 1 addition & 1 deletion zenoh-jni/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use std::sync::Arc;

use crate::{
errors::{Error, Result},
errors::{Result},
jni_error, session_error, throw_exception,
};
use jni::{
Expand Down
2 changes: 1 addition & 1 deletion zenoh-kotlin/src/commonMain/kotlin/io/zenoh/Session.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class Session private constructor(private val config: Config) : AutoCloseable {
* @param config The configuration for the session.
* @return A [Result] with the [Session] on success.
*/
fun open(config: Config): Result<Session> {
fun open(config: Config? = null): Result<Session> {
val session = Session(config)
return session.launch()
}
Expand Down
6 changes: 3 additions & 3 deletions zenoh-kotlin/src/commonMain/kotlin/io/zenoh/Zenoh.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ object Zenoh {
fun scout(
callback: Callback<Hello>,
whatAmI: Set<WhatAmI> = setOf(WhatAmI.Peer),
config: Config = Config.default()
config: Config? = null
): Scout<Unit> {
ZenohLoad
return JNIScout.scout(whatAmI = whatAmI, callback = callback, receiver = Unit, config = config)
Expand All @@ -43,7 +43,7 @@ object Zenoh {
fun <R> scout(
handler: Handler<Hello, R>,
whatAmI: Set<WhatAmI> = setOf(WhatAmI.Peer),
config: Config = Config.default()
config: Config? = null
): Scout<R> {
ZenohLoad
return JNIScout.scout(
Expand All @@ -57,7 +57,7 @@ object Zenoh {
fun scout(
channel: Channel<Hello>,
whatAmI: Set<WhatAmI> = setOf(WhatAmI.Peer),
config: Config = Config.default()
config: Config? = null
): Scout<Channel<Hello>> {
ZenohLoad
val handler = ChannelHandler(channel)
Expand Down
17 changes: 13 additions & 4 deletions zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNIScout.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,28 @@ class JNIScout(private val ptr: Long) {
fun <R> scout(
whatAmI: Set<WhatAmI>,
callback: Callback<Hello>,
config: Config,
config: Config?,
receiver: R
): Scout<R> {
val scoutCallback = JNIScoutCallback { whatAmI2: Int, id: String, locators: List<String> ->
callback.run(Hello(WhatAmI.fromInt(whatAmI2), ZenohID(id), locators))
}
val binaryWhatAmI : Int = whatAmI.map {it.value}.reduce {acc, it -> acc or it}
val ptr = scoutViaJNI(binaryWhatAmI, scoutCallback, config.jsonConfig.toString())
val binaryWhatAmI: Int = whatAmI.map { it.value }.reduce { acc, it -> acc or it }
val ptr = scoutViaJNI(
binaryWhatAmI, scoutCallback, config?.config, config?.format?.ordinal ?: 0,
config?.path?.toString()
)
return Scout(receiver, JNIScout(ptr))
}

@Throws(Exception::class)
private external fun scoutViaJNI(whatAmI: Int, callback: JNIScoutCallback, config: String): Long
private external fun scoutViaJNI(
whatAmI: Int,
callback: JNIScoutCallback,
config: String?,
format: Int,
path: String?
): Long

@Throws(Exception::class)
external fun freePtrViaJNI(ptr: Long)
Expand Down

0 comments on commit 33d4883

Please sign in to comment.