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

Lines changed: 10 additions & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 11 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 27 additions & 0 deletions
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

Lines changed: 17 additions & 10 deletions
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

Lines changed: 17 additions & 6 deletions
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

Lines changed: 18 additions & 11 deletions
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();

0 commit comments

Comments
 (0)