rmw_dds_common  master
Define a common interface between DDS implementations of ROS middleware.
graph_cache.hpp
Go to the documentation of this file.
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 
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
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 
72  void
74 
81 
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 
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 
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 
143  bool
144  remove_writer(const rmw_gid_t & gid);
145 
147 
152  bool
153  remove_reader(const rmw_gid_t & gid);
154 
156 
162  bool
163  remove_entity(const rmw_gid_t & gid, bool is_reader);
164 
172 
178  void
180  const rmw_gid_t & participant_gid,
181  const std::string & enclave);
182 
184 
189  bool
190  remove_participant(const rmw_gid_t & participant_gid);
191 
200 
205  void
206  update_participant_entities(const rmw_dds_common::msg::ParticipantEntitiesInfo & msg);
207 
215 
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 
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 
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 
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 
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 
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 
324  rmw_ret_t
326  const std::string & topic_name,
327  size_t * count) const;
328 
330 
339  rmw_ret_t
341  const std::string & topic_name,
342  size_t * count) const;
343 
345 
347 
358  rmw_ret_t
360  const std::string & topic_name,
361  DemangleFunctionT demangle_type,
362  rcutils_allocator_t * allocator,
363  rmw_topic_endpoint_info_array_t * endpoints_info) const;
364 
366 
377  rmw_ret_t
379  const std::string & topic_name,
380  DemangleFunctionT demangle_type,
381  rcutils_allocator_t * allocator,
382  rmw_topic_endpoint_info_array_t * endpoints_info) const;
383 
385 
401  rmw_ret_t
403  DemangleFunctionT demangle_topic,
404  DemangleFunctionT demangle_type,
405  rcutils_allocator_t * allocator,
406  rmw_names_and_types_t * topic_names_and_types) const;
407 
409 
427  rmw_ret_t
429  const std::string & node_name,
430  const std::string & namespace_,
431  DemangleFunctionT demangle_topic,
432  DemangleFunctionT demangle_type,
433  rcutils_allocator_t * allocator,
434  rmw_names_and_types_t * topic_names_and_types) const;
435 
437 
455  rmw_ret_t
457  const std::string & node_name,
458  const std::string & namespace_,
459  DemangleFunctionT demangle_topic,
460  DemangleFunctionT demangle_type,
461  rcutils_allocator_t * allocator,
462  rmw_names_and_types_t * topic_names_and_types) const;
463 
465 
470  size_t
471  get_number_of_nodes() const;
472 
474 
490  rmw_ret_t
492  rcutils_string_array_t * node_names,
493  rcutils_string_array_t * node_namespaces,
494  rcutils_string_array_t * enclaves,
495  rcutils_allocator_t * allocator) const;
496 
501  using NodeEntitiesInfoSeq =
502  decltype(std::declval<rmw_dds_common::msg::ParticipantEntitiesInfo>().node_entities_info_seq);
505  using GidSeq =
506  decltype(std::declval<rmw_dds_common::msg::NodeEntitiesInfo>().writer_gid_seq);
507 
508 private:
509  EntityGidToInfo data_writers_;
510  EntityGidToInfo data_readers_;
511  ParticipantToNodesMap participants_;
512  std::function<void()> on_change_callback_ = nullptr;
513 
514  mutable std::mutex mutex_;
515 };
516 
518 std::ostream &
519 operator<<(std::ostream & ostream, const GraphCache & topic_cache);
520 
522 {
525 };
526 
528 {
533 
535  const std::string & topic_name,
536  const std::string & topic_type,
537  const rmw_gid_t & participant_gid,
538  const rmw_qos_profile_t & qos)
542  qos(qos)
543  {}
544 };
545 
546 } // namespace rmw_dds_common
547 
548 #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::operator<<
std::ostream & operator<<(std::ostream &ostream, const rmw_gid_t &gid)
Stream operator for rmw_gid_t.
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::GraphCache::GidSeq
decltype(std::declval< rmw_dds_common::msg::NodeEntitiesInfo >().writer_gid_seq) GidSeq
Definition: graph_cache.hpp:506
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)
Definition: graph_cache.hpp:534
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
Definition: graph_cache.hpp:532
std::lock_guard
gid_utils.hpp
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
Definition: context.hpp:27
RMW_DDS_COMMON_PUBLIC
#define RMW_DDS_COMMON_PUBLIC
Definition: visibility_control.h:48
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
Definition: graph_cache.hpp:530
rmw_dds_common::ParticipantInfo::node_entities_info_seq
GraphCache::NodeEntitiesInfoSeq node_entities_info_seq
Definition: graph_cache.hpp:523
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
Definition: graph_cache.hpp:529
visibility_control.h
rmw_dds_common::ParticipantInfo::enclave
std::string enclave
Definition: graph_cache.hpp:524
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
Definition: graph_cache.hpp:527
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_dds_common::GraphCache::NodeEntitiesInfoSeq
decltype(std::declval< rmw_dds_common::msg::ParticipantEntitiesInfo >().node_entities_info_seq) NodeEntitiesInfoSeq
Definition: graph_cache.hpp:502
rmw_qos_profile_t
std::mutex
rmw_dds_common::ParticipantInfo
Definition: graph_cache.hpp:521
names_and_types.h
rmw_dds_common::GraphCache::operator<<
friend std::ostream & operator<<(std::ostream &ostream, const GraphCache &topic_cache)
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
Definition: graph_cache.hpp:531
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.