Skip to content

Commit 83bcf75

Browse files
committed
full timestamp representation in zenoh-flat
1 parent e234338 commit 83bcf75

13 files changed

Lines changed: 147 additions & 12 deletions

File tree

zenoh-flat-jni/build.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,12 @@ fn main() {
192192
.class_object_fun(pq!(encoding_video_raw))
193193
.class_object_fun(pq!(encoding_video_vp8))
194194
.class_object_fun(pq!(encoding_video_vp9))
195+
.package("time")
196+
.ptr_class(pq!(ZTimestamp))
197+
.class_fun(pq!(z_timestamp_ntp64))
198+
.class_fun(pq!(z_timestamp_id))
199+
.class_fun(pq!(z_timestamp_expand))
200+
.data_class(pq!(Timestamp))
195201
.package("sample")
196202
.enum_class(pq!(SampleKind))
197203
.ptr_class(pq!(ZSample))

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import io.zenoh.jni.sample.Sample
2323
import io.zenoh.jni.sample.SampleKind
2424
import io.zenoh.jni.sample.ZSample
2525
import io.zenoh.jni.scouting.ZScout
26+
import io.zenoh.jni.time.Timestamp
27+
import io.zenoh.jni.time.ZTimestamp
2628

2729
internal object JNINative {
2830
external fun encodingApplicationCbor(): Encoding
@@ -191,6 +193,9 @@ internal object JNINative {
191193
external fun zSamplePriority(s: Long): Int
192194
external fun zSampleTimestamp(s: Long): Long
193195
external fun zScout(whatami: Int, config: Long, callback: ZHelloCallback, onClose: Callback): Long
196+
external fun zTimestampExpand(t: Long): Timestamp
197+
external fun zTimestampId(t: Long): ByteArray
198+
external fun zTimestampNtp64(t: Long): Long
194199
external fun zZbytesFromBytes(bytes: ByteArray): Long
195200
external fun zZbytesToBytes(z: Long): ByteArray
196201
external fun zZenohIdToBytes(z: Long): ByteArray

zenoh-flat-jni/generated-kotlin/io/zenoh/jni/sample/Sample.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ import io.zenoh.jni.bytes.ZBytes
66
import io.zenoh.jni.keyexpr.KeyExpr
77
import io.zenoh.jni.qos.CongestionControl
88
import io.zenoh.jni.qos.Priority
9+
import io.zenoh.jni.time.Timestamp
910

1011
public data class Sample(
1112
val keyExpr: KeyExpr,
1213
val payload: ZBytes,
1314
val encoding: Encoding,
1415
val kind: SampleKind,
15-
val timestamp: Long?,
16+
val timestamp: Timestamp?,
1617
val express: Boolean,
1718
val priority: Priority,
1819
val congestionControl: CongestionControl,

zenoh-flat-jni/generated-kotlin/io/zenoh/jni/sample/ZSample.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import io.zenoh.jni.bytes.ZZBytes
88
import io.zenoh.jni.keyexpr.ZKeyExpr
99
import io.zenoh.jni.qos.CongestionControl
1010
import io.zenoh.jni.qos.Priority
11+
import io.zenoh.jni.time.ZTimestamp
1112

1213
/** Typed handle for a native Zenoh `ZSample`. */
1314
public class ZSample(initialPtr: Long) : AutoCloseable {
@@ -69,11 +70,11 @@ public class ZSample(initialPtr: Long) : AutoCloseable {
6970
}
7071

7172
@Throws(JniBindingError::class)
72-
public fun zSampleTimestamp(): Long {
73+
public fun zSampleTimestamp(): ZTimestamp? {
7374
synchronized(this) {
7475
val s_ptr = this.ptr
7576
if (s_ptr == 0L) throw JniBindingError("Operation on a closed native handle.")
76-
return JNINative.zSampleTimestamp(s_ptr)
77+
return ZTimestamp(JNINative.zSampleTimestamp(s_ptr))
7778
}
7879
}
7980

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Auto-generated by JniExt — do not edit by hand.
2+
package io.zenoh.jni.time
3+
4+
public data class Timestamp(
5+
val ntp64: Long,
6+
val id: ByteArray,
7+
) {
8+
public companion object {
9+
}
10+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Auto-generated by JniExt — do not edit by hand.
2+
package io.zenoh.jni.time
3+
4+
import io.zenoh.jni.JNINative
5+
import io.zenoh.jni.JniBindingError
6+
7+
/** Typed handle for a native Zenoh `ZTimestamp`. */
8+
public class ZTimestamp(initialPtr: Long) : AutoCloseable {
9+
@Volatile internal var ptr: Long = initialPtr
10+
11+
public fun peek(): Long = ptr
12+
public fun isClosed(): Boolean = ptr == 0L
13+
14+
@Synchronized
15+
override fun close() {
16+
val p = ptr
17+
if (p != 0L) {
18+
ptr = 0L
19+
freePtr(p)
20+
}
21+
}
22+
23+
@Synchronized
24+
public fun take(): ZTimestamp {
25+
val p = ptr
26+
ptr = 0L
27+
return ZTimestamp(p)
28+
}
29+
30+
@Throws(JniBindingError::class)
31+
public fun zTimestampNtp64(): Long {
32+
synchronized(this) {
33+
val t_ptr = this.ptr
34+
if (t_ptr == 0L) throw JniBindingError("Operation on a closed native handle.")
35+
return JNINative.zTimestampNtp64(t_ptr)
36+
}
37+
}
38+
39+
@Throws(JniBindingError::class)
40+
public fun zTimestampId(): ByteArray {
41+
synchronized(this) {
42+
val t_ptr = this.ptr
43+
if (t_ptr == 0L) throw JniBindingError("Operation on a closed native handle.")
44+
return JNINative.zTimestampId(t_ptr)
45+
}
46+
}
47+
48+
@Throws(JniBindingError::class)
49+
public fun zTimestampExpand(): Timestamp {
50+
synchronized(this) {
51+
val t_ptr = this.ptr
52+
if (t_ptr == 0L) throw JniBindingError("Operation on a closed native handle.")
53+
return JNINative.zTimestampExpand(t_ptr)
54+
}
55+
}
56+
57+
public companion object {
58+
@JvmStatic
59+
external fun freePtr(ptr: Long)
60+
}
61+
}

zenoh-flat/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub(crate) mod logger;
2525
pub(crate) mod publisher;
2626
pub(crate) mod query;
2727
pub(crate) mod sample;
28+
pub(crate) mod time;
2829
pub(crate) mod util;
2930

3031
// reexports to make all zenoh-flat API really flat
@@ -38,6 +39,7 @@ pub use logger::*;
3839
pub use publisher::*;
3940
pub use query::*;
4041
pub use sample::*;
42+
pub use time::*;
4143

4244
// reexports of zenoh types with Z prefix to distiguish them from zenoh-flat types
4345
pub type ZKeyExpr = zenoh::key_expr::KeyExpr<'static>;
@@ -53,4 +55,5 @@ pub type ZQueryable = zenoh::query::Queryable<()>;
5355
pub type ZQuerier = zenoh::query::Querier<'static>;
5456
pub type ZQuery = zenoh::query::Query;
5557
pub type ZSample = zenoh::sample::Sample;
56-
pub type ZReply = zenoh::query::Reply;
58+
pub type ZReply = zenoh::query::Reply;
59+
pub type ZTimestamp = zenoh::time::Timestamp;

zenoh-flat/src/sample/sample.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{CongestionControl, Encoding, KeyExpr, Priority, SampleKind, ZBytes, ZSample};
1+
use crate::{CongestionControl, Encoding, KeyExpr, Priority, SampleKind, Timestamp, ZBytes, ZSample};
22
use prebindgen_proc_macro::prebindgen;
33

44
/// Data-class twin of [`ZSample`]. Carries every field decoded so bindings
@@ -10,7 +10,7 @@ pub struct Sample {
1010
pub payload: ZBytes,
1111
pub encoding: Encoding,
1212
pub kind: SampleKind,
13-
pub timestamp: Option<i64>,
13+
pub timestamp: Option<Timestamp>,
1414
pub express: bool,
1515
pub priority: Priority,
1616
pub congestion_control: CongestionControl,
@@ -26,7 +26,7 @@ impl From<&ZSample> for Sample {
2626
payload: ZBytes::from(s.payload().clone()),
2727
encoding: Encoding::from(s.encoding()),
2828
kind: s.kind().into(),
29-
timestamp: s.timestamp().map(|t| t.get_time().as_u64() as i64),
29+
timestamp: s.timestamp().map(Timestamp::from),
3030
express: s.express(),
3131
priority: s.priority().into(),
3232
congestion_control: s.congestion_control().into(),

zenoh-flat/src/sample/z_sample.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::sample::Sample;
2-
use crate::{CongestionControl, Priority, SampleKind, ZEncoding, ZKeyExpr, ZSample, ZZBytes};
2+
use crate::{CongestionControl, Priority, SampleKind, ZEncoding, ZKeyExpr, ZSample, ZTimestamp, ZZBytes};
33
use prebindgen_proc_macro::prebindgen;
44

55
/// Key expression the sample was published on (owned handle).
@@ -26,10 +26,10 @@ pub fn z_sample_kind(s: &ZSample) -> SampleKind {
2626
s.kind().into()
2727
}
2828

29-
/// NTP64 timestamp, or `None` when the sample carries no timestamp.
29+
/// Timestamp handle, or `None` when the sample carries no timestamp.
3030
#[prebindgen]
31-
pub fn z_sample_timestamp(s: &ZSample) -> Option<i64> {
32-
s.timestamp().map(|t| t.get_time().as_u64() as i64)
31+
pub fn z_sample_timestamp(s: &ZSample) -> Option<ZTimestamp> {
32+
s.timestamp().copied()
3333
}
3434

3535
/// QoS express flag.

zenoh-flat/src/time/mod.rs

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

0 commit comments

Comments
 (0)