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

Fix/selector params #168

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 12 additions & 3 deletions examples/src/main/java/io/zenoh/ZGet.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,20 @@ public Integer call() throws ZError, InterruptedException {
Config config = loadConfig(emptyArgs, configFile, connect, listen, noMulticastScouting, mode);
Selector selector = Selector.tryFrom(this.selectorOpt);

ZBytes payload = Optional.ofNullable(this.payload)
.map(ZBytes::from)
.orElse(null);
ZBytes attachment = Optional.ofNullable(this.attachment)
.map(ZBytes::from)
.orElse(null);

// Load GET options
GetOptions options = new GetOptions();
options.setPayload(ZBytes.from(this.payload));

options.setPayload(payload);
options.setTarget(QueryTarget.valueOf(this.target));
options.setTimeout(Duration.ofMillis(this.timeout));
options.setAttachment(ZBytes.from(this.attachment));
options.setAttachment(attachment);


// A GET query can be performed in different ways, by default (using a blocking queue), using a callback
Expand Down Expand Up @@ -146,7 +154,8 @@ private void handleReply(Reply reply) {
@CommandLine.Option(
names = {"-t", "--target"},
description = "The target queryables of the query. Default: BEST_MATCHING. " +
"[possible values: BEST_MATCHING, ALL, ALL_COMPLETE]"
"[possible values: BEST_MATCHING, ALL, ALL_COMPLETE]",
defaultValue = "BEST_MATCHING"
)
private String target;

Expand Down
4 changes: 2 additions & 2 deletions zenoh-java/src/commonMain/kotlin/io/zenoh/jni/JNISession.kt
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ internal class JNISession {
getViaJNI(
selector.keyExpr.jniKeyExpr?.ptr ?: 0,
selector.keyExpr.keyExpr,
selector.parameters.toString(),
selector.parameters?.toString(),
sessionPtr.get(),
getCallback,
fun() {},
Expand Down Expand Up @@ -338,7 +338,7 @@ internal class JNISession {
getViaJNI(
selector.keyExpr.jniKeyExpr?.ptr ?: 0,
selector.keyExpr.keyExpr,
selector.parameters.toString(),
selector.parameters?.toString(),
sessionPtr.get(),
getCallback,
handler::onClose,
Expand Down
32 changes: 32 additions & 0 deletions zenoh-java/src/jvmTest/java/io/zenoh/SelectorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package io.zenoh;

import io.zenoh.exceptions.ZError;
import io.zenoh.keyexpr.KeyExpr;
import io.zenoh.query.Parameters;
import io.zenoh.query.Selector;
import org.junit.Test;
Expand Down Expand Up @@ -47,4 +48,35 @@ public void parametersTest() {
var parameters = Parameters.from("a=1;b=2;c=1|2|3");
assertEquals(List.of("1", "2", "3"), parameters.values("c"));
}

/**
* Check the queryable properly receives the query's selector with and without parameters.
*/
@Test
public void selectorQueryTest() throws ZError, InterruptedException {
var session = Zenoh.open(Config.loadDefault());
var queryableKeyExpr = KeyExpr.tryFrom("a/b/**");

Selector[] receivedQuerySelector = new Selector[1];
var queryable = session.declareQueryable(queryableKeyExpr, query -> {
receivedQuerySelector[0] = query.getSelector();
query.close();
}
);

var querySelector = Selector.tryFrom("a/b/c");
session.get(querySelector, reply -> {
});
Thread.sleep(1000);
assertEquals(querySelector, receivedQuerySelector[0]);

var querySelector2 = Selector.tryFrom("a/b/c?key=value");
session.get(querySelector2, reply -> {
});
Thread.sleep(1000);
assertEquals(querySelector2, receivedQuerySelector[0]);

queryable.close();
session.close();
}
}
Loading