Skip to content

Commit 3babda7

Browse files
committed
Change replier_id type to EntityGlobalId
1 parent db979cf commit 3babda7

File tree

7 files changed

+55
-25
lines changed

7 files changed

+55
-25
lines changed

zenoh-java/src/commonMain/kotlin/io/zenoh/config/ZenohId.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package io.zenoh.config
1616

1717
import io.zenoh.jni.JNIZenohId
18+
import kotlin.math.absoluteValue
1819

1920
/**
2021
* The global unique id of a Zenoh peer.
@@ -38,3 +39,20 @@ data class ZenohId internal constructor(internal val bytes: ByteArray) {
3839
return bytes.contentHashCode()
3940
}
4041
}
42+
43+
/**
44+
* The global unique id of a Zenoh entity.
45+
* Contains two fields:
46+
* - zid: the global unique id of a Zenoh peer.
47+
* - eid: *unsigned* unique identifier of the entity within the Zenoh peer.
48+
*/
49+
data class EntityGlobalId internal constructor(
50+
val zid: ZenohId,
51+
// Rename default getter which is not accessible on Java due to unsigned type
52+
@get:JvmName("getEidUInt") val eid: UInt,
53+
) {
54+
55+
@Suppress("unused")
56+
// Manually defined getter for Java
57+
fun getEid(): Long = eid.toLong().absoluteValue
58+
}

zenoh-java/src/commonMain/kotlin/io/zenoh/jni/JNILiveliness.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package io.zenoh.jni
1616

