Skip to content

Commit 2b58526

Browse files
committed
query and reply methods ported
1 parent 6ebbfba commit 2b58526

14 files changed

Lines changed: 267 additions & 413 deletions

File tree

zenoh-flat-jni/build.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,10 @@ fn main() {
200200
.package("query")
201201
.ptr_class(pq!(ZQueryable))
202202
.ptr_class(pq!(ZQuerier))
203+
.ptr_class(pq!(ZQuery))
204+
.class_fun(pq!(z_query_reply_success))
205+
.class_fun(pq!(z_query_reply_error))
206+
.class_fun(pq!(z_query_reply_delete))
203207
.into_sources(
204208
pq!(ZBytes),
205209
[

zenoh-flat-jni/generated-kotlin/io/zenoh/jni/JNINative.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ internal object JNINative {
158158
external fun zKeyexprTryFrom(s: String): Long
159159
external fun zPublisherDelete(publisher: Long, attachment: Any?)
160160
external fun zPublisherPut(publisher: Long, payload: Any, encoding: Any, attachment: Any?)
161+
external fun zQueryReplyDelete(query: Long, keyExpr: Any, timestampNtp64: Long?, attachment: Any?, express: Boolean)
162+
external fun zQueryReplyError(query: Long, payload: Any, encoding: Any)
163+
external fun zQueryReplySuccess(query: Long, keyExpr: Any, payload: Any, encoding: Any, timestampNtp64: Long?, attachment: Any?, express: Boolean)
161164
external fun zScout(whatami: Int, config: Long, callback: ZHelloCallback, onClose: Callback): Long
162165
external fun zZbytesFromBytes(bytes: ByteArray): Long
163166
external fun zZbytesToBytes(z: Long): ByteArray
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
// Auto-generated by JniExt — do not edit by hand.
2+
package io.zenoh.jni.query
3+
4+
import io.zenoh.jni.Error
5+
import io.zenoh.jni.JNINative
6+
import io.zenoh.jni.JniBindingError
7+
import io.zenoh.jni.bytes.ZEncoding
8+
import io.zenoh.jni.bytes.ZZBytes
9+
import io.zenoh.jni.keyexpr.ZKeyExpr
10+
11+
/** Typed handle for a native Zenoh `ZQuery`. */
12+
public class ZQuery(initialPtr: Long) : AutoCloseable {
13+
@Volatile internal var ptr: Long = initialPtr
14+
15+
public fun peek(): Long = ptr
16+
public fun isClosed(): Boolean = ptr == 0L
17+
18+
@Synchronized
19+
override fun close() {
20+
val p = ptr
21+
if (p != 0L) {
22+
ptr = 0L
23+
freePtr(p)
24+
}
25+
}
26+
27+
@Synchronized
28+
public fun take(): ZQuery {
29+
val p = ptr
30+
ptr = 0L
31+
return ZQuery(p)
32+
}
33+
34+
@Throws(Error::class, JniBindingError::class)
35+
public fun zQueryReplySuccess(keyExpr: Any, payload: Any, encoding: Any, timestampNtp64: Long?, attachment: Any?, express: Boolean) {
36+
synchronized(this) {
37+
val query_ptr = this.ptr
38+
if (query_ptr == 0L) throw JniBindingError("Operation on a closed native handle.")
39+
try {
40+
if (keyExpr is ZKeyExpr) synchronized(keyExpr) {
41+
if (payload is ZZBytes) synchronized(payload) {
42+
if (encoding is ZEncoding) synchronized(encoding) {
43+
if (attachment is ZZBytes) synchronized(attachment) {
44+
JNINative.zQueryReplySuccess(query_ptr, keyExpr, payload, encoding, timestampNtp64, attachment, express)
45+
} else {
46+
JNINative.zQueryReplySuccess(query_ptr, keyExpr, payload, encoding, timestampNtp64, attachment, express)
47+
}
48+
} else {
49+
if (attachment is ZZBytes) synchronized(attachment) {
50+
JNINative.zQueryReplySuccess(query_ptr, keyExpr, payload, encoding, timestampNtp64, attachment, express)
51+
} else {
52+
JNINative.zQueryReplySuccess(query_ptr, keyExpr, payload, encoding, timestampNtp64, attachment, express)
53+
}
54+
}
55+
} else {
56+
if (encoding is ZEncoding) synchronized(encoding) {
57+
if (attachment is ZZBytes) synchronized(attachment) {
58+
JNINative.zQueryReplySuccess(query_ptr, keyExpr, payload, encoding, timestampNtp64, attachment, express)
59+
} else {
60+
JNINative.zQueryReplySuccess(query_ptr, keyExpr, payload, encoding, timestampNtp64, attachment, express)
61+
}
62+
} else {
63+
if (attachment is ZZBytes) synchronized(attachment) {
64+
JNINative.zQueryReplySuccess(query_ptr, keyExpr, payload, encoding, timestampNtp64, attachment, express)
65+
} else {
66+
JNINative.zQueryReplySuccess(query_ptr, keyExpr, payload, encoding, timestampNtp64, attachment, express)
67+
}
68+
}
69+
}
70+
} else {
71+
if (payload is ZZBytes) synchronized(payload) {
72+
if (encoding is ZEncoding) synchronized(encoding) {
73+
if (attachment is ZZBytes) synchronized(attachment) {
74+
JNINative.zQueryReplySuccess(query_ptr, keyExpr, payload, encoding, timestampNtp64, attachment, express)
75+
} else {
76+
JNINative.zQueryReplySuccess(query_ptr, keyExpr, payload, encoding, timestampNtp64, attachment, express)
77+
}
78+
} else {
79+
if (attachment is ZZBytes) synchronized(attachment) {
80+
JNINative.zQueryReplySuccess(query_ptr, keyExpr, payload, encoding, timestampNtp64, attachment, express)
81+
} else {
82+
JNINative.zQueryReplySuccess(query_ptr, keyExpr, payload, encoding, timestampNtp64, attachment, express)
83+
}
84+
}
85+
} else {
86+
if (encoding is ZEncoding) synchronized(encoding) {
87+
if (attachment is ZZBytes) synchronized(attachment) {
88+
JNINative.zQueryReplySuccess(query_ptr, keyExpr, payload, encoding, timestampNtp64, attachment, express)
89+
} else {
90+
JNINative.zQueryReplySuccess(query_ptr, keyExpr, payload, encoding, timestampNtp64, attachment, express)
91+
}
92+
} else {
93+
if (attachment is ZZBytes) synchronized(attachment) {
94+
JNINative.zQueryReplySuccess(query_ptr, keyExpr, payload, encoding, timestampNtp64, attachment, express)
95+
} else {
96+
JNINative.zQueryReplySuccess(query_ptr, keyExpr, payload, encoding, timestampNtp64, attachment, express)
97+
}
98+
}
99+
}
100+
}
101+
} finally {
102+
ptr = 0L
103+
}
104+
}
105+
}
106+
107+
@Throws(Error::class, JniBindingError::class)
108+
public fun zQueryReplyError(payload: Any, encoding: Any) {
109+
synchronized(this) {
110+
val query_ptr = this.ptr
111+
if (query_ptr == 0L) throw JniBindingError("Operation on a closed native handle.")
112+
try {
113+
if (payload is ZZBytes) synchronized(payload) {
114+
if (encoding is ZEncoding) synchronized(encoding) {
115+
JNINative.zQueryReplyError(query_ptr, payload, encoding)
116+
} else {
117+
JNINative.zQueryReplyError(query_ptr, payload, encoding)
118+
}
119+
} else {
120+
if (encoding is ZEncoding) synchronized(encoding) {
121+
JNINative.zQueryReplyError(query_ptr, payload, encoding)
122+
} else {
123+
JNINative.zQueryReplyError(query_ptr, payload, encoding)
124+
}
125+
}
126+
} finally {
127+
ptr = 0L
128+
}
129+
}
130+
}
131+
132+
@Throws(Error::class, JniBindingError::class)
133+
public fun zQueryReplyDelete(keyExpr: Any, timestampNtp64: Long?, attachment: Any?, express: Boolean) {
134+
synchronized(this) {
135+
val query_ptr = this.ptr
136+
if (query_ptr == 0L) throw JniBindingError("Operation on a closed native handle.")
137+
try {
138+
if (keyExpr is ZKeyExpr) synchronized(keyExpr) {
139+
if (attachment is ZZBytes) synchronized(attachment) {
140+
JNINative.zQueryReplyDelete(query_ptr, keyExpr, timestampNtp64, attachment, express)
141+
} else {
142+
JNINative.zQueryReplyDelete(query_ptr, keyExpr, timestampNtp64, attachment, express)
143+
}
144+
} else {
145+
if (attachment is ZZBytes) synchronized(attachment) {
146+
JNINative.zQueryReplyDelete(query_ptr, keyExpr, timestampNtp64, attachment, express)
147+
} else {
148+
JNINative.zQueryReplyDelete(query_ptr, keyExpr, timestampNtp64, attachment, express)
149+
}
150+
}
151+
} finally {
152+
ptr = 0L
153+
}
154+
}
155+
}
156+
157+
public companion object {
158+
@JvmStatic
159+
external fun freePtr(ptr: Long)
160+
}
161+
}

zenoh-flat-jni/src/outdated/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@
44
pub mod liveliness;
55
pub mod logger;
66
pub mod querier;
7-
pub mod query;
87
pub mod session;
98
pub mod zenoh_id;

zenoh-flat-jni/src/outdated/query.rs

Lines changed: 0 additions & 63 deletions
This file was deleted.

zenoh-flat/src/keyexpr/keyexpr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ pub fn keyexpr_concat(
128128
Ok(KeyExpr::from(z_keyexpr_concat(&a_ke, b)?))
129129
}
130130

131-
fn into_native(k: KeyExpr) -> Result<ZKeyExpr, Error> {
131+
pub(crate) fn into_native(k: KeyExpr) -> Result<ZKeyExpr, Error> {
132132
match k.key_expr_native {
133133
Some(ke) => Ok(ke),
134134
None => Ok(ZKeyExpr::try_from(k.key_expr_string)?),

zenoh-flat/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub(crate) mod bytes;
2323
pub(crate) mod error;
2424
pub(crate) mod logger;
2525
pub(crate) mod publisher;
26+
pub(crate) mod query;
2627

2728
// reexports to make all zenoh-flat API really flat
2829
pub use keyexpr::*;
@@ -33,6 +34,7 @@ pub use bytes::*;
3334
pub use error::*;
3435
pub use logger::*;
3536
pub use publisher::*;
37+
pub use query::*;
3638

3739
// reexports of zenoh types with Z prefix to distiguish them from zenoh-flat types
3840
pub type ZKeyExpr = zenoh::key_expr::KeyExpr<'static>;
@@ -45,4 +47,5 @@ pub type ZEncoding = zenoh::bytes::Encoding;
4547
pub type ZPublisher = zenoh::pubsub::Publisher<'static>;
4648
pub type ZSubscriber = zenoh::pubsub::Subscriber<()>;
4749
pub type ZQueryable = zenoh::query::Queryable<()>;
48-
pub type ZQuerier = zenoh::query::Querier<'static>;
50+
pub type ZQuerier = zenoh::query::Querier<'static>;
51+
pub type ZQuery = zenoh::query::Query;

zenoh-flat/src/query/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub(crate) mod z_query;
2+
3+
pub use z_query::*;

zenoh-flat/src/query/z_query.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
use crate::{into_native, Encoding, Error, KeyExpr, ZBytes, ZEncoding, ZQuery};
2+
use prebindgen_proc_macro::prebindgen;
3+
use zenoh::{
4+
time::{Timestamp, TimestampId, NTP64},
5+
Wait,
6+
};
7+
8+
#[prebindgen]
9+
pub fn z_query_reply_success(
10+
query: ZQuery,
11+
key_expr: impl Into<KeyExpr> + Send + 'static,
12+
payload: impl Into<ZBytes> + Send + 'static,
13+
encoding: impl Into<Encoding> + Send + 'static,
14+
timestamp_ntp64: Option<i64>,
15+
attachment: Option<impl Into<ZBytes> + Send + 'static>,
16+
express: bool,
17+
) -> Result<(), Error> {
18+
let ke = into_native(key_expr.into())?;
19+
let payload: ZBytes = payload.into();
20+
let z_encoding: ZEncoding = encoding.into().try_into()?;
21+
let mut b = query.reply(ke, payload.bytes).encoding(z_encoding);
22+
if let Some(ntp) = timestamp_ntp64 {
23+
b = b.timestamp(Timestamp::new(NTP64(ntp as u64), TimestampId::rand()));
24+
}
25+
if let Some(att) = attachment {
26+
let att: ZBytes = att.into();
27+
b = b.attachment::<Vec<u8>>(att.bytes);
28+
}
29+
b.express(express).wait().map_err(Error::from)
30+
}
31+
32+
#[prebindgen]
33+
pub fn z_query_reply_error(
34+
query: ZQuery,
35+
payload: impl Into<ZBytes> + Send + 'static,
36+
encoding: impl Into<Encoding> + Send + 'static,
37+
) -> Result<(), Error> {
38+
let payload: ZBytes = payload.into();
39+
let z_encoding: ZEncoding = encoding.into().try_into()?;
40+
query
41+
.reply_err(payload.bytes)
42+
.encoding(z_encoding)
43+
.wait()
44+
.map_err(Error::from)
45+
}
46+
47+
#[prebindgen]
48+
pub fn z_query_reply_delete(
49+
query: ZQuery,
50+
key_expr: impl Into<KeyExpr> + Send + 'static,
51+
timestamp_ntp64: Option<i64>,
52+
attachment: Option<impl Into<ZBytes> + Send + 'static>,
53+
express: bool,
54+
) -> Result<(), Error> {
55+
let ke = into_native(key_expr.into())?;
56+
let mut b = query.reply_del(ke);
57+
if let Some(ntp) = timestamp_ntp64 {
58+
b = b.timestamp(Timestamp::new(NTP64(ntp as u64), TimestampId::rand()));
59+
}
60+
if let Some(att) = attachment {
61+
let att: ZBytes = att.into();
62+
b = b.attachment::<Vec<u8>>(att.bytes);
63+
}
64+
b.express(express).wait().map_err(Error::from)
65+
}

0 commit comments

Comments
 (0)