Skip to content

Commit

Permalink
fixed examples
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisBiryukov91 committed Dec 10, 2024
1 parent bd47d0f commit 51bd891
Show file tree
Hide file tree
Showing 32 changed files with 472 additions and 686 deletions.
193 changes: 109 additions & 84 deletions examples/getargs.hxx

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions examples/universal/z_delete.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ const char *default_keyexpr = "demo/example/zenoh-cpp-zenoh-pico-put";
using namespace zenoh;

int _main(int argc, char **argv) {
auto&& [config, args] = ConfigCliArgParser(argc, argv)
.named_value({"k", "key"}, "KEY_EXPRESSION", "The key expression to write to", default_keyexpr)
.run();
auto &&[config, args] =
ConfigCliArgParser(argc, argv)
.named_value({"k", "key"}, "KEY_EXPRESSION", "The key expression to write to", default_keyexpr)
.run();

std::string_view keyexpr = args("key").value;
std::string_view keyexpr = args.value("key");

std::cout << "Opening session...\n";
auto session = Session::open(std::move(config));
Expand Down
16 changes: 9 additions & 7 deletions examples/universal/z_get.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
using namespace zenoh;

int _main(int argc, char **argv) {
auto&& [config, args] = ConfigCliArgParser(argc, argv)
.named_value({"s", "selector"}, "SELECTOR", "Query selector (string)", "demo/example/**")
.named_value({"p", "payload"}, "PAYLOAD", "Query payload (string)", "")
.named_value({"t", "target"}, "TARGET", "Query target (BEST_MATCHING | ALL | ALL_COMPLETE)", "BEST_MATCHING")
.named_value({"o", "timeout"}, "TIMEOUT", "Timeout in ms (number)", "10000")
.run();
auto &&[config, args] =
ConfigCliArgParser(argc, argv)
.named_value({"s", "selector"}, "SELECTOR", "Query selector (string)", "demo/example/**")
.named_value({"p", "payload"}, "PAYLOAD", "Query payload (string)", "")
.named_value({"t", "target"}, "TARGET", "Query target (BEST_MATCHING | ALL | ALL_COMPLETE)",
"BEST_MATCHING")
.named_value({"o", "timeout"}, "TIMEOUT", "Timeout in ms (number)", "10000")
.run();

uint64_t timeout_ms = std::atoi(args.value("timeout").data());
QueryTarget query_target = parse_query_target(args.value("target"));
Expand Down Expand Up @@ -62,7 +64,7 @@ int _main(int argc, char **argv) {
};

Session::GetOptions options;
options.target = target;
options.target = query_target;
if (!payload.empty()) {
options.payload = payload;
}
Expand Down
14 changes: 8 additions & 6 deletions examples/universal/z_get_channel.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
using namespace zenoh;

int _main(int argc, char **argv) {
auto&& [config, args] = ConfigCliArgParser(argc, argv)
.named_value({"s", "selector"}, "SELECTOR", "Query selector (string)", "demo/example/**")
.named_value({"p", "payload"}, "PAYLOAD", "Query payload (string)", "")
.named_value({"t", "target"}, "TARGET", "Query target (BEST_MATCHING | ALL | ALL_COMPLETE)", "BEST_MATCHING")
.named_value({"o", "timeout"}, "TIMEOUT", "Timeout in ms (number)", "10000")
.run();
auto &&[config, args] =
ConfigCliArgParser(argc, argv)
.named_value({"s", "selector"}, "SELECTOR", "Query selector (string)", "demo/example/**")
.named_value({"p", "payload"}, "PAYLOAD", "Query payload (string)", "")
.named_value({"t", "target"}, "TARGET", "Query target (BEST_MATCHING | ALL | ALL_COMPLETE)",
"BEST_MATCHING")
.named_value({"o", "timeout"}, "TIMEOUT", "Timeout in ms (number)", "10000")
.run();

uint64_t timeout_ms = std::atoi(args.value("timeout").data());
QueryTarget query_target = parse_query_target(args.value("target"));
Expand Down
14 changes: 8 additions & 6 deletions examples/universal/z_get_channel_non_blocking.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ using namespace zenoh;
using namespace std::chrono_literals;

int _main(int argc, char **argv) {
auto&& [config, args] = ConfigCliArgParser(argc, argv)
.named_value({"s", "selector"}, "SELECTOR", "Query selector (string)", "demo/example/**")
.named_value({"p", "payload"}, "PAYLOAD", "Query payload (string)", "")
.named_value({"t", "target"}, "TARGET", "Query target (BEST_MATCHING | ALL | ALL_COMPLETE)", "BEST_MATCHING")
.named_value({"o", "timeout"}, "TIMEOUT", "Timeout in ms (number)", "10000")
.run();
auto &&[config, args] =
ConfigCliArgParser(argc, argv)
.named_value({"s", "selector"}, "SELECTOR", "Query selector (string)", "demo/example/**")
.named_value({"p", "payload"}, "PAYLOAD", "Query payload (string)", "")
.named_value({"t", "target"}, "TARGET", "Query target (BEST_MATCHING | ALL | ALL_COMPLETE)",
"BEST_MATCHING")
.named_value({"o", "timeout"}, "TIMEOUT", "Timeout in ms (number)", "10000")
.run();

uint64_t timeout_ms = std::atoi(args.value("timeout").data());
QueryTarget query_target = parse_query_target(args.value("target"));
Expand Down
19 changes: 13 additions & 6 deletions examples/universal/z_get_liveliness.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,28 @@
#include <iostream>
#include <string>

#include "../getargs.h"
#include "../getargs.hxx"
#include "zenoh.hxx"
using namespace zenoh;

int _main(int argc, char **argv) {
const char *expr = "group1/**";
Config config = parse_args(argc, argv, {}, {{"key_expression", &expr}});
auto &&[config, args] =
ConfigCliArgParser(argc, argv)
.named_value({"k", "key"}, "KEY_EXPRESSION", "Key expression to query (string)", "group1/**")
.named_value({"o", "timeout"}, "TIMEOUT", "Timeout in ms (number)", "10000")
.run();

KeyExpr keyexpr(expr);
uint64_t timeout_ms = std::atoi(args.value("timeout").data());

KeyExpr keyexpr(args.value("key"));

std::cout << "Opening session...\n";
auto session = Session::open(std::move(config));

std::cout << "Sending Liveliness Query '" << expr << "'...\n";
auto replies = session.liveliness_get(keyexpr, channels::FifoChannel(16));
std::cout << "Sending Liveliness Query '" << keyexpr.as_string_view() << "'...\n";
Session::LivelinessGetOptions opts;
opts.timeout_ms = timeout_ms;
auto replies = session.liveliness_get(keyexpr, channels::FifoChannel(16), std::move(opts));

for (auto res = replies.recv(); std::holds_alternative<Reply>(res); res = replies.recv()) {
const Reply &reply = std::get<Reply>(res);
Expand Down
4 changes: 2 additions & 2 deletions examples/universal/z_info.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@

#include <iostream>

#include "../getargs.h"
#include "../getargs.hxx"
#include "zenoh.hxx"
using namespace zenoh;

int _main(int argc, char** argv) {
const char* value = "Get from C++";
Config config = parse_args(argc, argv, {});
auto&& [config, args] = ConfigCliArgParser(argc, argv).run();

std::cout << "Opening session...\n";
auto session = Session::open(std::move(config));
Expand Down
9 changes: 6 additions & 3 deletions examples/universal/z_liveliness.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include <string>
#include <thread>

#include "../getargs.h"
#include "../getargs.hxx"
#include "zenoh.hxx"

using namespace zenoh;
Expand All @@ -28,8 +28,11 @@ using namespace std::chrono_literals;
const char *default_keyexpr = "group1/zenoh-cpp-c";

int _main(int argc, char **argv) {
const char *keyexpr = default_keyexpr;
Config config = parse_args(argc, argv, {}, {{"key_expression", &keyexpr}});
auto &&[config, args] =
ConfigCliArgParser(argc, argv)
.named_value({"k", "key"}, "KEY_EXPRESSION", "Key expression of the liveliness (string)", default_keyexpr)
.run();
auto keyexpr = args.value("key");

std::cout << "Opening session...\n";
auto session = Session::open(std::move(config));
Expand Down
33 changes: 17 additions & 16 deletions examples/universal/z_ping.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include <mutex>
#include <numeric>

#include "../getargs.h"
#include "../getargs.hxx"
#include "zenoh.hxx"

using namespace zenoh;
Expand All @@ -28,27 +28,28 @@ int _main(int argc, char** argv) {
using namespace std::literals;
std::mutex mutex;
std::condition_variable condvar;
const char* number_of_pings_str = "100";
const char* payload_size_str = "8";
const char* warmup_ms_str = "1000";
const char* timeout_ms_str = "100";
Config config = parse_args(
argc, argv, {}, {},
{{"-n", {"number of pings to be attempted", &number_of_pings_str}},
{"-s", {"size of the payload embedded in the ping and repeated by the pong", &payload_size_str}},
{"-w", {"the warmup time in ms during which pings will be emitted but not measured", &warmup_ms_str}},
{"-t", {"timeout for any individual ping, in ms", &timeout_ms_str}}});
unsigned int number_of_pings = std::atoi(number_of_pings_str);
unsigned int payload_size = std::atoi(payload_size_str);
unsigned int warmup_ms = std::atoi(warmup_ms_str);
unsigned int timeout_ms = std::atoi(timeout_ms_str);

auto&& [config, args] =
ConfigCliArgParser(argc, argv)
.positional("PAYLOAD_SIZE", "Size of the payload to publish (number)")
.named_value({"n", "samples"}, "SAMPLES", "The number of pings to be attempted (number)", "100")
.named_value({"w", "warmup"}, "WARMUP",
"The warmup time in ms during which pings will be emitted but not measured (number)", "1000")
.named_flag({"no-express"}, "Disable message batching")
.run();

size_t payload_size = std::atoi(args.positional(0).data());
size_t number_of_pings = std::atoi(args.value("samples").data());
size_t warmup_ms = std::atoi(args.value("warmup").data());
std::cout << "Opening session...\n";
auto session = Session::open(std::move(config));

auto sub = session.declare_subscriber(
KeyExpr("test/pong"), [&condvar](const Sample&) mutable { condvar.notify_one(); }, closures::none);
auto pub = session.declare_publisher(KeyExpr("test/ping"));

Session::PublisherOptions opts;
opts.is_express = !args.flag("no-express");
auto pub = session.declare_publisher(KeyExpr("test/ping"), std::move(opts));
std::vector<uint8_t> data(payload_size);
std::iota(data.begin(), data.end(), uint8_t{0});
Bytes payload = std::move(data);
Expand Down
8 changes: 5 additions & 3 deletions examples/universal/z_pong.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@
#include <cstdio>
#include <iostream>

#include "../getargs.h"
#include "../getargs.hxx"
#include "zenoh.hxx"

using namespace zenoh;

int _main(int argc, char **argv) {
Config config = parse_args(argc, argv, {});
auto &&[config, args] = ConfigCliArgParser(argc, argv).named_flag({"no-express"}, "Disable message batching").run();

std::cout << "Opening session...\n";
auto session = Session::open(std::move(config));

auto pub = session.declare_publisher(KeyExpr("test/pong"));
Session::PublisherOptions opts;
opts.is_express = args.flag("no-express");
auto pub = session.declare_publisher(KeyExpr("test/pong"), std::move(opts));
session.declare_background_subscriber(
KeyExpr("test/ping"),
[pub = std::move(pub)](const Sample &sample) mutable { pub.put(sample.get_payload().clone()); },
Expand Down
39 changes: 22 additions & 17 deletions examples/universal/z_pub.cxx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (c) 2022 ZettaScale Technology
// 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
Expand All @@ -20,7 +20,7 @@
#include <sstream>
#include <thread>

#include "../getargs.h"
#include "../getargs.hxx"
#include "zenoh.hxx"

using namespace zenoh;
Expand All @@ -37,24 +37,27 @@ const char *default_keyexpr = "demo/example/zenoh-cpp-zenoh-pico-pub";
#endif

int _main(int argc, char **argv) {
const char *keyexpr = default_keyexpr;
const char *value = default_value;
const char *add_matching_listener = "false";
Config config = parse_args(argc, argv, {}, {{"key_expression", &keyexpr}, {"payload_value", &value}}
#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API)
,
{{"--add-matching-listener", CmdArg{"", &add_matching_listener, true}}}
auto &&[config, args] =
ConfigCliArgParser(argc, argv)
.named_value({"k", "key"}, "KEY_EXPRESSION", "Key expression to publish to (string)", default_keyexpr)
.named_value({"p", "payload"}, "PAYLOAD", "Payload to publish (string)", default_value)
.named_value({"a", "attach"}, "ATTACHMENT", "Attachment to add to each put (string)", "")
#if defined(Z_FEATURE_UNSTABLE_API) && defined(ZENOHCXX_ZENOHC)
.named_flag({"add-matching-listener"}, "Add matching listener")
#endif
);
.run();

auto keyexpr = args.value("key");
auto payload = args.value("payload");
auto attachment = args.value("attach");

std::cout << "Opening session..." << std::endl;
auto session = Session::open(std::move(config));

std::cout << "Declaring Publisher on '" << keyexpr << "'..." << std::endl;
auto pub = session.declare_publisher(KeyExpr(keyexpr));

#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API)
if (std::string(add_matching_listener) == "true") {
#if defined(Z_FEATURE_UNSTABLE_API) && defined(ZENOHCXX_ZENOHC)
if (args.flag("add-matching-listener")) {
pub.declare_background_matching_listener(
[](const MatchingStatus &s) {
if (s.matching) {
Expand All @@ -71,13 +74,15 @@ int _main(int argc, char **argv) {
for (int idx = 0; idx < std::numeric_limits<int>::max(); ++idx) {
std::this_thread::sleep_for(1s);
std::ostringstream ss;
ss << "[" << idx << "] " << value;
ss << "[" << idx << "] " << payload;
auto s = ss.str();
std::cout << "Putting Data ('" << keyexpr << "': '" << s << "')...\n";
Publisher::PutOptions options;
if (!attachment.empty()) {
options.attachment = attachment;
}

auto put_options = Publisher::PutOptions{};
put_options.encoding = Encoding("text/plain");
pub.put(s, std::move(put_options));
pub.put(s, std::move(options));
}
return 0;
}
Expand Down
80 changes: 0 additions & 80 deletions examples/universal/z_pub_attachment.cxx

This file was deleted.

Loading

0 comments on commit 51bd891

Please sign in to comment.