1717
import io.zenoh.bytes.Encoding
1818
import io.zenoh.bytes.into
19+
import io.zenoh.config.EntityGlobalId
1920
import io.zenoh.config.ZenohId
2021
import io.zenoh.exceptions.ZError
2122
import io.zenoh.handlers.Callback
@@ -47,7 +48,8 @@ internal object JNILiveliness {
4748
onClose: Runnable
4849
): R {
4950
val getCallback = JNIGetCallback {
50-
replierId: ByteArray?,
51+
replierZid: ByteArray?,
52+
replierEid: Int,
5153
success: Boolean,
5254
keyExpr2: String?,
5355
payload: ByteArray,
@@ -73,10 +75,10 @@ internal object JNILiveliness {
7375
QoS(CongestionControl.fromInt(congestionControl), Priority.fromInt(priority), express),
7476
attachmentBytes?.into()
7577
)
76-
reply = Reply.Success(replierId?.let { ZenohId(it) }, sample)
78+
reply = Reply.Success(replierZid?.let { EntityGlobalId(ZenohId(it), replierEid.toUInt()) }, sample)
7779
} else {
7880
reply = Reply.Error(
79-
replierId?.let { ZenohId(it) },
81+
replierZid?.let { EntityGlobalId(ZenohId(it), replierEid.toUInt()) },
8082
payload.into(),
8183
Encoding(encodingId, schema = encodingSchema)
8284
)

zenoh-java/src/commonMain/kotlin/io/zenoh/jni/JNIQuerier.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import io.zenoh.annotations.Unstable
1818
import io.zenoh.bytes.Encoding
1919
import io.zenoh.bytes.IntoZBytes
2020
import io.zenoh.bytes.into
21+
import io.zenoh.config.EntityGlobalId
2122
import io.zenoh.config.ZenohId
2223
import io.zenoh.exceptions.ZError
2324
import io.zenoh.handlers.Callback
@@ -61,7 +62,8 @@ internal class JNIQuerier(val ptr: Long) {
6162
encoding: Encoding?
6263
): R {
6364
val getCallback = JNIGetCallback {
64-
replierId: ByteArray?,
65+
replierZid: ByteArray?,
66+
replierEid: Int,
6567
success: Boolean,
6668
keyExpr2: String?,
6769
payload2: ByteArray,
@@ -87,10 +89,10 @@ internal class JNIQuerier(val ptr: Long) {
8789
QoS(CongestionControl.fromInt(congestionControl), Priority.fromInt(priority), express),
8890
attachmentBytes?.into()
8991
)
90-
reply = Reply.Success(replierId?.let { ZenohId(it) }, sample)
92+
reply = Reply.Success(replierZid?.let { EntityGlobalId(ZenohId(it), replierEid.toUInt()) }, sample)
9193
} else {
9294
reply = Reply.Error(
93-
replierId?.let { ZenohId(it) },
95+
replierZid?.let { EntityGlobalId(ZenohId(it), replierEid.toUInt()) },
9496
payload2.into(),
9597
Encoding(encodingId, schema = encodingSchema)
9698
)

zenoh-java/src/commonMain/kotlin/io/zenoh/jni/JNISession.kt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import io.zenoh.config.ZenohId
2828
import io.zenoh.bytes.into
2929
import io.zenoh.Config
3030
import io.zenoh.annotations.Unstable
31+
import io.zenoh.config.EntityGlobalId
3132
import io.zenoh.handlers.Handler
3233
import io.zenoh.pubsub.*
3334
import io.zenoh.qos.CongestionControl
@@ -234,7 +235,8 @@ internal class JNISession(val sessionPtr: Long) {
234235
options: GetOptions
235236
) {
236237
val getCallback = JNIGetCallback {
237-
replierId: ByteArray?,
238+
replierZid: ByteArray?,
239+
replierEid: Int,
238240
success: Boolean,
239241
keyExpr: String?,
240242
payload1: ByteArray,
@@ -260,10 +262,10 @@ internal class JNISession(val sessionPtr: Long) {
260262
QoS(CongestionControl.fromInt(congestionControl), Priority.fromInt(priority), express),
261263
attachmentBytes?.into()
262264
)
263-
reply = Reply.Success(replierId?.let { ZenohId(it) }, sample)
265+
reply = Reply.Success(replierZid?.let { EntityGlobalId(ZenohId(it), replierEid.toUInt()) }, sample)
264266
} else {
265267
reply = Reply.Error(
266-
replierId?.let { ZenohId(it) },
268+
replierZid?.let { EntityGlobalId(ZenohId(it), replierEid.toUInt()) },
267269
payload1.into(),
268270
Encoding(encodingId, schema = encodingSchema)
269271
)
@@ -299,7 +301,8 @@ internal class JNISession(val sessionPtr: Long) {
299301
options: GetOptions
300302
): R {
301303
val getCallback = JNIGetCallback {
302-
replierId: ByteArray?,
304+
replierZid: ByteArray?,
305+
replierEid: Int,
303306
success: Boolean,
304307
keyExpr: String?,
305308
payload1: ByteArray,
@@ -325,10 +328,10 @@ internal class JNISession(val sessionPtr: Long) {
325328
QoS(CongestionControl.fromInt(congestionControl), Priority.fromInt(priority), express),
326329
attachmentBytes?.into()
327330
)
328-
reply = Reply.Success(replierId?.let { ZenohId(it) }, sample)
331+
reply = Reply.Success(replierZid?.let { EntityGlobalId(ZenohId(it), replierEid.toUInt()) }, sample)
329332
} else {
330333
reply = Reply.Error(
331-
replierId?.let { ZenohId(it) },
334+
replierZid?.let { EntityGlobalId(ZenohId(it), replierEid.toUInt()) },
332335
payload1.into(),
333336
Encoding(encodingId, schema = encodingSchema)
334337
)

zenoh-java/src/commonMain/kotlin/io/zenoh/jni/callbacks/JNIGetCallback.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ package io.zenoh.jni.callbacks
1717
internal fun interface JNIGetCallback {
1818

1919
fun run(
20-
replierId: ByteArray?,
20+
replierZid: ByteArray?,
21+
replierEid: Int,
2122
success: Boolean,
2223
keyExpr: String?,
2324
payload: ByteArray,

zenoh-java/src/commonMain/kotlin/io/zenoh/query/Reply.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import io.zenoh.ZenohType
1818
import io.zenoh.bytes.Encoding
1919
import io.zenoh.bytes.IntoZBytes
2020
import io.zenoh.bytes.ZBytes
21-
import io.zenoh.config.ZenohId
21+
import io.zenoh.config.EntityGlobalId
2222
import io.zenoh.sample.Sample
2323
import io.zenoh.qos.CongestionControl
2424
import io.zenoh.qos.Priority
@@ -28,16 +28,16 @@ import org.apache.commons.net.ntp.TimeStamp
2828
/**
2929
* Class to represent a Zenoh Reply to a remote query.
3030
*
31-
* @property replierId: unique ID identifying the replier.
31+
* @property replierId: unique global ID identifying the replier.
3232
* @see Success
3333
* @see Error
3434
*/
35-
sealed class Reply private constructor(val replierId: ZenohId?) : ZenohType {
35+
sealed class Reply private constructor(val replierId: EntityGlobalId?) : ZenohType {
3636

3737
/**
3838
* A Success reply.
3939
*/
40-
class Success internal constructor(replierId: ZenohId?, val sample: Sample) : Reply(replierId) {
40+
class Success internal constructor(replierId: EntityGlobalId?, val sample: Sample) : Reply(replierId) {
4141

4242
override fun toString(): String {
4343
return "Success(sample=$sample)"
@@ -58,7 +58,7 @@ sealed class Reply private constructor(val replierId: ZenohId?) : ZenohType {
5858
/**
5959
* An Error reply.
6060
*/
61-
class Error internal constructor(replierId: ZenohId?, val error: ZBytes, val encoding: Encoding) :
61+
class Error internal constructor(replierId: EntityGlobalId?, val error: ZBytes, val encoding: Encoding) :
6262
Reply(replierId) {
6363

6464
override fun toString(): String {

zenoh-jni/src/session.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use zenoh::{
2525
pubsub::{Publisher, Subscriber},
2626
query::{Querier, Query, Queryable, ReplyError, Selector},
2727
sample::Sample,
28-
session::{Session, ZenohId},
28+
session::{EntityGlobalId, Session, ZenohId},
2929
Wait,
3030
};
3131

@@ -964,19 +964,20 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNISession_getViaJNI(
964964

965965
pub(crate) fn on_reply_success(
966966
env: &mut JNIEnv,
967-
replier_id: Option<ZenohId>,
967+
replier_id: Option<EntityGlobalId>,
968968
sample: &Sample,
969969
callback_global_ref: &GlobalRef,
970970
) -> ZResult<()> {
971971
let zenoh_id = replier_id
972972
.map_or_else(
973973
|| Ok(JByteArray::default()),
974974
|replier_id| {
975-
env.byte_array_from_slice(&replier_id.to_le_bytes())
975+
env.byte_array_from_slice(&replier_id.zid().to_le_bytes())
976976
.map_err(|err| zerror!(err))
977977
},
978978
)
979979
.map(|value| env.auto_local(value))?;
980+
let eid = replier_id.map_or_else(|| 0, |replier_id| replier_id.eid() as jint);
980981

981982
let byte_array =
982983
bytes_to_java_array(env, sample.payload()).map(|value| env.auto_local(value))?;
@@ -1022,9 +1023,10 @@ pub(crate) fn on_reply_success(
10221023
let result = match env.call_method(
10231024
callback_global_ref,
10241025
"run",
1025-
"([BZLjava/lang/String;[BILjava/lang/String;IJZ[BZII)V",
1026+
"([BIZLjava/lang/String;[BILjava/lang/String;IJZ[BZII)V",
10261027
&[
10271028
JValue::from(&zenoh_id),
1029+
JValue::from(eid),
10281030
JValue::from(true),
10291031
JValue::from(&key_expr_str),
10301032
JValue::from(&byte_array),
@@ -1050,19 +1052,20 @@ pub(crate) fn on_reply_success(
10501052

10511053
pub(crate) fn on_reply_error(
10521054
env: &mut JNIEnv,
1053-
replier_id: Option<ZenohId>,
1055+
replier_id: Option<EntityGlobalId>,
10541056
reply_error: &ReplyError,
10551057
callback_global_ref: &GlobalRef,
10561058
) -> ZResult<()> {
10571059
let zenoh_id = replier_id
10581060
.map_or_else(
10591061
|| Ok(JByteArray::default()),
10601062
|replier_id| {
1061-
env.byte_array_from_slice(&replier_id.to_le_bytes())
1063+
env.byte_array_from_slice(&replier_id.zid().to_le_bytes())
10621064
.map_err(|err| zerror!(err))
10631065
},
10641066
)
10651067
.map(|value| env.auto_local(value))?;
1068+
let eid = replier_id.map_or_else(|| 0, |replier_id| replier_id.eid() as jint);
10661069

10671070
let payload =
10681071
bytes_to_java_array(env, reply_error.payload()).map(|value| env.auto_local(value))?;
@@ -1078,9 +1081,10 @@ pub(crate) fn on_reply_error(
10781081
let result = match env.call_method(
10791082
callback_global_ref,
10801083
"run",
1081-
"([BZLjava/lang/String;[BILjava/lang/String;IJZ[BZII)V",
1084+
"([BIZLjava/lang/String;[BILjava/lang/String;IJZ[BZII)V",
10821085
&[
10831086
JValue::from(&zenoh_id),
1087+
JValue::from(eid),
10841088
JValue::from(false),
10851089
JValue::from(&JString::default()),
10861090
JValue::from(&payload),

0 commit comments

Comments
 (0)