Capabilities2 0.3.0
Loading...
Searching...
No Matches
service_runner.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <chrono>
4
6
8{
9
15template <typename ServiceT>
17{
18public:
25
33 virtual void init_service(rclcpp::Node::SharedPtr node, const runner_opts& run_config,
34 const std::string& service_name)
35 {
36 // initialize the runner base by storing node pointer and run config
37 init_base(node, run_config);
38
39 // create a service client
40 service_client_ = node_->create_client<ServiceT>(service_name);
41
42 // wait for service server
43 RCLCPP_INFO(node_->get_logger(), "waiting for service: %s", service_name.c_str());
44
45 if (!service_client_->wait_for_service(std::chrono::seconds(3)))
46 {
47 RCLCPP_ERROR(node_->get_logger(), "failed to connect to service: %s", service_name.c_str());
48 throw runner_exception("failed to connect to server");
49 }
50
51 RCLCPP_INFO(node_->get_logger(), "connected with service: %s", service_name.c_str());
52 }
53
58 virtual void stop(const std::string& bond_id, const std::string& instance_id = "") override
59 {
60 // if the node pointer is empty then throw an error
61 // this means that the runner was not started and is being used out of order
62
63 if (!node_)
64 throw runner_exception("cannot stop runner that was not started");
65
66 // throw an error if the service client is null
67 // this can happen if the runner is not able to find the action resource
68
69 if (!service_client_)
70 throw runner_exception("cannot stop runner action that was not started");
71
72 // emit stopped event
73 emit_stopped(bond_id, instance_id, param_on_stopped());
74
75 RCLCPP_INFO(node_->get_logger(), "runner cleaned. stopping..");
76 }
77
78protected:
87 virtual void execution(capabilities2_events::EventParameters parameters, const std::string& thread_id) override
88 {
89 // split thread_id to get bond_id and instance_id (format: "bond_id/instance_id")
90 std::string bond_id = ThreadTriggerRunner::bond_from_thread_id(thread_id);
91 std::string instance_id = ThreadTriggerRunner::instance_from_thread_id(thread_id);
92
93 // generate a goal from parameters if provided
94 auto request_msg = std::make_shared<typename ServiceT::Request>(generate_request(parameters));
95
96 RCLCPP_INFO(node_->get_logger(), "request generated for event :%s", instance_id.c_str());
97
98 std::mutex block_mutex;
99 std::unique_lock<std::mutex> lock(block_mutex);
100 std::condition_variable cv;
101 bool completed = false;
102
103 auto result_future = service_client_->async_send_request(
104 request_msg, [this, &instance_id, &completed, &bond_id, &cv](typename rclcpp::Client<ServiceT>::SharedFuture future) {
105 if (!future.valid())
106 {
107 RCLCPP_ERROR(node_->get_logger(), "get result call failed");
108
109 // emit failed event
110 emit_failed(bond_id, instance_id, param_on_failure());
111 }
112 else
113 {
114 RCLCPP_INFO(node_->get_logger(), "get result call succeeded for event :%s", instance_id.c_str());
115
116 response_ = future.get();
117 process_response(response_);
118
119 // emit success event
120 emit_succeeded(bond_id, instance_id, param_on_success());
121 }
122
123 completed = true;
124 cv.notify_all();
125 });
126
127 // Conditional wait
128 cv.wait(lock, [&completed] { return completed; });
129 RCLCPP_INFO(node_->get_logger(), "Service request complete. Thread closing.");
130 }
131
132protected:
144 virtual typename ServiceT::Request generate_request(capabilities2_events::EventParameters& parameters) = 0;
145
156 virtual void process_response(typename ServiceT::Response::SharedPtr /*response*/) {}
157
158 typename rclcpp::Client<ServiceT>::SharedPtr service_client_;
159 typename ServiceT::Response::SharedPtr response_;
160};
161} // namespace capabilities2_runner
void init_base(rclcpp::Node::SharedPtr node, const runner_opts &run_config)
Initializer function for initializing the base runner in place of constructor due to plugin semantics...
Definition runner_base.hpp:126
virtual capabilities2_events::EventParameters param_on_stopped()
Update on_stopped event parameters with new data if available.
Definition runner_base.hpp:241
rclcpp::Node::SharedPtr node_
shared pointer to the capabilities node Allows to use ros node related functionalities
Definition runner_base.hpp:469
void emit_stopped(const std::string &bond_id, const std::string &instance_id, capabilities2_events::EventParameters parameters=capabilities2_events::EventParameters())
emit STOPPED event
Definition runner_base.hpp:431
service runner base class
Definition service_runner.hpp:17
virtual void process_response(typename ServiceT::Response::SharedPtr)
Process the reponse and print data as required.
Definition service_runner.hpp:156
virtual void stop(const std::string &bond_id, const std::string &instance_id="") override
stop function to cease functionality and shutdown
Definition service_runner.hpp:58
virtual ServiceT::Request generate_request(capabilities2_events::EventParameters &parameters)=0
Generate a request from parameters.
virtual void execution(capabilities2_events::EventParameters parameters, const std::string &thread_id) override
Trigger process to be executed.
Definition service_runner.hpp:87
ServiceRunner()
Constructor which needs to be empty due to plugin semantics.
Definition service_runner.hpp:22
rclcpp::Client< ServiceT >::SharedPtr service_client_
Definition service_runner.hpp:158
virtual void init_service(rclcpp::Node::SharedPtr node, const runner_opts &run_config, const std::string &service_name)
Initializer function for initializing the service runner in place of constructor due to plugin semant...
Definition service_runner.hpp:33
ServiceT::Response::SharedPtr response_
Definition service_runner.hpp:159
add threaded trigger execution to the runner
Definition threadtrigger_runner.hpp:21
static const std::string bond_from_thread_id(const std::string &thread_id)
helper function to extract bond_id from thread_id
Definition threadtrigger_runner.hpp:30
static const std::string instance_from_thread_id(const std::string &thread_id)
helper function to extract instance_id from thread_id
Definition threadtrigger_runner.hpp:44
Definition action_runner.hpp:19
capability options for a capability runner given by interface and provider
Definition event_parameters.hpp:167
runner exception
Definition runner_base.hpp:24
runner options
Definition runner_base.hpp:56