Skip to content

Commit

Permalink
[eclipse-iceoryx#264] Implement C++ publish subscribe dynamic data ex…
Browse files Browse the repository at this point in the history
…ample
  • Loading branch information
elfenpiff committed Jul 12, 2024
1 parent bca605d commit ba1fa2c
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 5 deletions.
8 changes: 4 additions & 4 deletions examples/cxx/publish_subscribe/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ project(example_cxx_publish_subscribe LANGUAGES CXX)

find_package(iceoryx2-cxx 0.3.0 REQUIRED)

add_executable(example_cxx_publisher src/publisher.cpp)
target_link_libraries(example_cxx_publisher iceoryx2-cxx::static-lib-cxx)
add_executable(example_cxx_publish_subscribe_publisher src/publisher.cpp)
target_link_libraries(example_cxx_publish_subscribe_publisher iceoryx2-cxx::static-lib-cxx)

add_executable(example_cxx_subscriber src/subscriber.cpp)
target_link_libraries(example_cxx_subscriber iceoryx2-cxx::static-lib-cxx)
add_executable(example_cxx_publish_subscribe_subscriber src/subscriber.cpp)
target_link_libraries(example_cxx_publish_subscribe_subscriber iceoryx2-cxx::static-lib-cxx)
2 changes: 1 addition & 1 deletion examples/cxx/publish_subscribe/src/subscriber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ int main() {
.expect("successful service creation/opening");

auto subscriber = service.subscriber_builder().create().expect(
"successful publisher creation");
"successful subscriber creation");

while (node.wait(CYCLE_TIME) == NodeEvent::Tick) {
auto sample = subscriber.receive().expect("receive succeeds");
Expand Down
22 changes: 22 additions & 0 deletions examples/cxx/publish_subscribe_dynamic_data/CMakeLists.txt
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 examples/cxx/publish_subscribe_dynamic_data/src/publisher.cpp
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 examples/cxx/publish_subscribe_dynamic_data/src/subscriber.cpp
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;
}
35 changes: 35 additions & 0 deletions iceoryx2-ffi/cxx/include/iox/slice.hpp
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
11 changes: 11 additions & 0 deletions iceoryx2-ffi/cxx/include/iox2/sample_mut.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#include "header_publish_subscribe.hpp"
#include "iox/expected.hpp"
#include "iox/function.hpp"
#include "iox/slice.hpp"
#include "service_type.hpp"

namespace iox2 {
Expand Down Expand Up @@ -57,6 +59,15 @@ class SampleMut<S, Payload, void> {
void write_payload(const Payload& payload) {}
};

template <ServiceType S, typename Payload>
class SampleMut<S, iox::Slice<Payload>, void> {
public:
const HeaderPublishSubscribe& header() const {}
const Payload& payload() const {}
Payload& payload_mut() {}
void write_from_fn(const iox::function<Payload(uint64_t)>& initializer) {}
};

template <ServiceType S, typename Payload, typename UserHeader>
iox::expected<uint64_t, PublisherSendError> send_sample(
SampleMut<S, Payload, UserHeader>&& sample) {}
Expand Down

0 comments on commit ba1fa2c

Please sign in to comment.