ROS 2 rclcpp + rcl - kilted  kilted
ROS 2 C++ Client Library with ROS Client Library
node_services.cpp
1 // Copyright 2016 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 #include "rclcpp/node_interfaces/node_services.hpp"
16 
17 #include <string>
18 
20 
21 NodeServices::NodeServices(rclcpp::node_interfaces::NodeBaseInterface * node_base)
22 : node_base_(node_base)
23 {}
24 
25 NodeServices::~NodeServices()
26 {}
27 
28 void
29 NodeServices::add_service(
30  rclcpp::ServiceBase::SharedPtr service_base_ptr,
31  rclcpp::CallbackGroup::SharedPtr group)
32 {
33  if (group) {
34  if (!node_base_->callback_group_in_node(group)) {
36  }
37  } else {
38  group = node_base_->get_default_callback_group();
39  }
40 
41  group->add_service(service_base_ptr);
42 
43  // Notify the executor that a new service was created using the parent Node.
44  try {
45  node_base_->trigger_notify_guard_condition();
46  group->trigger_notify_guard_condition();
47  } catch (const rclcpp::exceptions::RCLError & ex) {
48  throw std::runtime_error(
49  std::string("failed to notify wait set on service creation: ") + ex.what());
50  }
51 }
52 
53 void
54 NodeServices::add_client(
55  rclcpp::ClientBase::SharedPtr client_base_ptr,
56  rclcpp::CallbackGroup::SharedPtr group)
57 {
58  if (group) {
59  if (!node_base_->callback_group_in_node(group)) {
61  }
62  } else {
63  group = node_base_->get_default_callback_group();
64  }
65 
66  group->add_client(client_base_ptr);
67 
68  // Notify the executor that a new client was created using the parent Node.
69  try {
70  node_base_->trigger_notify_guard_condition();
71  group->trigger_notify_guard_condition();
72  } catch (const rclcpp::exceptions::RCLError & ex) {
73  throw std::runtime_error(
74  std::string("failed to notify wait set on client creation: ") + ex.what());
75  }
76 }
77 
78 std::string
79 NodeServices::resolve_service_name(const std::string & name, bool only_expand) const
80 {
81  return node_base_->resolve_topic_or_service_name(name, true, only_expand);
82 }
Thrown when a callback group is missing from the node, when it wants to utilize the group.
Definition: exceptions.hpp:244
Created when the return code does not match one of the other specialized exceptions.
Definition: exceptions.hpp:162
Pure virtual interface class for the NodeBase part of the Node API.
virtual RCLCPP_PUBLIC void trigger_notify_guard_condition()=0
Trigger the guard condition that notifies of internal node state changes.
virtual RCLCPP_PUBLIC bool callback_group_in_node(rclcpp::CallbackGroup::SharedPtr group)=0
Return true if the given callback group is associated with this node.
virtual RCLCPP_PUBLIC rclcpp::CallbackGroup::SharedPtr get_default_callback_group()=0
Return the default callback group.
virtual RCLCPP_PUBLIC std::string resolve_topic_or_service_name(const std::string &name, bool is_service, bool only_expand=false) const =0
Expand and remap a given topic or service name.
Implementation of the NodeServices part of the Node API.
RCLCPP_PUBLIC std::string resolve_service_name(const std::string &name, bool only_expand=false) const override
Get the remapped and expanded service name given a input name.