Skip to content

Commit

Permalink
chore: update zenoh version (#210)
Browse files Browse the repository at this point in the history
* chore: update zenoh version

* fix: fix subscriber undeclaration

* fix: remove error handling in session closing

`Session` now uses weak references inside entities.
  • Loading branch information
wyfo authored Sep 12, 2024
1 parent bc68fb4 commit d85304a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 68 deletions.
53 changes: 26 additions & 27 deletions zenoh-jni/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion zenoh-jni/src/queryable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use zenoh::query::Queryable;
pub(crate) unsafe extern "C" fn Java_io_zenoh_jni_JNIQueryable_freePtrViaJNI(
_env: JNIEnv,
_: JClass,
queryable_ptr: *const Queryable<'_, ()>,
queryable_ptr: *const Queryable<()>,
) {
Arc::from_raw(queryable_ptr);
}
67 changes: 27 additions & 40 deletions zenoh-jni/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,27 @@
// ZettaScale Zenoh Team, <[email protected]>
//

use crate::errors::Result;
use crate::key_expr::process_kotlin_key_expr;
use crate::{jni_error, utils::*};
use crate::{session_error, throw_exception};
use std::{mem, ops::Deref, ptr::null, sync::Arc, time::Duration};

use jni::objects::{GlobalRef, JByteArray, JClass, JObject, JString, JValue};
use jni::sys::{jboolean, jint, jlong};
use jni::JNIEnv;
use std::mem;
use std::ops::Deref;
use std::ptr::null;
use std::sync::Arc;
use std::time::Duration;
use zenoh::config::{Config, ZenohId};
use zenoh::key_expr::KeyExpr;
use zenoh::prelude::Wait;
use zenoh::pubsub::{Publisher, Subscriber};
use zenoh::query::{Query, Queryable, ReplyError, Selector};
use zenoh::sample::Sample;
use zenoh::session::{Session, SessionDeclarations};
use jni::{
objects::{GlobalRef, JByteArray, JClass, JObject, JString, JValue},
sys::{jboolean, jint, jlong},
JNIEnv,
};
use zenoh::{
config::{Config, ZenohId},
key_expr::KeyExpr,
prelude::Wait,
pubsub::{Publisher, Subscriber},
query::{Query, Queryable, ReplyError, Selector},
sample::Sample,
session::Session,
};

use crate::{
errors::Result, jni_error, key_expr::process_kotlin_key_expr, session_error, throw_exception,
utils::*,
};

/// Open a Zenoh session via JNI.
///
Expand Down Expand Up @@ -188,21 +189,7 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNISession_closeSessionViaJNI(
_class: JClass,
session_ptr: *const Session,
) {
let ptr = Arc::try_unwrap(Arc::from_raw(session_ptr));
match ptr {
Ok(session) => {
// Do nothing, the pointer will be freed.
}
Err(arc_session) => {
let ref_count = Arc::strong_count(&arc_session);
throw_exception!(env, session_error!(
"Attempted to close the session, but at least one strong reference to it is still alive
(ref count: {}). All the declared publishers, subscribers, and queryables need to be
dropped first.",
ref_count
));
}
};
Arc::from_raw(session_ptr);
}

/// Declare a Zenoh publisher via JNI.
Expand Down Expand Up @@ -444,9 +431,9 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNISession_declareSubscriberViaJNI(
session_ptr: *const Session,
callback: JObject,
on_close: JObject,
) -> *const Subscriber<'static, ()> {
) -> *const Subscriber<()> {
let session = Arc::from_raw(session_ptr);
|| -> Result<*const Subscriber<'static, ()>> {
|| -> Result<*const Subscriber<()>> {
let java_vm = Arc::new(get_java_vm(&mut env)?);
let callback_global_ref = get_callback_global_ref(&mut env, callback)?;
let on_close_global_ref = get_callback_global_ref(&mut env, on_close)?;
Expand All @@ -457,7 +444,7 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNISession_declareSubscriberViaJNI(

let result = session
.declare_subscriber(key_expr.to_owned())
.callback(move |sample| {
.with(move |sample: Sample| {
on_close.noop(); // Moves `on_close` inside the closure so it gets destroyed with the closure
let _ = || -> Result<()> {
let mut env = java_vm.attach_current_thread_as_daemon().map_err(|err| {
Expand Down Expand Up @@ -572,9 +559,9 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNISession_declareQueryableViaJNI(
callback: JObject,
on_close: JObject,
complete: jboolean,
) -> *const Queryable<'static, ()> {
) -> *const Queryable<()> {
let session = Arc::from_raw(session_ptr);
let query_ptr = || -> Result<*const Queryable<'static, ()>> {
let query_ptr = || -> Result<*const Queryable<()>> {
let java_vm = Arc::new(get_java_vm(&mut env)?);
let callback_global_ref = get_callback_global_ref(&mut env, callback)?;
let on_close_global_ref = get_callback_global_ref(&mut env, on_close)?;
Expand All @@ -584,7 +571,7 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNISession_declareQueryableViaJNI(
tracing::debug!("Declaring queryable through JNI on {}", key_expr);
let builder = session
.declare_queryable(key_expr)
.callback(move |query| {
.with(move |query: Query| {
on_close.noop(); // Does nothing, but moves `on_close` inside the closure so it gets destroyed with the closure
let env = match java_vm.attach_current_thread_as_daemon() {
Ok(env) => env,
Expand Down

0 comments on commit d85304a

Please sign in to comment.