From 0604b8a7125c081b77a279a26e21e63ee2846fdc Mon Sep 17 00:00:00 2001 From: Darius Maitia Date: Thu, 29 Aug 2024 18:44:31 +0200 Subject: [PATCH] Removing Value + Encoding modifications (#179) * Removing Value + refactor Encoding (WIP) * issue(encoding): removing ID enum * issue(encoding): updating encoding list --- examples/src/main/kotlin/io.zenoh/ZGet.kt | 5 +- examples/src/main/kotlin/io.zenoh/ZPubThr.kt | 7 +- examples/src/main/kotlin/io.zenoh/ZPut.kt | 2 +- .../src/main/kotlin/io.zenoh/ZQueryable.kt | 6 +- examples/src/main/kotlin/io.zenoh/ZSub.kt | 3 +- .../src/commonMain/kotlin/io/zenoh/Session.kt | 127 +++++--------- .../kotlin/io/zenoh/jni/JNIPublisher.kt | 10 +- .../kotlin/io/zenoh/jni/JNIQuery.kt | 12 +- .../kotlin/io/zenoh/jni/JNISession.kt | 28 +-- .../kotlin/io/zenoh/prelude/Encoding.kt | 164 ++++++++++-------- .../kotlin/io/zenoh/publication/Publisher.kt | 13 +- .../kotlin/io/zenoh/publication/Put.kt | 8 +- .../commonMain/kotlin/io/zenoh/query/Reply.kt | 15 +- .../kotlin/io/zenoh/queryable/Query.kt | 28 ++- .../kotlin/io/zenoh/sample/Sample.kt | 11 +- .../commonMain/kotlin/io/zenoh/value/Value.kt | 107 ------------ .../commonTest/kotlin/io/zenoh/ConfigTest.kt | 8 +- .../kotlin/io/zenoh/EncodingTest.kt | 79 ++++----- .../src/commonTest/kotlin/io/zenoh/GetTest.kt | 10 +- .../kotlin/io/zenoh/PublisherTest.kt | 17 +- .../src/commonTest/kotlin/io/zenoh/PutTest.kt | 9 +- .../kotlin/io/zenoh/QueryableTest.kt | 36 ++-- .../kotlin/io/zenoh/SubscriberTest.kt | 22 +-- .../kotlin/io/zenoh/UserAttachmentTest.kt | 13 +- 24 files changed, 312 insertions(+), 428 deletions(-) delete mode 100644 zenoh-kotlin/src/commonMain/kotlin/io/zenoh/value/Value.kt diff --git a/examples/src/main/kotlin/io.zenoh/ZGet.kt b/examples/src/main/kotlin/io.zenoh/ZGet.kt index 91c26f4f1..98b5b5296 100644 --- a/examples/src/main/kotlin/io.zenoh/ZGet.kt +++ b/examples/src/main/kotlin/io.zenoh/ZGet.kt @@ -21,7 +21,6 @@ import io.zenoh.protocol.into import io.zenoh.query.QueryTarget import io.zenoh.query.Reply import io.zenoh.selector.intoSelector -import io.zenoh.value.Value import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.runBlocking import java.time.Duration @@ -38,7 +37,7 @@ class ZGet(private val emptyArgs: Boolean) : CliktCommand( selector.intoSelector().onSuccess { selector -> session.get(selector, channel = Channel(), - value = payload?.let { Value(it) }, + payload = payload?.into(), target = target?.let { QueryTarget.valueOf(it.uppercase()) } ?: QueryTarget.BEST_MATCHING, attachment = attachment?.into(), timeout = Duration.ofMillis(timeout)) @@ -46,7 +45,7 @@ class ZGet(private val emptyArgs: Boolean) : CliktCommand( runBlocking { for (reply in channelReceiver) { when (reply) { - is Reply.Success -> println("Received ('${reply.sample.keyExpr}': '${reply.sample.value}')") + is Reply.Success -> println("Received ('${reply.sample.keyExpr}': '${reply.sample.payload}')") is Reply.Error -> println("Received (ERROR: '${reply.error}')") is Reply.Delete -> println("Received (DELETE '${reply.keyExpr}')") } diff --git a/examples/src/main/kotlin/io.zenoh/ZPubThr.kt b/examples/src/main/kotlin/io.zenoh/ZPubThr.kt index 81ef45956..9b1026709 100644 --- a/examples/src/main/kotlin/io.zenoh/ZPubThr.kt +++ b/examples/src/main/kotlin/io.zenoh/ZPubThr.kt @@ -23,10 +23,9 @@ import com.github.ajalt.clikt.parameters.types.int import com.github.ajalt.clikt.parameters.types.ulong import io.zenoh.keyexpr.intoKeyExpr import io.zenoh.prelude.CongestionControl -import io.zenoh.prelude.Encoding import io.zenoh.prelude.Priority import io.zenoh.prelude.QoS -import io.zenoh.value.Value +import io.zenoh.protocol.into class ZPubThr(private val emptyArgs: Boolean) : CliktCommand( help = "Zenoh Throughput example" @@ -37,7 +36,7 @@ class ZPubThr(private val emptyArgs: Boolean) : CliktCommand( for (i in 0.. keyExpr.use { - session.put(keyExpr, value, attachment = attachment?.into()) + session.put(keyExpr, value.into(), attachment = attachment?.into()) .onSuccess { println("Putting Data ('$keyExpr': '$value')...") } } } diff --git a/examples/src/main/kotlin/io.zenoh/ZQueryable.kt b/examples/src/main/kotlin/io.zenoh/ZQueryable.kt index aabd85192..cbcbb6f39 100644 --- a/examples/src/main/kotlin/io.zenoh/ZQueryable.kt +++ b/examples/src/main/kotlin/io.zenoh/ZQueryable.kt @@ -17,7 +17,7 @@ package io.zenoh import com.github.ajalt.clikt.core.CliktCommand import com.github.ajalt.clikt.parameters.options.* import io.zenoh.keyexpr.intoKeyExpr -import io.zenoh.value.Value +import io.zenoh.protocol.into import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.runBlocking import org.apache.commons.net.ntp.TimeStamp @@ -36,11 +36,11 @@ class ZQueryable(private val emptyArgs: Boolean) : CliktCommand( session.declareQueryable(keyExpr, Channel()).onSuccess { queryable -> runBlocking { for (query in queryable.receiver) { - val valueInfo = query.value?.let { value -> " with value '$value'" } ?: "" + val valueInfo = query.payload?.let { value -> " with value '$value'" } ?: "" println(">> [Queryable] Received Query '${query.selector}' $valueInfo") query.replySuccess( keyExpr, - value = Value(value), + payload = value.into(), timestamp = TimeStamp.getCurrentTime() ).onFailure { println(">> [Queryable ] Error sending reply: $it") } } diff --git a/examples/src/main/kotlin/io.zenoh/ZSub.kt b/examples/src/main/kotlin/io.zenoh/ZSub.kt index 5a7fd15d7..097a927b9 100644 --- a/examples/src/main/kotlin/io.zenoh/ZSub.kt +++ b/examples/src/main/kotlin/io.zenoh/ZSub.kt @@ -37,7 +37,7 @@ class ZSub(private val emptyArgs: Boolean) : CliktCommand( session.declareSubscriber(keyExpr, Channel()).onSuccess { subscriber -> runBlocking { for (sample in subscriber.receiver) { - println(">> [Subscriber] Received ${sample.kind} ('${sample.keyExpr}': '${sample.value}'" + "${ + println(">> [Subscriber] Received ${sample.kind} ('${sample.keyExpr}': '${sample.payload}'" + "${ sample.attachment?.let { ", with attachment: $it" } ?: "" @@ -73,4 +73,3 @@ class ZSub(private val emptyArgs: Boolean) : CliktCommand( } fun main(args: Array) = ZSub(args.isEmpty()).main(args) - diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/Session.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/Session.kt index 3852d4091..4899ccd92 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/Session.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/Session.kt @@ -20,6 +20,7 @@ import io.zenoh.handlers.ChannelHandler import io.zenoh.handlers.Handler import io.zenoh.jni.JNISession import io.zenoh.keyexpr.KeyExpr +import io.zenoh.prelude.Encoding import io.zenoh.prelude.QoS import io.zenoh.protocol.ZBytes import io.zenoh.publication.Delete @@ -32,7 +33,6 @@ import io.zenoh.sample.Sample import io.zenoh.selector.Selector import io.zenoh.subscriber.Reliability import io.zenoh.subscriber.Subscriber -import io.zenoh.value.Value import kotlinx.coroutines.channels.Channel import java.time.Duration @@ -256,7 +256,7 @@ class Session private constructor(private val config: Config) : AutoCloseable { * "demo/kotlin/greeting".intoKeyExpr().onSuccess { keyExpr -> * println("Declaring Queryable") * val queryable = session.declareQueryable(keyExpr, callback = { query -> - * query.replySuccess(keyExpr, value = Value("Hello!")) + * query.replySuccess(keyExpr, payload = "Hello!".into()) * .onSuccess { println("Replied hello.") } * .onFailure { println(it) } * }).getOrThrow() @@ -288,7 +288,7 @@ class Session private constructor(private val config: Config) : AutoCloseable { * * ```kotlin * class ExampleHandler: Handler { - * override fun handle(t: Query) = query.replySuccess(query.keyExpr, value = Value("Hello!")) + * override fun handle(t: Query) = query.replySuccess(query.keyExpr, "Hello!".into()) * * override fun receiver() = Unit * @@ -341,7 +341,7 @@ class Session private constructor(private val config: Config) : AutoCloseable { * for (query in queryable.receiver) { * val valueInfo = query.value?.let { value -> " with value '$value'" } ?: "" * println(">> [Queryable] Received Query '${query.selector}' $valueInfo") - * query.replySuccess(keyExpr, value = Value("Example reply")) + * query.replySuccess(keyExpr, payload = "Example reply".into()) * .onSuccess { println(">> [Queryable ] Performed reply...") } * .onFailure { println(">> [Queryable ] Error sending reply: $it") } * } @@ -450,7 +450,8 @@ class Session private constructor(private val config: Config) : AutoCloseable { * session.get( * selector, * callback = { reply -> println(reply) }, - * value = Value("Example value"), + * payload = "Example payload".into(), + * encoding = Encoding(TEXT_PLAIN), * target = QueryTarget.BEST_MATCHING, * attachment = ZBytes.from("Example attachment"), * timeout = Duration.ofMillis(1000), @@ -467,7 +468,8 @@ class Session private constructor(private val config: Config) : AutoCloseable { * * @param selector The [Selector] on top of which the get query will be performed. * @param callback [Callback] to handle the replies. - * @param value Optional [Value] for the query. + * @param payload Optional [ZBytes] payload for the query. + * @param encoding Encoding of the [payload]. * @param attachment Optional attachment. * @param target The [QueryTarget] of the query. * @param consolidation The [ConsolidationMode] configuration. @@ -479,14 +481,15 @@ class Session private constructor(private val config: Config) : AutoCloseable { fun get( selector: Selector, callback: Callback, - value: Value? = null, + payload: ZBytes? = null, + encoding: Encoding? = null, attachment: ZBytes? = null, timeout: Duration = Duration.ofMillis(10000), target: QueryTarget = QueryTarget.BEST_MATCHING, consolidation: ConsolidationMode = ConsolidationMode.NONE, onClose: (() -> Unit)? = null - ) : Result { - return resolveGet ( + ): Result { + return resolveGet( selector = selector, callback = callback, onClose = fun() { onClose?.invoke() }, @@ -494,7 +497,8 @@ class Session private constructor(private val config: Config) : AutoCloseable { timeout = timeout, target = target, consolidation = consolidation, - value = value, + payload = payload, + encoding = encoding, attachment = attachment ) } @@ -550,7 +554,8 @@ class Session private constructor(private val config: Config) : AutoCloseable { * session.get( * selector, * handler, - * value = Value("Example value"), + * payload = "Example payload".into(), + * encoding = Encoding.TEXT_PLAIN, * target = QueryTarget.BEST_MATCHING, * attachment = ZBytes.from("Example attachment"), * timeout = Duration.ofMillis(1000), @@ -571,7 +576,8 @@ class Session private constructor(private val config: Config) : AutoCloseable { * * @param selector The [Selector] on top of which the get query will be performed. * @param handler [Handler] to handle the replies. - * @param value Optional [Value] for the query. + * @param payload Optional [ZBytes] payload for the query. + * @param encoding Encoding of the [payload]. * @param attachment Optional attachment. * @param target The [QueryTarget] of the query. * @param consolidation The [ConsolidationMode] configuration. @@ -583,13 +589,14 @@ class Session private constructor(private val config: Config) : AutoCloseable { fun get( selector: Selector, handler: Handler, - value: Value? = null, + payload: ZBytes? = null, + encoding: Encoding? = null, attachment: ZBytes? = null, timeout: Duration = Duration.ofMillis(10000), target: QueryTarget = QueryTarget.BEST_MATCHING, consolidation: ConsolidationMode = ConsolidationMode.NONE, onClose: (() -> Unit)? = null - ) : Result { + ): Result { return resolveGet( selector = selector, callback = { r: Reply -> handler.handle(r) }, @@ -601,7 +608,8 @@ class Session private constructor(private val config: Config) : AutoCloseable { timeout = timeout, target = target, consolidation = consolidation, - value = value, + payload = payload, + encoding = encoding, attachment = attachment ) } @@ -639,7 +647,8 @@ class Session private constructor(private val config: Config) : AutoCloseable { * "a/b/c".intoSelector().onSuccess { selector -> * session.get(selector, * channel = Channel(), - * value = Value("Example value"), + * payload = "Example payload".into(), + * encoding = Encoding.TEXT_PLAIN, * target = QueryTarget.BEST_MATCHING, * attachment = ZBytes.from("Example attachment"), * timeout = Duration.ofMillis(1000), @@ -660,7 +669,8 @@ class Session private constructor(private val config: Config) : AutoCloseable { * * @param selector The [Selector] on top of which the get query will be performed. * @param channel Blocking [Channel] to handle the replies. - * @param value Optional [Value] for the query. + * @param payload Optional [ZBytes] payload for the query. + * @param encoding Encoding of the [payload]. * @param attachment Optional attachment. * @param target The [QueryTarget] of the query. * @param consolidation The [ConsolidationMode] configuration. @@ -672,27 +682,29 @@ class Session private constructor(private val config: Config) : AutoCloseable { fun get( selector: Selector, channel: Channel, - value: Value? = null, + payload: ZBytes? = null, + encoding: Encoding? = null, attachment: ZBytes? = null, timeout: Duration = Duration.ofMillis(10000), target: QueryTarget = QueryTarget.BEST_MATCHING, consolidation: ConsolidationMode = ConsolidationMode.NONE, onClose: (() -> Unit)? = null - ) : Result> { + ): Result> { val channelHandler = ChannelHandler(channel) return resolveGet( - selector = selector, + selector, callback = { r: Reply -> channelHandler.handle(r) }, onClose = fun() { channelHandler.onClose() onClose?.invoke() }, receiver = channelHandler.receiver(), - timeout = timeout, - target = target, - consolidation = consolidation, - value = value, - attachment = attachment + timeout, + target, + consolidation, + payload, + encoding, + attachment ) } @@ -704,54 +716,7 @@ class Session private constructor(private val config: Config) : AutoCloseable { * Session.open(config).onSuccess { session -> * session.use { * "a/b/c".intoKeyExpr().onSuccess { keyExpr -> - * session.put(keyExpr, value = Value("Example value")).getOrThrow() - * } - * // ... - * } - * } - * ``` - * - * Additionally, a [QoS] configuration can be specified as well as an attachment, for instance: - * ```kotlin - * Session.open(Config.default()).onSuccess { session -> - * session.use { - * "a/b/c".intoKeyExpr().onSuccess { keyExpr -> - * val exampleQoS = QoS( - * congestionControl = CongestionControl.DROP, - * express = true, - * priority = Priority.DATA_HIGH) - * val exampleAttachment = "exampleAttachment".into() - * session.put( - * keyExpr, - * value = Value("Example value"), - * qos = exampleQoS, - * attachment = exampleAttachment).getOrThrow() - * } - * // ... - * } - * } - * ``` - * - * @param keyExpr The [KeyExpr] to be used for the put operation. - * @param value The [Value] to be put. - * @param qos The [QoS] configuration. - * @param attachment Optional attachment. - * @return A [Result] with the status of the put operation. - */ - fun put(keyExpr: KeyExpr, value: Value, qos: QoS = QoS.default(), attachment: ZBytes? = null) : Result { - val put = Put(keyExpr, value, qos, attachment) - return resolvePut(keyExpr, put) - } - - /** - * Declare a [Put] with the provided message on the specified key expression. - * - * Example: - * ```kotlin - * Session.open(config).onSuccess { session -> - * session.use { - * "a/b/c".intoKeyExpr().onSuccess { keyExpr -> - * session.put(keyExpr, "Example message").getOrThrow() + * session.put(keyExpr, payload = "Example payload".into()).getOrThrow() * } * // ... * } @@ -770,7 +735,8 @@ class Session private constructor(private val config: Config) : AutoCloseable { * val exampleAttachment = "exampleAttachment".into() * session.put( * keyExpr, - * message = "Example message", + * payload = "Example payload".into(), + * encoding = Encoding.TEXT_PLAIN, * qos = exampleQoS, * attachment = exampleAttachment).getOrThrow() * } @@ -780,13 +746,13 @@ class Session private constructor(private val config: Config) : AutoCloseable { * ``` * * @param keyExpr The [KeyExpr] to be used for the put operation. - * @param message The [String] message to put. + * @param payload The [ZBytes] to be put. * @param qos The [QoS] configuration. * @param attachment Optional attachment. * @return A [Result] with the status of the put operation. */ - fun put(keyExpr: KeyExpr, message: String, qos: QoS = QoS.default(), attachment: ZBytes? = null) : Result { - val put = Put(keyExpr, Value(message), qos, attachment) + fun put(keyExpr: KeyExpr, payload: ZBytes, encoding: Encoding? = null, qos: QoS = QoS.default(), attachment: ZBytes? = null): Result { + val put = Put(keyExpr, payload, encoding ?: Encoding.default(), qos, attachment) return resolvePut(keyExpr, put) } @@ -858,7 +824,8 @@ class Session private constructor(private val config: Config) : AutoCloseable { timeout: Duration, target: QueryTarget, consolidation: ConsolidationMode, - value: Value?, + payload: ZBytes?, + encoding: Encoding?, attachment: ZBytes?, ): Result { return jniSession?.run { @@ -870,8 +837,8 @@ class Session private constructor(private val config: Config) : AutoCloseable { timeout, target, consolidation, - value?.payload, - value?.encoding, + payload, + encoding, attachment ) } ?: Result.failure(sessionClosedException) diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNIPublisher.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNIPublisher.kt index f1b57f971..0c7cd3273 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNIPublisher.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNIPublisher.kt @@ -14,8 +14,8 @@ package io.zenoh.jni +import io.zenoh.prelude.Encoding import io.zenoh.protocol.ZBytes -import io.zenoh.value.Value /** * Adapter class to handle the interactions with Zenoh through JNI for a [io.zenoh.publication.Publisher]. @@ -27,11 +27,13 @@ internal class JNIPublisher(private val ptr: Long) { /** * Put operation. * - * @param value The [Value] to be put. + * @param payload Payload of the put. + * @param encoding Encoding of the payload. * @param attachment Optional attachment. */ - fun put(value: Value, attachment: ZBytes?): Result = runCatching { - putViaJNI(value.payload.bytes, value.encoding.id.ordinal, value.encoding.schema, attachment?.bytes, ptr) + fun put(payload: ZBytes, encoding: Encoding?, attachment: ZBytes?): Result = runCatching { + val resolvedEncoding = encoding ?: Encoding.default() + putViaJNI(payload.bytes, resolvedEncoding.id, resolvedEncoding.schema, attachment?.bytes, ptr) } /** diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNIQuery.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNIQuery.kt index f881fbc1e..738da7fd6 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNIQuery.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNIQuery.kt @@ -15,10 +15,10 @@ package io.zenoh.jni import io.zenoh.keyexpr.KeyExpr +import io.zenoh.prelude.Encoding import io.zenoh.prelude.QoS import io.zenoh.protocol.ZBytes import io.zenoh.sample.Sample -import io.zenoh.value.Value import org.apache.commons.net.ntp.TimeStamp /** @@ -36,9 +36,9 @@ internal class JNIQuery(private val ptr: Long) { ptr, sample.keyExpr.jniKeyExpr?.ptr ?: 0, sample.keyExpr.keyExpr, - sample.value.payload.bytes, - sample.value.encoding.id.ordinal, - sample.value.encoding.schema, + sample.payload.bytes, + sample.encoding.id, + sample.encoding.schema, timestampEnabled, if (timestampEnabled) sample.timestamp!!.ntpValue() else 0, sample.attachment?.bytes, @@ -48,8 +48,8 @@ internal class JNIQuery(private val ptr: Long) { ) } - fun replyError(errorValue: Value): Result = runCatching { - replyErrorViaJNI(ptr, errorValue.payload.bytes, errorValue.encoding.id.ordinal, errorValue.encoding.schema) + fun replyError(error: ZBytes, encoding: Encoding): Result = runCatching { + replyErrorViaJNI(ptr, error.bytes, encoding.id, encoding.schema) } fun replyDelete(keyExpr: KeyExpr, timestamp: TimeStamp?, attachment: ZBytes?, qos: QoS): Result = diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNISession.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNISession.kt index 886b04b43..1af8d3c62 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNISession.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNISession.kt @@ -16,7 +16,6 @@ package io.zenoh.jni import io.zenoh.* import io.zenoh.prelude.Encoding -import io.zenoh.prelude.Encoding.ID import io.zenoh.exceptions.SessionException import io.zenoh.handlers.Callback import io.zenoh.jni.callbacks.JNIOnCloseCallback @@ -38,7 +37,6 @@ import io.zenoh.sample.Sample import io.zenoh.selector.Selector import io.zenoh.subscriber.Reliability import io.zenoh.subscriber.Subscriber -import io.zenoh.value.Value import org.apache.commons.net.ntp.TimeStamp import java.time.Duration import java.util.concurrent.atomic.AtomicLong @@ -97,7 +95,8 @@ internal class JNISession { val timestamp = if (timestampIsValid) TimeStamp(timestampNTP64) else null val sample = Sample( KeyExpr(keyExpr, null), - Value(payload, Encoding(ID.fromId(encodingId)!!, encodingSchema)), + payload.into(), + Encoding(encodingId, schema = encodingSchema), SampleKind.fromInt(kind), timestamp, QoS(CongestionControl.fromInt(congestionControl), Priority.fromInt(priority), express), @@ -123,8 +122,13 @@ internal class JNISession { } else { Selector(keyExpr2, selectorParams) } - val value = payload?.let { Value(it, Encoding(ID.fromId(encodingId)!!, encodingSchema)) } - val query = Query(keyExpr2, selector, value, attachmentBytes?.into(), jniQuery) + val query = Query( + keyExpr2, + selector, + payload?.into(), + payload?.let { Encoding(encodingId, schema = encodingSchema) }, + attachmentBytes?.into(), + jniQuery) callback.run(query) } val queryableRawPtr = declareQueryableViaJNI( @@ -167,7 +171,8 @@ internal class JNISession { SampleKind.PUT -> { val sample = Sample( KeyExpr(keyExpr!!, null), - Value(payload, Encoding(ID.fromId(encodingId)!!, encodingSchema)), + payload.into(), + Encoding(encodingId, schema = encodingSchema), SampleKind.fromInt(kind), timestamp, QoS(CongestionControl.fromInt(congestionControl), Priority.fromInt(priority), express), @@ -189,7 +194,8 @@ internal class JNISession { } else { reply = Reply.Error( replierId?.let { ZenohID(it) }, - Value(payload, Encoding(ID.fromId(encodingId)!!, encodingSchema)) + payload.into(), + Encoding(encodingId, schema = encodingSchema) ) } callback.run(reply) @@ -207,7 +213,7 @@ internal class JNISession { consolidation.ordinal, attachment?.bytes, payload?.bytes, - encoding?.id?.ordinal ?: ID.default().id, + encoding?.id ?: Encoding.default().id, encoding?.schema ) receiver @@ -234,9 +240,9 @@ internal class JNISession { keyExpr.jniKeyExpr?.ptr ?: 0, keyExpr.keyExpr, sessionPtr.get(), - put.value.payload.bytes, - put.value.encoding.id.ordinal, - put.value.encoding.schema, + put.payload.bytes, + put.encoding.id, + put.encoding.schema, put.qos.congestionControl.value, put.qos.priority.value, put.qos.express, diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/prelude/Encoding.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/prelude/Encoding.kt index 4fa635b70..424438fa0 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/prelude/Encoding.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/prelude/Encoding.kt @@ -24,81 +24,101 @@ package io.zenoh.prelude * * A set of associated constants are provided to cover the most common encodings for user convenience. * This is particularly useful in helping Zenoh to perform additional network optimizations. - * */ -data class Encoding(val id: ID, val schema: String? = null) { +class Encoding private constructor(val id: Int, val schema: String? = null, private val description: String? = null) { - /** - * The ID of the encoding. - * - * @property id The id of the encoding. - * @property encoding The encoding name. - */ - enum class ID(val id: Int, val encoding: String) { - ZENOH_BYTES(0, "zenoh/bytes"), - ZENOH_INT(1, "zenoh/int"), - ZENOH_UINT(2, "zenoh/uint"), - ZENOH_FLOAT(3, "zenoh/float"), - ZENOH_BOOL(4, "zenoh/bool"), - ZENOH_STRING(5, "zenoh/string"), - ZENOH_ERROR(6, "zenoh/error"), - APPLICATION_OCTET_STREAM(7, "application/octet-stream"), - TEXT_PLAIN(8, "text/plain"), - APPLICATION_JSON(9, "application/json"), - TEXT_JSON(10, "text/json"), - APPLICATION_CDR(11, "application/cdr"), - APPLICATION_CBOR(12, "application/cbor"), - APPLICATION_YAML(13, "application/yaml"), - TEXT_YAML(14, "text/yaml"), - TEXT_JSON5(15, "text/json5"), - APPLICATION_PYTHON_SERIALIZED_OBJECT(16, "application/python-serialized-object"), - APPLICATION_PROTOBUF(17, "application/protobuf"), - APPLICATION_JAVA_SERIALIZED_OBJECT(18, "application/java-serialized-object"), - APPLICATION_OPENMETRICS_TEXT(19, "application/openmetrics-text"), - IMAGE_PNG(20, "image/png"), - IMAGE_JPEG(21, "image/jpeg"), - IMAGE_GIF(22, "image/gif"), - IMAGE_BMP(23, "image/bmp"), - IMAGE_WEBP(24, "image/webp"), - APPLICATION_XML(25, "application/xml"), - APPLICATION_X_WWW_FORM_URLENCODED(26, "application/x-www-form-urlencoded"), - TEXT_HTML(27, "text/html"), - TEXT_XML(28, "text/xml"), - TEXT_CSS(29, "text/css"), - TEXT_JAVASCRIPT(30, "text/javascript"), - TEXT_MARKDOWN(31, "text/markdown"), - TEXT_CSV(32, "text/csv"), - APPLICATION_SQL(33, "application/sql"), - APPLICATION_COAP_PAYLOAD(34, "application/coap-payload"), - APPLICATION_JSON_PATCH_JSON(35, "application/json-patch+json"), - APPLICATION_JSON_SEQ(36, "application/json-seq"), - APPLICATION_JSONPATH(37, "application/jsonpath"), - APPLICATION_JWT(38, "application/jwt"), - APPLICATION_MP4(39, "application/mp4"), - APPLICATION_SOAP_XML(40, "application/soap+xml"), - APPLICATION_YANG(41, "application/yang"), - AUDIO_AAC(42, "audio/aac"), - AUDIO_FLAC(43, "audio/flac"), - AUDIO_MP4(44, "audio/mp4"), - AUDIO_OGG(45, "audio/ogg"), - AUDIO_VORBIS(46, "audio/vorbis"), - VIDEO_H261(47, "video/h261"), - VIDEO_H263(48, "video/h263"), - VIDEO_H264(49, "video/h264"), - VIDEO_H265(50, "video/h265"), - VIDEO_H266(51, "video/h266"), - VIDEO_MP4(52, "video/mp4"), - VIDEO_OGG(53, "video/ogg"), - VIDEO_RAW(54, "video/raw"), - VIDEO_VP8(55, "video/vp8"), - VIDEO_VP9(56, "video/vp9"); + constructor(id: Int, schema: String? = null) : this(id, schema, null) - companion object { - private val idToEnum = entries.associateBy(ID::id) - internal fun fromId(id: Int): ID? = idToEnum[id] - internal fun default() = ZENOH_BYTES - } + companion object { + val ZENOH_BYTES = Encoding(0, description = "zenoh/bytes") + val ZENOH_INT8 = Encoding(1, description = "zenoh/int8") + val ZENOH_INT16 = Encoding(2, description = "zenoh/int16") + val ZENOH_INT32 = Encoding(3, description = "zenoh/int32") + val ZENOH_INT64 = Encoding(4, description = "zenoh/int64") + val ZENOH_INT128 = Encoding(5, description = "zenoh/int128") + val ZENOH_UINT8 = Encoding(6, description = "zenoh/uint8") + val ZENOH_UINT16 = Encoding(7, description = "zenoh/uint16") + val ZENOH_UINT32 = Encoding(8, description = "zenoh/uint32") + val ZENOH_UINT64 = Encoding(9, description = "zenoh/uint64") + val ZENOH_UINT128 = Encoding(10, description = "zenoh/uint128") + val ZENOH_FLOAT32 = Encoding(11, description = "zenoh/float32") + val ZENOH_FLOAT64 = Encoding(12, description = "zenoh/float64") + val ZENOH_BOOL = Encoding(13, description = "zenoh/bool") + val ZENOH_STRING = Encoding(14, description = "zenoh/string") + val ZENOH_ERROR = Encoding(15, description = "zenoh/error") + val APPLICATION_OCTET_STREAM = Encoding(16, description = "application/octet-stream") + val TEXT_PLAIN = Encoding(17, description = "text/plain") + val APPLICATION_JSON = Encoding(18, description = "application/json") + val TEXT_JSON = Encoding(19, description = "text/json") + val APPLICATION_CDR = Encoding(20, description = "application/cdr") + val APPLICATION_CBOR = Encoding(21, description = "application/cbor") + val APPLICATION_YAML = Encoding(22, description = "application/yaml") + val TEXT_YAML = Encoding(23, description = "text/yaml") + val TEXT_JSON5 = Encoding(24, description = "text/json5") + val APPLICATION_PYTHON_SERIALIZED_OBJECT = Encoding(25, description = "application/python-serialized-object") + val APPLICATION_PROTOBUF = Encoding(26, description = "application/protobuf") + val APPLICATION_JAVA_SERIALIZED_OBJECT = Encoding(27, description = "application/java-serialized-object") + val APPLICATION_OPENMETRICS_TEXT = Encoding(28, description = "application/openmetrics-text") + val IMAGE_PNG = Encoding(29, description = "image/png") + val IMAGE_JPEG = Encoding(30, description = "image/jpeg") + val IMAGE_GIF = Encoding(31, description = "image/gif") + val IMAGE_BMP = Encoding(32, description = "image/bmp") + val IMAGE_WEBP = Encoding(33, description = "image/webp") + val APPLICATION_XML = Encoding(34, description = "application/xml") + val APPLICATION_X_WWW_FORM_URLENCODED = Encoding(35, description = "application/x-www-form-urlencoded") + val TEXT_HTML = Encoding(36, description = "text/html") + val TEXT_XML = Encoding(37, description = "text/xml") + val TEXT_CSS = Encoding(38, description = "text/css") + val TEXT_JAVASCRIPT = Encoding(39, description = "text/javascript") + val TEXT_MARKDOWN = Encoding(40, description = "text/markdown") + val TEXT_CSV = Encoding(41, description = "text/csv") + val APPLICATION_SQL = Encoding(42, description = "application/sql") + val APPLICATION_COAP_PAYLOAD = Encoding(43, description = "application/coap-payload") + val APPLICATION_JSON_PATCH_JSON = Encoding(44, description = "application/json-patch+json") + val APPLICATION_JSON_SEQ = Encoding(45, description = "application/json-seq") + val APPLICATION_JSONPATH = Encoding(46, description = "application/jsonpath") + val APPLICATION_JWT = Encoding(47, description = "application/jwt") + val APPLICATION_MP4 = Encoding(48, description = "application/mp4") + val APPLICATION_SOAP_XML = Encoding(49, description = "application/soap+xml") + val APPLICATION_YANG = Encoding(50, description = "application/yang") + val AUDIO_AAC = Encoding(51, description = "audio/aac") + val AUDIO_FLAC = Encoding(52, description = "audio/flac") + val AUDIO_MP4 = Encoding(53, description = "audio/mp4") + val AUDIO_OGG = Encoding(54, description = "audio/ogg") + val AUDIO_VORBIS = Encoding(55, description = "audio/vorbis") + val VIDEO_H261 = Encoding(56, description = "video/h261") + val VIDEO_H263 = Encoding(57, description = "video/h263") + val VIDEO_H264 = Encoding(58, description = "video/h264") + val VIDEO_H265 = Encoding(59, description = "video/h265") + val VIDEO_H266 = Encoding(60, description = "video/h266") + val VIDEO_MP4 = Encoding(61, description = "video/mp4") + val VIDEO_OGG = Encoding(62, description = "video/ogg") + val VIDEO_RAW = Encoding(63, description = "video/raw") + val VIDEO_VP8 = Encoding(64, description = "video/vp8") + val VIDEO_VP9 = Encoding(65, description = "video/vp9") + internal fun default() = ZENOH_BYTES + } + + fun withSchema(schema: String): Encoding { + return Encoding(this.id, schema, this.description) + } + + override fun toString(): String { + val base = description ?: "unknown(${this.id})" + val schemaInfo = schema?.let { ";$it" } ?: "" + return "$base$schemaInfo" } -} + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + other as Encoding + + return id == other.id && schema == other.schema + } + + override fun hashCode(): Int { + return id.hashCode() + } +} diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/publication/Publisher.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/publication/Publisher.kt index 024230338..af73963c0 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/publication/Publisher.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/publication/Publisher.kt @@ -18,11 +18,10 @@ import io.zenoh.* import io.zenoh.exceptions.SessionException import io.zenoh.jni.JNIPublisher import io.zenoh.keyexpr.KeyExpr -import io.zenoh.prelude.Priority -import io.zenoh.prelude.CongestionControl +import io.zenoh.prelude.Encoding import io.zenoh.prelude.QoS import io.zenoh.protocol.ZBytes -import io.zenoh.value.Value +import io.zenoh.protocol.into /** * # Publisher @@ -77,12 +76,12 @@ class Publisher internal constructor( val priority = qos.priority val express = qos.express - /** Performs a PUT operation on the specified [keyExpr] with the specified [value]. */ - fun put(value: Value, attachment: ZBytes? = null) = jniPublisher?.put(value, attachment) ?: InvalidPublisherResult + /** Performs a PUT operation on the specified [keyExpr] with the specified [payload]. */ + fun put(payload: ZBytes, encoding: Encoding? = null, attachment: ZBytes? = null) = jniPublisher?.put(payload, encoding, attachment) ?: InvalidPublisherResult - /** Performs a PUT operation on the specified [keyExpr] with the specified string [value]. */ - fun put(value: String, attachment: ZBytes? = null) = jniPublisher?.put(Value(value), attachment) ?: InvalidPublisherResult + /** Performs a PUT operation on the specified [keyExpr] with the specified string [message]. */ + fun put(message: String, encoding: Encoding? = null, attachment: ZBytes? = null) = jniPublisher?.put(message.into(), encoding, attachment) ?: InvalidPublisherResult /** * Performs a DELETE operation on the specified [keyExpr] diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/publication/Put.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/publication/Put.kt index 93c7340a7..c181c9697 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/publication/Put.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/publication/Put.kt @@ -15,21 +15,23 @@ package io.zenoh.publication import io.zenoh.keyexpr.KeyExpr +import io.zenoh.prelude.Encoding import io.zenoh.prelude.QoS import io.zenoh.protocol.ZBytes -import io.zenoh.value.Value /** * Put operation. * * @property keyExpr The [KeyExpr] to which the put operation will be performed. - * @property value The [Value] to put. + * @property payload The [ZBytes] to put. + * @property encoding The [Encoding] of the payload. * @property qos The [QoS] configuration. * @property attachment An optional user attachment. */ internal data class Put ( val keyExpr: KeyExpr, - val value: Value, + val payload: ZBytes, + val encoding: Encoding, val qos: QoS, val attachment: ZBytes? ) diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/query/Reply.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/query/Reply.kt index 00c39661d..6c644671f 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/query/Reply.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/query/Reply.kt @@ -16,8 +16,8 @@ package io.zenoh.query import io.zenoh.ZenohType import io.zenoh.sample.Sample -import io.zenoh.value.Value import io.zenoh.keyexpr.KeyExpr +import io.zenoh.prelude.Encoding import io.zenoh.prelude.QoS import io.zenoh.protocol.ZBytes import io.zenoh.protocol.ZenohID @@ -29,8 +29,7 @@ import org.apache.commons.net.ntp.TimeStamp * * A reply can be either successful ([Success]), an error ([Error]) or a delete request ([Delete]), both having different * information. - * For instance, the successful reply will contain a [Sample] while the error reply will only contain a [Value] - * with the error information. + * // TODO: fix comment and example after modifying the replies. * * Example: * ```kotlin @@ -44,7 +43,7 @@ import org.apache.commons.net.ntp.TimeStamp * println(">> [Queryable] Received Query '${query.selector}' $valueInfo") * query.replySuccess( * keyExpr, - * value = Value("Example value"), + * payload = "Example payload".into(), * timestamp = TimeStamp.getCurrentTime() * ).getOrThrow() * } @@ -79,10 +78,11 @@ sealed class Reply private constructor(open val replierId: ZenohID?) : ZenohType /** * An Error reply. * - * @property error: value with the error information.* - * @param replierId: unique ID identifying the replier. + * @property replierId Unique ID identifying the replier. + * @property error Error message. + * @property encoding [Encoding] of the error message. */ - data class Error internal constructor(override val replierId: ZenohID? = null, val error: Value) : Reply(replierId) { + data class Error internal constructor(override val replierId: ZenohID? = null, val error: ZBytes, val encoding: Encoding) : Reply(replierId) { override fun toString(): String { return "Error(error=$error)" @@ -111,4 +111,3 @@ sealed class Reply private constructor(open val replierId: ZenohID?) : ZenohType } } } - diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/queryable/Query.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/queryable/Query.kt index 28af57d7a..1b3e0d2c3 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/queryable/Query.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/queryable/Query.kt @@ -24,7 +24,6 @@ import io.zenoh.prelude.QoS import io.zenoh.prelude.SampleKind import io.zenoh.protocol.ZBytes import io.zenoh.sample.Sample -import io.zenoh.value.Value import org.apache.commons.net.ntp.TimeStamp /** @@ -34,7 +33,8 @@ import org.apache.commons.net.ntp.TimeStamp * * @property keyExpr The key expression to which the query is associated. * @property selector The selector - * @property value Optional value in case the received query was declared using "with query". + * @property payload Optional payload in case the received query was declared using "with query". + * @property encoding Encoding of the [payload]. * @property attachment Optional attachment. * @property jniQuery Delegate object in charge of communicating with the underlying native code. * @constructor Instances of Query objects are only meant to be created through the JNI upon receiving @@ -43,7 +43,8 @@ import org.apache.commons.net.ntp.TimeStamp class Query internal constructor( val keyExpr: KeyExpr, val selector: Selector, - val value: Value?, + val payload: ZBytes?, + val encoding: Encoding?, val attachment: ZBytes?, private var jniQuery: JNIQuery? ) : AutoCloseable, ZenohType { @@ -51,12 +52,6 @@ class Query internal constructor( /** Shortcut to the [selector]'s parameters. */ val parameters = selector.parameters - /** Payload of the query. */ - val payload: ZBytes? = value?.payload - - /** Encoding of the payload. */ - val encoding: Encoding? = value?.encoding - /** * Reply success to the remote [Query]. * @@ -65,19 +60,21 @@ class Query internal constructor( * * @param keyExpr Key expression to reply to. This parameter must not be necessarily the same * as the key expression from the Query, however it must intersect with the query key. - * @param value The [Value] with the reply information. + * @param payload The payload with the reply information. + * @param encoding Encoding of the payload. * @param qos The [QoS] for the reply. * @param timestamp Optional timestamp for the reply. * @param attachment Optional attachment for the reply. */ fun replySuccess( keyExpr: KeyExpr, - value: Value, + payload: ZBytes, + encoding: Encoding = Encoding.default(), qos: QoS = QoS.default(), timestamp: TimeStamp? = null, attachment: ZBytes? = null ): Result { - val sample = Sample(keyExpr, value, SampleKind.PUT, timestamp, qos, attachment) + val sample = Sample(keyExpr, payload, encoding, SampleKind.PUT, timestamp, qos, attachment) return jniQuery?.let { val result = it.replySuccess(sample) jniQuery = null @@ -91,11 +88,12 @@ class Query internal constructor( * A query can not be replied more than once. After the reply is performed, the query is considered * to be no more valid and further attempts to reply to it will fail. * - * @param error [Value] with the error information. + * @param error The error information. + * @param encoding The encoding of the [error]. */ - fun replyError(error: Value): Result { + fun replyError(error: ZBytes, encoding: Encoding = Encoding.default()): Result { return jniQuery?.let { - val result = it.replyError(error) + val result = it.replyError(error, encoding) jniQuery = null result } ?: Result.failure(SessionException("Query is invalid")) diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/sample/Sample.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/sample/Sample.kt index a69c098e3..9bf6fe1f1 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/sample/Sample.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/sample/Sample.kt @@ -18,18 +18,16 @@ import io.zenoh.ZenohType import io.zenoh.prelude.SampleKind import io.zenoh.prelude.QoS import io.zenoh.keyexpr.KeyExpr +import io.zenoh.prelude.Encoding import io.zenoh.protocol.ZBytes -import io.zenoh.value.Value import org.apache.commons.net.ntp.TimeStamp /** * Class representing a Zenoh Sample. * - * A sample consists of a [KeyExpr]-[Value] pair, annotated with the [SampleKind] (PUT or DELETE) of the publication - * used to emit it and a timestamp. - * * @property keyExpr The [KeyExpr] of the sample. - * @property value The [Value] of the sample. + * @property payload [ZBytes] with the payload of the sample. + * @property encoding [Encoding] of the payload. * @property kind The [SampleKind] of the sample. * @property timestamp Optional [TimeStamp]. * @property qos The Quality of Service settings used to deliver the sample. @@ -37,7 +35,8 @@ import org.apache.commons.net.ntp.TimeStamp */ data class Sample( val keyExpr: KeyExpr, - val value: Value, + val payload: ZBytes, + val encoding: Encoding, val kind: SampleKind, val timestamp: TimeStamp?, val qos: QoS, diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/value/Value.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/value/Value.kt deleted file mode 100644 index 0b2168dd1..000000000 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/value/Value.kt +++ /dev/null @@ -1,107 +0,0 @@ -// -// Copyright (c) 2023 ZettaScale Technology -// -// This program and the accompanying materials are made available under the -// terms of the Eclipse Public License 2.0 which is available at -// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 -// which is available at https://www.apache.org/licenses/LICENSE-2.0. -// -// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 -// -// Contributors: -// ZettaScale Zenoh Team, -// - -package io.zenoh.value - -import io.zenoh.prelude.Encoding -import io.zenoh.protocol.Serializable -import io.zenoh.protocol.ZBytes -import io.zenoh.protocol.into - -/** - * A Zenoh value. - * - * A Value is a pair of a binary payload, and a mime-type-like encoding string. - * - * @property payload The payload of this Value. - * @property encoding An encoding description indicating how the associated payload is encoded. - */ -class Value(val payload: ZBytes, val encoding: Encoding) { - - /** - * Constructs a value with the provided message, using [Encoding.ID.TEXT_PLAIN] for encoding. - */ - constructor(message: String): this(message.toByteArray().into(), Encoding(Encoding.ID.TEXT_PLAIN)) - - /** - * Constructs a value with the provided message and encoding. - */ - constructor(message: String, encoding: Encoding): this(message.toByteArray().into(), encoding) - - /** - * Constructs a value with the provided payload and encoding. - */ - constructor(payload: ByteArray, encoding: Encoding): this(payload.into(), encoding) - - /** - * Constructs a value with the provided payload and encoding. - */ - constructor(payload: Serializable, encoding: Encoding): this(payload.into(), encoding) - - /** - * Constructs a value with the provided message - * - * @param message The message for the value. - * @param encoding The [Encoding.ID] - * @param schema Optional [Encoding.schema] - */ - constructor(message: String, encoding: Encoding.ID, schema: String? = null): this(message.toByteArray().into(), Encoding(encoding, schema)) - - - /** - * Constructs a value with the provided [payload] - * - * @param payload The payload of the value. - * @param encoding The [Encoding.ID] - * @param schema Optional [Encoding.schema] - */ - constructor(payload: ByteArray, encoding: Encoding.ID, schema: String? = null): this(payload.into(), Encoding(encoding, schema)) - - /** - * Constructs a value with the provided [payload] - * - * @param payload The payload of the value. - * @param encoding The [Encoding.ID] - * @param schema Optional [Encoding.schema] - */ - constructor(payload: Serializable, encoding: Encoding.ID, schema: String? = null): this(payload.into(), Encoding(encoding, schema)) - - - companion object { - - /** Return an empty value. */ - fun empty(): Value { - return Value(ByteArray(0), Encoding(Encoding.ID.ZENOH_BYTES)) - } - } - - override fun toString(): String = payload.toString() - - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (javaClass != other?.javaClass) return false - - other as Value - - if (payload != other.payload) return false - - return encoding == other.encoding - } - - override fun hashCode(): Int { - var result = payload.bytes.hashCode() - result = 31 * result + encoding.hashCode() - return result - } -} diff --git a/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/ConfigTest.kt b/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/ConfigTest.kt index 07c91afad..d40591578 100644 --- a/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/ConfigTest.kt +++ b/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/ConfigTest.kt @@ -14,8 +14,8 @@ package io.zenoh import io.zenoh.keyexpr.intoKeyExpr +import io.zenoh.protocol.into import io.zenoh.sample.Sample -import io.zenoh.value.Value import kotlinx.coroutines.runBlocking import kotlinx.coroutines.delay import kotlinx.serialization.json.Json @@ -131,8 +131,8 @@ class ConfigTest { receivedSample = sample }).getOrThrow() - val value = Value("example message") - sessionClient.put(TEST_KEY_EXP, value).getOrThrow() + val payload = "example message".into() + sessionClient.put(TEST_KEY_EXP, payload).getOrThrow() delay(1000) @@ -141,7 +141,7 @@ class ConfigTest { sessionServer.close() assertNotNull(receivedSample) - assertEquals(receivedSample!!.value, value) + assertEquals(receivedSample!!.payload, payload) } } diff --git a/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/EncodingTest.kt b/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/EncodingTest.kt index 5a5deb10c..01d689ad6 100644 --- a/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/EncodingTest.kt +++ b/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/EncodingTest.kt @@ -2,10 +2,11 @@ package io.zenoh import io.zenoh.keyexpr.intoKeyExpr import io.zenoh.prelude.Encoding +import io.zenoh.protocol.ZBytes +import io.zenoh.protocol.into import io.zenoh.query.Reply import io.zenoh.sample.Sample import io.zenoh.selector.intoSelector -import io.zenoh.value.Value import kotlin.test.* class EncodingTest { @@ -20,23 +21,21 @@ class EncodingTest { val subscriber = session.declareSubscriber(keyExpr, callback = { sample -> receivedSample = sample }).getOrThrow() - var value = Value("test", Encoding(Encoding.ID.TEXT_CSV, "test_schema")) - session.put(keyExpr, value) + session.put(keyExpr, payload = "test".into(), encoding = Encoding.TEXT_CSV.withSchema("test_schema")) Thread.sleep(200) assertNotNull(receivedSample) - assertEquals(Encoding.ID.TEXT_CSV, receivedSample!!.value.encoding.id) - assertEquals("test_schema", receivedSample!!.value.encoding.schema) + assertEquals(Encoding.TEXT_CSV.id, receivedSample!!.encoding.id) + assertEquals("test_schema", receivedSample!!.encoding.schema) // Testing null schema receivedSample = null - value = Value("test2", Encoding(Encoding.ID.ZENOH_STRING, null)) - session.put(keyExpr, value) + session.put(keyExpr, payload = "test2".into(), encoding = Encoding.ZENOH_STRING) Thread.sleep(200) assertNotNull(receivedSample) - assertEquals(Encoding.ID.ZENOH_STRING, receivedSample!!.value.encoding.id) - assertNull(receivedSample!!.value.encoding.schema) + assertEquals(Encoding.ZENOH_STRING.id, receivedSample!!.encoding.id) + assertNull(receivedSample!!.encoding.schema) subscriber.close() session.close() @@ -49,13 +48,10 @@ class EncodingTest { val test1 = "example/testing/reply_success".intoSelector().getOrThrow() val test2 = "example/testing/reply_success_with_schema".intoSelector().getOrThrow() - val testValueA = Value("test", Encoding(Encoding.ID.TEXT_CSV, null)) - val testValueB = Value("test", Encoding(Encoding.ID.TEXT_CSV, "test_schema")) - val queryable = session.declareQueryable(keyExpr, callback = { query -> when (query.keyExpr) { - test1.keyExpr -> query.replySuccess(query.keyExpr, value = testValueA) - test2.keyExpr -> query.replySuccess(query.keyExpr, value = testValueB) + test1.keyExpr -> query.replySuccess(query.keyExpr, payload = "test".into(), encoding = Encoding.TEXT_CSV) + test2.keyExpr -> query.replySuccess(query.keyExpr, payload = "test".into(), encoding = Encoding.TEXT_CSV.withSchema("test_schema")) } }).getOrThrow() @@ -68,8 +64,8 @@ class EncodingTest { Thread.sleep(200) assertNotNull(receivedSample) - assertEquals(Encoding.ID.TEXT_CSV, receivedSample!!.value.encoding.id) - assertNull(receivedSample!!.value.encoding.schema) + assertEquals(Encoding.TEXT_CSV.id, receivedSample!!.encoding.id) + assertNull(receivedSample!!.encoding.schema) // Testing with non-null schema on a reply success scenario. receivedSample = null @@ -80,8 +76,8 @@ class EncodingTest { Thread.sleep(200) assertNotNull(receivedSample) - assertEquals(Encoding.ID.TEXT_CSV, receivedSample!!.value.encoding.id) - assertEquals("test_schema", receivedSample!!.value.encoding.schema) + assertEquals(Encoding.TEXT_CSV.id, receivedSample!!.encoding.id) + assertEquals("test_schema", receivedSample!!.encoding.schema) queryable.close() session.close() @@ -95,39 +91,42 @@ class EncodingTest { val test1 = "example/testing/reply_error".intoSelector().getOrThrow() val test2 = "example/testing/reply_error_with_schema".intoSelector().getOrThrow() - val testValueA = Value("test", Encoding(Encoding.ID.TEXT_CSV, null)) - val testValueB = Value("test", Encoding(Encoding.ID.TEXT_CSV, "test_schema")) - val queryable = session.declareQueryable(keyExpr, callback = { query -> when (query.keyExpr) { - test1.keyExpr -> query.replyError(testValueA) - test2.keyExpr -> query.replyError(testValueB) + test1.keyExpr -> query.replyError("test".into(), Encoding.TEXT_CSV) + test2.keyExpr -> query.replyError("test".into(), Encoding.TEXT_CSV.withSchema("test_schema")) } }).getOrThrow() // Testing with null schema on a reply error scenario. - var errorValue: Value? = null + var errorMessage: ZBytes? = null + var errorEncoding: Encoding? = null session.get(test1, callback = { reply -> assertTrue(reply is Reply.Error) - errorValue = reply.error + errorMessage = reply.error + errorEncoding = reply.encoding }).getOrThrow() Thread.sleep(200) - assertNotNull(errorValue) - assertEquals(Encoding.ID.TEXT_CSV, errorValue!!.encoding.id) - assertNull(errorValue!!.encoding.schema) + assertNotNull(errorMessage) + assertEquals(Encoding.TEXT_CSV.id, errorEncoding!!.id) + assertNull(errorEncoding!!.schema) + + Thread.sleep(200) // Testing with non-null schema on a reply error scenario. - errorValue = null + errorMessage = null + errorEncoding = null session.get(test2, callback = { reply -> assertTrue(reply is Reply.Error) - errorValue = reply.error + errorMessage = reply.error + errorEncoding = reply.encoding }).getOrThrow() Thread.sleep(200) - assertNotNull(errorValue) - assertEquals(Encoding.ID.TEXT_CSV, errorValue!!.encoding.id) - assertEquals("test_schema", errorValue!!.encoding.schema) + assertNotNull(errorMessage) + assertEquals(Encoding.TEXT_CSV.id, errorEncoding!!.id) + assertEquals("test_schema", errorEncoding!!.schema) queryable.close() session.close() @@ -137,8 +136,8 @@ class EncodingTest { fun encoding_queryTest() { val session = Session.open(Config.default()).getOrThrow() val selector = "example/testing/keyexpr".intoSelector().getOrThrow() - val encodingA = Encoding(Encoding.ID.TEXT_CSV, null) - val encodingB = Encoding(Encoding.ID.TEXT_CSV, "test_schema") + val encodingA = Encoding.TEXT_CSV + val encodingB = Encoding(123, "test_schema") var receivedEncoding: Encoding? = null val queryable = session.declareQueryable(selector.keyExpr, callback = { query -> @@ -147,20 +146,22 @@ class EncodingTest { }).getOrThrow() // Testing with null schema - session.get(selector, callback = {}, value = Value("test", encodingA)) + session.get(selector, callback = {}, payload = "test".into(), encoding = encodingA) Thread.sleep(200) assertNotNull(receivedEncoding) - assertEquals(Encoding.ID.TEXT_CSV, receivedEncoding!!.id) + assertEquals(Encoding.TEXT_CSV.id, receivedEncoding!!.id) assertNull(receivedEncoding!!.schema) + Thread.sleep(200) + // Testing non-null schema receivedEncoding = null - session.get(selector, callback = {}, value = Value("test", encodingB)) + session.get(selector, callback = {}, payload = "test".into(), encoding = encodingB) Thread.sleep(200) assertNotNull(receivedEncoding) - assertEquals(Encoding.ID.TEXT_CSV, receivedEncoding!!.id) + assertEquals(123, receivedEncoding!!.id) assertEquals("test_schema", receivedEncoding!!.schema) queryable.close() diff --git a/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/GetTest.kt b/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/GetTest.kt index 51f9f1e71..7542216aa 100644 --- a/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/GetTest.kt +++ b/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/GetTest.kt @@ -16,11 +16,11 @@ package io.zenoh import io.zenoh.handlers.Handler import io.zenoh.prelude.SampleKind +import io.zenoh.protocol.into import io.zenoh.query.Reply import io.zenoh.queryable.Queryable import io.zenoh.selector.Selector import io.zenoh.selector.intoSelector -import io.zenoh.value.Value import org.apache.commons.net.ntp.TimeStamp import java.time.Duration import java.util.* @@ -29,7 +29,7 @@ import kotlin.test.* class GetTest { companion object { - val value = Value("Test") + val payload = "Test".into() val timestamp = TimeStamp.getCurrentTime() val kind = SampleKind.PUT } @@ -43,7 +43,7 @@ class GetTest { session = Session.open(Config.default()).getOrThrow() selector = "example/testing/keyexpr".intoSelector().getOrThrow() queryable = session.declareQueryable(selector.keyExpr, callback = { query -> - query.replySuccess(query.keyExpr, value, timestamp = timestamp) + query.replySuccess(query.keyExpr, payload, timestamp = timestamp) }).getOrThrow() } @@ -63,7 +63,7 @@ class GetTest { assertTrue(reply is Reply.Success) val sample = (reply as Reply.Success).sample - assertEquals(value, sample.value) + assertEquals(payload, sample.payload) assertEquals(kind, sample.kind) assertEquals(selector.keyExpr, sample.keyExpr) assertEquals(timestamp, sample.timestamp) @@ -76,7 +76,7 @@ class GetTest { for (reply in receiver) { reply as Reply.Success val receivedSample = reply.sample - assertEquals(value, receivedSample.value) + assertEquals(payload, receivedSample.payload) assertEquals(SampleKind.PUT, receivedSample.kind) assertEquals(timestamp, receivedSample.timestamp) } diff --git a/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/PublisherTest.kt b/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/PublisherTest.kt index b28361d04..d7f8f7c79 100644 --- a/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/PublisherTest.kt +++ b/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/PublisherTest.kt @@ -18,10 +18,10 @@ import io.zenoh.keyexpr.KeyExpr import io.zenoh.prelude.Encoding import io.zenoh.keyexpr.intoKeyExpr import io.zenoh.prelude.SampleKind +import io.zenoh.protocol.into import io.zenoh.publication.Publisher import io.zenoh.sample.Sample import io.zenoh.subscriber.Subscriber -import io.zenoh.value.Value import kotlin.test.* class PublisherTest { @@ -54,17 +54,18 @@ class PublisherTest { @Test fun putTest() { - val testValues = arrayListOf( - Value("Test 1", Encoding(Encoding.ID.TEXT_PLAIN)), - Value("Test 2", Encoding(Encoding.ID.TEXT_JSON)), - Value("Test 3", Encoding(Encoding.ID.TEXT_CSV)) + val testPayloads = arrayListOf( + Pair("Test 1".into(), Encoding.TEXT_PLAIN), + Pair("Test 2".into(), Encoding.TEXT_JSON), + Pair("Test 3".into(), Encoding.TEXT_CSV), ) - testValues.forEach() { value -> publisher.put(value) } + testPayloads.forEach() { value -> publisher.put(value.first, encoding = value.second) } - assertEquals(receivedSamples.size, testValues.size) + assertEquals(receivedSamples.size, testPayloads.size) for ((index, sample) in receivedSamples.withIndex()) { - assertEquals(sample.value, testValues[index]) + assertEquals(sample.payload, testPayloads[index].first) + assertEquals(sample.encoding, testPayloads[index].second) } } diff --git a/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/PutTest.kt b/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/PutTest.kt index ef0cd05ed..8da359fe4 100644 --- a/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/PutTest.kt +++ b/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/PutTest.kt @@ -16,8 +16,8 @@ package io.zenoh import io.zenoh.keyexpr.intoKeyExpr import io.zenoh.prelude.Encoding +import io.zenoh.protocol.into import io.zenoh.sample.Sample -import io.zenoh.value.Value import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertNotNull @@ -26,7 +26,7 @@ class PutTest { companion object { const val TEST_KEY_EXP = "example/testing/keyexpr" - const val TEST_PAYLOAD = "Hello" + val TEST_PAYLOAD = "Hello".into() } @Test @@ -35,11 +35,10 @@ class PutTest { var receivedSample: Sample? = null val keyExpr = TEST_KEY_EXP.intoKeyExpr().getOrThrow() val subscriber = session.declareSubscriber(keyExpr, callback = { sample -> receivedSample = sample }).getOrThrow() - val value = Value(TEST_PAYLOAD, Encoding(Encoding.ID.TEXT_PLAIN)) - session.put(keyExpr, value) + session.put(keyExpr, TEST_PAYLOAD, encoding = Encoding.TEXT_PLAIN) subscriber.close() session.close() assertNotNull(receivedSample) - assertEquals(value, receivedSample!!.value) + assertEquals(TEST_PAYLOAD, receivedSample!!.payload) } } diff --git a/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/QueryableTest.kt b/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/QueryableTest.kt index 7f4e32354..5f6106c51 100644 --- a/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/QueryableTest.kt +++ b/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/QueryableTest.kt @@ -18,12 +18,10 @@ import io.zenoh.handlers.Handler import io.zenoh.keyexpr.KeyExpr import io.zenoh.keyexpr.intoKeyExpr import io.zenoh.prelude.* -import io.zenoh.prelude.Encoding.ID.ZENOH_STRING import io.zenoh.protocol.into import io.zenoh.query.Reply import io.zenoh.queryable.Query import io.zenoh.sample.Sample -import io.zenoh.value.Value import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.delay @@ -38,7 +36,7 @@ import kotlin.test.* class QueryableTest { companion object { - const val testPayload = "Hello queryable" + val testPayload = "Hello queryable".into() } private lateinit var session: Session @@ -61,13 +59,14 @@ class QueryableTest { fun queryable_runsWithCallback() = runBlocking { val sample = Sample( testKeyExpr, - Value(testPayload), + testPayload, + Encoding.default(), SampleKind.PUT, TimeStamp(Date.from(Instant.now())), QoS() ) val queryable = session.declareQueryable(testKeyExpr, callback = { query -> - query.replySuccess(testKeyExpr, value = sample.value, timestamp = sample.timestamp) + query.replySuccess(testKeyExpr, payload = sample.payload, timestamp = sample.timestamp) }).getOrThrow() var reply: Reply? = null @@ -116,14 +115,14 @@ class QueryableTest { assertNull(receivedQuery!!.attachment) receivedQuery = null - val payload = "Test value" + val payload = "Test value".into() val attachment = "Attachment".into() - session.get(testKeyExpr.intoSelector(), callback = {}, value = Value(payload, ZENOH_STRING), attachment = attachment) + session.get(testKeyExpr.intoSelector(), callback = {}, payload = payload, encoding = Encoding.ZENOH_STRING, attachment = attachment) delay(100) assertNotNull(receivedQuery) - assertEquals(payload, receivedQuery!!.payload!!.bytes.decodeToString()) - assertEquals(ZENOH_STRING, receivedQuery!!.encoding!!.id) + assertEquals(payload, receivedQuery!!.payload) + assertEquals(Encoding.ZENOH_STRING.id, receivedQuery!!.encoding!!.id) assertEquals(attachment, receivedQuery!!.attachment) queryable.close() @@ -131,14 +130,14 @@ class QueryableTest { @Test fun queryReplySuccessTest() { - val message = "Test message" + val message = "Test message".into() val timestamp = TimeStamp.getCurrentTime() val qos = QoS(priority = Priority.DATA_HIGH, express = true, congestionControl = CongestionControl.DROP) val priority = Priority.DATA_HIGH val express = true val congestionControl = CongestionControl.DROP val queryable = session.declareQueryable(testKeyExpr, callback = { query -> - query.replySuccess(testKeyExpr, value = Value(message), timestamp = timestamp, qos = qos) + query.replySuccess(testKeyExpr, payload = message, timestamp = timestamp, qos = qos) }).getOrThrow() var receivedReply: Reply? = null @@ -148,7 +147,7 @@ class QueryableTest { assertTrue(receivedReply is Reply.Success) val reply = receivedReply as Reply.Success - assertEquals(message, reply.sample.value.payload.bytes.decodeToString()) + assertEquals(message, reply.sample.payload) assertEquals(timestamp, reply.sample.timestamp) assertEquals(priority, reply.sample.qos.priority) assertEquals(express, reply.sample.qos.express) @@ -157,9 +156,9 @@ class QueryableTest { @Test fun queryReplyErrorTest() { - val message = "Error message" + val errorMessage = "Error message".into() val queryable = session.declareQueryable(testKeyExpr, callback = { query -> - query.replyError(error = Value(message)) + query.replyError(error = errorMessage) }).getOrThrow() var receivedReply: Reply? = null @@ -171,7 +170,7 @@ class QueryableTest { assertNotNull(receivedReply) assertTrue(receivedReply is Reply.Error) val reply = receivedReply as Reply.Error - assertEquals(message, reply.error.payload.bytes.decodeToString()) + assertEquals(errorMessage, reply.error) } @Test @@ -231,16 +230,17 @@ private class QueryHandler : Handler { override fun onClose() {} fun reply(query: Query) { - val payload = "Hello queryable $counter!" + val payload = "Hello queryable $counter!".into() counter++ val sample = Sample( query.keyExpr, - Value(payload), + payload, + Encoding.default(), SampleKind.PUT, TimeStamp(Date.from(Instant.now())), QoS() ) performedReplies.add(sample) - query.replySuccess(query.keyExpr, value = sample.value, timestamp = sample.timestamp) + query.replySuccess(query.keyExpr, payload = payload, timestamp = sample.timestamp) } } diff --git a/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/SubscriberTest.kt b/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/SubscriberTest.kt index 83ac61443..62f381fc4 100644 --- a/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/SubscriberTest.kt +++ b/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/SubscriberTest.kt @@ -19,10 +19,10 @@ import io.zenoh.keyexpr.KeyExpr import io.zenoh.keyexpr.intoKeyExpr import io.zenoh.prelude.Encoding import io.zenoh.sample.Sample -import io.zenoh.value.Value import io.zenoh.prelude.CongestionControl import io.zenoh.prelude.Priority import io.zenoh.prelude.QoS +import io.zenoh.protocol.into import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.runBlocking @@ -38,9 +38,9 @@ class SubscriberTest { val TEST_CONGESTION_CONTROL = CongestionControl.BLOCK val testValues = arrayListOf( - Value("Test 1", Encoding(Encoding.ID.TEXT_PLAIN)), - Value("Test 2", Encoding(Encoding.ID.TEXT_JSON)), - Value("Test 3", Encoding(Encoding.ID.TEXT_CSV)) + Pair("Test 1".into(), Encoding.TEXT_PLAIN), + Pair("Test 2".into(), Encoding.TEXT_JSON), + Pair("Test 3".into(), Encoding.TEXT_CSV), ) } @@ -66,12 +66,13 @@ class SubscriberTest { session.declareSubscriber(testKeyExpr, callback = { sample -> receivedSamples.add(sample)}).getOrThrow() testValues.forEach { value -> - session.put(testKeyExpr, value, qos = QoS(priority = TEST_PRIORITY, congestionControl = TEST_CONGESTION_CONTROL)) + session.put(testKeyExpr, value.first, encoding = value.second, qos = QoS(priority = TEST_PRIORITY, congestionControl = TEST_CONGESTION_CONTROL)) } assertEquals(receivedSamples.size, testValues.size) receivedSamples.zip(testValues).forEach { (sample, value) -> - assertEquals(sample.value, value) + assertEquals(sample.payload, value.first) + assertEquals(sample.encoding, value.second) assertEquals(sample.qos.priority, TEST_PRIORITY) assertEquals(sample.qos.congestionControl, TEST_CONGESTION_CONTROL) } @@ -85,12 +86,13 @@ class SubscriberTest { val subscriber = session.declareSubscriber(testKeyExpr, handler = handler).getOrThrow() testValues.forEach { value -> - session.put(testKeyExpr, value, qos = QoS(priority = TEST_PRIORITY, congestionControl = TEST_CONGESTION_CONTROL)) + session.put(testKeyExpr, value.first, encoding = value.second, qos = QoS(priority = TEST_PRIORITY, congestionControl = TEST_CONGESTION_CONTROL)) } assertEquals(handler.queue.size, testValues.size) handler.queue.zip(testValues).forEach { (sample, value) -> - assertEquals(sample.value, value) + assertEquals(sample.payload, value.first) + assertEquals(sample.encoding, value.second) assertEquals(sample.qos.priority, TEST_PRIORITY) assertEquals(sample.qos.congestionControl, TEST_CONGESTION_CONTROL) } @@ -106,13 +108,13 @@ class SubscriberTest { val receivedSamples = ArrayList() val subscriber = session.declareSubscriber(keyExpr, callback = { sample -> receivedSamples.add(sample) }).getOrThrow() - testValues.forEach { value -> session.put(testKeyExpr, value) } + testValues.forEach { value -> session.put(testKeyExpr, value.first) } subscriber.close() assertEquals(receivedSamples.size, testValues.size) for ((index, sample) in receivedSamples.withIndex()) { - assertEquals(sample.value, testValues[index]) + assertEquals(sample.payload, testValues[index].first) } } diff --git a/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/UserAttachmentTest.kt b/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/UserAttachmentTest.kt index dc527d9c3..180ac986b 100644 --- a/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/UserAttachmentTest.kt +++ b/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/UserAttachmentTest.kt @@ -16,11 +16,10 @@ package io.zenoh import io.zenoh.keyexpr.KeyExpr import io.zenoh.keyexpr.intoKeyExpr -import io.zenoh.prelude.Encoding import io.zenoh.protocol.ZBytes +import io.zenoh.protocol.into import io.zenoh.query.Reply import io.zenoh.sample.Sample -import io.zenoh.value.Value import java.time.Duration import kotlin.test.* @@ -30,7 +29,7 @@ class UserAttachmentTest { private lateinit var keyExpr: KeyExpr companion object { - val value = Value("test", Encoding(Encoding.ID.TEXT_PLAIN)) + val payload = "test".into() const val keyExprString = "example/testing/attachment" const val attachment = "mock_attachment" val attachmentZBytes = ZBytes.from(attachment) @@ -52,7 +51,7 @@ class UserAttachmentTest { fun putWithAttachmentTest() { var receivedSample: Sample? = null val subscriber = session.declareSubscriber(keyExpr, callback = { sample -> receivedSample = sample }).getOrThrow() - session.put(keyExpr, value, attachment = attachmentZBytes) + session.put(keyExpr, payload, attachment = attachmentZBytes) subscriber.close() @@ -135,7 +134,7 @@ class UserAttachmentTest { var receivedAttachment: ZBytes? = null val queryable = session.declareQueryable(keyExpr, callback = { query -> receivedAttachment = query.attachment - query.replySuccess(keyExpr, value = Value("test")) + query.replySuccess(keyExpr, payload) }).getOrThrow() session.get(keyExpr.intoSelector(), callback = {}, attachment = attachmentZBytes, timeout = Duration.ofMillis(1000)).getOrThrow() @@ -151,7 +150,7 @@ class UserAttachmentTest { fun queryReplyWithAttachmentTest() { var reply: Reply? = null val queryable = session.declareQueryable(keyExpr, callback = { query -> - query.replySuccess(keyExpr, value = Value("test"), attachment = attachmentZBytes) + query.replySuccess(keyExpr, payload, attachment = attachmentZBytes) }).getOrThrow() session.get(keyExpr.intoSelector(), callback = { @@ -172,7 +171,7 @@ class UserAttachmentTest { fun queryReplyWithoutAttachmentTest() { var reply: Reply? = null val queryable = session.declareQueryable(keyExpr, callback = { query -> - query.replySuccess(keyExpr, value = Value("test")) + query.replySuccess(keyExpr, payload) }).getOrThrow() session.get(keyExpr.intoSelector(), callback = {