Skip to content

Commit

Permalink
Merge pull request #553 from elfenpiff/iox2-264-remaining-cxx-bindings
Browse files Browse the repository at this point in the history
[#264] node id and remove stale resources of dead nodes
  • Loading branch information
elfenpiff authored Dec 19, 2024
2 parents d332b3e + 0190c25 commit dc18bff
Show file tree
Hide file tree
Showing 22 changed files with 658 additions and 111 deletions.
1 change: 1 addition & 0 deletions iceoryx2-ffi/cxx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ add_library(iceoryx2-cxx-object-lib OBJECT
src/messaging_pattern.cpp
src/node.cpp
src/node_details.cpp
src/node_id.cpp
src/node_name.cpp
src/node_state.cpp
src/notifier.cpp
Expand Down
3 changes: 3 additions & 0 deletions iceoryx2-ffi/cxx/include/iox2/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,9 @@ class Config {
friend class ConfigView;
friend class config::Global;
friend class NodeBuilder;
template <ServiceType>
friend class DeadNodeView;

explicit Config(iox2_config_h handle);
void drop();

Expand Down
16 changes: 16 additions & 0 deletions iceoryx2-ffi/cxx/include/iox2/enum_translation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1492,6 +1492,22 @@ constexpr auto from<iox2::AllocationStrategy, iox2_allocation_strategy_e>(const

IOX_UNREACHABLE();
}

template <>
constexpr auto from<int, iox2::NodeCleanupFailure>(const int value) noexcept -> iox2::NodeCleanupFailure {
const auto variant = static_cast<iox2_node_cleanup_failure_e>(value);

switch (variant) {
case iox2_node_cleanup_failure_e_INTERRUPT:
return iox2::NodeCleanupFailure::Interrupt;
case iox2_node_cleanup_failure_e_INTERNAL_ERROR:
return iox2::NodeCleanupFailure::InternalError;
case iox2_node_cleanup_failure_e_INSUFFICIENT_PERMISSIONS:
return iox2::NodeCleanupFailure::InsufficientPermissions;
}

IOX_UNREACHABLE();
}
} // namespace iox

#endif
1 change: 0 additions & 1 deletion iceoryx2-ffi/cxx/include/iox2/event_id.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

#include "iox2/internal/iceoryx2.hpp"

#include <cstdint>
#include <iostream>

namespace iox2 {
Expand Down
4 changes: 3 additions & 1 deletion iceoryx2-ffi/cxx/include/iox2/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#ifndef IOX2_NODE_HPP
#define IOX2_NODE_HPP

#include "iox/assertions_addendum.hpp"
#include "iox/builder_addendum.hpp"
#include "iox/duration.hpp"
#include "iox/expected.hpp"
Expand Down Expand Up @@ -44,6 +43,9 @@ class Node {
auto operator=(const Node&) -> Node& = delete;
~Node();

/// Returns the [`Config`] that the [`Node`] will use to create any iceoryx2 entity.
auto config() const -> ConfigView;

/// Returns the name of the node inside a [`NodeNameView`].
auto name() const -> NodeNameView;

Expand Down
16 changes: 14 additions & 2 deletions iceoryx2-ffi/cxx/include/iox2/node_details.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,33 @@
#ifndef IOX2_NODE_DETAILS_HPP
#define IOX2_NODE_DETAILS_HPP

#include "iox/file_name.hpp"
#include "iox2/config.hpp"
#include "iox2/node_name.hpp"

namespace iox2 {
/// Contains details of a [`Node`].
class NodeDetails {
public:
NodeDetails(NodeName name, Config config);

/// Returns the executable [`FileName`] of the [`Node`]s owner process.
auto executable() const -> const iox::FileName&;
/// Returns a reference of the [`NodeName`].
auto name() const -> const NodeName&;
/// Returns a reference to the [`Config`] the [`Node`] uses.
auto config() const -> const Config&;

private:
template <ServiceType>
friend auto list_callback(iox2_node_state_e,
iox2_node_id_ptr,
const char* executable,
iox2_node_name_ptr,
iox2_config_ptr,
iox2_callback_context) -> iox2_callback_progression_e;

NodeDetails(iox::FileName executable, NodeName name, Config config);

iox::FileName m_executable;
NodeName m_node_name;
Config m_config;
};
Expand Down
9 changes: 9 additions & 0 deletions iceoryx2-ffi/cxx/include/iox2/node_failure_enums.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,16 @@ enum class NodeCreationFailure : uint8_t {
InternalError
};

/// Failures of [`DeadNodeView::remove_stale_resources()`] that occur when the stale resources of
/// a dead [`Node`] are removed.
enum class NodeCleanupFailure : uint8_t {
/// The process received an interrupt signal while cleaning up all stale resources of a dead [`Node`].
Interrupt,
/// Errors that indicate either an implementation issue or a wrongly configured system.
InternalError,
/// The stale resources of a dead [`Node`] could not be removed since the process does not have sufficient
/// permissions.
InsufficientPermissions,
};

} // namespace iox2
Expand Down
49 changes: 48 additions & 1 deletion iceoryx2-ffi/cxx/include/iox2/node_id.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,55 @@
#ifndef IOX2_NODE_ID_HPP
#define IOX2_NODE_ID_HPP

#include "iox2/iceoryx2.h"
#include "iox2/service_type.hpp"

#include <cstdint>
#include <ctime>
#include <iostream>

namespace iox2 {
class NodeId { };
/// The system-wide unique id of a [`Node`]
class NodeId {
public:
NodeId(const NodeId& rhs);
NodeId(NodeId&& rhs) noexcept;
auto operator=(const NodeId& rhs) -> NodeId&;
auto operator=(NodeId&& rhs) noexcept -> NodeId&;
~NodeId();

/// Returns the high bits of the underlying value of the [`NodeId`].
auto value_high() const -> uint64_t;

/// Returns the low bits of the underlying value of the [`NodeId`].
auto value_low() const -> uint64_t;

/// Returns the [`ProcessId`] of the process that owns the [`Node`].
auto pid() const -> int32_t;

/// Returns the time the [`Node`] was created.
auto creation_time() const -> timespec;

private:
template <ServiceType>
friend class Node;
template <ServiceType>
friend class DeadNodeView;
template <ServiceType>
friend auto list_callback(
iox2_node_state_e, iox2_node_id_ptr, const char*, iox2_node_name_ptr, iox2_config_ptr, iox2_callback_context)
-> iox2_callback_progression_e;


explicit NodeId(iox2_node_id_h handle);
void drop();

iox2_node_id_h m_handle = nullptr;
};

auto operator<<(std::ostream& stream, const NodeId& node) -> std::ostream&;
auto operator==(const NodeId& lhs, const NodeId& rhs) -> bool;
auto operator!=(const NodeId& lhs, const NodeId& rhs) -> bool;
} // namespace iox2

#endif
9 changes: 6 additions & 3 deletions iceoryx2-ffi/cxx/include/iox2/node_name.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ class NodeNameView {
friend class Node;
friend class NodeName;
template <ServiceType>
friend auto
list_callback(iox2_node_state_e, iox2_node_id_ptr, iox2_node_name_ptr, iox2_config_ptr, iox2_callback_context)
-> iox2_callback_progression_e;
friend auto list_callback(iox2_node_state_e,
iox2_node_id_ptr,
const char* executable,
iox2_node_name_ptr,
iox2_config_ptr,
iox2_callback_context) -> iox2_callback_progression_e;

explicit NodeNameView(iox2_node_name_ptr ptr);
iox2_node_name_ptr m_ptr = nullptr;
Expand Down
15 changes: 10 additions & 5 deletions iceoryx2-ffi/cxx/include/iox2/node_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class AliveNodeView {
auto operator=(AliveNodeView&&) -> AliveNodeView& = default;
~AliveNodeView() = default;

AliveNodeView(const NodeId& node_id, const iox::optional<NodeDetails>& details);
AliveNodeView(NodeId node_id, const iox::optional<NodeDetails>& details);

/// Returns the [`NodeId`].
auto id() const -> const NodeId&;
Expand Down Expand Up @@ -84,10 +84,6 @@ class NodeState {
auto operator=(NodeState&&) -> NodeState& = default;
~NodeState() = default;

explicit NodeState(const AliveNodeView<T>& view);
explicit NodeState(const DeadNodeView<T>& view);
NodeState(iox2_node_state_e node_state, const NodeId& node_id);

/// If the [`Node`] is alive the provided callback is called with an [`AliveNodeView`]
/// as argument.
auto alive(const iox::function<void(AliveNodeView<T>&)>& callback) -> NodeState&;
Expand All @@ -105,6 +101,15 @@ class NodeState {
auto undefined(const iox::function<void(NodeId&)>& callback) -> NodeState&;

private:
template <ServiceType>
friend auto list_callback(
iox2_node_state_e, iox2_node_id_ptr, const char*, iox2_node_name_ptr, iox2_config_ptr, iox2_callback_context)
-> iox2_callback_progression_e;

explicit NodeState(const AliveNodeView<T>& view);
explicit NodeState(const DeadNodeView<T>& view);
NodeState(iox2_node_state_e node_state, const NodeId& node_id);

iox::variant<AliveNodeView<T>, DeadNodeView<T>, NodeId, NodeId> m_state;
};
} // namespace iox2
Expand Down
34 changes: 26 additions & 8 deletions iceoryx2-ffi/cxx/src/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,18 @@ auto Node<T>::name() const -> NodeNameView {
return NodeNameView { node_name_ptr };
}

template <ServiceType T>
auto Node<T>::config() const -> ConfigView {
const auto* config_ptr = iox2_node_config(&m_handle);
return ConfigView { config_ptr };
}

template <ServiceType T>
auto Node<T>::id() const -> NodeId {
IOX_TODO();
const auto* node_id_ptr = iox2_node_id(&m_handle, iox::into<iox2_service_type_e>(T));
iox2_node_id_h node_id_handle = nullptr;
iox2_node_id_clone_from_ptr(nullptr, node_id_ptr, &node_id_handle);
return NodeId(node_id_handle);
}

template <ServiceType T>
Expand All @@ -77,28 +86,37 @@ auto Node<T>::service_builder(const ServiceName& name) const -> ServiceBuilder<T
template <ServiceType T>
// NOLINTBEGIN(readability-function-size)
auto list_callback(iox2_node_state_e node_state,
iox2_node_id_ptr node_id,
iox2_node_id_ptr node_id_ptr,
const char* executable,
iox2_node_name_ptr node_name,
iox2_config_ptr config,
iox2_callback_context context) -> iox2_callback_progression_e {
auto node_details = [&] {
if (node_id == nullptr || config == nullptr) {
if (node_id_ptr == nullptr || config == nullptr) {
return iox::optional<NodeDetails>();
}

return iox::optional<NodeDetails>(NodeDetails { NodeNameView { node_name }.to_owned(), Config {} });
return iox::optional<NodeDetails>(NodeDetails {
iox::FileName::create(iox::string<iox::FileName::capacity()>(iox::TruncateToCapacity, executable))
.expect("The executable file name is always valid."),
NodeNameView { node_name }.to_owned(),
Config {} });
}();

iox2_node_id_h node_id_handle = nullptr;
iox2_node_id_clone_from_ptr(nullptr, node_id_ptr, &node_id_handle);
NodeId node_id { node_id_handle };

auto node_state_object = [&] {
switch (node_state) {
case iox2_node_state_e_ALIVE:
return NodeState<T> { AliveNodeView<T> { NodeId {}, node_details } };
return NodeState<T> { AliveNodeView<T> { node_id, node_details } };
case iox2_node_state_e_DEAD:
return NodeState<T> { DeadNodeView<T> { AliveNodeView<T> { NodeId {}, node_details } } };
return NodeState<T> { DeadNodeView<T> { AliveNodeView<T> { node_id, node_details } } };
case iox2_node_state_e_UNDEFINED:
return NodeState<T> { iox2_node_state_e_UNDEFINED, NodeId {} };
return NodeState<T> { iox2_node_state_e_UNDEFINED, node_id };
case iox2_node_state_e_INACCESSIBLE:
return NodeState<T> { iox2_node_state_e_INACCESSIBLE, NodeId {} };
return NodeState<T> { iox2_node_state_e_INACCESSIBLE, node_id };
}

IOX_UNREACHABLE();
Expand Down
5 changes: 3 additions & 2 deletions iceoryx2-ffi/cxx/src/node_details.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
#include "iox2/node_details.hpp"

namespace iox2 {
NodeDetails::NodeDetails(NodeName name, Config config)
: m_node_name { std::move(name) }
NodeDetails::NodeDetails(iox::FileName executable, NodeName name, Config config)
: m_executable { std::move(executable) }
, m_node_name { std::move(name) }
, m_config { std::move(config) } {
}

Expand Down
Loading

0 comments on commit dc18bff

Please sign in to comment.