rclcpp_components  master
Package containing tools for dynamically loadable components
component_manager.hpp
Go to the documentation of this file.
1 // Copyright 2019 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 
41 #ifndef RCLCPP_COMPONENTS__COMPONENT_MANAGER_HPP__
42 #define RCLCPP_COMPONENTS__COMPONENT_MANAGER_HPP__
43 
44 #include <map>
45 #include <memory>
46 #include <string>
47 #include <utility>
48 #include <vector>
49 
50 #include "composition_interfaces/srv/load_node.hpp"
51 #include "composition_interfaces/srv/unload_node.hpp"
52 #include "composition_interfaces/srv/list_nodes.hpp"
53 
54 #include "rclcpp/executor.hpp"
55 #include "rclcpp/node_options.hpp"
56 #include "rclcpp/rclcpp.hpp"
57 
60 
61 namespace class_loader
62 {
63 class ClassLoader;
64 } // namespace class_loader
65 
67 {
68 
71 {
72 public:
73  explicit ComponentManagerException(const std::string & error_desc)
74  : std::runtime_error(error_desc) {}
75 };
76 
79 {
80 public:
81  using LoadNode = composition_interfaces::srv::LoadNode;
82  using UnloadNode = composition_interfaces::srv::UnloadNode;
83  using ListNodes = composition_interfaces::srv::ListNodes;
84 
86 
90 
92 
103  std::string node_name = "ComponentManager",
104  const rclcpp::NodeOptions & node_options = rclcpp::NodeOptions()
105  .start_parameter_services(false)
106  .start_parameter_event_publisher(false));
107 
109  virtual ~ComponentManager();
110 
112  /*
113  * \param package_name name of the package
114  * \param resource_index name of the executable
115  * \throws ComponentManagerException if the resource was not found or a invalid resource entry
116  * \return a list of component resources
117  */
121  const std::string & package_name,
122  const std::string & resource_index = "rclcpp_components") const;
123 
125  /*
126  * \param resource a component resource (class name + library path)
127  * \return a NodeFactory interface
128  */
132 
133 protected:
135  /*
136  * This function allows to add parameters, remap rules, a specific node, name a namespace
137  * and/or additional arguments.
138  *
139  * \param request_header unused
140  * \param request information with the node to load
141  * \param response
142  * \throws std::overflow_error if node_id suffers an overflow. Very unlikely to happen at 1 kHz
143  * (very optimistic rate). it would take 585 years.
144  * \throws ComponentManagerException In the case that the component constructor throws an
145  * exception, rethrow into the following catch block.
146  */
148  virtual void
149  OnLoadNode(
150  const std::shared_ptr<rmw_request_id_t> request_header,
153 
155  /*
156  * \param request_header unused
157  * \param request unique identifier to remove from the component
158  * \param response true on the success field if the node unload was succefully, otherwise false
159  * and the error_message field contains the error.
160  */
162  virtual void
163  OnUnloadNode(
164  const std::shared_ptr<rmw_request_id_t> request_header,
167 
169  /*
170  * Return a two list: one with the unique identifiers and other with full name of the nodes.
171  *
172  * \param request_header unused
173  * \param request unused
174  * \param response list with the unique ids and full node names
175  */
177  virtual void
178  OnListNodes(
179  const std::shared_ptr<rmw_request_id_t> request_header,
182 
183 private:
185 
186  uint64_t unique_id_ {1};
189 
193 };
194 
195 } // namespace rclcpp_components
196 
197 #endif // RCLCPP_COMPONENTS__COMPONENT_MANAGER_HPP__
std::string
std::shared_ptr
node_factory.hpp
rclcpp_components::ComponentManager
ComponentManager handles the services to load, unload, and get the list of loaded components.
Definition: component_manager.hpp:78
class_loader
rclcpp_components::ComponentManager::ListNodes
composition_interfaces::srv::ListNodes ListNodes
Definition: component_manager.hpp:83
std::pair
RCLCPP_COMPONENTS_PUBLIC
#define RCLCPP_COMPONENTS_PUBLIC
Definition: visibility_control.hpp:50
std::vector
rclcpp::Service
rclcpp_components::ComponentManagerException
Thrown when an error happens in the component Manager class.
Definition: component_manager.hpp:70
executor.hpp
rclcpp_components::ComponentManager::UnloadNode
composition_interfaces::srv::UnloadNode UnloadNode
Definition: component_manager.hpp:82
rclcpp_components::ComponentManager::LoadNode
composition_interfaces::srv::LoadNode LoadNode
Definition: component_manager.hpp:81
node_options.hpp
rclcpp_components::ComponentManager::ComponentManager
ComponentManager(std::weak_ptr< rclcpp::Executor > executor, std::string node_name="ComponentManager", const rclcpp::NodeOptions &node_options=rclcpp::NodeOptions() .start_parameter_services(false) .start_parameter_event_publisher(false))
Default constructor.
rclcpp::Node
rclcpp.hpp
rclcpp_components::ComponentManager::create_component_factory
virtual std::shared_ptr< rclcpp_components::NodeFactory > create_component_factory(const ComponentResource &resource)
Instantiate a component from a dynamic library.
std::runtime_error
std::map
std::weak_ptr< rclcpp::Executor >
rclcpp_components::ComponentManager::OnLoadNode
virtual void OnLoadNode(const std::shared_ptr< rmw_request_id_t > request_header, const std::shared_ptr< LoadNode::Request > request, std::shared_ptr< LoadNode::Response > response)
Service callback to load a new node in the component.
rclcpp_components::ComponentManagerException::ComponentManagerException
ComponentManagerException(const std::string &error_desc)
Definition: component_manager.hpp:73
rclcpp_components::ComponentManager::~ComponentManager
virtual ~ComponentManager()
visibility_control.hpp
std
rclcpp_components::ComponentManager::OnListNodes
virtual void OnListNodes(const std::shared_ptr< rmw_request_id_t > request_header, const std::shared_ptr< ListNodes::Request > request, std::shared_ptr< ListNodes::Response > response)
Service callback to get the list of nodes in the component.
rclcpp::NodeOptions
rclcpp_components
Definition: component_manager.hpp:66
rclcpp_components::ComponentManager::get_component_resources
virtual std::vector< ComponentResource > get_component_resources(const std::string &package_name, const std::string &resource_index="rclcpp_components") const
Return a list of valid loadable components in a given package.
rclcpp_components::ComponentManager::OnUnloadNode
virtual void OnUnloadNode(const std::shared_ptr< rmw_request_id_t > request_header, const std::shared_ptr< UnloadNode::Request > request, std::shared_ptr< UnloadNode::Response > response)
Service callback to unload a node in the component.