Program Listing for File liveliness_utils.hpp

Return to documentation for file (src/detail/liveliness_utils.hpp)

// Copyright 2023 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef DETAIL__LIVELINESS_UTILS_HPP_
#define DETAIL__LIVELINESS_UTILS_HPP_

#include <array>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <vector>

#include <zenoh.hxx>

#include "rmw/types.h"

namespace rmw_zenoh_cpp
{
namespace liveliness
{
struct NodeInfo
{
  std::size_t domain_id_;
  std::string ns_;
  std::string name_;
  std::string enclave_;

  NodeInfo(
    std::size_t domain_id,
    std::string ns,
    std::string name,
    std::string enclave);
};

struct TopicInfo
{
  std::string name_;
  std::string type_;
  std::string type_hash_;
  std::string topic_keyexpr_;
  rmw_qos_profile_t qos_;

  TopicInfo(
    std::size_t domain_id,
    std::string name,
    std::string type,
    std::string type_hash,
    rmw_qos_profile_t qos);
};

std::string subscription_token(size_t domain_id);

enum class EntityType : uint8_t
{
  Node,
  Publisher,
  Subscription,
  Service,
  Client
};

// An struct to bundle results of parsing a token.
class Entity;
using EntityPtr = std::shared_ptr<Entity>;
using ConstEntityPtr = std::shared_ptr<const Entity>;
class Entity
{
public:
  // TODO(Yadunund): Find a way to better bundle the type and the associated data.
  static EntityPtr make(
    zenoh::Id zid,
    const std::string & nid,
    const std::string & id,
    EntityType type,
    NodeInfo node_info,
    std::optional<TopicInfo> topic_info = std::nullopt);

  static EntityPtr make(const std::string & keyexpr);

  // Get the zenoh session id as a string. This is not unique as entities
  // created within the same session, will have the same ids.
  std::string zid() const;

  // Get the id of the node of this entity.
  std::string nid() const;

  // Get the id of the entity local to a zenoh session.
  // Use gid_hash() to retrieve a globally unique id.
  std::string id() const;

  // Interim method to get a globally unique id for this entity which is the hash of the keyexpr.
  std::size_t gid_hash() const;

  EntityType type() const;

  std::string node_namespace() const;

  std::string node_name() const;

  std::string node_enclave() const;

  NodeInfo node_info() const;

  std::optional<TopicInfo> topic_info() const;

  std::string liveliness_keyexpr() const;

  // Two entities are equal if their gid_hash are equal.
  bool operator==(const Entity & other) const;

  std::array<uint8_t, 16> copy_gid() const;

private:
  Entity(
    std::string zid,
    std::string nid,
    std::string id,
    EntityType type,
    NodeInfo node_info,
    std::optional<TopicInfo> topic_info);

  std::string zid_;
  std::string nid_;
  std::string id_;
  std::size_t gid_hash_;
  EntityType type_;
  NodeInfo node_info_;
  std::optional<TopicInfo> topic_info_;
  std::string liveliness_keyexpr_;
  std::array<uint8_t, 16> gid_{};
};

std::string mangle_name(const std::string & input);

std::string demangle_name(const std::string & input);


std::string qos_to_keyexpr(const rmw_qos_profile_t & qos);

std::optional<rmw_qos_profile_t> keyexpr_to_qos(const std::string & keyexpr);
}  // namespace liveliness

size_t hash_gid(const std::array<uint8_t, 16> gid);
}  // namespace rmw_zenoh_cpp

// Allow Entity to be hashed and used as a key in unordered_maps/sets
namespace std
{
template<>
struct hash<rmw_zenoh_cpp::liveliness::Entity>
{
  auto operator()(const rmw_zenoh_cpp::liveliness::Entity & entity) const -> size_t
  {
    return entity.gid_hash();
  }
};

template<>
struct hash<rmw_zenoh_cpp::liveliness::ConstEntityPtr>
{
  auto operator()(const rmw_zenoh_cpp::liveliness::ConstEntityPtr & entity) const -> size_t
  {
    return entity->gid_hash();
  }
};

template<>
struct equal_to<rmw_zenoh_cpp::liveliness::ConstEntityPtr>
{
  auto operator()(
    const rmw_zenoh_cpp::liveliness::ConstEntityPtr & lhs,
    const rmw_zenoh_cpp::liveliness::ConstEntityPtr & rhs) const -> bool
  {
    return lhs->gid_hash() == rhs->gid_hash();
  }
};
}  // namespace std

#endif  // DETAIL__LIVELINESS_UTILS_HPP_