-
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.
Adding onClose behaviour for Get, Queryable and Subscribers. (#9)
* Restructuring packages aiming for Android compatibility, using Kotlin multiplatform plugin * Edit cargo task in build.gradle.kts * Reenabling examples. * Making jvmTest commonTest * [WIP] Target building Zenoh JNI * Editing workflows after multiplatforming project. * Editing README and build.gradle.kts * Publish to JVM. * Publish to JVM. Also: - fix Examples build - tidy up build.gradle.kts files - triggering compile zenoh-jni if needed when running gradle tasks. * Fix CI + edit README * Fix CI + edit README * OnFinish callback feature. The possibility of adding an onFinish callback for Get requests, Subscribers and Queryables is added to their respective builders. Also the Handler interface is expanded with an "onFinish" callback (in the case of the default handler, which is ChannelHandler, the implementation consists of closing the channel upon finishing). * Fix doc comment. * Fix formatting of Zenoh-JNI * Renaming 'onFinish' with 'onClose'. * Fix cargo fmt * Fix CI workflow
- Loading branch information
Showing
23 changed files
with
297 additions
and
73 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
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
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
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 |
---|---|---|
|
@@ -12,6 +12,8 @@ | |
// ZettaScale Zenoh Team, <[email protected]> | ||
// | ||
|
||
use std::sync::Arc; | ||
|
||
use jni::{ | ||
objects::{JObject, JString}, | ||
JNIEnv, JavaVM, | ||
|
@@ -46,3 +48,53 @@ pub(crate) fn get_callback_global_ref( | |
)) | ||
}) | ||
} | ||
|
||
/// A type that calls a function when dropped | ||
pub(crate) struct CallOnDrop<F: FnOnce()>(core::mem::MaybeUninit<F>); | ||
impl<F: FnOnce()> CallOnDrop<F> { | ||
/// Constructs a value that calls `f` when dropped. | ||
pub fn new(f: F) -> Self { | ||
Self(core::mem::MaybeUninit::new(f)) | ||
} | ||
/// Does nothing, but tricks closures into moving the value inside, | ||
/// so that the closure's destructor will call `drop(self)`. | ||
pub fn noop(&self) {} | ||
} | ||
impl<F: FnOnce()> Drop for CallOnDrop<F> { | ||
fn drop(&mut self) { | ||
// Take ownership of the closure that is always initialized, | ||
// since the only constructor uses `MaybeUninit::new` | ||
let f = unsafe { self.0.assume_init_read() }; | ||
// Call the now owned function | ||
f(); | ||
} | ||
} | ||
|
||
pub(crate) fn load_on_close( | ||
java_vm: &Arc<jni::JavaVM>, | ||
on_close_global_ref: jni::objects::GlobalRef, | ||
) -> CallOnDrop<impl FnOnce()> { | ||
CallOnDrop::new({ | ||
let java_vm = java_vm.clone(); | ||
move || { | ||
let mut env = match java_vm.attach_current_thread_as_daemon() { | ||
Ok(env) => env, | ||
Err(err) => { | ||
log::error!("Unable to attach thread for 'onClose' callback: {}", err); | ||
return; | ||
} | ||
}; | ||
match env.call_method(on_close_global_ref, "run", "()V", &[]) { | ||
Ok(_) => (), | ||
Err(err) => { | ||
_ = env.exception_describe(); | ||
_ = Error::Jni(format!("Error while running 'onClose' callback: {}", err)) | ||
.throw_on_jvm(&mut env) | ||
.map_err(|err| { | ||
log::error!("Unable to throw exception upon 'onClose' failure: {}", err) | ||
}); | ||
} | ||
} | ||
} | ||
}) | ||
} |
Oops, something went wrong.