rmw_dds_common  master
Define a common interface between DDS implementations of ROS middleware.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
graph_cache.hpp
1 // Copyright 2020 Open Source Robotics Foundation, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef RMW_DDS_COMMON__GRAPH_CACHE_HPP_
16 #define RMW_DDS_COMMON__GRAPH_CACHE_HPP_
17 
18 #include <functional>
19 #include <map>
20 #include <mutex>
21 #include <string>
22 #include <unordered_map>
23 #include <utility>
24 #include <vector>
25 
26 #include "rcutils/logging_macros.h"
27 
28 #include "rmw/names_and_types.h"
31 #include "rmw/types.h"
32 
33 #include "rmw_dds_common/gid_utils.hpp"
34 #include "rmw_dds_common/visibility_control.h"
35 #include "rmw_dds_common/msg/gid.hpp"
36 #include "rmw_dds_common/msg/node_entities_info.hpp"
37 #include "rmw_dds_common/msg/participant_entities_info.hpp"
38 
39 namespace rmw_dds_common
40 {
41 
42 // Forward-declaration, defined at end of file.
43 struct EntityInfo;
44 struct ParticipantInfo;
45 
47 
51 {
52  friend
53  RMW_DDS_COMMON_PUBLIC
54  std::ostream &
55  operator<<(std::ostream & ostream, const GraphCache & topic_cache);
56 
57 public:
59 
62  template<typename CallbackT>
63  void
64  set_on_change_callback(CallbackT && callback)
65  {
66  std::lock_guard<std::mutex> lock(mutex_);
67  on_change_callback_ = callback;
68  }
69 
71  RMW_DDS_COMMON_PUBLIC
72  void
74 
81 
90  RMW_DDS_COMMON_PUBLIC
91  bool
92  add_writer(
93  const rmw_gid_t & writer_gid,
94  const std::string & topic_name,
95  const std::string & type_name,
96  const rmw_gid_t & participant_gid,
97  const rmw_qos_profile_t & qos);
98 
100 
108  RMW_DDS_COMMON_PUBLIC
109  bool
110  add_reader(
111  const rmw_gid_t & reader_gid,
112  const std::string & topic_name,
113  const std::string & type_name,
114  const rmw_gid_t & participant_gid,
115  const rmw_qos_profile_t & qos);
116 
118 
127  RMW_DDS_COMMON_PUBLIC
128  bool
129  add_entity(
130  const rmw_gid_t & gid,
131  const std::string & topic_name,
132  const std::string & type_name,
133  const rmw_gid_t & participant_gid,
134  const rmw_qos_profile_t & qos,
135  bool is_reader);
136 
138 
142  RMW_DDS_COMMON_PUBLIC
143  bool
144  remove_writer(const rmw_gid_t & gid);
145 
147 
151  RMW_DDS_COMMON_PUBLIC
152  bool
153  remove_reader(const rmw_gid_t & gid);
154 
156 
161  RMW_DDS_COMMON_PUBLIC
162  bool
163  remove_entity(const rmw_gid_t & gid, bool is_reader);
164 
172 
177  RMW_DDS_COMMON_PUBLIC
178  void
180  const rmw_gid_t & participant_gid,
181  const std::string & enclave);
182 
184 
188  RMW_DDS_COMMON_PUBLIC
189  bool
190  remove_participant(const rmw_gid_t & participant_gid);
191 
200 
204  RMW_DDS_COMMON_PUBLIC
205  void
206  update_participant_entities(const rmw_dds_common::msg::ParticipantEntitiesInfo & msg);
207 
215 
222  RMW_DDS_COMMON_PUBLIC
223  rmw_dds_common::msg::ParticipantEntitiesInfo
224  add_node(
225  const rmw_gid_t & participant_gid,
226  const std::string & node_name,
227  const std::string & node_namespace);
228 
230 
236  RMW_DDS_COMMON_PUBLIC
237  rmw_dds_common::msg::ParticipantEntitiesInfo
238  remove_node(
239  const rmw_gid_t & participant_gid,
240  const std::string & node_name,
241  const std::string & node_namespace);
242 
244 
251  RMW_DDS_COMMON_PUBLIC
252  rmw_dds_common::msg::ParticipantEntitiesInfo
254  const rmw_gid_t & writer_gid,
255  const rmw_gid_t & participant_gid,
256  const std::string & node_name,
257  const std::string & node_namespace);
258 
260 
267  RMW_DDS_COMMON_PUBLIC
268  rmw_dds_common::msg::ParticipantEntitiesInfo
270  const rmw_gid_t & writer_gid,
271  const rmw_gid_t & participant_gid,
272  const std::string & node_name,
273  const std::string & node_namespace);
274 
276 
283  RMW_DDS_COMMON_PUBLIC
284  rmw_dds_common::msg::ParticipantEntitiesInfo
286  const rmw_gid_t & reader_gid,
287  const rmw_gid_t & participant_gid,
288  const std::string & node_name,
289  const std::string & node_namespace);
290 
292 
299  RMW_DDS_COMMON_PUBLIC
300  rmw_dds_common::msg::ParticipantEntitiesInfo
302  const rmw_gid_t & reader_gid,
303  const rmw_gid_t & participant_gid,
304  const std::string & node_name,
305  const std::string & node_namespace);
306 
314 
323  RMW_DDS_COMMON_PUBLIC
324  rmw_ret_t
326  const std::string & topic_name,
327  size_t * count) const;
328 
330 
338  RMW_DDS_COMMON_PUBLIC
339  rmw_ret_t
341  const std::string & topic_name,
342  size_t * count) const;
343 
346 
348 
358  RMW_DDS_COMMON_PUBLIC
359  rmw_ret_t
361  const std::string & topic_name,
362  DemangleFunctionT demangle_type,
363  rcutils_allocator_t * allocator,
364  rmw_topic_endpoint_info_array_t * endpoints_info) const;
365 
367 
377  RMW_DDS_COMMON_PUBLIC
378  rmw_ret_t
380  const std::string & topic_name,
381  DemangleFunctionT demangle_type,
382  rcutils_allocator_t * allocator,
383  rmw_topic_endpoint_info_array_t * endpoints_info) const;
384 
386 
401  RMW_DDS_COMMON_PUBLIC
402  rmw_ret_t
404  DemangleFunctionT demangle_topic,
405  DemangleFunctionT demangle_type,
406  rcutils_allocator_t * allocator,
407  rmw_names_and_types_t * topic_names_and_types) const;
408 
410 
427  RMW_DDS_COMMON_PUBLIC
428  rmw_ret_t
430  const std::string & node_name,
431  const std::string & namespace_,
432  DemangleFunctionT demangle_topic,
433  DemangleFunctionT demangle_type,
434  rcutils_allocator_t * allocator,
435  rmw_names_and_types_t * topic_names_and_types) const;
436 
438 
455  RMW_DDS_COMMON_PUBLIC
456  rmw_ret_t
458  const std::string & node_name,
459  const std::string & namespace_,
460  DemangleFunctionT demangle_topic,
461  DemangleFunctionT demangle_type,
462  rcutils_allocator_t * allocator,
463  rmw_names_and_types_t * topic_names_and_types) const;
464 
466 
470  RMW_DDS_COMMON_PUBLIC
471  size_t
472  get_number_of_nodes() const;
473 
475 
490  RMW_DDS_COMMON_PUBLIC
491  rmw_ret_t
493  rcutils_string_array_t * node_names,
494  rcutils_string_array_t * node_namespaces,
495  rcutils_string_array_t * enclaves,
496  rcutils_allocator_t * allocator) const;
497 
502  using NodeEntitiesInfoSeq =
505  decltype(std::declval<rmw_dds_common::msg::ParticipantEntitiesInfo>().node_entities_info_seq);
514  using GidSeq =
515  decltype(std::declval<rmw_dds_common::msg::NodeEntitiesInfo>().writer_gid_seq);
516 
517 private:
518  EntityGidToInfo data_writers_;
519  EntityGidToInfo data_readers_;
520  ParticipantToNodesMap participants_;
521  std::function<void()> on_change_callback_ = nullptr;
522 
523  mutable std::mutex mutex_;
524 };
525 
526 RMW_DDS_COMMON_PUBLIC
527 std::ostream &
528 operator<<(std::ostream & ostream, const GraphCache & topic_cache);
529 
532 {
534  GraphCache::NodeEntitiesInfoSeq node_entities_info_seq;
537 };
538 
541 {
550 
553  const std::string & topic_name,
554  const std::string & topic_type,
555  const rmw_gid_t & participant_gid,
556  const rmw_qos_profile_t & qos)
560  qos(qos)
561  {}
562 };
563 
564 } // namespace rmw_dds_common
565 
566 #endif // RMW_DDS_COMMON__GRAPH_CACHE_HPP_
rmw_dds_common::GraphCache::get_readers_info_by_topic
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
Get an array with information about the readers in a topic.
rmw_dds_common::GraphCache::get_writer_count
rmw_ret_t get_writer_count(const std::string &topic_name, size_t *count) const
Get the number of publishers of a topic.
std::string
rmw_dds_common::GraphCache
Graph cache data structure.
Definition: graph_cache.hpp:50
rmw_dds_common::GraphCache::associate_writer
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)
Remove a node to the graph, and get the message to be sent.
rmw_dds_common::GraphCache::add_participant
void add_participant(const rmw_gid_t &participant_gid, const std::string &enclave)
Add a discovered participant to the cache.
rmw_dds_common::GraphCache::dissociate_reader
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)
Dissociate a reader from a node, and get an update message to be sent.
rmw_dds_common::EntityInfo::EntityInfo
EntityInfo(const std::string &topic_name, const std::string &topic_type, const rmw_gid_t &participant_gid, const rmw_qos_profile_t &qos)
Simple constructor.
Definition: graph_cache.hpp:552
rmw_dds_common::GraphCache::get_writer_names_and_types_by_node
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
Get the topic names and types that a node is publishing.
rmw_dds_common::EntityInfo::qos
rmw_qos_profile_t qos
Quality of service of the topic.
Definition: graph_cache.hpp:549
std::lock_guard
std::function
logging_macros.h
rmw_dds_common::GraphCache::remove_writer
bool remove_writer(const rmw_gid_t &gid)
Remove a data writer based on discovery.
rcutils_string_array_t
rmw_dds_common::GraphCache::remove_entity
bool remove_entity(const rmw_gid_t &gid, bool is_reader)
Remove a data reader or writer based based on discovery.
topic_endpoint_info.h
rmw_dds_common::GraphCache::get_reader_count
rmw_ret_t get_reader_count(const std::string &topic_name, size_t *count) const
Get the number of subscriptions of a topic.
rmw_dds_common::GraphCache::add_entity
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)
Add a data reader based on discovery.
topic_endpoint_info_array.h
std::ostream
rmw_dds_common::EntityInfo::topic_type
std::string topic_type
Topic type.
Definition: graph_cache.hpp:545
rmw_dds_common::ParticipantInfo::node_entities_info_seq
GraphCache::NodeEntitiesInfoSeq node_entities_info_seq
Discovery information of each Node created from a Participant.
Definition: graph_cache.hpp:534
rmw_dds_common::GraphCache::add_reader
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)
Add a data reader based on discovery.
rmw_dds_common::GraphCache::get_node_names
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
Copy the names and namespaces of the discovered nodes.
rmw_dds_common::GraphCache::dissociate_writer
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)
Dissociate a writer from a node, and get an update message to be sent.
rmw_dds_common::GraphCache::add_node
rmw_dds_common::msg::ParticipantEntitiesInfo add_node(const rmw_gid_t &participant_gid, const std::string &node_name, const std::string &node_namespace)
Add a node to the graph, and get the message to be sent.
std::map< rmw_gid_t, EntityInfo, Compare_rmw_gid_t >
rmw_ret_t
int32_t rmw_ret_t
rmw_names_and_types_t
rmw_dds_common::EntityInfo::topic_name
std::string topic_name
Topic name.
Definition: graph_cache.hpp:543
rmw_dds_common::ParticipantInfo::enclave
std::string enclave
Name of the enclave.
Definition: graph_cache.hpp:536
rmw_dds_common::GraphCache::get_number_of_nodes
size_t get_number_of_nodes() const
Get the number of nodes that have been discovered.
rcutils_allocator_t
rmw_gid_t
rmw_dds_common::GraphCache::update_participant_entities
void update_participant_entities(const rmw_dds_common::msg::ParticipantEntitiesInfo &msg)
Update participant info based on a received ParticipantEntitiesInfo message.
rmw_dds_common::EntityInfo
Structure to represent the discovery data of an endpoint (reader or writer).
Definition: graph_cache.hpp:540
rmw_topic_endpoint_info_array_t
rmw_dds_common::GraphCache::set_on_change_callback
void set_on_change_callback(CallbackT &&callback)
Set a callback that will be called when the state of the object changes.
Definition: graph_cache.hpp:64
rmw_qos_profile_t
std::mutex
rmw_dds_common::ParticipantInfo
Structure to represent the discovery data of a Participant.
Definition: graph_cache.hpp:531
names_and_types.h
operator<<
std::ostream & operator<<(std::ostream &os, const path &p)
types.h
rmw_dds_common::GraphCache::get_names_and_types
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
Get all the topic names and types.
rmw_dds_common::GraphCache::associate_reader
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)
Associate a reader with a node, and get an update message to be sent.
rmw_dds_common::GraphCache::remove_node
rmw_dds_common::msg::ParticipantEntitiesInfo remove_node(const rmw_gid_t &participant_gid, const std::string &node_name, const std::string &node_namespace)
Remove a node to the graph, and get the message to be sent.
rmw_dds_common::GraphCache::remove_reader
bool remove_reader(const rmw_gid_t &gid)
Remove a data reader based on discovery.
rmw_dds_common::EntityInfo::participant_gid
rmw_gid_t participant_gid
Participant gid.
Definition: graph_cache.hpp:547
rmw_dds_common::GraphCache::get_writers_info_by_topic
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
Get an array with information about the writers in a topic.
rmw_dds_common::GraphCache::get_reader_names_and_types_by_node
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
Get the topic names and types that a node is subscribing.
rmw_dds_common::GraphCache::clear_on_change_callback
void clear_on_change_callback()
Clear previously registered "on change" callback.
rmw_dds_common::GraphCache::remove_participant
bool remove_participant(const rmw_gid_t &participant_gid)
Remove a participant based on discovery.
rmw_dds_common::GraphCache::add_writer
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)
Add a data writer based on discovery.