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 
107  virtual ~ComponentManager();
108 
110  /*
111  * \param package_name name of the package
112  * \param resource_index name of the executable
113  * \throws ComponentManagerException if the resource was not found or a invalid resource entry
114  * \return a list of component resources
115  */
119  const std::string & package_name,
120  const std::string & resource_index = "rclcpp_components") const;
121 
123  /*
124  * \param resource a component resource (class name + library path)
125  * \return a NodeFactory interface
126  */
130 
131 protected:
133  /*
134  * This function allows to add parameters, remap rules, a specific node, name a namespace
135  * and/or additional arguments.
136  *
137  * \param request_header unused
138  * \param request information with the node to load
139  * \param response
140  * \throws std::overflow_error if node_id suffers an overflow. Very unlikely to happen at 1 kHz
141  * (very optimistic rate). it would take 585 years.
142  * \throws ComponentManagerException In the case that the component constructor throws an
143  * exception, rethrow into the following catch block.
144  */
146  virtual void
147  OnLoadNode(
148  const std::shared_ptr<rmw_request_id_t> request_header,
151 
153  /*
154  * \param request_header unused
155  * \param request unique identifier to remove from the component
156  * \param response true on the success field if the node unload was succefully, otherwise false
157  * and the error_message field contains the error.
158  */
160  virtual void
161  OnUnloadNode(
162  const std::shared_ptr<rmw_request_id_t> request_header,
165 
167  /*
168  * Return a two list: one with the unique identifiers and other with full name of the nodes.
169  *
170  * \param request_header unused
171  * \param request unused
172  * \param response list with the unique ids and full node names
173  */
175  virtual void
176  OnListNodes(
177  const std::shared_ptr<rmw_request_id_t> request_header,
180 
181 private:
183 
184  uint64_t unique_id_ {1};
187 
191 };
192 
193 } // namespace rclcpp_components
194 
195 #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::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::ComponentManager::ComponentManager
ComponentManager(std::weak_ptr< rclcpp::Executor > executor, std::string node_name="ComponentManager", const rclcpp::NodeOptions &node_options=rclcpp::NodeOptions())
Default constructor.
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.