Skip to content

Commit

Permalink
[eclipse-iceoryx#264] Implement C++ publish subscribe example
Browse files Browse the repository at this point in the history
  • Loading branch information
elfenpiff committed Jul 10, 2024
1 parent b8cc6f3 commit fa06884
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 10 deletions.
23 changes: 20 additions & 3 deletions examples/cxx/publish_subscribe/src/publisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

#include <iostream>

#include "iox/duration.hpp"
#include "iox2/node.hpp"
#include "transmission_data.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(
Expand All @@ -28,9 +34,20 @@ int main() {
auto publisher = service.publisher_builder().create().expect(
"successful publisher creation");

// Node<NodeType::ZERO_COPY>::list(
// Config{}, [](auto) { return iox::ok(CallbackProgression::Continue);
// });
auto counter = 0;
while (node.wait(CYCLE_TIME) == NodeEvent::Tick) {
counter += 1;
auto sample = publisher.loan_uninit().expect("acquire sample");

sample.write_payload(TransmissionData{
.x = counter, .y = counter * 3, .funky = counter * 812.12});

send_sample(std::move(sample)).expect("send successful");

std::cout << "Send sample " << counter << "..." << std::endl;
}

std::cout << "exit" << std::endl;

return 0;
}
31 changes: 31 additions & 0 deletions examples/cxx/publish_subscribe/src/subscriber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,39 @@
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

#include <iostream>

#include "iox/duration.hpp"
#include "iox2/node.hpp"
#include "transmission_data.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("My/Funk/ServiceName")
.expect("valid service name"))
.publish_subscribe<TransmissionData>()
.open_or_create()
.expect("successful service creation/opening");

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

while (node.wait(CYCLE_TIME) == NodeEvent::Tick) {
auto sample = subscriber.receive().expect("receive succeeds");
while (sample.has_value()) {
std::cout << "received: " << sample->payload() << std::endl;
sample = subscriber.receive().expect("receive succeeds");
}
}

std::cout << "exit" << std::endl;

return 0;
}
8 changes: 8 additions & 0 deletions examples/cxx/publish_subscribe/src/transmission_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,19 @@
#define IOX2_EXAMPLES_TRANSMISSION_DATA_HPP_

#include <cstdint>
#include <iostream>

struct TransmissionData {
std::int32_t x;
std::int32_t y;
double funky;
};

inline std::ostream& operator<<(std::ostream& stream,
const TransmissionData& value) {
std::cout << "TransmissionData { x: " << value.x << ", y: " << value.y
<< ", funky: " << value.funky << "}";
return stream;
}

#endif
19 changes: 14 additions & 5 deletions iceoryx2-ffi/cxx/include/iox2/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
#ifndef IOX2_NODE_HPP_
#define IOX2_NODE_HPP_

#include <iox/builder.hpp>
#include <iox/expected.hpp>
#include <iox/function.hpp>
#include <iox/optional.hpp>

#include "callback_progression.hpp"
#include "config.hpp"
#include "internal/iceoryx2.hpp"
#include "iox/builder.hpp"
#include "iox/duration.hpp"
#include "iox/expected.hpp"
#include "iox/function.hpp"
#include "node_id.hpp"
#include "node_name.hpp"
#include "node_state.hpp"
Expand All @@ -28,12 +27,22 @@
#include "service_type.hpp"

namespace iox2 {
enum class NodeEvent {
/// The timeout passed.
Tick,
/// SIGTERM signal was received
TerminationRequest,
/// SIGINT signal was received
InterruptSignal,
};

template <ServiceType T>
class Node {
public:
NodeName& name() const {}
NodeId& id() const {}
ServiceBuilder<T> service_builder(const ServiceName& name) const {}
NodeEvent wait(const iox::units::Duration& cycle_time) const {}

static iox::expected<void, NodeListFailure> list(
const Config& config,
Expand Down
10 changes: 10 additions & 0 deletions iceoryx2-ffi/cxx/include/iox2/sample.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "unique_port_id.hpp"

namespace iox2 {

template <ServiceType, typename Payload, typename UserHeader>
class Sample {
public:
Expand All @@ -25,6 +26,15 @@ class Sample {
const HeaderPublishSubscribe& header() const {}
UniquePublisherId origin() const {}
};

template <ServiceType S, typename Payload>
class Sample<S, Payload, void> {
public:
const Payload& payload() const {}
const HeaderPublishSubscribe& header() const {}
UniquePublisherId origin() const {}
};

} // namespace iox2

#endif
16 changes: 14 additions & 2 deletions iceoryx2-ffi/cxx/include/iox2/sample_mut.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,22 @@ class SampleMut {
UserHeader& user_header_mut() {}
const Payload& payload() const {}
Payload& payload_mut() {}
void write_payload(const Payload& payload) {}
};

static iox::expected<uint64_t, PublisherSendError> send(
SampleMut&& sample) {}
template <ServiceType S, typename Payload>
class SampleMut<S, Payload, void> {
public:
const HeaderPublishSubscribe& header() const {}
const Payload& payload() const {}
Payload& payload_mut() {}
void write_payload(const Payload& payload) {}
};

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

} // namespace iox2

#endif

0 comments on commit fa06884

Please sign in to comment.