From 60ce1e7da102b1e3fcd52d5f8332f65ad0cb96a2 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Tue, 17 Sep 2024 12:57:48 +0300 Subject: [PATCH 01/12] wip on z_bytes example --- examples/universal/z_bytes.cxx | 134 +++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 examples/universal/z_bytes.cxx diff --git a/examples/universal/z_bytes.cxx b/examples/universal/z_bytes.cxx new file mode 100644 index 00000000..4e30669a --- /dev/null +++ b/examples/universal/z_bytes.cxx @@ -0,0 +1,134 @@ +// +// Copyright (c) 2022 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 +// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// +// Contributors: +// ZettaScale Zenoh Team, +// +#include + +#include "stdio.h" +#include "zenoh.hxx" +using namespace zenoh; + +int _main(int argc, char **argv) { + // Numeric: u8, u16, u32, u128, usize, i8, i16, i32, i128, isize, f32, f64 + { + const uint32_t input = 1234; + const auto payload = Bytes::serialize(input); + const auto output = payload.deserialize(); + assert(input == output); + // Corresponding encoding to be used in operations like `.put()`, `.reply()`, etc. + const auto encoding = Encoding("zenoh/uint32"); + } + + // String + { + // C-String + { + const char *input = "test"; + const auto payload = Bytes::serialize(input); + const auto output = payload.deserialize(); + assert(input == output); + } + // std::string + { + const std::string input = "test"; + const auto payload = Bytes::serialize(input); + const auto output = payload.deserialize(); + assert(input == output); + } + // Corresponding encoding to be used in operations like `.put()`, `.reply()`, etc. + const auto encoding = Encoding("zenoh/string"); + } + + // Vec: The deserialization should be infallible + { + const std::vector input = {1, 2, 3, 4}; + const auto payload = Bytes::serialize(input); + const auto output = payload.deserialize>(); + assert(input == output); + // Corresponding encoding to be used in operations like `.put()`, `.reply()`, etc. + const auto encoding = Encoding("zenoh/bytes"); + } + + // Writer & Reader + { + // serialization + Bytes bytes; + auto writer = bytes.writer(); + + const uint32_t i1 = 1234; + const std::string i2 = "test"; + const std::vector i3 = {1, 2, 3, 4}; + + writer.append_bounded(i1); + writer.append_bounded(i2); + writer.append_bounded(i3); + + // deserialization + auto reader = bytes.reader(); + + const auto o1 = reader.read_bounded().deserialize(); + const auto o2 = reader.read_bounded().deserialize(); + const auto o3 = reader.read_bounded().deserialize>(); + + assert(i1 == o1); + assert(i2 == o2); + assert(i3 == o3); + } + + //// Tuple + //{ + // let input = (1234_u32, String::from("test")); + // let payload = ZBytes::serialize(input.clone()); + // let output: (u32, String) = payload.deserialize().unwrap(); + // assert_eq!(input, output); + //} + + // Iterator + { + const int32_t input[] = {1, 2, 3, 4}; + const auto payload = Bytes::serialize_from_iter(input, input + 4); + + auto idx = 0; + auto it = payload.iter(); + for (auto elem = it.next(); elem.has_value(); elem = it.next()) { + assert(input[idx++] == elem.value().deserialize()); + } + } + + //// Iterator RAW + // let input: [i32; 4] = [1, 2, 3, 4]; + // let payload = ZBytes::from_iter(input.iter()); + // for slice in payload.slices() { + // println!("{:02x?}", slice); + // } + + // HashMap + { + const std::unordered_map input = {{0, "abc"}, {1, "def"}}; + const auto payload = Bytes::serialize(input); + const auto output = payload.deserialize>(); + assert(input == output); + } + + return 0; +} + +int main(int argc, char **argv) { + try { +#ifdef ZENOHCXX_ZENOHC + init_log_from_env_or("error"); +#endif + return _main(argc, argv); + } catch (ZException e) { + std::cout << "Received an error :" << e.what() << "\n"; + } +} \ No newline at end of file From a2ba2fa9debc6cf698a1813cd783898d75e9b31b Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Tue, 17 Sep 2024 15:08:45 +0300 Subject: [PATCH 02/12] - use crossplatform integer types for serialization example --- examples/universal/z_bytes.cxx | 58 ++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/examples/universal/z_bytes.cxx b/examples/universal/z_bytes.cxx index 4e30669a..37e85a28 100644 --- a/examples/universal/z_bytes.cxx +++ b/examples/universal/z_bytes.cxx @@ -113,12 +113,66 @@ int _main(int argc, char **argv) { // HashMap { - const std::unordered_map input = {{0, "abc"}, {1, "def"}}; + const std::unordered_map input = {{0, "abc"}, {1, "def"}}; const auto payload = Bytes::serialize(input); - const auto output = payload.deserialize>(); + const auto output = payload.deserialize>(); assert(input == output); } + /* + // JSON + let data = r#" + { + "name": "John Doe", + "age": 43, + "phones": [ + "+44 1234567", + "+44 2345678" + ] + }"#; + let input: serde_json::Value = serde_json::from_str(data).unwrap(); + let payload = ZBytes::try_serialize(input.clone()).unwrap(); + let output: serde_json::Value = payload.deserialize().unwrap(); + assert_eq!(input, output); + // Corresponding encoding to be used in operations like `.put()`, `.reply()`, etc. + // let encoding = Encoding::APPLICATION_JSON; + + // YAML + let data = r#" + name: "John Doe" + age: 43 + phones: + - "+44 1234567" + - "+44 2345678" + "#; + let input: serde_yaml::Value = serde_yaml::from_str(data).unwrap(); + let payload = ZBytes::try_serialize(input.clone()).unwrap(); + let output: serde_yaml::Value = payload.deserialize().unwrap(); + assert_eq!(input, output); + // Corresponding encoding to be used in operations like `.put()`, `.reply()`, etc. + // let encoding = Encoding::APPLICATION_YAML; + + // Protobuf + use prost::Message; + #[derive(Message, Eq, PartialEq)] + struct EntityInfo { + #[prost(uint32)] + id: u32, + #[prost(string)] + name: String, + } + let input = EntityInfo { + id: 1234, + name: String::from("John Doe"), + }; + let payload = ZBytes::from(input.encode_to_vec()); + let output = + EntityInfo::decode(Cursor::new(payload.deserialize::>().unwrap())).unwrap(); + assert_eq!(input, output); + // Corresponding encoding to be used in operations like `.put()`, `.reply()`, etc. + // let encoding = Encoding::APPLICATION_PROTOBUF; + */ + return 0; } From b3a19f37610fb6b2e53a1c1192648bec7c40df69 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Thu, 19 Sep 2024 13:08:51 +0300 Subject: [PATCH 03/12] Update z_bytes.cxx example --- examples/universal/z_bytes.cxx | 89 +++++++--------------------------- 1 file changed, 18 insertions(+), 71 deletions(-) diff --git a/examples/universal/z_bytes.cxx b/examples/universal/z_bytes.cxx index 37e85a28..88c9c27f 100644 --- a/examples/universal/z_bytes.cxx +++ b/examples/universal/z_bytes.cxx @@ -17,7 +17,7 @@ #include "zenoh.hxx" using namespace zenoh; -int _main(int argc, char **argv) { +int _main(int argc, char** argv) { // Numeric: u8, u16, u32, u128, usize, i8, i16, i32, i128, isize, f32, f64 { const uint32_t input = 1234; @@ -32,7 +32,7 @@ int _main(int argc, char **argv) { { // C-String { - const char *input = "test"; + const char* input = "test"; const auto payload = Bytes::serialize(input); const auto output = payload.deserialize(); assert(input == output); @@ -84,14 +84,6 @@ int _main(int argc, char **argv) { assert(i3 == o3); } - //// Tuple - //{ - // let input = (1234_u32, String::from("test")); - // let payload = ZBytes::serialize(input.clone()); - // let output: (u32, String) = payload.deserialize().unwrap(); - // assert_eq!(input, output); - //} - // Iterator { const int32_t input[] = {1, 2, 3, 4}; @@ -104,12 +96,21 @@ int _main(int argc, char **argv) { } } - //// Iterator RAW - // let input: [i32; 4] = [1, 2, 3, 4]; - // let payload = ZBytes::from_iter(input.iter()); - // for slice in payload.slices() { - // println!("{:02x?}", slice); - // } + // Iterator RAW + { + const uint8_t input[] = {1, 2, 3, 4}; + const auto slice = make_slice(input, 4); + const auto payload = Bytes::serialize(slice); + + auto idx = 0; + auto it = payload.slice_iter(); + for (auto elem = it.next(); elem.has_value(); elem = it.next()) { + const auto& slice = elem.value(); + for (size_t i = 0; i < slice.len; ++i) { + assert(input[idx++] == slice.data[i]); + } + } + } // HashMap { @@ -119,64 +120,10 @@ int _main(int argc, char **argv) { assert(input == output); } - /* - // JSON - let data = r#" - { - "name": "John Doe", - "age": 43, - "phones": [ - "+44 1234567", - "+44 2345678" - ] - }"#; - let input: serde_json::Value = serde_json::from_str(data).unwrap(); - let payload = ZBytes::try_serialize(input.clone()).unwrap(); - let output: serde_json::Value = payload.deserialize().unwrap(); - assert_eq!(input, output); - // Corresponding encoding to be used in operations like `.put()`, `.reply()`, etc. - // let encoding = Encoding::APPLICATION_JSON; - - // YAML - let data = r#" - name: "John Doe" - age: 43 - phones: - - "+44 1234567" - - "+44 2345678" - "#; - let input: serde_yaml::Value = serde_yaml::from_str(data).unwrap(); - let payload = ZBytes::try_serialize(input.clone()).unwrap(); - let output: serde_yaml::Value = payload.deserialize().unwrap(); - assert_eq!(input, output); - // Corresponding encoding to be used in operations like `.put()`, `.reply()`, etc. - // let encoding = Encoding::APPLICATION_YAML; - - // Protobuf - use prost::Message; - #[derive(Message, Eq, PartialEq)] - struct EntityInfo { - #[prost(uint32)] - id: u32, - #[prost(string)] - name: String, - } - let input = EntityInfo { - id: 1234, - name: String::from("John Doe"), - }; - let payload = ZBytes::from(input.encode_to_vec()); - let output = - EntityInfo::decode(Cursor::new(payload.deserialize::>().unwrap())).unwrap(); - assert_eq!(input, output); - // Corresponding encoding to be used in operations like `.put()`, `.reply()`, etc. - // let encoding = Encoding::APPLICATION_PROTOBUF; - */ - return 0; } -int main(int argc, char **argv) { +int main(int argc, char** argv) { try { #ifdef ZENOHCXX_ZENOHC init_log_from_env_or("error"); From a989edc1ba0d2cc1eb0f0f58cccbd8470e251bf0 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Thu, 19 Sep 2024 15:05:01 +0300 Subject: [PATCH 04/12] Do not serialize Slice --- examples/universal/z_bytes.cxx | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/examples/universal/z_bytes.cxx b/examples/universal/z_bytes.cxx index 88c9c27f..5f1cedda 100644 --- a/examples/universal/z_bytes.cxx +++ b/examples/universal/z_bytes.cxx @@ -98,9 +98,8 @@ int _main(int argc, char** argv) { // Iterator RAW { - const uint8_t input[] = {1, 2, 3, 4}; - const auto slice = make_slice(input, 4); - const auto payload = Bytes::serialize(slice); + const int32_t input[] = {1, 2, 3, 4}; + const auto payload = Bytes::serialize_from_iter(input, input + 4); auto idx = 0; auto it = payload.slice_iter(); From debba5d958a0b450c4a86e366ca26f017ccbb5cb Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Thu, 19 Sep 2024 15:26:42 +0300 Subject: [PATCH 05/12] Fix RAW iterator example --- examples/universal/z_bytes.cxx | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/examples/universal/z_bytes.cxx b/examples/universal/z_bytes.cxx index 5f1cedda..0bf73c40 100644 --- a/examples/universal/z_bytes.cxx +++ b/examples/universal/z_bytes.cxx @@ -98,15 +98,16 @@ int _main(int argc, char** argv) { // Iterator RAW { - const int32_t input[] = {1, 2, 3, 4}; - const auto payload = Bytes::serialize_from_iter(input, input + 4); - + const std::vector input = {1, 2, 3, 4}; + const auto payload = Bytes::serialize(input); + auto idx = 0; auto it = payload.slice_iter(); for (auto elem = it.next(); elem.has_value(); elem = it.next()) { const auto& slice = elem.value(); for (size_t i = 0; i < slice.len; ++i) { - assert(input[idx++] == slice.data[i]); + const auto index = idx++; + assert(input[index] == slice.data[i]); } } } From ee0f296ed156bce7f97af3752d8458a81b53645a Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Thu, 19 Sep 2024 15:43:32 +0300 Subject: [PATCH 06/12] clang format --- examples/universal/z_bytes.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/universal/z_bytes.cxx b/examples/universal/z_bytes.cxx index 0bf73c40..a6484b39 100644 --- a/examples/universal/z_bytes.cxx +++ b/examples/universal/z_bytes.cxx @@ -100,7 +100,7 @@ int _main(int argc, char** argv) { { const std::vector input = {1, 2, 3, 4}; const auto payload = Bytes::serialize(input); - + auto idx = 0; auto it = payload.slice_iter(); for (auto elem = it.next(); elem.has_value(); elem = it.next()) { From 1e679f56d4b082d2eba2545aa97817f10cc526c4 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Fri, 20 Sep 2024 00:22:12 +0300 Subject: [PATCH 07/12] Add optional Protobuf entry to z_bytes example --- CMakeLists.txt | 1 + examples/CMakeLists.txt | 23 ++++++++++++++ examples/universal/proto/test.proto | 8 +++++ examples/universal/z_bytes.cxx | 48 +++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+) create mode 100644 examples/universal/proto/test.proto diff --git a/CMakeLists.txt b/CMakeLists.txt index 3c0f9c9d..66fe4386 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,6 +28,7 @@ status_print(project_version) declare_cache_var(ZENOHCXX_ZENOHC ON BOOL "Build for Zenoh-c target") declare_cache_var(ZENOHCXX_ZENOHPICO OFF BOOL "Build for Zenoh-pico target") +declare_cache_var(ZENOHCXX_EXAMPLES_PROTOBUF ON BOOL "Build Protobuf example (turn off if you have problems with installed Protobuf version)") set_default_build_type(Release) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 8bfcad8f..71eacb16 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -10,6 +10,26 @@ if(ZENOHCXX_ZENOHPICO) add_dependencies(examples examples_zenohpico) endif() +function(add_protobuf target) + if(ZENOHCXX_EXAMPLES_PROTOBUF) + find_package(Protobuf) + if(Protobuf_FOUND) + message(STATUS "Found Protobuf ${protobuf_VERSION}, will build Protobuf example!") + + protobuf_generate_cpp(pb_src pb_hdr ${CMAKE_CURRENT_LIST_DIR}/universal/proto/test.proto) + + add_library(example_message ${pb_hdr} ${pb_src}) + target_include_directories(example_message INTERFACE ${CMAKE_CURRENT_BINARY_DIR}) + target_link_libraries(example_message PUBLIC protobuf::libprotobuf) + + target_link_libraries(${target} PRIVATE example_message) + target_compile_definitions(${target} PRIVATE -DZENOH_CPP_EXMAPLE_WITH_PROTOBUF) + else() + message("Protobuf not found, will build without Protobuf example!") + endif() + endif() +endfunction() + function(add_example file mode lib) get_filename_component(filename ${file} NAME_WE) set(target ${filename}_${mode}) @@ -22,6 +42,9 @@ function(add_example file mode lib) target_link_libraries(${target} PUBLIC ${lib}) set_property(TARGET ${target} PROPERTY LANGUAGE CXX) set_property(TARGET ${target} PROPERTY CXX_STANDARD 17) + if("${target}" STREQUAL "z_bytes_zenohc") + add_protobuf(${target}) + endif() copy_dlls(${target}) endfunction() diff --git a/examples/universal/proto/test.proto b/examples/universal/proto/test.proto new file mode 100644 index 00000000..834dedae --- /dev/null +++ b/examples/universal/proto/test.proto @@ -0,0 +1,8 @@ +syntax = "proto3"; + +message Book { + int64 isbn = 1; + string title = 2; + string author = 3; +} + diff --git a/examples/universal/z_bytes.cxx b/examples/universal/z_bytes.cxx index a6484b39..9a438726 100644 --- a/examples/universal/z_bytes.cxx +++ b/examples/universal/z_bytes.cxx @@ -13,6 +13,10 @@ // #include +#ifdef ZENOH_CPP_EXMAPLE_WITH_PROTOBUF +#include "test.pb.h" +#endif + #include "stdio.h" #include "zenoh.hxx" using namespace zenoh; @@ -120,6 +124,50 @@ int _main(int argc, char** argv) { assert(input == output); } +#ifdef ZENOH_CPP_EXMAPLE_WITH_PROTOBUF + // Protobuf + // This example is conditionally compiled depending on build system being able to find Protobuf installation + { + // (Protobuf recommendation) Verify that the version of the library that we linked against is + // compatible with the version of the headers we compiled against. + GOOGLE_PROTOBUF_VERIFY_VERSION; + + // Construct PB message + Book input; + input.set_author("H. P. Lovecraft"); + input.set_title("The Call of Cthulhu"); + input.set_isbn(931082); + + // Serialize PB message into wire format + const auto input_wire_pb = input.SerializeAsString(); + + // Put PB wire format into Bytes + const auto payload = Bytes::serialize(input_wire_pb); + + // Extract PB wire format + const auto output_wire_pb = payload.deserialize(); + + // wire PB data is equal + assert(input_wire_pb == output_wire_pb); + + // deserialize output wire PB into PB message + Book output; + const auto parsed = output.ParseFromString(output_wire_pb); + assert(parsed); + + // data is equal + assert(input.author() == output.author()); + assert(input.title() == output.title()); + assert(input.isbn() == output.isbn()); + + // Corresponding encoding to be used in operations like `.put()`, `.reply()`, etc. + const auto encoding = Encoding("application/protobuf"); + + // (Protobuf recommendation) Optional: Delete all global objects allocated by libprotobuf. + google::protobuf::ShutdownProtobufLibrary(); + } +#endif + return 0; } From f7effcbbe2251a339d4468d60136adcfdc7dfe54 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Fri, 20 Sep 2024 00:33:47 +0300 Subject: [PATCH 08/12] Review fixes --- examples/universal/z_bytes.cxx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/universal/z_bytes.cxx b/examples/universal/z_bytes.cxx index 9a438726..0dea29b5 100644 --- a/examples/universal/z_bytes.cxx +++ b/examples/universal/z_bytes.cxx @@ -110,8 +110,7 @@ int _main(int argc, char** argv) { for (auto elem = it.next(); elem.has_value(); elem = it.next()) { const auto& slice = elem.value(); for (size_t i = 0; i < slice.len; ++i) { - const auto index = idx++; - assert(input[index] == slice.data[i]); + assert(input[idx++] == slice.data[i]); } } } From 639865669951c580cc108c1b7b2f3e6d96876f29 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Fri, 20 Sep 2024 08:39:45 +0300 Subject: [PATCH 09/12] kick the CI --- examples/universal/z_bytes.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/universal/z_bytes.cxx b/examples/universal/z_bytes.cxx index 0dea29b5..d250380d 100644 --- a/examples/universal/z_bytes.cxx +++ b/examples/universal/z_bytes.cxx @@ -105,7 +105,7 @@ int _main(int argc, char** argv) { const std::vector input = {1, 2, 3, 4}; const auto payload = Bytes::serialize(input); - auto idx = 0; + size_t idx = 0; auto it = payload.slice_iter(); for (auto elem = it.next(); elem.has_value(); elem = it.next()) { const auto& slice = elem.value(); From bfba9ca4b51823160c8bade58529cf59801530a0 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Fri, 20 Sep 2024 10:06:07 +0300 Subject: [PATCH 10/12] review fixes --- examples/CMakeLists.txt | 2 +- examples/universal/z_bytes.cxx | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 71eacb16..cc8232cd 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -42,7 +42,7 @@ function(add_example file mode lib) target_link_libraries(${target} PUBLIC ${lib}) set_property(TARGET ${target} PROPERTY LANGUAGE CXX) set_property(TARGET ${target} PROPERTY CXX_STANDARD 17) - if("${target}" STREQUAL "z_bytes_zenohc") + if("${target}" STREQUAL "z_bytes_*") add_protobuf(${target}) endif() copy_dlls(${target}) diff --git a/examples/universal/z_bytes.cxx b/examples/universal/z_bytes.cxx index d250380d..76997a34 100644 --- a/examples/universal/z_bytes.cxx +++ b/examples/universal/z_bytes.cxx @@ -17,7 +17,6 @@ #include "test.pb.h" #endif -#include "stdio.h" #include "zenoh.hxx" using namespace zenoh; From 33f5db81d987bc9e112d7af09eb01831808d2cb9 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Fri, 20 Sep 2024 10:06:58 +0300 Subject: [PATCH 11/12] Update CMakeLists.txt --- examples/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index cc8232cd..3f84b26f 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -42,7 +42,7 @@ function(add_example file mode lib) target_link_libraries(${target} PUBLIC ${lib}) set_property(TARGET ${target} PROPERTY LANGUAGE CXX) set_property(TARGET ${target} PROPERTY CXX_STANDARD 17) - if("${target}" STREQUAL "z_bytes_*") + if("${target}" MATCHES "z_bytes_*") add_protobuf(${target}) endif() copy_dlls(${target}) From eb45243347f1581844e42ec3a4eb75aaf02c6119 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Fri, 20 Sep 2024 11:11:36 +0300 Subject: [PATCH 12/12] Align proto with other examples --- examples/CMakeLists.txt | 2 +- examples/universal/proto/entity.proto | 6 ++++++ examples/universal/proto/test.proto | 8 -------- examples/universal/z_bytes.cxx | 16 +++++++--------- 4 files changed, 14 insertions(+), 18 deletions(-) create mode 100644 examples/universal/proto/entity.proto delete mode 100644 examples/universal/proto/test.proto diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 3f84b26f..48e9cf5a 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -16,7 +16,7 @@ function(add_protobuf target) if(Protobuf_FOUND) message(STATUS "Found Protobuf ${protobuf_VERSION}, will build Protobuf example!") - protobuf_generate_cpp(pb_src pb_hdr ${CMAKE_CURRENT_LIST_DIR}/universal/proto/test.proto) + protobuf_generate_cpp(pb_src pb_hdr ${CMAKE_CURRENT_LIST_DIR}/universal/proto/entity.proto) add_library(example_message ${pb_hdr} ${pb_src}) target_include_directories(example_message INTERFACE ${CMAKE_CURRENT_BINARY_DIR}) diff --git a/examples/universal/proto/entity.proto b/examples/universal/proto/entity.proto new file mode 100644 index 00000000..6908ddae --- /dev/null +++ b/examples/universal/proto/entity.proto @@ -0,0 +1,6 @@ +syntax = "proto3"; + +message Entity { + uint32 id = 1; + string name = 2; +} \ No newline at end of file diff --git a/examples/universal/proto/test.proto b/examples/universal/proto/test.proto deleted file mode 100644 index 834dedae..00000000 --- a/examples/universal/proto/test.proto +++ /dev/null @@ -1,8 +0,0 @@ -syntax = "proto3"; - -message Book { - int64 isbn = 1; - string title = 2; - string author = 3; -} - diff --git a/examples/universal/z_bytes.cxx b/examples/universal/z_bytes.cxx index 76997a34..f3065abb 100644 --- a/examples/universal/z_bytes.cxx +++ b/examples/universal/z_bytes.cxx @@ -14,7 +14,7 @@ #include #ifdef ZENOH_CPP_EXMAPLE_WITH_PROTOBUF -#include "test.pb.h" +#include "entity.pb.h" #endif #include "zenoh.hxx" @@ -131,10 +131,9 @@ int _main(int argc, char** argv) { GOOGLE_PROTOBUF_VERIFY_VERSION; // Construct PB message - Book input; - input.set_author("H. P. Lovecraft"); - input.set_title("The Call of Cthulhu"); - input.set_isbn(931082); + Entity input; + input.set_id(1234); + input.set_name("John Doe"); // Serialize PB message into wire format const auto input_wire_pb = input.SerializeAsString(); @@ -149,14 +148,13 @@ int _main(int argc, char** argv) { assert(input_wire_pb == output_wire_pb); // deserialize output wire PB into PB message - Book output; + Entity output; const auto parsed = output.ParseFromString(output_wire_pb); assert(parsed); // data is equal - assert(input.author() == output.author()); - assert(input.title() == output.title()); - assert(input.isbn() == output.isbn()); + assert(input.id() == output.id()); + assert(input.name() == output.name()); // Corresponding encoding to be used in operations like `.put()`, `.reply()`, etc. const auto encoding = Encoding("application/protobuf");