Skip to content

Commit

Permalink
Sync with dev/1.0.0 from Zenoh-Kotlin (#93)
Browse files Browse the repository at this point in the history
* fix: cargo-deb install from +stable and --locked (#83)

* build: Sync  with eclipse-zenoh/zenoh@25f06bd from 2024-05-21 (#84)

Co-authored-by: eclipse-zenoh-bot <[email protected]>

* build: Sync  with eclipse-zenoh/zenoh@3118d31 from 2024-05-28 (#85)

Co-authored-by: eclipse-zenoh-bot <[email protected]>

* build: Sync  with eclipse-zenoh/zenoh@009f666 from 2024-05-30 (#86)

Co-authored-by: eclipse-zenoh-bot <[email protected]>

* build: Sync  with eclipse-zenoh/zenoh@d574654 from 2024-06-03 (#87)

Co-authored-by: eclipse-zenoh-bot <[email protected]>

* chore: Update artifacts action to v4 (#88)

artifacts actions v3 are deprecated

* build: Sync  with eclipse-zenoh/zenoh@c279982 from 2024-06-05 (#89)

Co-authored-by: eclipse-zenoh-bot <[email protected]>

* build: Sync  with eclipse-zenoh/zenoh@d8e66de from 2024-06-10 (#90)

Co-authored-by: eclipse-zenoh-bot <[email protected]>

* issue(1.0.0 upgrade): sync zenoh-jni with zenoh-kotlin repository's dev/1.0.0 branch

* issue(1.0.0 upgrade): sync with zenoh-kotlin dev/1.0.0 branch

* issue(1.0.0 upgrade): update examples

---------

Co-authored-by: Diogo Matsubara <[email protected]>
Co-authored-by: eclipse-zenoh-bot <[email protected]>
Co-authored-by: eclipse-zenoh-bot <[email protected]>
  • Loading branch information
4 people authored Jun 13, 2024
1 parent d7b2d3b commit a43f437
Show file tree
Hide file tree
Showing 49 changed files with 2,851 additions and 2,343 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/publish-jvm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ jobs:
shell: bash
run: |
case ${{ matrix.job.target }} in
*-linux-gnu*) cargo install cargo-deb ;;
*-linux-gnu*) cargo +stable install cargo-deb --locked;;
esac
case ${{ matrix.job.target }} in
Expand Down Expand Up @@ -120,7 +120,7 @@ jobs:
esac
- name: "Upload packages"
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.job.target }}
path: |
Expand All @@ -143,7 +143,7 @@ jobs:
run: mkdir ${{env.JNI_LIB_PATHS}}

- name: Download result of previous builds
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
path: ${{env.JNI_LIB_PATHS}}

Expand Down
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
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 a43f437

Please sign in to comment.