Skip to content

Commit 30af980

Browse files
authored
API Alignment (#157)
* Alignment: bytes package * Alignment: encoding update * Alignment: config package * Alignment: replacing all exceptions with ZError * Alignment: ext package * Alignment: jni callbacks * Alignment: config * Alignment: JNIKeyExpr * Alignment: JNIPublisher * Alignment: JNIQuery * Alignment: JNIScout * Alignment: JNISession * Alignment JNIZBytes & JNIZenohId * Alignment: KeyExpr & SetIntersectionLevel * Alignment: pubsub package * Alignment: Publisher & qos package * Alignment: query package - wip * Alignment: adding scouting package * Alignment: removing Value * Alignment: logger * Alignmment: Zenoh.kt - wip * Alignment: publisher - adding encoding and reliability to builder * Alignment: subscriber * Alignment: sample * Alignment: query & reply - wip * Alignment: Put & Session * Alignment: fix 'IntoSelector' * Alignment: updating zenoh-jni * Alignment: wip - converting tests to java tests. Added config tests. * Alignment: wip - converting tests to java tests. Added delete test. * Alignment: wip - converting tests to java tests. Added encoding tests. * Alignment: wip - converting tests to java tests. Added get tests. * Alignment: wip - converting tests to java tests. Added key expr tests. * Alignment: wip - converting tests to java tests. Added parameters tests. * Alignment: wip - converting tests to java tests. Added publisher tests. * Alignment: wip - converting tests to java tests. Added Queryable tests. * Alignment: wip - converting tests to java tests. Added Put tests. * Alignment: wip - Added Scouting tests + adding scouting builder. * Alignment: wrapping up scouting, closing queue upon scout close. * Alignment: adding selector tests * Alignment: fix SessionInfo + adding tests * Alignment: fix Scouting queue test * amend! Alignment: fix SessionInfo + adding tests Alignment: fix SessionInfo + adding tests * Alignment: session tests * Alignment: user attachment tests * Alignment: subscriber tests * Alignment: removing the zenoh-ext package (to be added later in another PR) * Alignment: Publisher config params * Alignment: Subscriber config params * Alignment: Queryable config params * Alignment: Subscriber config params refactor * Alignment: Queryable declaration and Query.reply config params. * Alignment: Get config params * Alignment: Subscriber config params refactor * Fix config test * Alignment - Scouting config params * Alignment: adding Liveliness * Gitignore update * Alignment: fix logging * Alignment: publisher put and delete config param * Alignment: examples - adding picocli for CLI args * Alignment: examples - adding missing examples * Alignment: examples - adding ping and pong examples * Alignment: examples refactor + refactor queryable config logic * Alignment: fix publisher put encoding fallback * Alignment: removing SubscriberConfig.kt * Alignment: renaming PublisherConfig to PublisherOptions * Alignment: renaming DeleteConfig to DeleteOptions and removing 'builder' functions in it. * Alignment: renaming GetConfig to GetOptions and removing 'builder' functions in it. * Alignment: renaming PutConfig to PutOptions and removing 'builder' functions in it. * Alignment: renaming ReplyConfig to ReplyOptions and removing 'builder' functions in it. * Alignment: renaming ReplyDelConfig to ReplyDelOptions and removing 'builder' functions in it. * Alignment: renaming ReplyErrConfig to ReplyErrOptions and removing 'builder' functions in it. * Alignment: renaming QueryableConfig to QueryableOptions and removing 'builder' functions in it. * Alignment: renaming ScoutConfig to ScoutOptions and removing 'builder' functions in it. * Alignment: renaming Liveliness.SubscriberConfig + renaming variables * Alignment: queryable options refactor * Alignment: removing Resolvable * Alignment: splitting Queryable, Subscriber and Get into Handler and Callback subclasses + tidying up documentation. * Alignment: removing JNIZBytes * Alignment: removing unused kotlin json dependency * Alignment: options refactor on QoS param
1 parent 9880420 commit 30af980

File tree

129 files changed

+9393
-4180
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

129 files changed

+9393
-4180
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@
1919
/examples/build/
2020
/build/
2121
/.gradle/
22+
*/build/

examples/build.gradle.kts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
plugins {
1616
kotlin("jvm")
17+
kotlin("plugin.serialization") version "1.9.0"
1718
}
1819

1920
kotlin {
@@ -23,17 +24,26 @@ kotlin {
2324
dependencies {
2425
implementation(project(":zenoh-java"))
2526
implementation("commons-net:commons-net:3.9.0")
27+
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0")
28+
implementation("info.picocli:picocli:4.7.4")
2629
}
2730

2831
tasks {
2932
val examples = listOf(
3033
"ZDelete",
3134
"ZGet",
35+
"ZGetLiveliness",
36+
"ZInfo",
37+
"ZLiveliness",
38+
"ZPing",
39+
"ZPong",
3240
"ZPub",
3341
"ZPubThr",
3442
"ZPut",
3543
"ZQueryable",
44+
"ZScout",
3645
"ZSub",
46+
"ZSubLiveliness",
3747
"ZSubThr"
3848
)
3949

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//
2+
// Copyright (c) 2023 ZettaScale Technology
3+
//
4+
// This program and the accompanying materials are made available under the
5+
// terms of the Eclipse Public License 2.0 which is available at
6+
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7+
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
8+
//
9+
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
10+
//
11+
// Contributors:
12+
// ZettaScale Zenoh Team, <[email protected]>
13+
//
14+
15+
package io.zenoh
16+
17+
import kotlinx.serialization.Serializable
18+
import kotlinx.serialization.SerialName
19+
import kotlinx.serialization.json.Json
20+
import kotlinx.serialization.json.encodeToJsonElement
21+
import kotlin.io.path.Path
22+
23+
@Serializable
24+
data class ConfigData(
25+
@SerialName("connect") var connect: Connect? = null,
26+
@SerialName("listen") var listen: Listen? = null,
27+
@SerialName("mode") var mode: String? = null,
28+
@SerialName("scouting") var scouting: Scouting? = null,
29+
)
30+
31+
@Serializable
32+
data class Connect(
33+
@SerialName("endpoints") var endpoints: List<String>
34+
)
35+
36+
@Serializable
37+
data class Listen(
38+
@SerialName("endpoints") var endpoints: List<String>
39+
)
40+
41+
@Serializable
42+
data class Scouting(
43+
@SerialName("multicast") var multicast: Multicast,
44+
)
45+
46+
@Serializable
47+
data class Multicast(
48+
@SerialName("enabled") var enabled: Boolean,
49+
)
50+
51+
internal fun loadConfig(
52+
emptyArgs: Boolean,
53+
configFile: String?,
54+
connectEndpoints: List<String>?,
55+
listenEndpoints: List<String>?,
56+
noMulticastScouting: Boolean?,
57+
mode: String?
58+
): Config {
59+
return if (emptyArgs) {
60+
Config.loadDefault()
61+
} else {
62+
configFile?.let {
63+
Config.fromFile(path = Path(it))
64+
} ?: run {
65+
val connect = connectEndpoints?.let {Connect(it)}
66+
val listen = listenEndpoints?.let {Listen(it)}
67+
val scouting = noMulticastScouting?.let { Scouting(Multicast(!it)) }
68+
val configData = ConfigData(connect, listen, mode, scouting)
69+
val jsonConfig = Json.encodeToJsonElement(configData).toString()
70+
println(jsonConfig)
71+
Config.fromJson(jsonConfig)
72+
}
73+
}
74+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package io.zenoh;
2+
3+
import io.zenoh.handlers.Handler;
4+
5+
import java.util.ArrayDeque;
6+
7+
/**
8+
* Sample handler for the sake of the examples.
9+
*
10+
* A custom handler can be implemented to handle incoming samples, queries or replies for
11+
* subscribers, get operations, query operations or queryables.
12+
*
13+
* The example below shows a queue handler, in which an ArrayDeque is specified as the receiver type.
14+
* The function handle will be called everytime an element of type T is received and in our example
15+
* implementation, elements are simply enqueued into the queue, which can later be retrieved.
16+
*/
17+
class QueueHandler<T extends ZenohType> implements Handler<T, ArrayDeque<T>> {
18+
19+
final ArrayDeque<T> queue = new ArrayDeque<>();
20+
21+
@Override
22+
public void handle(T t) {
23+
queue.add(t);
24+
}
25+
26+
@Override
27+
public ArrayDeque<T> receiver() {
28+
return queue;
29+
}
30+
31+
@Override
32+
public void onClose() {}
33+
}

examples/src/main/java/io/zenoh/ZDelete.java

Lines changed: 80 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,89 @@
1414

1515
package io.zenoh;
1616

17-
import io.zenoh.exceptions.ZenohException;
17+
import io.zenoh.exceptions.ZError;
1818
import io.zenoh.keyexpr.KeyExpr;
19+
import picocli.CommandLine;
1920

20-
public class ZDelete {
21-
public static void main(String[] args) throws ZenohException {
21+
import java.util.List;
22+
import java.util.concurrent.Callable;
23+
24+
import static io.zenoh.ConfigKt.loadConfig;
25+
26+
@CommandLine.Command(
27+
name = "ZDelete",
28+
mixinStandardHelpOptions = true,
29+
description = "Zenoh Delete example"
30+
)
31+
public class ZDelete implements Callable<Integer> {
32+
33+
@Override
34+
public Integer call() throws ZError {
35+
Zenoh.initLogFromEnvOr("error");
2236
System.out.println("Opening session...");
23-
try (Session session = Session.open()) {
24-
try (KeyExpr keyExpr = KeyExpr.tryFrom("demo/example/zenoh-java-put")) {
25-
System.out.println("Deleting resources matching '" + keyExpr + "'...");
26-
session.delete(keyExpr).res();
27-
}
37+
Config config = loadConfig(emptyArgs, configFile, connect, listen, noMulticastScouting, mode);
38+
try (Session session = Zenoh.open(config)) {
39+
KeyExpr keyExpr = KeyExpr.tryFrom(key);
40+
System.out.println("Deleting resources matching '" + keyExpr + "'...");
41+
session.delete(keyExpr);
2842
}
43+
return 0;
44+
}
45+
46+
47+
/**
48+
* ----- Example CLI arguments and private fields -----
49+
*/
50+
51+
private final Boolean emptyArgs;
52+
53+
ZDelete(Boolean emptyArgs) {
54+
this.emptyArgs = emptyArgs;
55+
}
56+
57+
@CommandLine.Option(
58+
names = {"-e", "--connect"},
59+
description = "Endpoints to connect to.",
60+
split = ","
61+
)
62+
private List<String> connect;
63+
64+
@CommandLine.Option(
65+
names = {"-l", "--listen"},
66+
description = "Endpoints to listen on.",
67+
split = ","
68+
)
69+
private List<String> listen;
70+
71+
@CommandLine.Option(
72+
names = {"-c", "--config"},
73+
description = "A configuration file."
74+
)
75+
private String configFile;
76+
77+
@CommandLine.Option(
78+
names = {"-k", "--key"},
79+
description = "The key expression to delete [default: demo/example/zenoh-java-delete].",
80+
defaultValue = "demo/example/zenoh-java-delete"
81+
)
82+
private String key;
83+
84+
@CommandLine.Option(
85+
names = {"-m", "--mode"},
86+
description = "The session mode. Default: peer. Possible values: [peer, client, router].",
87+
defaultValue = "peer"
88+
)
89+
private String mode;
90+
91+
@CommandLine.Option(
92+
names = {"--no-multicast-scouting"},
93+
description = "Disable the multicast-based scouting mechanism.",
94+
defaultValue = "false"
95+
)
96+
private boolean noMulticastScouting;
97+
98+
public static void main(String[] args) {
99+
int exitCode = new CommandLine(new ZDelete(args.length == 0)).execute(args);
100+
System.exit(exitCode);
29101
}
30102
}

0 commit comments

Comments
 (0)