Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merging dev/1.0.0 into main #176

Closed
wants to merge 42 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
ae14a17
`dev/1.0.0` compatible API (#107)
DariusIMP Jun 12, 2024
45881e6
Removing setPriority and setCongestionControl from publisher + adding…
DariusIMP Jun 12, 2024
d890243
chore: Sync Rust toolchain
eclipse-zenoh-bot Jun 13, 2024
1ff8fc3
Merge pull request #113 from eclipse-zenoh/eclipse-zenoh-bot/sync-too…
eclipse-zenoh-bot Jun 13, 2024
bb8449c
fix(examples): fix examples after protocol changes updates + moving d…
DariusIMP Jun 17, 2024
a0f0469
Refactor/jni (#116)
DariusIMP Jul 5, 2024
7a83575
Cargo update
DariusIMP Jul 5, 2024
de20f32
Bumping version to 1.0.0-dev
DariusIMP Jul 5, 2024
62e53b8
Merge pull request #122 from ZettaScaleLabs/dev/1.0.0
kydos Jul 5, 2024
5cc63dd
Enabling snapshot publications for dev/1.0.0 (#125)
DariusIMP Jul 8, 2024
1b0b604
Using 'android' property to enable/disable Android configuration (#127)
DariusIMP Jul 9, 2024
d5695d0
fix(local publication): triggering compilation of zenoh jni when publ…
DariusIMP Jul 12, 2024
7274598
chore: Sync Rust toolchain
eclipse-zenoh-bot Jul 16, 2024
318e488
Merge pull request #131 from eclipse-zenoh/eclipse-zenoh-bot/sync-too…
eclipse-zenoh-bot Jul 16, 2024
e3b80cd
chore: Sync Rust toolchain
eclipse-zenoh-bot Jul 16, 2024
7c8110e
Merge pull request #132 from eclipse-zenoh/eclipse-zenoh-bot/sync-too…
eclipse-zenoh-bot Jul 16, 2024
2554448
Update actions (#130)
diogomatsubara Jul 16, 2024
427935d
fix: Update gradle actions to newer versions (#134) (#135)
diogomatsubara Jul 19, 2024
9c79835
Zenoh native lib loading refactor (#145)
DariusIMP Aug 1, 2024
19171df
Allowing session declarations to stay alive for the lifespan of a ses…
DariusIMP Aug 1, 2024
3a4907b
fix(ci publications): adding branch param to snapshot publication ste…
DariusIMP Aug 1, 2024
8c5a839
build: Sync with eclipse-zenoh/zenoh@5d09cf7 from 2024-08-01 (#150)
eclipse-zenoh-bot Aug 1, 2024
719c139
Bumping version to 1.0.0.alpha-dev
DariusIMP Aug 1, 2024
bc0e490
Merge pull request #151 from ZettaScaleLabs/dev/1.0.0.alpha
kydos Aug 1, 2024
3c0e17c
Revert "Bumping version to 1.0.0.alpha-dev"
DariusIMP Aug 1, 2024
c283f7c
Merge pull request #152 from ZettaScaleLabs/revert-151-dev/1.0.0.alpha
kydos Aug 1, 2024
2600933
Merge 'main' into 'dev/1.0.0' (#153)
DariusIMP Aug 5, 2024
03692f0
build: Sync with eclipse-zenoh/zenoh@b1e4dba from 2024-08-05 (#155)
eclipse-zenoh-bot Aug 6, 2024
ad9a4f7
build: Sync with eclipse-zenoh/zenoh@b7d42ef from 2024-08-06 (#156)
eclipse-zenoh-bot Aug 6, 2024
c0510ce
build: Sync with eclipse-zenoh/zenoh@2d5ab7c from 2024-08-06 (#157)
eclipse-zenoh-bot Aug 6, 2024
feefac9
Add Query `Consolidation::Auto` to JNI and Kotlin API (#158)
oteffahi Aug 6, 2024
6ec7988
Change default consolidation mode to AUTO (#159)
oteffahi Aug 7, 2024
15b574b
build: Sync with eclipse-zenoh/zenoh@0e2f78a from 2024-08-14 (#166)
eclipse-zenoh-bot Aug 14, 2024
aefc4c9
Optimizing KeyExpr processing (#161)
DariusIMP Aug 16, 2024
c6aff70
Api alignment (#146)
DariusIMP Aug 19, 2024
47e3ad0
Revert "Api alignment (#146)" (#168)
Mallets Aug 19, 2024
c0aa350
Api alignment (#169)
DariusIMP Aug 19, 2024
363e7c5
Bump dev version to `1.0.0-dev` (#165)
zettascale-bot Aug 20, 2024
6648c50
refactor(config): enabling config for JSON, JSON5 and YAML. (#170)
DariusIMP Aug 22, 2024
bdd08c6
feat(mandatory config): making config loading explicit when opening a…
DariusIMP Aug 27, 2024
ba9d2c4
build: Sync with eclipse-zenoh/zenoh@7f7d648 from 2024-08-28 (#175)
eclipse-zenoh-bot Aug 29, 2024
a06cbd3
Merge branch 'main' of github.com:eclipse-zenoh/zenoh-kotlin into dev…
DariusIMP Aug 29, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ dependencies {

tasks {
val examples = listOf(
"ZBytes",
"ZDelete",
"ZGet",
"ZPub",
Expand Down
13 changes: 4 additions & 9 deletions examples/src/main/kotlin/io.zenoh/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

package io.zenoh

import io.zenoh.sample.Attachment
import kotlinx.serialization.Serializable
import kotlinx.serialization.SerialName
import kotlinx.serialization.json.Json
Expand Down Expand Up @@ -57,10 +56,12 @@ internal fun loadConfig(
noMulticastScouting: Boolean,
mode: String?
): Config {
val config = if (emptyArgs) {
return if (emptyArgs) {
Config.default()
} else {
configFile?.let { Config.from(Path(it)) } ?: run {
configFile?.let {
Config.from(path = Path(it))
} ?: run {
val connect = Connect(connectEndpoints)
val listen = Listen(listenEndpoints)
val scouting = Scouting(Multicast(!noMulticastScouting))
Expand All @@ -69,10 +70,4 @@ internal fun loadConfig(
Config.from(jsonConfig)
}
}
return config
}

internal fun decodeAttachment(attachment: String): Attachment {
val pairs = attachment.split("&").map { it.split("=").let { (k, v) -> k.toByteArray() to v.toByteArray() } }
return Attachment.Builder().addAll(pairs).res()
}
237 changes: 237 additions & 0 deletions examples/src/main/kotlin/io.zenoh/ZBytes.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
package io.zenoh

import io.zenoh.protocol.*
import java.nio.ByteBuffer
import java.nio.ByteOrder
import kotlin.reflect.typeOf

fun main() {

/***********************************************
* Standard serialization and deserialization. *
***********************************************/

/** Numeric: byte, short, int, float, double */
val intInput = 1234
var payload = ZBytes.from(intInput)
var intOutput = payload.deserialize<Int>().getOrThrow()
check(intInput == intOutput)

// Alternatively you can serialize into the type.
payload = ZBytes.serialize(intInput).getOrThrow()
intOutput = payload.deserialize<Int>().getOrThrow()
check(intInput == intOutput)

// Alternatively, `Numeric.into()`: ZBytes can be used
payload = intInput.into()
intOutput = payload.deserialize<Int>().getOrThrow()
check(intInput == intOutput)

// Another example with float
val floatInput = 3.1415f
payload = ZBytes.from(floatInput)
val floatOutput = payload.deserialize<Float>().getOrThrow()
check(floatInput == floatOutput)

/** String serialization and deserialization. */
val stringInput = "example"
payload = ZBytes.from(stringInput)
// Alternatively, you can also call `String.into()` to convert
// a string into a ZBytes object:
// payload = stringInput.into()
var stringOutput = payload.deserialize<String>().getOrThrow()
check(stringInput == stringOutput)

// For the case of strings, ZBytes::toString() is equivalent:
stringOutput = payload.toString()
check(stringInput == stringOutput)

/** ByteArray serialization and deserialization. */
val byteArrayInput = "example".toByteArray()
payload = ZBytes.from(byteArrayInput) // Equivalent to `byteArrayInput.into()`
var byteArrayOutput = payload.deserialize<ByteArray>().getOrThrow()
check(byteArrayInput.contentEquals(byteArrayOutput))
// Alternatively, we can directly access the bytes of property of ZBytes:
byteArrayOutput = payload.toByteArray()
check(byteArrayInput.contentEquals(byteArrayOutput))

/** List serialization and deserialization.
*
* Supported types: String, ByteArray, ZBytes, Byte, Short, Int, Long, Float and Double.
*/
val inputList = listOf("sample1", "sample2", "sample3")
payload = ZBytes.serialize(inputList).getOrThrow()
val outputList = payload.deserialize<List<String>>().getOrThrow()
check(inputList == outputList)

val inputListZBytes = inputList.map { value -> value.into() }
payload = ZBytes.serialize(inputListZBytes).getOrThrow()
val outputListZBytes = payload.deserialize<List<ZBytes>>().getOrThrow()
check(inputListZBytes == outputListZBytes)

val inputListByteArray = inputList.map { value -> value.toByteArray() }
payload = ZBytes.serialize(inputListByteArray).getOrThrow()
val outputListByteArray = payload.deserialize<List<ByteArray>>().getOrThrow()
check(compareByteArrayLists(inputListByteArray, outputListByteArray))

/**
* Map serialization and deserialization.
*
* Maps with the following Type combinations are supported: String, ByteArray, ZBytes, Byte, Short, Int, Long, Float and Double.
*/
val inputMap = mapOf("key1" to "value1", "key2" to "value2", "key3" to "value3")
payload = ZBytes.serialize(inputMap).getOrThrow()
val outputMap = payload.deserialize<Map<String, String>>().getOrThrow()
check(inputMap == outputMap)

val combinedInputMap = mapOf("key1" to ZBytes.from("zbytes1"), "key2" to ZBytes.from("zbytes2"))
payload = ZBytes.serialize(combinedInputMap).getOrThrow()
val combinedOutputMap = payload.deserialize<Map<String, ZBytes>>().getOrThrow()
check(combinedInputMap == combinedOutputMap)

/*********************************************
* Custom serialization and deserialization. *
*********************************************/

/**
* The examples below use [MyZBytes], an example class consisting that implements the [Serializable] interface.
*
* In order for the serialization and deserialization to be successful on a custom class,
* the class itself must override the `into(): ZBytes` function, but also the companion
* object must implement the [Deserializable.From] interface.
*
* @see MyZBytes
*/
val inputMyZBytes = MyZBytes("example")
payload = ZBytes.serialize(inputMyZBytes).getOrThrow()
val outputMyZBytes = payload.deserialize<MyZBytes>().getOrThrow()
check(inputMyZBytes == outputMyZBytes)

/** List of MyZBytes. */
val inputListMyZBytes = inputList.map { value -> MyZBytes(value) }
payload = ZBytes.serialize<List<MyZBytes>>(inputListMyZBytes).getOrThrow()
val outputListMyZBytes = payload.deserialize<List<MyZBytes>>().getOrThrow()
check(inputListMyZBytes == outputListMyZBytes)

/** Map of MyZBytes. */
val inputMapMyZBytes = inputMap.map { (k, v) -> MyZBytes(k) to MyZBytes(v)}.toMap()
payload = ZBytes.serialize<Map<MyZBytes, MyZBytes>>(inputMapMyZBytes).getOrThrow()
val outputMapMyZBytes = payload.deserialize<Map<MyZBytes, MyZBytes>>().getOrThrow()
check(inputMapMyZBytes == outputMapMyZBytes)

val combinedMap = mapOf(MyZBytes("foo") to 1, MyZBytes("bar") to 2)
payload = ZBytes.serialize<Map<MyZBytes, Int>>(combinedMap).getOrThrow()
val combinedOutput = payload.deserialize<Map<MyZBytes, Int>>().getOrThrow()
check(combinedMap == combinedOutput)

/**
* Providing a map of deserializers.
*
* Alternatively, [ZBytes.deserialize] also accepts a deserializers parameter of type
* `Map<KType, KFunction1<ByteArray, Any>>`. That is, a map of types that is associated
* to a function receiving a ByteArray, that returns Any. This way, you can provide a series
* of deserializer functions that extend the deserialization mechanisms we provide by default.
*
* For example, let's say we have a custom map serializer, with its own deserializer:
*/
val fooMap = mapOf(Foo("foo1") to Foo("bar1"), Foo("foo2") to Foo("bar2"))
val fooMapSerialized = ZBytes.from(serializeFooMap(fooMap))
val deserializersMap = mapOf(typeOf<Map<Foo, Foo>>() to ::deserializeFooMap)
val deserializedFooMap = fooMapSerialized.deserialize<Map<Foo, Foo>>(deserializersMap).getOrThrow()
check(fooMap == deserializedFooMap)
}

class MyZBytes(val content: String) : Serializable, Deserializable {

override fun into(): ZBytes = content.into()

companion object : Deserializable.From {
override fun from(zbytes: ZBytes): MyZBytes {
return MyZBytes(zbytes.toString())
}
}

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

other as MyZBytes

return content == other.content
}

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

/** Example class for the deserialization map examples. */
class Foo(val content: String) {

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

other as Foo

return content == other.content
}

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

/** Example serializer and deserializer. */
private fun serializeFooMap(testMap: Map<Foo, Foo>): ByteArray {
return testMap.map {
val key = it.key.content.toByteArray()
val keyLength = ByteBuffer.allocate(Int.SIZE_BYTES).order(ByteOrder.LITTLE_ENDIAN).putInt(key.size).array()
val value = it.value.content.toByteArray()
val valueLength =
ByteBuffer.allocate(Int.SIZE_BYTES).order(ByteOrder.LITTLE_ENDIAN).putInt(value.size).array()
keyLength + key + valueLength + value
}.reduce { acc, bytes -> acc + bytes }
}

private fun deserializeFooMap(serializedMap: ZBytes): Map<Foo, Foo> {
var idx = 0
var sliceSize: Int
val bytes = serializedMap.toByteArray()
val decodedMap = mutableMapOf<Foo, Foo>()
while (idx < bytes.size) {
sliceSize = ByteBuffer.wrap(bytes.sliceArray(IntRange(idx, idx + Int.SIZE_BYTES - 1)))
.order(ByteOrder.LITTLE_ENDIAN).int
idx += Int.SIZE_BYTES

val key = bytes.sliceArray(IntRange(idx, idx + sliceSize - 1))
idx += sliceSize

sliceSize = ByteBuffer.wrap(bytes.sliceArray(IntRange(idx, idx + Int.SIZE_BYTES - 1))).order(
ByteOrder.LITTLE_ENDIAN
).int
idx += Int.SIZE_BYTES

val value = bytes.sliceArray(IntRange(idx, idx + sliceSize - 1))
idx += sliceSize

decodedMap[Foo(key.decodeToString())] = Foo(value.decodeToString())
}
return decodedMap
}

/** Utils for this example. */

private fun compareByteArrayLists(list1: List<ByteArray>, list2: List<ByteArray>): Boolean {
if (list1.size != list2.size) {
return false
}

for (i in list1.indices) {
if (!list1[i].contentEquals(list2[i])) {
return false
}
}

return true
}
29 changes: 13 additions & 16 deletions examples/src/main/kotlin/io.zenoh/ZDelete.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ import io.zenoh.keyexpr.intoKeyExpr
class ZDelete(private val emptyArgs: Boolean) : CliktCommand(
help = "Zenoh Delete example"
) {
override fun run() {
val config = loadConfig(emptyArgs, configFile, connect, listen, noMulticastScouting, mode)

println("Opening session...")
Session.open(config).onSuccess { session ->
session.use {
key.intoKeyExpr().onSuccess { keyExpr ->
println("Deleting resources matching '$keyExpr'...")
session.delete(keyExpr)
}
}
}
}

private val connect: List<String> by option(
"-e", "--connect", help = "Endpoints to connect to.", metavar = "connect"
Expand All @@ -41,22 +54,6 @@ class ZDelete(private val emptyArgs: Boolean) : CliktCommand(
private val noMulticastScouting: Boolean by option(
"--no-multicast-scouting", help = "Disable the multicast-based scouting mechanism."
).flag(default = false)

override fun run() {
val config = loadConfig(emptyArgs, configFile, connect, listen, noMulticastScouting,mode)

println("Opening session...")
Session.open(config).onSuccess { session ->
session.use {
key.intoKeyExpr().onSuccess { keyExpr ->
keyExpr.use {
println("Deleting resources matching '$keyExpr'...")
session.delete(keyExpr).res()
}
}
}
}
}
}

fun main(args: Array<String>) = ZDelete(args.isEmpty()).main(args)
Loading