-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Api alignment: Config alignment #188
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
10f5afd
refactor(config): returning a Result when attempting to create a conf…
DariusIMP 0574178
refactor(config):
DariusIMP 72619a5
refactor(config): fix load from JSON element + adding docs to zenoh-jni
DariusIMP e95863a
Cargo fmt + cargo clippy
DariusIMP File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,133 @@ | ||
// | ||
// 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]> | ||
// | ||
|
||
use std::{ptr::null, sync::Arc}; | ||
|
||
use jni::{ | ||
objects::{JClass, JString}, | ||
JNIEnv, | ||
}; | ||
use zenoh::Config; | ||
|
||
use crate::errors::Result; | ||
use crate::{session_error, throw_exception, utils::decode_string}; | ||
|
||
/// Loads the default configuration, returning a raw pointer to it. | ||
/// | ||
/// The pointer to the config is expected to be freed later on upon the destruction of the | ||
/// Kotlin Config instance. | ||
/// | ||
#[no_mangle] | ||
#[allow(non_snake_case)] | ||
pub extern "C" fn Java_io_zenoh_jni_JNIConfig_00024Companion_loadDefaultConfigViaJNI( | ||
_env: JNIEnv, | ||
_class: JClass, | ||
) -> *const Config { | ||
let config = Config::default(); | ||
Arc::into_raw(Arc::new(config)) | ||
} | ||
|
||
/// Loads the config from a file, returning a pointer to the loaded config in case of success. | ||
/// In case of failure, an exception is thrown via JNI. | ||
/// | ||
/// The pointer to the config is expected to be freed later on upon the destruction of the | ||
/// Kotlin Config instance. | ||
/// | ||
#[no_mangle] | ||
#[allow(non_snake_case)] | ||
pub extern "C" fn Java_io_zenoh_jni_JNIConfig_00024Companion_loadConfigFileViaJNI( | ||
mut env: JNIEnv, | ||
_class: JClass, | ||
config_path: JString, | ||
) -> *const Config { | ||
|| -> Result<*const Config> { | ||
let config_file_path = decode_string(&mut env, &config_path)?; | ||
let config = Config::from_file(config_file_path).map_err(|err| session_error!(err))?; | ||
Ok(Arc::into_raw(Arc::new(config))) | ||
}() | ||
.unwrap_or_else(|err| { | ||
throw_exception!(env, err); | ||
null() | ||
}) | ||
} | ||
|
||
/// Loads the config from a json/json5 formatted string, returning a pointer to the loaded config | ||
/// in case of success. In case of failure, an exception is thrown via JNI. | ||
/// | ||
/// The pointer to the config is expected to be freed later on upon the destruction of the | ||
/// Kotlin Config instance. | ||
/// | ||
#[no_mangle] | ||
#[allow(non_snake_case)] | ||
pub extern "C" fn Java_io_zenoh_jni_JNIConfig_00024Companion_loadJsonConfigViaJNI( | ||
mut env: JNIEnv, | ||
_class: JClass, | ||
json_config: JString, | ||
) -> *const Config { | ||
|| -> Result<*const Config> { | ||
let json_config = decode_string(&mut env, &json_config)?; | ||
let mut deserializer = | ||
json5::Deserializer::from_str(&json_config).map_err(|err| session_error!(err))?; | ||
let config = Config::from_deserializer(&mut deserializer).map_err(|err| match err { | ||
Ok(c) => session_error!("Invalid configuration: {}", c), | ||
Err(e) => session_error!("JSON error: {}", e), | ||
})?; | ||
Ok(Arc::into_raw(Arc::new(config))) | ||
}() | ||
.unwrap_or_else(|err| { | ||
throw_exception!(env, err); | ||
null() | ||
}) | ||
} | ||
|
||
/// Loads the config from a yaml-formatted string, returning a pointer to the loaded config | ||
/// in case of success. In case of failure, an exception is thrown via JNI. | ||
/// | ||
/// The pointer to the config is expected to be freed later on upon the destruction of the | ||
/// Kotlin Config instance. | ||
/// | ||
#[no_mangle] | ||
#[allow(non_snake_case)] | ||
pub extern "C" fn Java_io_zenoh_jni_JNIConfig_00024Companion_loadYamlConfigViaJNI( | ||
mut env: JNIEnv, | ||
_class: JClass, | ||
yaml_config: JString, | ||
) -> *const Config { | ||
|| -> Result<*const Config> { | ||
let yaml_config = decode_string(&mut env, &yaml_config)?; | ||
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!("YAML error: {}", e), | ||
})?; | ||
Ok(Arc::into_raw(Arc::new(config))) | ||
}() | ||
.unwrap_or_else(|err| { | ||
throw_exception!(env, err); | ||
null() | ||
}) | ||
} | ||
|
||
/// Frees the pointer to the config. The pointer should be valid and should have been obtained through | ||
/// one of the preceding `load` functions. This function should be called upon destruction of the kotlin | ||
/// Config instance. | ||
#[no_mangle] | ||
#[allow(non_snake_case)] | ||
pub(crate) unsafe extern "C" fn Java_io_zenoh_jni_JNIConfig_00024Companion_freePtrViaJNI( | ||
_env: JNIEnv, | ||
_: JClass, | ||
config_ptr: *const Config, | ||
) { | ||
Arc::from_raw(config_ptr); | ||
} |
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 |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
// ZettaScale Zenoh Team, <[email protected]> | ||
// | ||
|
||
mod config; | ||
mod errors; | ||
mod key_expr; | ||
mod logger; | ||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this an automatically generated code? The names look a bit strange.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, its not 😅 . It's strange because it's meant to be called from Kotlin through the JNI by a function named
loadDefaultConfigViaJNI
located at io/zenoh/jni/JNIConfig$Companion. It must have this weird looking signature for Java/Kotlin to locate the corresponding native function.