Skip to content

Commit 4827f39

Browse files
authored
Make SHM sub\queryable examples more robust (#1261)
* Make SHM sub\queryable examples more robust * fix clippy * fix clippy
1 parent 7aeb4ad commit 4827f39

File tree

2 files changed

+111
-16
lines changed

2 files changed

+111
-16
lines changed

examples/examples/z_queryable_shm.rs

+59-10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
//
1414
use clap::Parser;
1515
use zenoh::{
16+
bytes::ZBytes,
1617
key_expr::KeyExpr,
1718
prelude::*,
1819
shm::{
@@ -63,18 +64,29 @@ async fn main() {
6364

6465
println!("Press CTRL-C to quit...");
6566
while let Ok(query) = queryable.recv_async().await {
66-
print!(
67-
">> [Queryable] Received Query '{}' ('{}'",
68-
query.selector(),
69-
query.key_expr().as_str(),
70-
);
71-
if let Some(query_payload) = query.payload() {
72-
match query_payload.deserialize::<&zshm>() {
73-
Ok(p) => print!(": '{}'", String::from_utf8_lossy(p)),
74-
Err(e) => print!(": 'Not a ShmBufInner: {:?}'", e),
67+
// Print overall query payload information
68+
match query.payload() {
69+
Some(payload) => {
70+
let (payload_type, payload) = handle_bytes(payload);
71+
print!(
72+
">> [Queryable] Received Query [{}] ('{}': '{}')",
73+
payload_type,
74+
query.selector(),
75+
payload
76+
);
77+
}
78+
None => {
79+
print!(">> Received Query '{}'", query.selector());
7580
}
81+
};
82+
83+
// Print attachment information
84+
if let Some(att) = query.attachment() {
85+
let (attachment_type, attachment) = handle_bytes(att);
86+
print!(" ({}: {})", attachment_type, attachment);
7687
}
77-
println!(")");
88+
89+
println!();
7890

7991
// Allocate an SHM buffer
8092
// NOTE: For allocation API please check z_alloc_shm.rs example
@@ -119,3 +131,40 @@ fn parse_args() -> (Config, KeyExpr<'static>, String, bool) {
119131
let args = Args::parse();
120132
(args.common.into(), args.key, args.payload, args.complete)
121133
}
134+
135+
fn handle_bytes(bytes: &ZBytes) -> (&str, String) {
136+
// Determine buffer type for indication purpose
137+
let bytes_type = {
138+
// if Zenoh is built without SHM support, the only buffer type it can receive is RAW
139+
#[cfg(not(feature = "shared-memory"))]
140+
{
141+
"RAW"
142+
}
143+
144+
// if Zenoh is built with SHM support but without SHM API (that is unstable), it can
145+
// receive buffers of any type, but there is no way to detect the buffer type
146+
#[cfg(all(feature = "shared-memory", not(feature = "unstable")))]
147+
{
148+
"UNKNOWN"
149+
}
150+
151+
// if Zenoh is built with SHM support and with SHM API we can detect the exact buffer type
152+
#[cfg(all(feature = "shared-memory", feature = "unstable"))]
153+
match bytes.deserialize::<&zshm>() {
154+
Ok(_) => "SHM",
155+
Err(_) => "RAW",
156+
}
157+
};
158+
159+
// In order to indicate the real underlying buffer type the code above is written ^^^
160+
// Sample is SHM-agnostic: Sample handling code works both with SHM and RAW data transparently.
161+
// In other words, the common application compiled with "shared-memory" feature will be able to
162+
// handle incoming SHM data without any changes in the application code.
163+
//
164+
// Refer to z_bytes.rs to see how to deserialize different types of message
165+
let bytes_string = bytes
166+
.deserialize::<String>()
167+
.unwrap_or_else(|e| format!("{}", e));
168+
169+
(bytes_type, bytes_string)
170+
}

examples/examples/z_sub_shm.rs

+52-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
// ZettaScale Zenoh Team, <zenoh@zettascale.tech>
1313
//
1414
use clap::Parser;
15-
use zenoh::{config::Config, key_expr::KeyExpr, prelude::*, shm::zshm};
15+
#[cfg(all(feature = "shared-memory", feature = "unstable"))]
16+
use zenoh::shm::zshm;
17+
use zenoh::{bytes::ZBytes, config::Config, key_expr::KeyExpr, prelude::*};
1618
use zenoh_examples::CommonArgs;
1719

1820
#[tokio::main]
@@ -35,16 +37,23 @@ async fn main() {
3537

3638
println!("Press CTRL-C to quit...");
3739
while let Ok(sample) = subscriber.recv_async().await {
40+
// Print overall payload information
41+
let (payload_type, payload) = handle_bytes(sample.payload());
3842
print!(
39-
">> [Subscriber] Received {} ('{}': ",
43+
">> [Subscriber] Received [{}] {} ('{}': '{}')",
44+
payload_type,
4045
sample.kind(),
4146
sample.key_expr().as_str(),
47+
payload
4248
);
43-
match sample.payload().deserialize::<&zshm>() {
44-
Ok(payload) => print!("'{}'", String::from_utf8_lossy(payload)),
45-
Err(e) => print!("'Not a ShmBufInner: {:?}'", e),
49+
50+
// Print attachment information
51+
if let Some(att) = sample.attachment() {
52+
let (attachment_type, attachment) = handle_bytes(att);
53+
print!(" ({}: {})", attachment_type, attachment);
4654
}
47-
println!(")");
55+
56+
println!();
4857
}
4958

5059
// // Try to get a mutable reference to the SHM buffer. If this subscriber is the only subscriber
@@ -81,3 +90,40 @@ fn parse_args() -> (Config, KeyExpr<'static>) {
8190
let args = SubArgs::parse();
8291
(args.common.into(), args.key)
8392
}
93+
94+
fn handle_bytes(bytes: &ZBytes) -> (&str, String) {
95+
// Determine buffer type for indication purpose
96+
let bytes_type = {
97+
// if Zenoh is built without SHM support, the only buffer type it can receive is RAW
98+
#[cfg(not(feature = "shared-memory"))]
99+
{
100+
"RAW"
101+
}
102+
103+
// if Zenoh is built with SHM support but without SHM API (that is unstable), it can
104+
// receive buffers of any type, but there is no way to detect the buffer type
105+
#[cfg(all(feature = "shared-memory", not(feature = "unstable")))]
106+
{
107+
"UNKNOWN"
108+
}
109+
110+
// if Zenoh is built with SHM support and with SHM API we can detect the exact buffer type
111+
#[cfg(all(feature = "shared-memory", feature = "unstable"))]
112+
match bytes.deserialize::<&zshm>() {
113+
Ok(_) => "SHM",
114+
Err(_) => "RAW",
115+
}
116+
};
117+
118+
// In order to indicate the real underlying buffer type the code above is written ^^^
119+
// Sample is SHM-agnostic: Sample handling code works both with SHM and RAW data transparently.
120+
// In other words, the common application compiled with "shared-memory" feature will be able to
121+
// handle incoming SHM data without any changes in the application code.
122+
//
123+
// Refer to z_bytes.rs to see how to deserialize different types of message
124+
let bytes_string = bytes
125+
.deserialize::<String>()
126+
.unwrap_or_else(|e| format!("{}", e));
127+
128+
(bytes_type, bytes_string)
129+
}

0 commit comments

Comments
 (0)