Program Listing for File graph_cache.hpp

Return to documentation for file (include/rmw_dds_common/graph_cache.hpp)

// Copyright 2020 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 RMW_DDS_COMMON__GRAPH_CACHE_HPP_
#define RMW_DDS_COMMON__GRAPH_CACHE_HPP_

#include <functional>
#include <map>
#include <mutex>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#include "rcutils/logging_macros.h"

#include "rmw/names_and_types.h"
#include "rmw/topic_endpoint_info.h"
#include "rmw/topic_endpoint_info_array.h"
#include "rmw/types.h"

#include "rmw_dds_common/gid_utils.hpp"
#include "rmw_dds_common/visibility_control.h"
#include "rmw_dds_common/msg/gid.hpp"
#include "rmw_dds_common/msg/node_entities_info.hpp"
#include "rmw_dds_common/msg/participant_entities_info.hpp"

namespace rmw_dds_common
{

// Forward-declaration, defined at end of file.
struct EntityInfo;
struct ParticipantInfo;


class GraphCache
{
  friend
  RMW_DDS_COMMON_PUBLIC
  std::ostream &
  operator<<(std::ostream & ostream, const GraphCache & topic_cache);

public:

  template<typename CallbackT>
  void
  set_on_change_callback(CallbackT && callback)
  {
    std::lock_guard<std::mutex> lock(mutex_);
    on_change_callback_ = callback;
  }

  RMW_DDS_COMMON_PUBLIC
  void
  clear_on_change_callback();


  RMW_DDS_COMMON_PUBLIC
  bool
  add_writer(
    const rmw_gid_t & writer_gid,
    const std::string & topic_name,
    const std::string & type_name,
    const rmw_gid_t & participant_gid,
    const rmw_qos_profile_t & qos);


  RMW_DDS_COMMON_PUBLIC
  bool
  add_reader(
    const rmw_gid_t & reader_gid,
    const std::string & topic_name,
    const std::string & type_name,
    const rmw_gid_t & participant_gid,
    const rmw_qos_profile_t & qos);


  RMW_DDS_COMMON_PUBLIC
  bool
  add_entity(
    const rmw_gid_t & gid,
    const std::string & topic_name,
    const std::string & type_name,
    const rmw_gid_t & participant_gid,
    const rmw_qos_profile_t & qos,
    bool is_reader);


  RMW_DDS_COMMON_PUBLIC
  bool
  remove_writer(const rmw_gid_t & gid);


  RMW_DDS_COMMON_PUBLIC
  bool
  remove_reader(const rmw_gid_t & gid);


  RMW_DDS_COMMON_PUBLIC
  bool
  remove_entity(const rmw_gid_t & gid, bool is_reader);


  RMW_DDS_COMMON_PUBLIC
  void
  add_participant(
    const rmw_gid_t & participant_gid,
    const std::string & enclave);


  RMW_DDS_COMMON_PUBLIC
  bool
  remove_participant(const rmw_gid_t & participant_gid);


  RMW_DDS_COMMON_PUBLIC
  void
  update_participant_entities(const rmw_dds_common::msg::ParticipantEntitiesInfo & msg);


  RMW_DDS_COMMON_PUBLIC
  rmw_dds_common::msg::ParticipantEntitiesInfo
  add_node(
    const rmw_gid_t & participant_gid,
    const std::string & node_name,
    const std::string & node_namespace);


  RMW_DDS_COMMON_PUBLIC
  rmw_dds_common::msg::ParticipantEntitiesInfo
  remove_node(
    const rmw_gid_t & participant_gid,
    const std::string & node_name,
    const std::string & node_namespace);


  RMW_DDS_COMMON_PUBLIC
  rmw_dds_common::msg::ParticipantEntitiesInfo
  associate_writer(
    const rmw_gid_t & writer_gid,
    const rmw_gid_t & participant_gid,
    const std::string & node_name,
    const std::string & node_namespace);


  RMW_DDS_COMMON_PUBLIC
  rmw_dds_common::msg::ParticipantEntitiesInfo
  dissociate_writer(
    const rmw_gid_t & writer_gid,
    const rmw_gid_t & participant_gid,
    const std::string & node_name,
    const std::string & node_namespace);


  RMW_DDS_COMMON_PUBLIC
  rmw_dds_common::msg::ParticipantEntitiesInfo
  associate_reader(
    const rmw_gid_t & reader_gid,
    const rmw_gid_t & participant_gid,
    const std::string & node_name,
    const std::string & node_namespace);


