Skip to content

Commit 725a14b

Browse files
add querier support (#297)
* add querier * move querier under unstable * switch zenoh-c to main
1 parent e8eca99 commit 725a14b

22 files changed

+791
-104
lines changed

README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,16 @@ The `z_pub` should receive message sent by `z_sub`.
120120
./z_get
121121
```
122122

123-
The `z_get` should receive the data from `z_queryable`.
123+
### Queryable and Querier Example
124+
```bash
125+
./z_queryable
126+
```
127+
128+
```bash
129+
./z_querier
130+
```
131+
132+
The `z_querier` should continuously send queries and receive replies from `z_queryable`.
124133

125134
### Throughput Examples
126135
```bash

docs/api.rst

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ API Reference
2727
scouting
2828
publish_subscribe
2929
query_reply
30+
matching
3031
serialization_deserialization
3132
channels
3233
interop

docs/matching.rst

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Matching
2+
=================
3+
Classes related to getting information about matching Zenoh entities.
4+
5+
.. doxygenstruct:: zenoh::MatchingStatus
6+
:members:
7+
:membergroups: Constructors Operators Methods Fields
8+
9+
.. doxygenclass:: zenoh::MatchingListener
10+
:members:
11+
:membergroups: Constructors Operators Methods

docs/publish_subscribe.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Classes related to publish-subscribe pattern.
1818

1919
.. doxygenclass:: zenoh::Publisher
2020
:members:
21-
:membergroups: Constructors Operators Methods
21+
:membergroups: Constructors Operators Methods Fields
2222

2323
.. doxygenclass:: zenoh::Subscriber
2424
:members:

docs/query_reply.rst

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ Query-Reply
1616
===========
1717
Classes related to query-reply pattern.
1818

19+
.. doxygenclass:: zenoh::Querier
20+
:members:
21+
:membergroups: Constructors Operators Methods Fields
22+
1923
.. doxygenclass:: zenoh::Queryable
2024
:members:
2125
:membergroups: Constructors Operators Methods

examples/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ function(add_examples glob mode lib)
6666
if(${file} MATCHES ".*liveliness.*$")
6767
continue()
6868
endif()
69+
if(${file} MATCHES ".*querier.*$")
70+
continue()
71+
endif()
6972
if(${file} MATCHES ".*query_sub.*$")
7073
continue()
7174
endif()

examples/getargs.h

+27
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <cstring>
1717
#include <iostream>
1818
#include <string>
19+
#include <string_view>
1920
#include <unordered_map>
2021
#include <vector>
2122

@@ -155,4 +156,30 @@ inline zenoh::Config parse_args(int argc, char **argv, const std::vector<CmdArg>
155156
}
156157
#endif
157158
return std::move(config);
159+
}
160+
161+
zenoh::QueryTarget parse_query_target(std::string_view v) {
162+
if (v == "BEST_MATCHING") {
163+
return zenoh::QueryTarget::Z_QUERY_TARGET_BEST_MATCHING;
164+
} else if (v == "ALL") {
165+
return zenoh::QueryTarget::Z_QUERY_TARGET_ALL;
166+
} else if (v == "ALL_COMPLETE") {
167+
return zenoh::QueryTarget::Z_QUERY_TARGET_ALL_COMPLETE;
168+
}
169+
170+
throw std::runtime_error(std::string("Unsupported QueryTarget: ") + std::string(v));
171+
}
172+
173+
struct Selector {
174+
std::string key_expr;
175+
std::string parameters;
176+
};
177+
178+
Selector parse_selector(const std::string &selector_string) {
179+
size_t pos = selector_string.find('?');
180+
if (pos == std::string::npos) {
181+
return Selector{selector_string, ""};
182+
} else {
183+
return Selector{selector_string.substr(0, pos), selector_string.substr(pos + 1)};
184+
}
158185
}

examples/universal/z_get.cxx

+17-10
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,18 @@ using namespace zenoh;
2525

