forked from eclipse-iceoryx/iceoryx2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[eclipse-iceoryx#264] Implement C++ publish subscribe dynamic data ex…
…ample
- Loading branch information
Showing
7 changed files
with
186 additions
and
5 deletions.
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
22 changes: 22 additions & 0 deletions
22
examples/cxx/publish_subscribe_dynamic_data/CMakeLists.txt
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,22 @@ | ||
# Copyright (c) 2024 Contributors to the Eclipse Foundation | ||
# | ||
# See the NOTICE file(s) distributed with this work for additional | ||
# information regarding copyright ownership. | ||
# | ||
# This program and the accompanying materials are made available under the | ||
# terms of the Apache Software License 2.0 which is available at | ||
# https://www.apache.org/licenses/LICENSE-2.0, or the MIT license | ||
# which is available at https://opensource.org/licenses/MIT. | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
||
cmake_minimum_required(VERSION 3.22) | ||
project(example_cxx_publish_subscribe_dynamic_data LANGUAGES CXX) | ||
|
||
find_package(iceoryx2-cxx 0.3.0 REQUIRED) | ||
|
||
add_executable(example_cxx_publish_subscribe_dyn_publisher src/publisher.cpp) | ||
target_link_libraries(example_cxx_publish_subscribe_dyn_publisher iceoryx2-cxx::static-lib-cxx) | ||
|
||
add_executable(example_cxx_publish_subscribe_dyn_subscriber src/subscriber.cpp) | ||
target_link_libraries(example_cxx_publish_subscribe_dyn_subscriber iceoryx2-cxx::static-lib-cxx) |
60 changes: 60 additions & 0 deletions
60
examples/cxx/publish_subscribe_dynamic_data/src/publisher.cpp
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,60 @@ | ||
// Copyright (c) 2024 Contributors to the Eclipse Foundation | ||
// | ||
// See the NOTICE file(s) distributed with this work for additional | ||
// information regarding copyright ownership. | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Apache Software License 2.0 which is available at | ||
// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license | ||
// which is available at https://opensource.org/licenses/MIT. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
||
#include <cstdint> | ||
#include <iostream> | ||
|
||
#include "iox/duration.hpp" | ||
#include "iox/slice.hpp" | ||
#include "iox2/node.hpp" | ||
|
||
constexpr iox::units::Duration CYCLE_TIME = | ||
iox::units::Duration::fromSeconds(1); | ||
|
||
int main() { | ||
using namespace iox2; | ||
auto node = NodeBuilder().template create<ServiceType::Ipc>().expect( | ||
"successful node creation"); | ||
|
||
auto service = | ||
node.service_builder(ServiceName::create("Service With Dynamic Data") | ||
.expect("valid service name")) | ||
.publish_subscribe<iox::Slice<uint8_t>>() | ||
.open_or_create() | ||
.expect("successful service creation/opening"); | ||
|
||
uint64_t worst_case_memory_size = 1024; | ||
auto publisher = service.publisher_builder() | ||
.max_slice_len(worst_case_memory_size) | ||
.create() | ||
.expect("successful publisher creation"); | ||
|
||
auto counter = 1; | ||
|
||
while (node.wait(CYCLE_TIME) == NodeEvent::Tick) { | ||
counter += 1; | ||
|
||
auto required_memory_size = (8 + counter) % 16; | ||
auto sample = publisher.loan_slice_uninit(required_memory_size) | ||
.expect("acquire sample"); | ||
sample.write_from_fn( | ||
[&](auto byte_idx) { return (byte_idx + counter) % 255; }); | ||
|
||
send_sample(std::move(sample)).expect("send successful"); | ||
|
||
std::cout << "Send sample " << counter << "..." << std::endl; | ||
} | ||
|
||
std::cout << "exit" << std::endl; | ||
|
||
return 0; | ||
} |
53 changes: 53 additions & 0 deletions
53
examples/cxx/publish_subscribe_dynamic_data/src/subscriber.cpp
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,53 @@ | ||
// Copyright (c) 2024 Contributors to the Eclipse Foundation | ||
// | ||
// See the NOTICE file(s) distributed with this work for additional | ||
// information regarding copyright ownership. | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Apache Software License 2.0 which is available at | ||
// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license | ||
// which is available at https://opensource.org/licenses/MIT. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
||
#include <cstdint> | ||
#include <iostream> | ||
|
||
#include "iox/duration.hpp" | ||
#include "iox/slice.hpp" | ||
#include "iox2/node.hpp" | ||
|
||
constexpr iox::units::Duration CYCLE_TIME = | ||
iox::units::Duration::fromSeconds(1); | ||
|
||
int main() { | ||
using namespace iox2; | ||
auto node = NodeBuilder().template create<ServiceType::Ipc>().expect( | ||
"successful node creation"); | ||
|
||
auto service = | ||
node.service_builder(ServiceName::create("Service With Dynamic Data") | ||
.expect("valid service name")) | ||
.publish_subscribe<iox::Slice<uint8_t>>() | ||
.open_or_create() | ||
.expect("successful service creation/opening"); | ||
|
||
auto subscriber = service.subscriber_builder().create().expect( | ||
"successful subscriber creation"); | ||
|
||
while (node.wait(CYCLE_TIME) == NodeEvent::Tick) { | ||
auto sample = subscriber.receive().expect("receive succeeds"); | ||
while (sample.has_value()) { | ||
std::cout << "received " << sample->payload().size() << " bytes: "; | ||
for (auto byte : sample->payload()) { | ||
std::cout << std::hex << byte << " "; | ||
} | ||
std::cout << std::endl; | ||
sample = subscriber.receive().expect("receive succeeds"); | ||
} | ||
} | ||
|
||
std::cout << "exit" << std::endl; | ||
|
||
return 0; | ||
} |
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,35 @@ | ||
// Copyright (c) 2024 Contributors to the Eclipse Foundation | ||
// | ||
// See the NOTICE file(s) distributed with this work for additional | ||
// information regarding copyright ownership. | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Apache Software License 2.0 which is available at | ||
// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license | ||
// which is available at https://opensource.org/licenses/MIT. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
#ifndef IOX_SLICE_HPP_ | ||
#define IOX_SLICE_HPP_ | ||
|
||
#include <cstdint> | ||
|
||
namespace iox { | ||
template <typename T> | ||
class Slice { | ||
public: | ||
using iterator = T*; | ||
using const_iterator = const T*; | ||
|
||
uint64_t size() const {} | ||
const T& operator[](const uint64_t n) const {} | ||
T& operator[](const uint64_t n) {} | ||
|
||
iterator begin() {} | ||
const_iterator begin() const {} | ||
iterator end() {} | ||
const_iterator end() const {} | ||
}; | ||
} // namespace iox | ||
|
||
#endif |
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