From 4ed68c7c8002200a42d3d583d5bf8e70866d1cbc Mon Sep 17 00:00:00 2001 From: oteffahi <70609372+oteffahi@users.noreply.github.com> Date: Fri, 19 Apr 2024 12:04:15 +0200 Subject: [PATCH] Align examples and remove reading from stdin (#45) * 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 d2b45dd4e4008725a7044819117d33beddf55c57. * Add TODO to report function --- examples/src/main/java/io/zenoh/ZPub.java | 5 +++-- examples/src/main/java/io/zenoh/ZPubThr.java | 1 + examples/src/main/java/io/zenoh/ZQueryable.java | 6 ++++-- examples/src/main/java/io/zenoh/ZSub.java | 1 + examples/src/main/java/io/zenoh/ZSubThr.java | 13 +++++-------- 5 files changed, 14 insertions(+), 12 deletions(-) diff --git a/examples/src/main/java/io/zenoh/ZPub.java b/examples/src/main/java/io/zenoh/ZPub.java index cf798eb8..24965fc8 100644 --- a/examples/src/main/java/io/zenoh/ZPub.java +++ b/examples/src/main/java/io/zenoh/ZPub.java @@ -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++; } diff --git a/examples/src/main/java/io/zenoh/ZPubThr.java b/examples/src/main/java/io/zenoh/ZPubThr.java index 1ec30567..366dd1c9 100644 --- a/examples/src/main/java/io/zenoh/ZPubThr.java +++ b/examples/src/main/java/io/zenoh/ZPubThr.java @@ -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(); } diff --git a/examples/src/main/java/io/zenoh/ZQueryable.java b/examples/src/main/java/io/zenoh/ZQueryable.java index 1dd67503..6a710a97 100644 --- a/examples/src/main/java/io/zenoh/ZQueryable.java +++ b/examples/src/main/java/io/zenoh/ZQueryable.java @@ -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>> queryable = session.declareQueryable(keyExpr).res()) { BlockingQueue> receiver = queryable.getReceiver(); assert receiver != null; + System.out.println("Press CTRL-C to quit..."); handleRequests(receiver, keyExpr); } } diff --git a/examples/src/main/java/io/zenoh/ZSub.java b/examples/src/main/java/io/zenoh/ZSub.java index bcf0710d..883139f7 100644 --- a/examples/src/main/java/io/zenoh/ZSub.java +++ b/examples/src/main/java/io/zenoh/ZSub.java @@ -32,6 +32,7 @@ public static void main(String[] args) throws ZenohException, InterruptedExcepti try (Subscriber>> subscriber = session.declareSubscriber(keyExpr).res()) { BlockingQueue> receiver = subscriber.getReceiver(); assert receiver != null; + System.out.println("Press CTRL-C to quit..."); while (true) { Optional wrapper = receiver.take(); if (wrapper.isEmpty()) { diff --git a/examples/src/main/java/io/zenoh/ZSubThr.java b/examples/src/main/java/io/zenoh/ZSubThr.java index d226fb68..d7afc00a 100644 --- a/examples/src/main/java/io/zenoh/ZSubThr.java +++ b/examples/src/main/java/io/zenoh/ZSubThr.java @@ -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; @@ -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; @@ -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 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(); } }