Skip to content

Commit

Permalink
Merge pull request #139 from eclipse-zenoh/dev/1.0.0
Browse files Browse the repository at this point in the history
Merge dev/1.0.0 into main
  • Loading branch information
Mallets authored Aug 30, 2024
2 parents 427eeb1 + 41500f9 commit 0749bab
Show file tree
Hide file tree
Showing 56 changed files with 2,872 additions and 3,168 deletions.
5 changes: 2 additions & 3 deletions examples/src/main/java/io/zenoh/ZPubThr.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@

import io.zenoh.exceptions.ZenohException;
import io.zenoh.keyexpr.KeyExpr;
import io.zenoh.prelude.CongestionControl;
import io.zenoh.prelude.Encoding;
import io.zenoh.prelude.KnownEncoding;
import io.zenoh.publication.CongestionControl;
import io.zenoh.publication.Publisher;
import io.zenoh.value.Value;

Expand All @@ -30,7 +29,7 @@ public static void main(String[] args) throws ZenohException {
for (int i = 0; i < size; i++) {
data[i] = (byte) (i % 10);
}
Value value = new Value(data, new Encoding(KnownEncoding.EMPTY));
Value value = new Value(data, new Encoding(Encoding.ID.ZENOH_BYTES, null));
try (Session session = Session.open()) {
try (KeyExpr keyExpr = KeyExpr.tryFrom("test/thr")) {
try (Publisher publisher = session.declarePublisher(keyExpr).congestionControl(CongestionControl.BLOCK).res()) {
Expand Down
5 changes: 2 additions & 3 deletions examples/src/main/java/io/zenoh/ZPut.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import io.zenoh.exceptions.ZenohException;
import io.zenoh.keyexpr.KeyExpr;
import io.zenoh.prelude.SampleKind;
import io.zenoh.publication.CongestionControl;
import io.zenoh.publication.Priority;
import io.zenoh.prelude.CongestionControl;
import io.zenoh.prelude.Priority;

public class ZPut {
public static void main(String[] args) throws ZenohException {
Expand All @@ -29,7 +29,6 @@ public static void main(String[] args) throws ZenohException {
session.put(keyExpr, value)
.congestionControl(CongestionControl.BLOCK)
.priority(Priority.REALTIME)
.kind(SampleKind.PUT)
.res();
System.out.println("Putting Data ('" + keyExpr + "': '" + value + "')...");
}
Expand Down
2 changes: 1 addition & 1 deletion examples/src/main/java/io/zenoh/ZQueryable.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private static void handleRequests(BlockingQueue<Optional<Query>> receiver, KeyE
String valueInfo = query.getValue() != null ? " with value '" + query.getValue() + "'" : "";
System.out.println(">> [Queryable] Received Query '" + query.getSelector() + "'" + valueInfo);
try {
query.reply(keyExpr).success("Queryable from Java!").withKind(SampleKind.PUT).withTimeStamp(TimeStamp.getCurrentTime()).res();
query.reply(keyExpr).success("Queryable from Java!").timestamp(TimeStamp.getCurrentTime()).res();
} catch (Exception e) {
System.out.println(">> [Queryable] Error sending reply: " + e);
}
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[toolchain]
channel = "1.72.0"
channel = "1.75.0"
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.11.0-dev
1.0.0-dev
13 changes: 7 additions & 6 deletions zenoh-java/src/commonMain/kotlin/io/zenoh/Session.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ import io.zenoh.exceptions.ZenohException
import io.zenoh.handlers.Callback
import io.zenoh.jni.JNISession
import io.zenoh.keyexpr.KeyExpr
import io.zenoh.prelude.QoS
import io.zenoh.publication.Delete
import io.zenoh.publication.Publisher
import io.zenoh.publication.Put
import io.zenoh.query.*
import io.zenoh.queryable.Query
import io.zenoh.queryable.Queryable
import io.zenoh.sample.Attachment
import io.zenoh.sample.Sample
import io.zenoh.selector.Selector
import io.zenoh.subscriber.Reliability
Expand Down Expand Up @@ -104,6 +104,7 @@ class Session private constructor(private val config: Config) : AutoCloseable {
jniSession = null
}

@Suppress("removal")
protected fun finalize() {
jniSession?.close()
}
Expand Down Expand Up @@ -355,10 +356,10 @@ class Session private constructor(private val config: Config) : AutoCloseable {
}

@Throws(SessionException::class)
internal fun resolvePublisher(builder: Publisher.Builder): Publisher {
internal fun resolvePublisher(keyExpr: KeyExpr, qos: QoS): Publisher {
return jniSession?.run {
declarePublisher(builder)
} ?: throw (sessionClosedException)
declarePublisher(keyExpr, qos)
} ?: throw(sessionClosedException)
}

@Throws(ZenohException::class)
Expand Down Expand Up @@ -389,7 +390,7 @@ class Session private constructor(private val config: Config) : AutoCloseable {
target: QueryTarget,
consolidation: ConsolidationMode,
value: Value?,
attachment: Attachment?,
attachment: ByteArray?,
): R? {
if (jniSession == null) {
throw sessionClosedException
Expand All @@ -404,7 +405,7 @@ class Session private constructor(private val config: Config) : AutoCloseable {

@Throws(ZenohException::class)
internal fun resolveDelete(keyExpr: KeyExpr, delete: Delete) {
jniSession?.run { performPut(keyExpr, delete) }
jniSession?.run { performDelete(keyExpr, delete) }
}

/** Launches the session through the jni session, returning the [Session] on success. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ package io.zenoh.exceptions
* This type of exception is thrown from the native code when something goes wrong regarding the
* communication between the Java/Kotlin layer and the native layer through the JNI.
*/
class JNIException : ZenohException()
class JNIException(msg: String?) : ZenohException(msg)
68 changes: 23 additions & 45 deletions zenoh-java/src/commonMain/kotlin/io/zenoh/jni/JNIKeyExpr.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,68 +24,46 @@ internal class JNIKeyExpr(internal val ptr: Long) {
@Throws(ZenohException::class)
fun tryFrom(keyExpr: String): KeyExpr {
Zenoh.load() // It may happen the zenoh library is not yet loaded when creating a key expression.
val keyExprPtr = tryFromViaJNI(keyExpr)
return KeyExpr(JNIKeyExpr(keyExprPtr))
return KeyExpr(tryFromViaJNI(keyExpr))
}

@Throws(ZenohException::class)
fun autocanonize(keyExpr: String): KeyExpr {
Zenoh.load()
val keyExprPtr = autocanonizeViaJNI(keyExpr)
return KeyExpr(JNIKeyExpr(keyExprPtr))
return KeyExpr(autocanonizeViaJNI(keyExpr))
}

@Throws(ZenohException::class)
private external fun tryFromViaJNI(keyExpr: String): Long
fun intersects(keyExprA: KeyExpr, keyExprB: KeyExpr): Boolean = intersectsViaJNI(
keyExprA.jniKeyExpr?.ptr ?: 0,
keyExprA.keyExpr,
keyExprB.jniKeyExpr?.ptr ?: 0,
keyExprB.keyExpr
)

@Throws(ZenohException::class)
private external fun autocanonizeViaJNI(keyExpr: String): Long
}
fun includes(keyExprA: KeyExpr, keyExprB: KeyExpr): Boolean = includesViaJNI(
keyExprA.jniKeyExpr?.ptr ?: 0,
keyExprA.keyExpr,
keyExprB.jniKeyExpr?.ptr ?: 0,
keyExprB.keyExpr
)

override fun toString(): String {
return getStringValueViaJNI(ptr)
}
@Throws(Exception::class)
private external fun tryFromViaJNI(keyExpr: String): String

fun intersects(other: KeyExpr): Boolean {
if (other.jniKeyExpr == null) {
return false
}
return intersectsViaJNI(ptr, other.jniKeyExpr!!.ptr)
}
@Throws(Exception::class)
private external fun autocanonizeViaJNI(keyExpr: String): String

fun includes(other: KeyExpr): Boolean {
if (other.jniKeyExpr == null) {
return false
}
return includesViaJNI(ptr, other.jniKeyExpr!!.ptr)
@Throws(Exception::class)
private external fun intersectsViaJNI(ptrA: Long, keyExprA: String, ptrB: Long, keyExprB: String): Boolean

@Throws(Exception::class)
private external fun includesViaJNI(ptrA: Long, keyExprA: String, ptrB: Long, keyExprB: String): Boolean
}

fun close() {
freePtrViaJNI(ptr)
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as JNIKeyExpr

return equalsViaJNI(ptr, other.ptr)
}

override fun hashCode(): Int {
return ptr.hashCode()
}

private external fun equalsViaJNI(ptrA: Long, ptrB: Long): Boolean

private external fun intersectsViaJNI(ptrA: Long, ptrB: Long): Boolean

private external fun includesViaJNI(ptrA: Long, ptrB: Long): Boolean

@Throws(ZenohException::class)
private external fun getStringValueViaJNI(ptr: Long): String

/** Frees the underlying native KeyExpr. */
private external fun freePtrViaJNI(ptr: Long)
}
73 changes: 10 additions & 63 deletions zenoh-java/src/commonMain/kotlin/io/zenoh/jni/JNIPublisher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,35 @@

package io.zenoh.jni

import io.zenoh.*
import io.zenoh.exceptions.ZenohException
import io.zenoh.publication.CongestionControl
import io.zenoh.publication.Priority
import io.zenoh.sample.Attachment
import io.zenoh.value.Value

/**
* Adapter class to handle the interactions with Zenoh through JNI for a [Publisher].
* Adapter class to handle the interactions with Zenoh through JNI for a [io.zenoh.publication.Publisher].
*
* @property ptr: raw pointer to the underlying native Publisher.
*/
internal class JNIPublisher(private val ptr: Long) {

/**
* Put value through the publisher.
* Put operation.
*
* @param value The [Value] to be put.
* @param attachment Optional [Attachment].
* @param attachment Optional attachment.
*/
@Throws(ZenohException::class)
fun put(value: Value, attachment: Attachment?) {
putViaJNI(value.payload, value.encoding.knownEncoding.ordinal, attachment?.let { encodeAttachment(it) }, ptr)
fun put(value: Value, attachment: ByteArray?) {
putViaJNI(value.payload, value.encoding.id.ordinal, value.encoding.schema, attachment, ptr)
}

/**
* Delete operation.
*
* @param attachment Optional [Attachment].
* @param attachment Optional attachment.
*/
@Throws(ZenohException::class)
fun delete(attachment: Attachment?) {
deleteViaJNI(attachment?.let { encodeAttachment(it) }, ptr)
fun delete(attachment: ByteArray?) {
deleteViaJNI(attachment, ptr)
}

/**
Expand All @@ -58,63 +54,14 @@ internal class JNIPublisher(private val ptr: Long) {
freePtrViaJNI(ptr)
}

/**
* Set the congestion control policy of the publisher.
*
* This function is not thread safe.
*
* @param congestionControl: The [CongestionControl] policy.
*/
@Throws(ZenohException::class)
fun setCongestionControl(congestionControl: CongestionControl) {
setCongestionControlViaJNI(congestionControl.ordinal, ptr)
}

/**
* Set the priority policy of the publisher.
*
* This function is not thread safe.
*
* @param priority: The [Priority] policy.
*/
@Throws(ZenohException::class)
fun setPriority(priority: Priority) {
setPriorityViaJNI(priority.value, ptr)
}

/**
* Set the congestion control policy of the publisher through JNI.
*
* This function is NOT thread safe.
*
* @param congestionControl The congestion control policy.
* @param ptr Pointer to the publisher.
*/
@Throws(ZenohException::class)
private external fun setCongestionControlViaJNI(congestionControl: Int, ptr: Long)

/**
* Set the priority policy of the publisher through JNI.
*
* This function is NOT thread safe.
*
* @param priority The priority policy.
* @param ptr Pointer to the publisher.
*/
@Throws(ZenohException::class)
private external fun setPriorityViaJNI(priority: Int, ptr: Long)


/** Puts through the native Publisher. */
@Throws(ZenohException::class)
private external fun putViaJNI(
valuePayload: ByteArray, valueEncoding: Int, encodedAttachment: ByteArray?, ptr: Long
valuePayload: ByteArray, encodingId: Int, encodingSchema: String?, attachment: ByteArray?, ptr: Long
)

@Throws(ZenohException::class)
private external fun deleteViaJNI(encodedAttachment: ByteArray?, ptr: Long)
private external fun deleteViaJNI(attachment: ByteArray?, ptr: Long)

/** Frees the underlying native Publisher. */
private external fun freePtrViaJNI(ptr: Long)

}
Loading

0 comments on commit 0749bab

Please sign in to comment.