Skip to content

Commit cb840a1

Browse files
committed
expand callbacks
1 parent f2f9b6f commit cb840a1

16 files changed

Lines changed: 82 additions & 154 deletions

File tree

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@ package io.zenoh.jni
33

44
import io.zenoh.jni.bytes.ZEncoding
55
import io.zenoh.jni.bytes.ZZBytes
6-
import io.zenoh.jni.callbacks.Callback
7-
import io.zenoh.jni.callbacks.ZHelloCallback
8-
import io.zenoh.jni.callbacks.ZQueryCallback
9-
import io.zenoh.jni.callbacks.ZReplyCallback
10-
import io.zenoh.jni.callbacks.ZSampleCallback
116
import io.zenoh.jni.config.WhatAmI
127
import io.zenoh.jni.config.ZConfig
138
import io.zenoh.jni.config.ZZenohId
@@ -111,13 +106,13 @@ internal object JNINative {
111106
external fun zKeyexprRelationTo(aSel: Int, a0: String?, a1: Long, bSel: Int, b0: String?, b1: Long, errorSink: Any): Int
112107
external fun zKeyexprToString(ke: Long, errorSink: Any): String
113108
external fun zKeyexprTryFrom(s: String, errorSink: Any): Long
114-
external fun zLivelinessDeclareSubscriber(session: Long, keyExprSel: Int, keyExpr0: String?, keyExpr1: Long, history: Boolean, callback: ZSampleCallback, onClose: Callback, errorSink: Any): Long
109+
external fun zLivelinessDeclareSubscriber(session: Long, keyExprSel: Int, keyExpr0: String?, keyExpr1: Long, history: Boolean, callback: Any, onClose: Any, errorSink: Any): Long
115110
external fun zLivelinessDeclareToken(session: Long, keyExprSel: Int, keyExpr0: String?, keyExpr1: Long, errorSink: Any): Long
116-
external fun zLivelinessGet(session: Long, keyExprSel: Int, keyExpr0: String?, keyExpr1: Long, timeoutMs: Long, callback: ZReplyCallback, onClose: Callback, errorSink: Any)
111+
external fun zLivelinessGet(session: Long, keyExprSel: Int, keyExpr0: String?, keyExpr1: Long, timeoutMs: Long, callback: Any, onClose: Any, errorSink: Any)
117112
external fun zOpen(config: Long, errorSink: Any): Long
118113
external fun zPublisherDelete(publisher: Long, attachment: ByteArray?, errorSink: Any)
119114
external fun zPublisherPut(publisher: Long, payload: ByteArray, encoding: String?, attachment: ByteArray?, errorSink: Any)
120-
external fun zQuerierGet(querier: Long, parameters: String?, payload: ByteArray?, encoding: String?, attachment: ByteArray?, callback: ZReplyCallback, onClose: Callback, errorSink: Any)
115+
external fun zQuerierGet(querier: Long, parameters: String?, payload: ByteArray?, encoding: String?, attachment: ByteArray?, callback: Any, onClose: Any, errorSink: Any)
121116
external fun zQueryAcceptsReplies(q: Long, errorSink: Any): Int
122117
external fun zQueryAttachment(q: Long, errorSink: Any): Long
123118
external fun zQueryEncoding(q: Long, errorSink: Any): Long
@@ -143,14 +138,14 @@ internal object JNINative {
143138
external fun zSamplePayload(s: Long, errorSink: Any): Long
144139
external fun zSamplePriority(s: Long, errorSink: Any): Int
145140
external fun zSampleTimestamp(s: Long, errorSink: Any): Long
146-
external fun zScout(whatami: Int, config: Long, callback: ZHelloCallback, onClose: Callback, errorSink: Any): Long
141+
external fun zScout(whatami: Int, config: Long, callback: Any, onClose: Any, errorSink: Any): Long
147142
external fun zSessionDeclareKeyexpr(session: Long, keyExpr: String, errorSink: Any): Long
148143
external fun zSessionDeclarePublisher(session: Long, keyExprSel: Int, keyExpr0: String?, keyExpr1: Long, congestionControl: Int?, priority: Int?, express: Boolean?, reliability: Int?, errorSink: Any): Long
149144
external fun zSessionDeclareQuerier(session: Long, keyExprSel: Int, keyExpr0: String?, keyExpr1: Long, target: Int?, consolidation: Int?, congestionControl: Int?, priority: Int?, express: Boolean?, timeoutMs: Long?, acceptReplies: Int?, errorSink: Any): Long
150-
external fun zSessionDeclareQueryable(session: Long, keyExprSel: Int, keyExpr0: String?, keyExpr1: Long, complete: Boolean?, callback: ZQueryCallback, onClose: Callback, errorSink: Any): Long
151-
external fun zSessionDeclareSubscriber(session: Long, keyExprSel: Int, keyExpr0: String?, keyExpr1: Long, callback: ZSampleCallback, onClose: Callback, errorSink: Any): Long
145+
external fun zSessionDeclareQueryable(session: Long, keyExprSel: Int, keyExpr0: String?, keyExpr1: Long, complete: Boolean?, callback: Any, onClose: Any, errorSink: Any): Long
146+
external fun zSessionDeclareSubscriber(session: Long, keyExprSel: Int, keyExpr0: String?, keyExpr1: Long, callback: Any, onClose: Any, errorSink: Any): Long
152147
external fun zSessionDelete(session: Long, keyExprSel: Int, keyExpr0: String?, keyExpr1: Long, congestionControl: Int?, priority: Int?, express: Boolean?, attachment: ByteArray?, reliability: Int?, errorSink: Any)
153-
external fun zSessionGet(session: Long, keyExprSel: Int, keyExpr0: String?, keyExpr1: Long, parameters: String?, timeoutMs: Long?, target: Int?, consolidation: Int?, acceptReplies: Int?, congestionControl: Int?, priority: Int?, express: Boolean?, payload: ByteArray?, encoding: String?, attachment: ByteArray?, callback: ZReplyCallback, onClose: Callback, errorSink: Any)
148+
external fun zSessionGet(session: Long, keyExprSel: Int, keyExpr0: String?, keyExpr1: Long, parameters: String?, timeoutMs: Long?, target: Int?, consolidation: Int?, acceptReplies: Int?, congestionControl: Int?, priority: Int?, express: Boolean?, payload: ByteArray?, encoding: String?, attachment: ByteArray?, callback: Any, onClose: Any, errorSink: Any)
154149
external fun zSessionPeersZid(session: Long, errorSink: Any): List<ByteArray>
155150
external fun zSessionPut(session: Long, keyExprSel: Int, keyExpr0: String?, keyExpr1: Long, payload: ByteArray, encoding: String?, congestionControl: Int?, priority: Int?, express: Boolean?, attachment: ByteArray?, reliability: Int?, errorSink: Any)
156151
external fun zSessionRoutersZid(session: Long, errorSink: Any): List<ByteArray>

zenoh-flat-jni/generated-kotlin/io/zenoh/jni/callbacks/Callback.kt

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

zenoh-flat-jni/generated-kotlin/io/zenoh/jni/callbacks/ZHelloCallback.kt

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

zenoh-flat-jni/generated-kotlin/io/zenoh/jni/callbacks/ZQueryCallback.kt

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

zenoh-flat-jni/generated-kotlin/io/zenoh/jni/callbacks/ZReplyCallback.kt

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

zenoh-flat-jni/generated-kotlin/io/zenoh/jni/callbacks/ZSampleCallback.kt

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

zenoh-flat-jni/generated-kotlin/io/zenoh/jni/query/JNIquery.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import io.zenoh.jni.NativeHandle
55
import io.zenoh.jni.ZException
66
import io.zenoh.jni.bytes.ZEncoding
77
import io.zenoh.jni.bytes.ZZBytes
8-
import io.zenoh.jni.callbacks.Callback
9-
import io.zenoh.jni.callbacks.ZReplyCallback
108
import io.zenoh.jni.config.ZZenohId
119
import io.zenoh.jni.keyexpr.ZKeyExpr
1210
import io.zenoh.jni.query.ReplyKeyExpr
@@ -16,7 +14,7 @@ import io.zenoh.jni.query.ZReply
1614
import io.zenoh.jni.withSortedHandleLocks
1715
import io.zenoh.jni.JNINative
1816

19-
public fun zQuerierGet(querier: ZQuerier, parameters: String?, payload: ByteArray?, encoding: String?, attachment: ByteArray?, callback: ZReplyCallback, onClose: Callback, onError: (String?, String) -> Unit = { __de_je, __de_z0 -> throw ZException(__de_je ?: __de_z0) }) {
17+
public fun zQuerierGet(querier: ZQuerier, parameters: String?, payload: ByteArray?, encoding: String?, attachment: ByteArray?, callback: (ZReply) -> Unit, onClose: () -> Unit, onError: (String?, String) -> Unit = { __de_je, __de_z0 -> throw ZException(__de_je ?: __de_z0) }) {
2018
if (querier.ptr == 0L) { onError("Operation on a closed native handle.", ""); return }
2119
var __cap_failed = false
2220
var __cap_je: String? = null

zenoh-flat-jni/generated-kotlin/io/zenoh/jni/scouting/JNIscouting.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package io.zenoh.jni.scouting
33

44
import io.zenoh.jni.NativeHandle
55
import io.zenoh.jni.ZException
6-
import io.zenoh.jni.callbacks.Callback
7-
import io.zenoh.jni.callbacks.ZHelloCallback
86
import io.zenoh.jni.config.WhatAmI
97
import io.zenoh.jni.config.ZConfig
108
import io.zenoh.jni.config.ZZenohId
@@ -52,7 +50,7 @@ public fun zHelloLocators(h: ZHello, onError: (String?) -> List<String> = { __de
5250
return __ret
5351
}
5452

55-
public fun zScout(whatami: Int, config: ZConfig?, callback: ZHelloCallback, onClose: Callback, onError: (String?, String) -> ZScout = { __de_je, __de_z0 -> throw ZException(__de_je ?: __de_z0) }): ZScout {
53+
public fun zScout(whatami: Int, config: ZConfig?, callback: (ZHello) -> Unit, onClose: () -> Unit, onError: (String?, String) -> ZScout = { __de_je, __de_z0 -> throw ZException(__de_je ?: __de_z0) }): ZScout {
5654
if (config != null && config.ptr == 0L) return onError("Operation on a closed native handle.", "")
5755
var __cap_failed = false
5856
var __cap_je: String? = null

zenoh-flat-jni/generated-kotlin/io/zenoh/jni/session/JNIsession.kt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ package io.zenoh.jni.session
33

44
import io.zenoh.jni.NativeHandle
55
import io.zenoh.jni.ZException
6-
import io.zenoh.jni.callbacks.Callback
7-
import io.zenoh.jni.callbacks.ZQueryCallback
8-
import io.zenoh.jni.callbacks.ZReplyCallback
9-
import io.zenoh.jni.callbacks.ZSampleCallback
106
import io.zenoh.jni.config.ZConfig
117
import io.zenoh.jni.config.ZZenohId
128
import io.zenoh.jni.keyexpr.ZKeyExpr
@@ -20,7 +16,9 @@ import io.zenoh.jni.query.ConsolidationMode
2016
import io.zenoh.jni.query.QueryTarget
2117
import io.zenoh.jni.query.ReplyKeyExpr
2218
import io.zenoh.jni.query.ZQuerier
19+
import io.zenoh.jni.query.ZQuery
2320
import io.zenoh.jni.query.ZQueryable
21+
import io.zenoh.jni.query.ZReply
2422
import io.zenoh.jni.session.ZSession
2523
import io.zenoh.jni.withSortedHandleLocks
2624
import io.zenoh.jni.JNINative
@@ -108,7 +106,7 @@ public fun zSessionDelete(session: ZSession, keyExprSel: Int, keyExpr0: String?,
108106
if (__cap_failed) return onError(__cap_je, (__cap_ze0 ?: ""))
109107
}
110108

111-
public fun zSessionDeclareSubscriber(session: ZSession, keyExprSel: Int, keyExpr0: String?, keyExpr1: ZKeyExpr?, callback: ZSampleCallback, onClose: Callback, onError: (String?, String) -> ZSubscriber = { __de_je, __de_z0 -> throw ZException(__de_je ?: __de_z0) }): ZSubscriber {
109+
public fun zSessionDeclareSubscriber(session: ZSession, keyExprSel: Int, keyExpr0: String?, keyExpr1: ZKeyExpr?, callback: (ZKeyExpr, String, ByteArray, String, Int, Long?, Boolean, Int, Int, ByteArray?) -> Unit, onClose: () -> Unit, onError: (String?, String) -> ZSubscriber = { __de_je, __de_z0 -> throw ZException(__de_je ?: __de_z0) }): ZSubscriber {
112110
if (session.ptr == 0L) return onError("Operation on a closed native handle.", "")
113111
if (keyExpr1 != null && keyExpr1.ptr == 0L) return onError("Operation on a closed native handle.", "")
114112
var __cap_failed = false
@@ -158,7 +156,7 @@ public fun zSessionDeclareQuerier(session: ZSession, keyExprSel: Int, keyExpr0:
158156
return __ret
159157
}
160158

161-
public fun zSessionDeclareQueryable(session: ZSession, keyExprSel: Int, keyExpr0: String?, keyExpr1: ZKeyExpr?, complete: Boolean?, callback: ZQueryCallback, onClose: Callback, onError: (String?, String) -> ZQueryable = { __de_je, __de_z0 -> throw ZException(__de_je ?: __de_z0) }): ZQueryable {
159+
public fun zSessionDeclareQueryable(session: ZSession, keyExprSel: Int, keyExpr0: String?, keyExpr1: ZKeyExpr?, complete: Boolean?, callback: (ZQuery) -> Unit, onClose: () -> Unit, onError: (String?, String) -> ZQueryable = { __de_je, __de_z0 -> throw ZException(__de_je ?: __de_z0) }): ZQueryable {
162160
if (session.ptr == 0L) return onError("Operation on a closed native handle.", "")
163161
if (keyExpr1 != null && keyExpr1.ptr == 0L) return onError("Operation on a closed native handle.", "")
164162
var __cap_failed = false
@@ -216,7 +214,7 @@ public fun zSessionUndeclareKeyexpr(session: ZSession, keyExpr: ZKeyExpr, onErro
216214
if (__cap_failed) return onError(__cap_je, (__cap_ze0 ?: ""))
217215
}
218216

219-
public fun zSessionGet(session: ZSession, keyExprSel: Int, keyExpr0: String?, keyExpr1: ZKeyExpr?, parameters: String?, timeoutMs: Long?, target: QueryTarget?, consolidation: ConsolidationMode?, acceptReplies: ReplyKeyExpr?, congestionControl: CongestionControl?, priority: Priority?, express: Boolean?, payload: ByteArray?, encoding: String?, attachment: ByteArray?, callback: ZReplyCallback, onClose: Callback, onError: (String?, String) -> Unit = { __de_je, __de_z0 -> throw ZException(__de_je ?: __de_z0) }) {
217+
public fun zSessionGet(session: ZSession, keyExprSel: Int, keyExpr0: String?, keyExpr1: ZKeyExpr?, parameters: String?, timeoutMs: Long?, target: QueryTarget?, consolidation: ConsolidationMode?, acceptReplies: ReplyKeyExpr?, congestionControl: CongestionControl?, priority: Priority?, express: Boolean?, payload: ByteArray?, encoding: String?, attachment: ByteArray?, callback: (ZReply) -> Unit, onClose: () -> Unit, onError: (String?, String) -> Unit = { __de_je, __de_z0 -> throw ZException(__de_je ?: __de_z0) }) {
220218
if (session.ptr == 0L) { onError("Operation on a closed native handle.", ""); return }
221219
if (keyExpr1 != null && keyExpr1.ptr == 0L) { onError("Operation on a closed native handle.", ""); return }
222220
var __cap_failed = false
@@ -300,7 +298,7 @@ public fun zLivelinessDeclareToken(session: ZSession, keyExprSel: Int, keyExpr0:
300298
return __ret
301299
}
302300

303-
public fun zLivelinessGet(session: ZSession, keyExprSel: Int, keyExpr0: String?, keyExpr1: ZKeyExpr?, timeoutMs: Long, callback: ZReplyCallback, onClose: Callback, onError: (String?, String) -> Unit = { __de_je, __de_z0 -> throw ZException(__de_je ?: __de_z0) }) {
301+
public fun zLivelinessGet(session: ZSession, keyExprSel: Int, keyExpr0: String?, keyExpr1: ZKeyExpr?, timeoutMs: Long, callback: (ZReply) -> Unit, onClose: () -> Unit, onError: (String?, String) -> Unit = { __de_je, __de_z0 -> throw ZException(__de_je ?: __de_z0) }) {
304302
if (session.ptr == 0L) { onError("Operation on a closed native handle.", ""); return }
305303
if (keyExpr1 != null && keyExpr1.ptr == 0L) { onError("Operation on a closed native handle.", ""); return }
306304
var __cap_failed = false
@@ -320,7 +318,7 @@ public fun zLivelinessGet(session: ZSession, keyExprSel: Int, keyExpr0: String?,
320318
if (__cap_failed) return onError(__cap_je, (__cap_ze0 ?: ""))
321319
}
322320

323-
public fun zLivelinessDeclareSubscriber(session: ZSession, keyExprSel: Int, keyExpr0: String?, keyExpr1: ZKeyExpr?, history: Boolean, callback: ZSampleCallback, onClose: Callback, onError: (String?, String) -> ZSubscriber = { __de_je, __de_z0 -> throw ZException(__de_je ?: __de_z0) }): ZSubscriber {
321+
public fun zLivelinessDeclareSubscriber(session: ZSession, keyExprSel: Int, keyExpr0: String?, keyExpr1: ZKeyExpr?, history: Boolean, callback: (ZKeyExpr, String, ByteArray, String, Int, Long?, Boolean, Int, Int, ByteArray?) -> Unit, onClose: () -> Unit, onError: (String?, String) -> ZSubscriber = { __de_je, __de_z0 -> throw ZException(__de_je ?: __de_z0) }): ZSubscriber {
324322
if (session.ptr == 0L) return onError("Operation on a closed native handle.", "")
325323
if (keyExpr1 != null && keyExpr1.ptr == 0L) return onError("Operation on a closed native handle.", "")
326324
var __cap_failed = false

zenoh-java/src/commonMain/kotlin/io/zenoh/FlatCallbacks.kt

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,40 +22,41 @@ import io.zenoh.sample.Sample
2222
import io.zenoh.scouting.Hello
2323

2424
/**
25-
* Adapters from the raw JNI callback fun-interfaces (each delivers a single
26-
* opaque native handle — `ZSample`/`ZQuery`/`ZReply`/`ZHello`) to a plain
27-
* `(SdkType) -> Unit`. The SDK object graph is reconstructed from the handle's
28-
* `z_*` accessors; the delivered handle is closed afterwards, except `ZQuery`,
29-
* which the resulting [Query] retains to reply through.
25+
* Adapters from the generated JNI callback lambdas to a plain
26+
* `(SdkType) -> Unit`. A callback argument whose type has a canonical output
27+
* is decomposed natively — `ZSample` arrives as its 10 leaves in ONE JNI
28+
* crossing (no transient handle, no per-field accessor calls) and the SDK
29+
* object graph is built from them via [Sample.fromParts]. Plan-less argument
30+
* types (`ZQuery`/`ZReply`/`ZHello`) still arrive as a whole opaque handle;
31+
* native closes it after the lambda returns (a no-op when the handle was
32+
* consumed — [Query]'s reply methods consume `zq`, so replying keeps working).
3033
*/
3134

32-
internal fun sampleCallbackOf(f: (Sample) -> Unit): io.zenoh.jni.callbacks.ZSampleCallback =
33-
io.zenoh.jni.callbacks.ZSampleCallback { zs ->
34-
try {
35-
f(Sample.from(zs))
36-
} finally {
37-
zs.close()
38-
}
35+
internal fun sampleCallbackOf(
36+
f: (Sample) -> Unit
37+
): (io.zenoh.jni.keyexpr.ZKeyExpr, String, ByteArray, String, Int, Long?, Boolean, Int, Int, ByteArray?) -> Unit =
38+
{ keH, keStr, payload, encStr, kindInt, ntp64, express, prioInt, ccInt, attach ->
39+
f(Sample.fromParts(keH, keStr, payload, encStr, kindInt, ntp64, express, prioInt, ccInt, attach))
3940
}
4041

41-
internal fun queryCallbackOf(f: (Query) -> Unit): io.zenoh.jni.callbacks.ZQueryCallback =
42-
io.zenoh.jni.callbacks.ZQueryCallback { zq ->
43-
// The [Query] retains `zq` (its reply methods consume it), so it is NOT
44-
// closed here.
42+
internal fun queryCallbackOf(f: (Query) -> Unit): (io.zenoh.jni.query.ZQuery) -> Unit =
43+
{ zq ->
44+
// The [Query] retains `zq` (its reply methods consume it); the native
45+
// post-invoke close is a no-op once a reply consumed the handle.
4546
f(Query.from(zq))
4647
}
4748

48-
internal fun replyCallbackOf(f: (Reply) -> Unit): io.zenoh.jni.callbacks.ZReplyCallback =
49-
io.zenoh.jni.callbacks.ZReplyCallback { zr ->
49+
internal fun replyCallbackOf(f: (Reply) -> Unit): (io.zenoh.jni.query.ZReply) -> Unit =
50+
{ zr ->
5051
try {
5152
f(Reply.from(zr))
5253
} finally {
5354
zr.close()
5455
}
5556
}
5657

57-
internal fun helloCallbackOf(f: (Hello) -> Unit): io.zenoh.jni.callbacks.ZHelloCallback =
58-
io.zenoh.jni.callbacks.ZHelloCallback { zh ->
58+
internal fun helloCallbackOf(f: (Hello) -> Unit): (io.zenoh.jni.scouting.ZHello) -> Unit =
59+
{ zh ->
5960
try {
6061
f(
6162
Hello(

0 commit comments

Comments
 (0)