Skip to content

Commit

Permalink
Align examples and remove reading from stdin (#45)
Browse files Browse the repository at this point in the history
* Remove reading from stdin

* Fix payload content in ZPub example

* Add queryable keyexpr to stdout

* Remove report function and associated declarations

* Revert "Remove report function and associated declarations"

This reverts commit d2b45dd.

* Add TODO to report function
  • Loading branch information
oteffahi authored Apr 19, 2024
1 parent 0d72123 commit 4ed68c7
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 12 deletions.
5 changes: 3 additions & 2 deletions examples/src/main/java/io/zenoh/ZPub.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ public static void main(String[] args) throws ZenohException, InterruptedExcepti
try (KeyExpr keyExpr = KeyExpr.tryFrom("demo/example/zenoh-java-pub")) {
System.out.println("Declaring publisher on '" + keyExpr + "'...");
try (Publisher publisher = session.declarePublisher(keyExpr).res()) {
String payload = "Pub from Java!";
System.out.println("Press CTRL-C to quit...");
int idx = 0;
while (true) {
Thread.sleep(1000);
System.out.println("Putting Data ('" + keyExpr + "': '[" + String.format("%4s", idx) + "] " + payload + "')...");
String payload = String.format("[%4s] Pub from Java!", idx);
System.out.println("Putting Data ('" + keyExpr + "': '"+payload+"')...");
publisher.put(payload).res();
idx++;
}
Expand Down
1 change: 1 addition & 0 deletions examples/src/main/java/io/zenoh/ZPubThr.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public static void main(String[] args) throws ZenohException {
try (KeyExpr keyExpr = KeyExpr.tryFrom("test/thr")) {
try (Publisher publisher = session.declarePublisher(keyExpr).congestionControl(CongestionControl.BLOCK).res()) {
System.out.println("Publisher declared on test/thr.");
System.out.println("Press CTRL-C to quit...");
while (true) {
publisher.put(value).res();
}
Expand Down
6 changes: 4 additions & 2 deletions examples/src/main/java/io/zenoh/ZQueryable.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
public class ZQueryable {

public static void main(String[] args) throws ZenohException, InterruptedException {
String keyExprString = "demo/example/zenoh-java-queryable";
try (Session session = Session.open()) {
try (KeyExpr keyExpr = KeyExpr.tryFrom("demo/example/zenoh-java-queryable")) {
System.out.println("Declaring Queryable");
try (KeyExpr keyExpr = KeyExpr.tryFrom(keyExprString)) {
System.out.println("Declaring Queryable on " + keyExprString + "...");
try (Queryable<BlockingQueue<Optional<Query>>> queryable = session.declareQueryable(keyExpr).res()) {
BlockingQueue<Optional<Query>> receiver = queryable.getReceiver();
assert receiver != null;
System.out.println("Press CTRL-C to quit...");
handleRequests(receiver, keyExpr);
}
}
Expand Down
1 change: 1 addition & 0 deletions examples/src/main/java/io/zenoh/ZSub.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public static void main(String[] args) throws ZenohException, InterruptedExcepti
try (Subscriber<BlockingQueue<Optional<Sample>>> subscriber = session.declareSubscriber(keyExpr).res()) {
BlockingQueue<Optional<Sample>> receiver = subscriber.getReceiver();
assert receiver != null;
System.out.println("Press CTRL-C to quit...");
while (true) {
Optional<Sample> wrapper = receiver.take();
if (wrapper.isEmpty()) {
Expand Down
13 changes: 5 additions & 8 deletions examples/src/main/java/io/zenoh/ZSubThr.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import io.zenoh.subscriber.Subscriber;
import kotlin.Unit;

import java.util.Scanner;

public class ZSubThr {

private static final long NANOS_TO_SEC = 1_000_000_000L;
Expand Down Expand Up @@ -50,6 +48,7 @@ public static void listener() {
count = 0;
}

// TODO: perform report at end of measurement
public static void report() {
long end = System.nanoTime();
long total = batchCount * n + count;
Expand All @@ -59,19 +58,17 @@ public static void report() {
": averaged " + avg + " msgs/sec");
}

public static void main(String[] args) throws ZenohException {
public static void main(String[] args) throws ZenohException, InterruptedException {
System.out.println("Opening Session");
try (Session session = Session.open()) {
try (KeyExpr keyExpr = KeyExpr.tryFrom("test/thr")) {
try (Subscriber<Unit> subscriber = session.declareSubscriber(keyExpr).with(sample -> listener()).res()) {
Scanner scanner = new Scanner(System.in);
while (!scanner.nextLine().equals("q")) {
// Do nothing
System.out.println("Press CTRL-C to quit...");
while (true) {
Thread.sleep(1000);
}
scanner.close();
}
}
}
report();
}
}

0 comments on commit 4ed68c7

Please sign in to comment.