From e581797da536870694689d8c3067121021b6e550 Mon Sep 17 00:00:00 2001 From: Darius Maitia Date: Thu, 19 Sep 2024 13:25:57 -0300 Subject: [PATCH] alignment(publisher): adding encoding argument to declarePublisher --- zenoh-kotlin/src/commonMain/kotlin/io/zenoh/Session.kt | 8 +++++--- .../src/commonMain/kotlin/io/zenoh/jni/JNISession.kt | 3 ++- .../commonMain/kotlin/io/zenoh/publication/Publisher.kt | 6 +++--- .../src/commonTest/kotlin/io/zenoh/PublisherTest.kt | 9 ++++++++- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/Session.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/Session.kt index dc69eb350..a196584aa 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/Session.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/Session.kt @@ -121,15 +121,17 @@ class Session private constructor(private val config: Config) : AutoCloseable { * * @param keyExpr The [KeyExpr] the publisher will be associated to. * @param qos The [QoS] configuration of the publisher. + * @param encoding The default [Encoding] for the publications. * @param reliability The [Reliability] the publisher wishes to obtain from the network. * @return The result of the declaration, returning the publisher in case of success. */ fun declarePublisher( keyExpr: KeyExpr, qos: QoS = QoS.default(), + encoding: Encoding = Encoding.default(), reliability: Reliability = Reliability.RELIABLE ): Result { - return resolvePublisher(keyExpr, qos, reliability) + return resolvePublisher(keyExpr, qos, encoding, reliability) } /** @@ -807,9 +809,9 @@ class Session private constructor(private val config: Config) : AutoCloseable { return SessionInfo(this) } - private fun resolvePublisher(keyExpr: KeyExpr, qos: QoS, reliability: Reliability): Result { + private fun resolvePublisher(keyExpr: KeyExpr, qos: QoS, encoding: Encoding, reliability: Reliability): Result { return jniSession?.run { - declarePublisher(keyExpr, qos, reliability).onSuccess { declarations.add(it) } + declarePublisher(keyExpr, qos, encoding, reliability).onSuccess { declarations.add(it) } } ?: Result.failure(sessionClosedException) } 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 f212b013b..01a86be95 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNISession.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNISession.kt @@ -63,7 +63,7 @@ internal class JNISession { closeSessionViaJNI(sessionPtr.get()) } - fun declarePublisher(keyExpr: KeyExpr, qos: QoS, reliability: Reliability): Result = runCatching { + fun declarePublisher(keyExpr: KeyExpr, qos: QoS, encoding: Encoding, reliability: Reliability): Result = runCatching { val publisherRawPtr = declarePublisherViaJNI( keyExpr.jniKeyExpr?.ptr ?: 0, keyExpr.keyExpr, @@ -76,6 +76,7 @@ internal class JNISession { Publisher( keyExpr, qos, + encoding, JNIPublisher(publisherRawPtr), ) } 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 043c6a191..4146e922f 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/publication/Publisher.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/publication/Publisher.kt @@ -65,6 +65,7 @@ import io.zenoh.protocol.into class Publisher internal constructor( val keyExpr: KeyExpr, val qos: QoS, + val encoding: Encoding, private var jniPublisher: JNIPublisher?, ) : SessionDeclaration, AutoCloseable { @@ -79,11 +80,10 @@ class Publisher internal constructor( fun priority() = qos.priority /** Performs a PUT operation on the specified [keyExpr] with the specified [payload]. */ - fun put(payload: IntoZBytes, encoding: Encoding? = null, attachment: IntoZBytes? = null) = jniPublisher?.put(payload, encoding, attachment) ?: InvalidPublisherResult - + fun put(payload: IntoZBytes, encoding: Encoding? = null, attachment: IntoZBytes? = null) = jniPublisher?.put(payload, encoding ?: this.encoding, attachment) ?: InvalidPublisherResult /** Performs a PUT operation on the specified [keyExpr] with the specified string [message]. */ - fun put(message: String, encoding: Encoding? = null, attachment: IntoZBytes? = null) = jniPublisher?.put(message.into(), encoding, attachment) ?: InvalidPublisherResult + fun put(message: String, encoding: Encoding? = null, attachment: IntoZBytes? = null) = jniPublisher?.put(message.into(), encoding ?: this.encoding, attachment) ?: InvalidPublisherResult /** * Performs a DELETE operation on the specified [keyExpr] diff --git a/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/PublisherTest.kt b/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/PublisherTest.kt index d7f8f7c79..d534edf88 100644 --- a/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/PublisherTest.kt +++ b/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/PublisherTest.kt @@ -36,7 +36,7 @@ class PublisherTest { fun setUp() { session = Session.open(Config.default()).getOrThrow() keyExpr = "example/testing/keyexpr".intoKeyExpr().getOrThrow() - publisher = session.declarePublisher(keyExpr).getOrThrow() + publisher = session.declarePublisher(keyExpr, encoding = Encoding.ZENOH_STRING).getOrThrow() subscriber = session.declareSubscriber(keyExpr, callback = { sample -> receivedSamples.add(sample) }).getOrThrow() @@ -75,4 +75,11 @@ class PublisherTest { assertEquals(1, receivedSamples.size) assertEquals(SampleKind.DELETE, receivedSamples[0].kind) } + + @Test + fun `when encoding is not provided a put should fallback to the publisher encoding`() { + publisher.put("Test") + assertEquals(1, receivedSamples.size) + assertEquals(Encoding.ZENOH_STRING, receivedSamples[0].encoding) + } }