-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
z_bytes example #218
Merged
Mallets
merged 15 commits into
eclipse-zenoh:main
from
ZettaScaleLabs:align_z_bytes_example
Sep 20, 2024
Merged
z_bytes example #218
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
60ce1e7
wip on z_bytes example
yellowhatter a2ba2fa
- use crossplatform integer types for serialization example
yellowhatter 54887c2
Merge commit '22695066cb71b339d22fece6bee7ae04c1e446c8'
yellowhatter 040e849
Merge commit 'a345711fa46cf93f43d48b16045443eea8f7cb0d'
yellowhatter b3a19f3
Update z_bytes.cxx example
yellowhatter a989edc
Do not serialize Slice
yellowhatter debba5d
Fix RAW iterator example
yellowhatter ee0f296
clang format
yellowhatter 1e679f5
Add optional Protobuf entry to z_bytes example
yellowhatter 9b3b978
Merge commit '5e99201f918eb518f5a48d00836ed44ecfe62db8'
yellowhatter f7effcb
Review fixes
yellowhatter 6398656
kick the CI
yellowhatter bfba9ca
review fixes
yellowhatter 33f5db8
Update CMakeLists.txt
yellowhatter eb45243
Align proto with other examples
yellowhatter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
syntax = "proto3"; | ||
|
||
message Entity { | ||
uint32 id = 1; | ||
string name = 2; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
// | ||
// 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, <[email protected]> | ||
// | ||
#include <iostream> | ||
|
||
#ifdef ZENOH_CPP_EXMAPLE_WITH_PROTOBUF | ||
#include "entity.pb.h" | ||
#endif | ||
|
||
#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<uint32_t>(); | ||
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<std::string>(); | ||
assert(input == output); | ||
} | ||
// std::string | ||
{ | ||
const std::string input = "test"; | ||
const auto payload = Bytes::serialize(input); | ||
const auto output = payload.deserialize<std::string>(); | ||
assert(input == output); | ||
} | ||
// Corresponding encoding to be used in operations like `.put()`, `.reply()`, etc. | ||
const auto encoding = Encoding("zenoh/string"); | ||
} | ||
|
||
// Vec<u8>: The deserialization should be infallible | ||
{ | ||
const std::vector<uint8_t> input = {1, 2, 3, 4}; | ||
const auto payload = Bytes::serialize(input); | ||
const auto output = payload.deserialize<std::vector<uint8_t>>(); | ||
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<uint8_t> 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<uint32_t>(); | ||
const auto o2 = reader.read_bounded().deserialize<std::string>(); | ||
const auto o3 = reader.read_bounded().deserialize<std::vector<uint8_t>>(); | ||
|
||
assert(i1 == o1); | ||
assert(i2 == o2); | ||
assert(i3 == o3); | ||
} | ||
|
||
// 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<int32_t>()); | ||
} | ||
} | ||
|
||
// Iterator RAW | ||
{ | ||
const std::vector<uint8_t> input = {1, 2, 3, 4}; | ||
const auto payload = Bytes::serialize(input); | ||
|
||
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(); | ||
for (size_t i = 0; i < slice.len; ++i) { | ||
assert(input[idx++] == slice.data[i]); | ||
} | ||
} | ||
} | ||
|
||
// HashMap | ||
{ | ||
const std::unordered_map<uint64_t, std::string> input = {{0, "abc"}, {1, "def"}}; | ||
const auto payload = Bytes::serialize(input); | ||
const auto output = payload.deserialize<std::unordered_map<uint64_t, std::string>>(); | ||
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 | ||
Entity input; | ||
input.set_id(1234); | ||
input.set_name("John Doe"); | ||
|
||
// 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<std::string>(); | ||
|
||
// wire PB data is equal | ||
assert(input_wire_pb == output_wire_pb); | ||
|
||
// deserialize output wire PB into PB message | ||
Entity output; | ||
const auto parsed = output.ParseFromString(output_wire_pb); | ||
assert(parsed); | ||
|
||
// data is equal | ||
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"); | ||
|
||
// (Protobuf recommendation) Optional: Delete all global objects allocated by libprotobuf. | ||
google::protobuf::ShutdownProtobufLibrary(); | ||
} | ||
#endif | ||
|
||
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"; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have
Encoding
constants instead of using the string form?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not yet, will do in separate pr, since they are introduced in pico by eclipse-zenoh/zenoh-pico#673