  RMW_DDS_COMMON_PUBLIC
  rmw_dds_common::msg::ParticipantEntitiesInfo
  dissociate_reader(
    const rmw_gid_t & reader_gid,
    const rmw_gid_t & participant_gid,
    const std::string & node_name,
    const std::string & node_namespace);


  RMW_DDS_COMMON_PUBLIC
  rmw_ret_t
  get_writer_count(
    const std::string & topic_name,
    size_t * count) const;


  RMW_DDS_COMMON_PUBLIC
  rmw_ret_t
  get_reader_count(
    const std::string & topic_name,
    size_t * count) const;

  using DemangleFunctionT = std::function<std::string(const std::string &)>;


  RMW_DDS_COMMON_PUBLIC
  rmw_ret_t
  get_writers_info_by_topic(
    const std::string & topic_name,
    DemangleFunctionT demangle_type,
    rcutils_allocator_t * allocator,
    rmw_topic_endpoint_info_array_t * endpoints_info) const;


  RMW_DDS_COMMON_PUBLIC
  rmw_ret_t
  get_readers_info_by_topic(
    const std::string & topic_name,
    DemangleFunctionT demangle_type,
    rcutils_allocator_t * allocator,
    rmw_topic_endpoint_info_array_t * endpoints_info) const;


  RMW_DDS_COMMON_PUBLIC
  rmw_ret_t
  get_names_and_types(
    DemangleFunctionT demangle_topic,
    DemangleFunctionT demangle_type,
    rcutils_allocator_t * allocator,
    rmw_names_and_types_t * topic_names_and_types) const;


  RMW_DDS_COMMON_PUBLIC
  rmw_ret_t
  get_writer_names_and_types_by_node(
    const std::string & node_name,
    const std::string & namespace_,
    DemangleFunctionT demangle_topic,
    DemangleFunctionT demangle_type,
    rcutils_allocator_t * allocator,
    rmw_names_and_types_t * topic_names_and_types) const;


  RMW_DDS_COMMON_PUBLIC
  rmw_ret_t
  get_reader_names_and_types_by_node(
    const std::string & node_name,
    const std::string & namespace_,
    DemangleFunctionT demangle_topic,
    DemangleFunctionT demangle_type,
    rcutils_allocator_t * allocator,
    rmw_names_and_types_t * topic_names_and_types) const;

  RMW_DDS_COMMON_PUBLIC
  size_t
  get_number_of_nodes() const;


  RMW_DDS_COMMON_PUBLIC
  rmw_ret_t
  get_node_names(
    rcutils_string_array_t * node_names,
    rcutils_string_array_t * node_namespaces,
    rcutils_string_array_t * enclaves,
    rcutils_allocator_t * allocator) const;

  using NodeEntitiesInfoSeq =
    decltype(std::declval<rmw_dds_common::msg::ParticipantEntitiesInfo>().node_entities_info_seq);
  using EntityGidToInfo = std::map<rmw_gid_t, EntityInfo, Compare_rmw_gid_t>;
  using ParticipantToNodesMap = std::map<rmw_gid_t, ParticipantInfo, Compare_rmw_gid_t>;
  using GidSeq =
    decltype(std::declval<rmw_dds_common::msg::NodeEntitiesInfo>().writer_gid_seq);

private:
  EntityGidToInfo data_writers_;
  EntityGidToInfo data_readers_;
  ParticipantToNodesMap participants_;
  std::function<void()> on_change_callback_ = nullptr;

  mutable std::mutex mutex_;
};

RMW_DDS_COMMON_PUBLIC
std::ostream &
operator<<(std::ostream & ostream, const GraphCache & topic_cache);

struct ParticipantInfo
{
  GraphCache::NodeEntitiesInfoSeq node_entities_info_seq;
  std::string enclave;
};

struct EntityInfo
{
  std::string topic_name;
  std::string topic_type;
  rmw_gid_t participant_gid;
  rmw_qos_profile_t qos;

  EntityInfo(
    const std::string & topic_name,
    const std::string & topic_type,
    const rmw_gid_t & participant_gid,
    const rmw_qos_profile_t & qos)
  : topic_name(topic_name),
    topic_type(topic_type),
    participant_gid(participant_gid),
    qos(qos)
  {}
};

}  // namespace rmw_dds_common

#endif  // RMW_DDS_COMMON__GRAPH_CACHE_HPP_