12
12
// ZettaScale Zenoh Team, <zenoh@zettascale.tech>
13
13
//
14
14
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:: * } ;
16
18
use zenoh_examples:: CommonArgs ;
17
19
18
20
#[ tokio:: main]
@@ -35,16 +37,23 @@ async fn main() {
35
37
36
38
println ! ( "Press CTRL-C to quit..." ) ;
37
39
while let Ok ( sample) = subscriber. recv_async ( ) . await {
40
+ // Print overall payload information
41
+ let ( payload_type, payload) = handle_bytes ( sample. payload ( ) ) ;
38
42
print ! (
39
- ">> [Subscriber] Received {} ('{}': " ,
43
+ ">> [Subscriber] Received [{}] {} ('{}': '{}')" ,
44
+ payload_type,
40
45
sample. kind( ) ,
41
46
sample. key_expr( ) . as_str( ) ,
47
+ payload
42
48
) ;
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) ;
46
54
}
47
- println ! ( ")" ) ;
55
+
56
+ println ! ( ) ;
48
57
}
49
58
50
59
// // 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>) {
81
90
let args = SubArgs :: parse ( ) ;
82
91
( args. common . into ( ) , args. key )
83
92
}
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