2626
int _main(int argc, char **argv) {
2727
const char *expr = "demo/example/**";
28-
const char *value = "Get from C++";
29-
Config config = parse_args(argc, argv, {}, {{"key_expression", &expr}, {"payload_value", &value}});
28+
const char *value = nullptr;
29+
const char *target = "BEST_MATCHING";
30+
const char *timeout = "10000";
3031

31-
KeyExpr keyexpr(expr);
32+
Config config = parse_args(argc, argv, {}, {},
33+
{{"-s", CmdArg{"Query selector (string)", &expr}},
34+
{"-p", CmdArg{"Query payload (string)", &value}},
35+
{"-t", CmdArg{"Query target (BEST_MATCHING | ALL | ALL_COMPLETE)", &target}},
36+
{"-o", CmdArg{"Timeout in ms (number)", &timeout}}});
37+
uint64_t timeout_ms = std::stoi(timeout);
38+
QueryTarget query_target = parse_query_target(target);
39+
Selector selector = parse_selector(expr);
3240

3341
std::cout << "Opening session...\n";
3442
auto session = Session::open(std::move(config));
@@ -55,14 +63,13 @@ int _main(int argc, char **argv) {
5563
done_signal.notify_all();
5664
};
5765

58-
#if __cpp_designated_initializers >= 201707L
59-
session.get(keyexpr, "", on_reply, on_done, {.target = Z_QUERY_TARGET_ALL, .payload = Bytes::serialize(value)});
60-
#else
6166
Session::GetOptions options;
62-
options.target = Z_QUERY_TARGET_ALL;
63-
options.payload = value;
64-
session.get(keyexpr, "", on_reply, on_done, std::move(options));
65-
#endif
67+
options.target = query_target;
68+
if (value != nullptr) {
69+
options.payload = value;
70+
}
71+
options.timeout_ms = timeout_ms;
72+
session.get(selector.key_expr, selector.parameters, on_reply, on_done, std::move(options));
6673

6774
std::unique_lock lock(m);
6875
done_signal.wait(lock, [&done] { return done; });

examples/universal/z_get_attachment.cxx

+17-6
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,18 @@ using namespace zenoh;
2525

2626
int _main(int argc, char **argv) {
2727
const char *expr = "demo/example/**";
28-
const char *value = "Get from C++";
29-
Config config = parse_args(argc, argv, {}, {{"key_expression", &expr}, {"payload value", &value}});
28+
const char *value = nullptr;
29+
const char *target = "BEST_MATCHING";
30+
const char *timeout = "10000";
3031

31-
KeyExpr keyexpr(expr);
32+
Config config = parse_args(argc, argv, {}, {},
33+
{{"-s", CmdArg{"Query selector (string)", &expr}},
34+
{"-p", CmdArg{"Query payload (string)", &value}},
35+
{"-t", CmdArg{"Query target (BEST_MATCHING | ALL | ALL_COMPLETE)", &target}},
36+
{"-o", CmdArg{"Timeout in ms (number)", &timeout}}});
37+
uint64_t timeout_ms = std::stoi(timeout);
38+
QueryTarget query_target = parse_query_target(target);
39+
Selector selector = parse_selector(expr);
3240

3341
printf("Opening session...\n");
3442
auto session = Session::open(std::move(config));
@@ -66,10 +74,13 @@ int _main(int argc, char **argv) {
6674
std::unordered_map<std::string, std::string> attachment = {{"Source", "C++"}};
6775

6876
Session::GetOptions options;
69-
options.target = QueryTarget::Z_QUERY_TARGET_ALL;
70-
options.payload = value;
77+
options.target = query_target;
78+
if (value != nullptr) {
79+
options.payload = value;
80+
}
81+
options.timeout_ms = timeout_ms;
7182
options.attachment = ext::serialize(attachment);
72-
session.get(keyexpr, "", on_reply, on_done, std::move(options));
83+
session.get(selector.key_expr, selector.parameters, on_reply, on_done, std::move(options));
7384

7485
std::unique_lock lock(m);
7586
done_signal.wait(lock, [&done] { return done; });

examples/universal/z_get_channel.cxx

+18-11
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,31 @@ using namespace zenoh;
2323

2424
int _main(int argc, char **argv) {
2525
const char *expr = "demo/example/**";
26-
const char *value = "Get from C++";
27-
Config config = parse_args(argc, argv, {}, {{"key_expression", &expr}, {"payload value", &value}});
26+
const char *value = nullptr;
27+
const char *target = "BEST_MATCHING";
28+
const char *timeout = "10000";
2829

29-
KeyExpr keyexpr(expr);
30+
Config config = parse_args(argc, argv, {}, {},
31+
{{"-s", CmdArg{"Query selector (string)", &expr}},
32+
{"-p", CmdArg{"Query payload (string)", &value}},
33+
{"-t", CmdArg{"Query target (BEST_MATCHING | ALL | ALL_COMPLETE)", &target}},
34+
{"-o", CmdArg{"Timeout in ms (number)", &timeout}}});
35+
uint64_t timeout_ms = std::stoi(timeout);
36+
QueryTarget query_target = parse_query_target(target);
37+
Selector selector = parse_selector(expr);
3038

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

3442
std::cout << "Sending Query '" << expr << "'...\n";
35-
#if __cpp_designated_initializers >= 201707L
36-
auto replies = session.get(keyexpr, "", channels::FifoChannel(16),
37-
{.target = QueryTarget::Z_QUERY_TARGET_ALL, .payload = value});
38-
#else
43+
3944
Session::GetOptions options;
40-
options.target = QueryTarget::Z_QUERY_TARGET_ALL;
41-
options.payload = value;
42-
auto replies = session.get(keyexpr, "", channels::FifoChannel(16), std::move(options));
43-
#endif
45+
options.target = query_target;
46+
if (value != nullptr) {
47+
options.payload = value;
48+
}
49+
options.timeout_ms = timeout_ms;
50+
auto replies = session.get(selector.key_expr, selector.parameters, channels::FifoChannel(16), std::move(options));
4451

4552
for (auto res = replies.recv(); std::holds_alternative<Reply>(res); res = replies.recv()) {
4653
const auto &sample = std::get<Reply>(res).get_ok();

examples/universal/z_get_channel_non_blocking.cxx

+18-11
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,31 @@ using namespace std::chrono_literals;
2525

2626
int _main(int argc, char **argv) {
2727
const char *expr = "demo/example/**";
28-
const char *value = "Get from C++";
29-
Config config = parse_args(argc, argv, {}, {{"key_expression", &expr}, {"payload value", &value}});
28+
const char *value = nullptr;
29+
const char *target = "BEST_MATCHING";
30+
const char *timeout = "10000";
3031

31-
KeyExpr keyexpr(expr);
32+
Config config = parse_args(argc, argv, {}, {},
33+
{{"-s", CmdArg{"Query selector (string)", &expr}},
34+
{"-p", CmdArg{"Query payload (string)", &value}},
35+
{"-t", CmdArg{"Query target (BEST_MATCHING | ALL | ALL_COMPLETE)", &target}},
36+
{"-o", CmdArg{"Timeout in ms (number)", &timeout}}});
37+
uint64_t timeout_ms = std::stoi(timeout);
38+
QueryTarget query_target = parse_query_target(target);
39+
40+
Selector selector = parse_selector(expr);
3241
std::cout << "Opening session...\n";
3342
auto session = Session::open(std::move(config));
3443

3544
std::cout << "Sending Query '" << expr << "'...\n";
3645

37-
#if __cpp_designated_initializers >= 201707L
38-
auto replies = session.get(keyexpr, "", channels::FifoChannel(16),
39-
{.target = QueryTarget::Z_QUERY_TARGET_ALL, .payload = value});
40-
#else
4146
Session::GetOptions options;
42-
options.target = QueryTarget::Z_QUERY_TARGET_ALL;
43-
options.payload = value;
44-
auto replies = session.get(keyexpr, "", channels::FifoChannel(16), std::move(options));
45-
#endif
47+
options.target = query_target;
48+
if (value != nullptr) {
49+
options.payload = value;
50+
}
51+
options.timeout_ms = timeout_ms;
52+
auto replies = session.get(selector.key_expr, selector.parameters, channels::FifoChannel(16), std::move(options));
4653

4754
while (true) {
4855
auto res = replies.try_recv();

examples/universal/z_pub.cxx

+5-9
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ int _main(int argc, char **argv) {
4343
Config config = parse_args(argc, argv, {}, {{"key_expression", &keyexpr}, {"payload_value", &value}}
4444
#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API)
4545
,
46-
{{"--add-matching-listener", {CmdArg{"", &add_matching_listener, true}}}}
46+
{{"--add-matching-listener", CmdArg{"", &add_matching_listener, true}}}
4747
#endif
4848
);
4949

@@ -53,15 +53,14 @@ int _main(int argc, char **argv) {
5353
std::cout << "Declaring Publisher on '" << keyexpr << "'..." << std::endl;
5454
auto pub = session.declare_publisher(KeyExpr(keyexpr));
5555

56-
std::cout << "Publisher on '" << keyexpr << "' declared" << std::endl;
5756
#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API)
5857
if (std::string(add_matching_listener) == "true") {
5958
pub.declare_background_matching_listener(
60-
[](const Publisher::MatchingStatus &s) {
59+
[](const MatchingStatus &s) {
6160
if (s.matching) {
62-
std::cout << "Subscriber matched" << std::endl;
61+
std::cout << "Publisher has matching subscribers." << std::endl;
6362
} else {
64-
std::cout << "No subscribers matched" << std::endl;
63+
std::cout << "Publisher has NO MORE matching subscribers." << std::endl;
6564
}
6665
},
6766
closures::none);
@@ -75,13 +74,10 @@ int _main(int argc, char **argv) {
7574
ss << "[" << idx << "] " << value;
7675
auto s = ss.str();
7776
std::cout << "Putting Data ('" << keyexpr << "': '" << s << "')...\n";
78-
#if __cpp_designated_initializers >= 201707L
79-
pub.put(s, {.encoding = Encoding("text/plain")});
80-
#else
77+
8178
auto put_options = Publisher::PutOptions{};
8279
put_options.encoding = Encoding("text/plain");
8380
pub.put(s, std::move(put_options));
84-
#endif
8581
}
8682
return 0;
8783
}

examples/universal/z_queryable.cxx

+6-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ const char *value = "Queryable from C++ zenoh-pico!";
3535
const char *locator = nullptr;
3636

3737
int _main(int argc, char **argv) {
38-
Config config = parse_args(argc, argv, {}, {{"key_expression", &expr}, {"payload_value", &value}});
38+
const char *complete = "false";
39+
Config config = parse_args(argc, argv, {}, {{"key_expression", &expr}, {"payload_value", &value}},
40+
{{"--complete", {CmdArg{"", &complete, true}}}});
3941

4042
printf("Opening session...\n");
4143
auto session = Session::open(std::move(config));
@@ -64,7 +66,9 @@ int _main(int argc, char **argv) {
6466

6567
auto on_drop_queryable = []() { std::cout << "Destroying queryable\n"; };
6668

67-
auto queryable = session.declare_queryable(keyexpr, on_query, on_drop_queryable);
69+
Session::QueryableOptions opts;
70+
opts.complete = std::string(complete) == "true";
71+
auto queryable = session.declare_queryable(keyexpr, on_query, on_drop_queryable, std::move(opts));
6872

6973
std::cout << "Press CTRL-C to quit...\n";
7074
while (true) {

examples/universal/z_queryable_attachment.cxx

+7-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ const char *value = "Queryable from C++ zenoh-pico!";
3535
#endif
3636

3737
int _main(int argc, char **argv) {
38-
Config config = parse_args(argc, argv, {}, {{"key_expression", &expr}, {"payload_value", &value}});
38+
const char *complete = "false";
39+
Config config = parse_args(argc, argv, {}, {{"key_expression", &expr}, {"payload_value", &value}},
40+
{{"--complete", {CmdArg{"", &complete, true}}}});
3941

4042
std::cout << "Opening session...\n";
4143
auto session = Session::open(std::move(config));
@@ -71,7 +73,10 @@ int _main(int argc, char **argv) {
7173
};
7274

7375
auto on_drop_queryable = []() { std::cout << "Destroying queryable\n"; };
74-
auto queryable = session.declare_queryable(keyexpr, on_query, on_drop_queryable);
76+
77+
Session::QueryableOptions opts;
78+
opts.complete = std::string(complete) == "true";
79+
auto queryable = session.declare_queryable(keyexpr, on_query, on_drop_queryable, std::move(opts));
7580

7681
printf("Press CTRL-C to quit...\n");
7782
while (true) {

examples/zenohc/z_pub_shm.cxx

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ int _main(int argc, char **argv) {
3636
Config config = parse_args(argc, argv, {}, {{"key_expression", &keyexpr}, {"payload_value", &value}}
3737
#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API)
3838
,
39-
{{"--add-matching-listener", {CmdArg{"", &add_matching_listener, true}}}}
39+
{{"--add-matching-listener", CmdArg{"", &add_matching_listener, true}}}
4040
#endif
4141
);
4242

@@ -49,7 +49,7 @@ int _main(int argc, char **argv) {
4949
#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API)
5050
if (std::string(add_matching_listener) == "true") {
5151
pub.declare_background_matching_listener(
52-
[](const Publisher::MatchingStatus &s) {
52+
[](const MatchingStatus &s) {
5353
if (s.matching) {
5454
std::cout << "Subscriber matched" << std::endl;
5555
} else {

0 commit comments

Comments
 (0)