Skip to content

Commit

Permalink
NodeData API to construct liveliness token
Browse files Browse the repository at this point in the history
Signed-off-by: Yadunund <[email protected]>
  • Loading branch information
Yadunund committed Sep 6, 2024
1 parent 3fd565b commit bda3ed6
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 43 deletions.
50 changes: 11 additions & 39 deletions rmw_zenoh_cpp/src/detail/rmw_context_impl_s.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -458,55 +458,27 @@ bool rmw_context_impl_s::create_node_data(
return false;
}

// Check that the Zenoh session is still valid.
if (!z_check(data_->session_)) {
RMW_ZENOH_LOG_ERROR_NAMED(
"rmw_zenoh_cpp",
"Unable to create NodeData as Zenoh session is invalid.");
return false;
}

// Create the entity.
z_session_t session = z_loan(data_->session_);
const size_t node_id = this->get_next_entity_id();
auto entity = rmw_zenoh_cpp::liveliness::Entity::make(
z_info_zid(session),
std::to_string(node_id),
std::to_string(node_id),
rmw_zenoh_cpp::liveliness::EntityType::Node,
rmw_zenoh_cpp::liveliness::NodeInfo{
auto node_data = rmw_zenoh_cpp::NodeData::make(
this->get_next_entity_id(),
z_loan(data_->session_),
data_->domain_id_,
ns,
node_name,
data_->enclave_
}
);
if (entity == nullptr) {
RMW_ZENOH_LOG_ERROR_NAMED(
"rmw_zenoh_cpp",
"Unable to create node entity.");
return false;
}

// Create the liveliness token.
zc_owned_liveliness_token_t token = zc_liveliness_declare_token(
session,
z_keyexpr(entity->liveliness_keyexpr().c_str()),
NULL
);
auto free_token = rcpputils::make_scope_exit(
[&token]() {
z_drop(z_move(token));
});
if (!z_check(token)) {
RMW_ZENOH_LOG_ERROR_NAMED(
"rmw_zenoh_cpp",
"Unable to create liveliness token for the node.");
data_->enclave_);
if (node_data == nullptr) {
// Error already handled.
return false;
}
free_token.cancel();

node_insertion.first->second = std::make_shared<rmw_zenoh_cpp::NodeData>(
std::move(node_id),
std::move(entity),
std::move(token)
);
node_insertion.first->second = std::move(node_data);

return true;
}
Expand Down
57 changes: 57 additions & 0 deletions rmw_zenoh_cpp/src/detail/rmw_node_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#include "rmw_node_data.hpp"

#include <memory>
#include <string>
#include <utility>

#include "logging_macros.hpp"
Expand All @@ -22,6 +24,61 @@

namespace rmw_zenoh_cpp
{
///=============================================================================
std::shared_ptr<NodeData> NodeData::make(
std::size_t id,
z_session_t session,
std::size_t domain_id,
const std::string & namespace_,
const std::string & node_name,
const std::string & enclave)
{
// Create the entity.
auto entity = rmw_zenoh_cpp::liveliness::Entity::make(
z_info_zid(session),
std::to_string(id),
std::to_string(id),
rmw_zenoh_cpp::liveliness::EntityType::Node,
rmw_zenoh_cpp::liveliness::NodeInfo{
domain_id,
namespace_,
node_name,
enclave
}
);
if (entity == nullptr) {
RMW_ZENOH_LOG_ERROR_NAMED(
"rmw_zenoh_cpp",
"Unable to make NodeData as node entity is invalid.");
return nullptr;
}

// Create the liveliness token.
zc_owned_liveliness_token_t token = zc_liveliness_declare_token(
session,
z_keyexpr(entity->liveliness_keyexpr().c_str()),
NULL
);
auto free_token = rcpputils::make_scope_exit(
[&token]() {
z_drop(z_move(token));
});
if (!z_check(token)) {
RMW_ZENOH_LOG_ERROR_NAMED(
"rmw_zenoh_cpp",
"Unable to create liveliness token for the node.");
return nullptr;
}
free_token.cancel();

return std::shared_ptr<NodeData>(
new NodeData{
id,
std::move(entity),
std::move(token)
});
}

///=============================================================================
NodeData::NodeData(
std::size_t id,
Expand Down
17 changes: 13 additions & 4 deletions rmw_zenoh_cpp/src/detail/rmw_node_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <memory>
#include <mutex>
#include <string>

#include "liveliness_utils.hpp"

Expand All @@ -29,11 +30,14 @@ namespace rmw_zenoh_cpp
class NodeData final
{
public:
// Constructor.
NodeData(
// Make a shared_ptr of NodeData. Returns nullptr if construction fails.
static std::shared_ptr<NodeData> make(
std::size_t id,
std::shared_ptr<liveliness::Entity> entity,
zc_owned_liveliness_token_t token);
z_session_t session,
std::size_t domain_id,
const std::string & namespace_,
const std::string & node_name,
const std::string & enclave);

// Get the id of this node.
std::size_t id() const;
Expand All @@ -42,6 +46,11 @@ class NodeData final
~NodeData();

private:
// Constructor.
NodeData(
std::size_t id,
std::shared_ptr<liveliness::Entity> entity,
zc_owned_liveliness_token_t token);
// Internal mutex.
mutable std::mutex mutex_;
// The entity id of this node as generated by get_next_entity_id().
Expand Down

0 comments on commit bda3ed6

Please sign in to comment.