Nav2 Navigation Stack - kilted  kilted
ROS 2 Navigation Stack
dummy_service.hpp
1 // Copyright (c) 2020 Sarthak Mittal
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. Reserved.
14 
15 #ifndef BEHAVIOR_TREE__DUMMY_SERVICE_HPP_
16 #define BEHAVIOR_TREE__DUMMY_SERVICE_HPP_
17 
18 #include <memory>
19 #include <string>
20 #include <utility>
21 #include <vector>
22 #include <chrono>
23 
24 #include "rclcpp_action/rclcpp_action.hpp"
25 #include "rclcpp/rclcpp.hpp"
26 
27 namespace nav2_system_tests
28 {
29 
30 template<class ServiceT>
32 {
33 public:
34  explicit DummyService(
35  const rclcpp::Node::SharedPtr & node,
36  std::string service_name)
37  : node_(node),
38  service_name_(service_name),
39  request_count_(0),
40  disabled_(false)
41  {
42  service_ = node->create_service<ServiceT>(
43  service_name,
44  std::bind(&DummyService::handle_service, this, _1, _2, _3));
45  }
46 
47  virtual ~DummyService() = default;
48 
49  void disable()
50  {
51  service_.reset();
52  disabled_ = true;
53  }
54 
55  void enable()
56  {
57  if (disabled_) {
58  service_ = node_->create_service<ServiceT>(
59  service_name_,
60  std::bind(&DummyService::handle_service, this, _1, _2, _3));
61  disabled_ = false;
62  }
63  }
64 
65  void reset()
66  {
67  enable();
68  request_count_ = 0;
69  }
70 
71  int getRequestCount() const
72  {
73  return request_count_;
74  }
75 
76 protected:
77  virtual void fillResponse(
78  const std::shared_ptr<typename ServiceT::Request>/*request*/,
79  const std::shared_ptr<typename ServiceT::Response>/*response*/) {}
80 
81  void handle_service(
82  const std::shared_ptr<rmw_request_id_t>/*request_header*/,
83  const std::shared_ptr<typename ServiceT::Request> request,
84  const std::shared_ptr<typename ServiceT::Response> response)
85  {
86  request_count_++;
87  fillResponse(request, response);
88  }
89 
90 private:
91  rclcpp::Node::SharedPtr node_;
92  typename rclcpp::Service<ServiceT>::SharedPtr service_;
93  std::string service_name_;
94  int request_count_;
95  bool disabled_;
96 };
97 
98 } // namespace nav2_system_tests
99 
100 #endif // BEHAVIOR_TREE__DUMMY_SERVICE_HPP_