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

Serialization feature #158

Open
wants to merge 85 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
85 commits
Select commit Hold shift + click to select a range
e275db2
Alignment: bytes package
DariusIMP Oct 21, 2024
bd94fe3
Alignment: encoding update
DariusIMP Oct 21, 2024
9b62e1e
Alignment: config package
DariusIMP Oct 21, 2024
e9ba08a
Alignment: replacing all exceptions with ZError
DariusIMP Oct 21, 2024
941efe8
Alignment: ext package
DariusIMP Oct 21, 2024
1ee563b
Alignment: jni callbacks
DariusIMP Oct 21, 2024
95a13e5
Alignment: config
DariusIMP Oct 21, 2024
7f372b9
Alignment: JNIKeyExpr
DariusIMP Oct 21, 2024
9cb1dcc
Alignment: JNIPublisher
DariusIMP Oct 21, 2024
4390040
Alignment: JNIQuery
DariusIMP Oct 21, 2024
c9e0416
Alignment: JNIScout
DariusIMP Oct 21, 2024
5b2194f
Alignment: JNISession
DariusIMP Oct 21, 2024
5ed9777
Alignment JNIZBytes & JNIZenohId
DariusIMP Oct 22, 2024
aee2546
Alignment: KeyExpr & SetIntersectionLevel
DariusIMP Oct 22, 2024
52300e5
Alignment: pubsub package
DariusIMP Oct 22, 2024
afe3766
Alignment: Publisher & qos package
DariusIMP Oct 22, 2024
c68cef3
Alignment: query package - wip
DariusIMP Oct 22, 2024
a643bca
Alignment: adding scouting package
DariusIMP Oct 22, 2024
1d2121c
Alignment: removing Value
DariusIMP Oct 22, 2024
377661a
Alignment: logger
DariusIMP Oct 23, 2024
5d3705c
Alignmment: Zenoh.kt - wip
DariusIMP Oct 23, 2024
ba0fd31
Alignment: publisher - adding encoding and reliability to builder
DariusIMP Oct 23, 2024
14844c6
Alignment: subscriber
DariusIMP Oct 23, 2024
383f86e
Alignment: sample
DariusIMP Oct 23, 2024
eb9eeb8
Alignment: query & reply - wip
DariusIMP Oct 25, 2024
1f9b13d
Alignment: Put & Session
DariusIMP Oct 28, 2024
283c1f3
Alignment: fix 'IntoSelector'
DariusIMP Oct 28, 2024
6dcfe69
Alignment: updating zenoh-jni
DariusIMP Oct 29, 2024
68b18cf
Alignment: wip - converting tests to java tests. Added config tests.
DariusIMP Nov 12, 2024
4869b07
Alignment: wip - converting tests to java tests. Added delete test.
DariusIMP Nov 12, 2024
f2d666d
Alignment: wip - converting tests to java tests. Added encoding tests.
DariusIMP Nov 13, 2024
8badd0c
Alignment: wip - converting tests to java tests. Added get tests.
DariusIMP Nov 13, 2024
5f1c920
Alignment: wip - converting tests to java tests. Added key expr tests.
DariusIMP Nov 13, 2024
ce8147a
Alignment: wip - converting tests to java tests. Added parameters tests.
DariusIMP Nov 13, 2024
faccac9
Alignment: wip - converting tests to java tests. Added publisher tests.
DariusIMP Nov 13, 2024
6b9d6e1
Alignment: wip - converting tests to java tests. Added Queryable tests.
DariusIMP Nov 13, 2024
163b953
Alignment: wip - converting tests to java tests. Added Put tests.
DariusIMP Nov 14, 2024
790a131
Alignment: wip - Added Scouting tests + adding scouting builder.
DariusIMP Nov 14, 2024
f94ee4b
Alignment: wrapping up scouting, closing queue upon scout close.
DariusIMP Nov 18, 2024
5426942
Alignment: adding selector tests
DariusIMP Nov 18, 2024
98db96e
Alignment: fix SessionInfo + adding tests
DariusIMP Nov 18, 2024
1fa7d9d
Alignment: fix Scouting queue test
DariusIMP Nov 18, 2024
9a859e1
amend! Alignment: fix SessionInfo + adding tests
DariusIMP Nov 18, 2024
384ba33
Alignment: session tests
DariusIMP Nov 18, 2024
2424498
Alignment: user attachment tests
DariusIMP Nov 20, 2024
025bcf4
Alignment: subscriber tests
DariusIMP Nov 20, 2024
f5565f2
Alignment: removing the zenoh-ext package (to be added later in anoth…
DariusIMP Nov 21, 2024
14a615e
Alignment: Publisher config params
DariusIMP Nov 22, 2024
5d53262
Alignment: Subscriber config params
DariusIMP Nov 22, 2024
a98180d
Alignment: Queryable config params
DariusIMP Nov 25, 2024
66f158f
Alignment: Subscriber config params refactor
DariusIMP Nov 25, 2024
d9eca7d
Alignment: Queryable declaration and Query.reply config params.
DariusIMP Nov 26, 2024
bebd45f
Alignment: Get config params
DariusIMP Nov 26, 2024
73fad03
Alignment: Subscriber config params refactor
DariusIMP Nov 26, 2024
9962353
Fix config test
DariusIMP Nov 27, 2024
6e6c86d
Alignment - Scouting config params
DariusIMP Nov 27, 2024
95f86aa
Alignment: adding Liveliness
DariusIMP Nov 28, 2024
8c59360
Gitignore update
DariusIMP Nov 28, 2024
6b35c68
Alignment: fix logging
DariusIMP Nov 28, 2024
b580619
Alignment: publisher put and delete config param
DariusIMP Nov 29, 2024
2cc4260
Alignment: examples - adding picocli for CLI args
DariusIMP Nov 29, 2024
931b980
Alignment: examples - adding missing examples
DariusIMP Nov 29, 2024
bbf20c2
Alignment: examples - adding ping and pong examples
DariusIMP Nov 29, 2024
da98b95
Alignment: examples refactor + refactor queryable config logic
DariusIMP Dec 2, 2024
e18807a
Alignment: fix publisher put encoding fallback
DariusIMP Dec 2, 2024
997108a
Alignment: removing SubscriberConfig.kt
DariusIMP Dec 4, 2024
4e1bc01
Alignment: renaming PublisherConfig to PublisherOptions
DariusIMP Dec 4, 2024
7affa19
Alignment: renaming DeleteConfig to DeleteOptions and removing 'build…
DariusIMP Dec 4, 2024
3b916ed
Alignment: renaming GetConfig to GetOptions and removing 'builder' fu…
DariusIMP Dec 4, 2024
4d9dfe8
Alignment: renaming PutConfig to PutOptions and removing 'builder' fu…
DariusIMP Dec 4, 2024
569438f
Alignment: renaming ReplyConfig to ReplyOptions and removing 'builder…
DariusIMP Dec 5, 2024
94bbab4
Alignment: renaming ReplyDelConfig to ReplyDelOptions and removing 'b…
DariusIMP Dec 5, 2024
07f22dc
Alignment: renaming ReplyErrConfig to ReplyErrOptions and removing 'b…
DariusIMP Dec 5, 2024
c5d92dd
Alignment: renaming QueryableConfig to QueryableOptions and removing …
DariusIMP Dec 5, 2024
752c453
Alignment: renaming ScoutConfig to ScoutOptions and removing 'builder…
DariusIMP Dec 5, 2024
0577e5b
Alignment: renaming Liveliness.SubscriberConfig + renaming variables
DariusIMP Dec 5, 2024
1e72dd6
Alignment: queryable options refactor
DariusIMP Dec 5, 2024
1dffad8
Alignment: removing Resolvable
DariusIMP Dec 5, 2024
e3cdd11
Alignment: splitting Queryable, Subscriber and Get into Handler and C…
DariusIMP Dec 10, 2024
795fbff
Alignment: removing JNIZBytes
DariusIMP Dec 11, 2024
2f74120
Alignment: removing unused kotlin json dependency
DariusIMP Dec 11, 2024
ec297d2
feat(serialization): implementation of the serialization
DariusIMP Dec 12, 2024
3f932ad
feat(serialization): implementation of the deserialization
DariusIMP Dec 13, 2024
e16d2b7
feat(serialization): adding ZBytes examples.
DariusIMP Dec 13, 2024
d2e2a3d
feat(serialization): cargo clippy
DariusIMP Dec 13, 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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@
/examples/build/
/build/
/.gradle/
*/build/
12 changes: 12 additions & 0 deletions examples/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

plugins {
kotlin("jvm")
kotlin("plugin.serialization") version "1.9.0"
}

kotlin {
Expand All @@ -23,17 +24,28 @@ kotlin {
dependencies {
implementation(project(":zenoh-java"))
implementation("commons-net:commons-net:3.9.0")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0")
implementation("info.picocli:picocli:4.7.4")
implementation("com.google.guava:guava:33.3.1-jre")
}

tasks {
val examples = listOf(
"ZBytesExamples",
"ZDelete",
"ZGet",
"ZGetLiveliness",
"ZInfo",
"ZLiveliness",
"ZPing",
"ZPong",
"ZPub",
"ZPubThr",
"ZPut",
"ZQueryable",
"ZScout",
"ZSub",
"ZSubLiveliness",
"ZSubThr"
)

Expand Down
74 changes: 74 additions & 0 deletions examples/src/main/java/io/zenoh/Config.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//
// 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, <[email protected]>
//

package io.zenoh

import kotlinx.serialization.Serializable
import kotlinx.serialization.SerialName
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.encodeToJsonElement
import kotlin.io.path.Path

@Serializable
data class ConfigData(
@SerialName("connect") var connect: Connect? = null,
@SerialName("listen") var listen: Listen? = null,
@SerialName("mode") var mode: String? = null,
@SerialName("scouting") var scouting: Scouting? = null,
)

@Serializable
data class Connect(
@SerialName("endpoints") var endpoints: List<String>
)

@Serializable
data class Listen(
@SerialName("endpoints") var endpoints: List<String>
)

@Serializable
data class Scouting(
@SerialName("multicast") var multicast: Multicast,
)

@Serializable
data class Multicast(
@SerialName("enabled") var enabled: Boolean,
)

internal fun loadConfig(
emptyArgs: Boolean,
configFile: String?,
connectEndpoints: List<String>?,
listenEndpoints: List<String>?,
noMulticastScouting: Boolean?,
mode: String?
): Config {
return if (emptyArgs) {
Config.loadDefault()
} else {
configFile?.let {
Config.fromFile(path = Path(it))
} ?: run {
val connect = connectEndpoints?.let {Connect(it)}
val listen = listenEndpoints?.let {Listen(it)}
val scouting = noMulticastScouting?.let { Scouting(Multicast(!it)) }
val configData = ConfigData(connect, listen, mode, scouting)
val jsonConfig = Json.encodeToJsonElement(configData).toString()
println(jsonConfig)
Config.fromJson(jsonConfig)
}
}
}
33 changes: 33 additions & 0 deletions examples/src/main/java/io/zenoh/QueueHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.zenoh;

import io.zenoh.handlers.Handler;

import java.util.ArrayDeque;

/**
* Sample handler for the sake of the examples.
*
* A custom handler can be implemented to handle incoming samples, queries or replies for
* subscribers, get operations, query operations or queryables.
*
* The example below shows a queue handler, in which an ArrayDeque is specified as the receiver type.
* The function handle will be called everytime an element of type T is received and in our example
* implementation, elements are simply enqueued into the queue, which can later be retrieved.
*/
class QueueHandler<T extends ZenohType> implements Handler<T, ArrayDeque<T>> {

final ArrayDeque<T> queue = new ArrayDeque<>();

@Override
public void handle(T t) {
queue.add(t);
}

@Override
public ArrayDeque<T> receiver() {
return queue;
}

@Override
public void onClose() {}
}
155 changes: 155 additions & 0 deletions examples/src/main/java/io/zenoh/ZBytesExamples.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
//
// 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, <[email protected]>
//

package io.zenoh;

import com.google.common.reflect.TypeToken;
import io.zenoh.bytes.ZBytes;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static io.zenoh.ext.ZDeserializerKt.zDeserialize;
import static io.zenoh.ext.ZSerializerKt.zSerialize;

public class ZBytesExamples {

public static void main(String[] args) {

/*
* ZBytes
*
* A ZBytes instance can be created from a String and from a Byte Array with the `ZBytes.from(string: String)`
* and `ZBytes.from(bytes: byte[])` functions.
*
* A ZBytes can be converted back into a [String] with the functions [ZBytes.toString] and [ZBytes.tryToString].
* Similarly, with [ZBytes.toBytes] you can get the inner byte representation.
*/

var exampleString = "example string";
var zbytesA = ZBytes.from(exampleString);
var outputA = zbytesA.toString();
assert exampleString.equals(outputA);

var exampleBytes = new byte[]{1, 2, 3, 4, 5};
var zbytesB = ZBytes.from(exampleBytes);
var outputB = zbytesB.toBytes();
assert Arrays.equals(exampleBytes, outputB);

/*
* Serialization and deserialization.
*
* Additionally, the Zenoh API provides a series of serialization and deserialization utilities for processing
* the received payloads.
*
* Serialization and deserialization supports the following types:
* - Boolean
* - Byte
* - Byte Array
* - Short
* - Int
* - Long
* - Float
* - Double
* - String
* - List
* - Map
*
* For List and Map, the inner types can be a combination of the above types, including themselves.
*
* These serialization and deserialization utilities can be used across the Zenoh ecosystem with Zenoh
* versions based on other supported languages such as Rust, Python, C and C++.
* This works when the types are equivalent (a `Byte` corresponds to an `i8` in Rust, a `Short` to an `i16`, etc).
*/

// Boolean example
Boolean input1 = true;
var zbytes1 = zSerialize(input1, new TypeToken<>() {});
Boolean output1 = zDeserialize(zbytes1, new TypeToken<>() {});
assert input1.equals(output1);

// Byte example
Byte input2 = 126;
var zbytes2 = zSerialize(input2, new TypeToken<>() {});
Byte output2 = zDeserialize(zbytes2, new TypeToken<>() {});
assert input2.equals(output2);

// Short example
Short input3 = 256;
var zbytes3 = zSerialize(input3, new TypeToken<>() {});
Short output3 = zDeserialize(zbytes3, new TypeToken<>() {});
assert input3.equals(output3);

// Int example
Integer input4 = 123456;
var zbytes4 = zSerialize(input4, new TypeToken<>() {});
Integer output4 = zDeserialize(zbytes4, new TypeToken<>() {});
assert input4.equals(output4);

// Long example
Long input5 = 123456789L;
var zbytes5 = zSerialize(input5, new TypeToken<>() {});
Long output5 = zDeserialize(zbytes5, new TypeToken<>() {});
assert input5.equals(output5);

// Float example
Float input6 = 123.45f;
var zbytes6 = zSerialize(input6, new TypeToken<>() {});
Float output6 = zDeserialize(zbytes6, new TypeToken<>() {});
assert input6.equals(output6);

// Double example
Double input7 = 12345.6789;
var zbytes7 = zSerialize(input7, new TypeToken<>() {});
Double output7 = zDeserialize(zbytes7, new TypeToken<>() {});
assert input7.equals(output7);

// List example
List<Integer> input12 = List.of(1, 2, 3, 4, 5);
var zbytes12 = zSerialize(input12, new TypeToken<>() {});
List<Integer> output12 = zDeserialize(zbytes12, new TypeToken<>() {});
assert input12.equals(output12);

// String example
String input13 = "Hello, World!";
var zbytes13 = zSerialize(input13, new TypeToken<>() {});
String output13 = zDeserialize(zbytes13, new TypeToken<>() {});
assert input13.equals(output13);

// ByteArray example
byte[] input14 = new byte[]{1, 2, 3, 4, 5};
var zbytes14 = zSerialize(input14, new TypeToken<>() {});
byte[] output14 = zDeserialize(zbytes14, new TypeToken<>() {});
assert Arrays.equals(input14, output14);

// Map example
Map<String, Integer> input15 = Map.of("one", 1, "two", 2, "three", 3);
var zbytes15 = zSerialize(input15, new TypeToken<>() {});
Map<String, Integer> output15 = zDeserialize(zbytes15, new TypeToken<>() {});
assert input15.equals(output15);

// Nested List example
List<List<Integer>> input18 = List.of(List.of(1, 2, 3));
var zbytes18 = zSerialize(input18, new TypeToken<>() {});
List<List<Integer>> output18 = zDeserialize(zbytes18, new TypeToken<>() {});
assert input18.equals(output18);

// Combined types example
List<Map<String, Integer>> input19 = List.of(Map.of("a", 1, "b", 2));
var zbytes19 = zSerialize(input19, new TypeToken<>() {});
List<Map<String, Integer>> output19 = zDeserialize(zbytes19, new TypeToken<>() {});
assert input19.equals(output19);
}
}
Loading
Loading