Capabilities2 0.3.0
Loading...
Searching...
No Matches
runner_base.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <stdexcept>
4#include <string>
5#include <functional>
6
7#include <rclcpp/rclcpp.hpp>
8
11
12#include <capabilities2_msgs/msg/capability.hpp>
13#include <capabilities2_msgs/msg/capability_event_code.hpp>
14
16{
23struct runner_exception : public std::runtime_error
24{
25 using std::runtime_error::runtime_error;
26
27 runner_exception(const std::string& what) : std::runtime_error(what)
28 {
29 }
30
31 virtual const char* what() const noexcept override
32 {
33 return std::runtime_error::what();
34 }
35};
36
42{
43 std::string name;
44 std::string resource_type;
45 std::string msg_type;
46};
47
56{
57 std::string interface;
58 std::string provider;
59 std::vector<resource> resources;
60 std::string global_namespace;
61 std::string runner;
62 std::string started_by;
63 std::string pid;
65};
66
74{
75public:
77 {
78 }
79
80 ~RunnerBase() = default;
81
84 // incorporates event callbacks
85
94 virtual void start(rclcpp::Node::SharedPtr node, const runner_opts& run_config, const std::string& bond_id) = 0;
95
101 virtual void stop(const std::string& bond_id, const std::string& instance_id = "") = 0;
102
117 virtual void trigger(capabilities2_events::EventParameters& parameters, const std::string& bond_id,
118 const std::string& instance_id) = 0;
119
126 void init_base(rclcpp::Node::SharedPtr node, const runner_opts& run_config)
127 {
128 // store node connection source
129 capabilities2_msgs::msg::Capability source_capability;
130 source_capability.capability = run_config.interface;
131 source_capability.provider = run_config.provider;
132 set_source(source_capability);
133
134 // store node pointer and opts
135 node_ = node;
136 run_config_ = run_config;
137 }
138
144 void enable_events(std::shared_ptr<capabilities2_events::EventBase> events)
145 {
147 {
148 set_event_emitter(events);
149 }
150 }
151
164 void add_connection(const std::string& connection_id, const capabilities2_msgs::msg::CapabilityEventCode& type,
165 const capabilities2_msgs::msg::Capability& target,
166 std::function<void(const std::string&, const std::string&, const std::string&,
168 callback)
169 {
170 EventNode::add_connection(connection_id, type, target, callback);
171 }
172
176 const std::string get_package_name()
177 {
178 return run_config_.interface.substr(0, run_config_.interface.find("/"));
179 }
180
184 const std::string& get_interface() const
185 {
186 return run_config_.interface;
187 }
188
192 const std::string& get_provider() const
193 {
194 return run_config_.provider;
195 }
196
200 const std::string& get_started_by() const
201 {
202 return run_config_.started_by;
203 }
204
208 const std::string& get_pid() const
209 {
210 return run_config_.pid;
211 }
212
213protected:
214 // FIXME: implement new event subsystem
215
230
245
260
275
276 // run config getters
277
289 const std::string get_resource_name_by_type(const std::string& resource_type, const std::string& msg_type) const
290 {
291 for (const auto& resource : run_config_.resources)
292 {
293 if (resource.resource_type == resource_type)
294 {
295 if (resource.msg_type == msg_type)
296 {
297 return resource.name;
298 }
299 }
300 }
301
302 throw runner_exception("no resource found: " + msg_type);
303 }
304
311 const std::string get_parameter_name_by_type(const std::string& param_type) const
312 {
313 return get_resource_name_by_type("parameter", param_type);
314 }
315
322 const std::string get_topic_name_by_type(const std::string& topic_type) const
323 {
324 return get_resource_name_by_type("topic", topic_type);
325 }
326
333 const std::string get_service_name_by_type(const std::string& srv_type) const
334 {
335 return get_resource_name_by_type("service", srv_type);
336 }
337
344 const std::string get_action_name_by_type(const std::string& action_type) const
345 {
346 return get_resource_name_by_type("action", action_type);
347 }
348
356 const std::string get_first_resource_name(const std::string& resource_type) const
357 {
358 for (const auto& resource : run_config_.resources)
359 {
360 if (resource.resource_type == resource_type)
361 {
362 return resource.name;
363 }
364 }
365
366 throw runner_exception("no " + resource_type + " resource found for interface: " + run_config_.interface);
367 }
368
374 const std::string get_first_parameter_name() const
375 {
376 return get_first_resource_name("parameter");
377 }
378
384 const std::string get_first_topic_name() const
385 {
386 return get_first_resource_name("topic");
387 }
388
394 const std::string get_first_service_name() const
395 {
396 return get_first_resource_name("service");
397 }
398
404 const std::string get_first_action_name() const
405 {
406 return get_first_resource_name("action");
407 }
408
409 // FIXME: implement new event subsystem
410 // STATE CHANGE HELPERS
411
418 void emit_started(const std::string& bond_id, const std::string& instance_id,
420 {
421 RCLCPP_INFO(node_->get_logger(), "emitting STARTED event with bond_id: %s", bond_id.c_str());
422 emit_event(bond_id, instance_id, capabilities2_msgs::msg::CapabilityEventCode::STARTED, parameters);
423 }
424
431 void emit_stopped(const std::string& bond_id, const std::string& instance_id,
433 {
434 RCLCPP_INFO(node_->get_logger(), "emitting STOPPED event with bond_id: %s", bond_id.c_str());
435 emit_event(bond_id, instance_id, capabilities2_msgs::msg::CapabilityEventCode::STOPPED, parameters);
436 }
437
444 void emit_succeeded(const std::string& bond_id, const std::string& instance_id,
446 {
447 RCLCPP_INFO(node_->get_logger(), "emitting SUCCEEDED event with bond_id: %s", bond_id.c_str());
448 emit_event(bond_id, instance_id, capabilities2_msgs::msg::CapabilityEventCode::SUCCEEDED, parameters);
449 }
450
457 void emit_failed(const std::string& bond_id, const std::string& instance_id,
459 {
460 RCLCPP_INFO(node_->get_logger(), "emitting FAILED event with bond_id: %s", bond_id.c_str());
461 emit_event(bond_id, instance_id, capabilities2_msgs::msg::CapabilityEventCode::FAILED, parameters);
462 }
463
464protected:
469 rclcpp::Node::SharedPtr node_;
470
475};
476
477} // namespace capabilities2_runner
an event node is a source of an event
Definition event_node.hpp:26
void set_source(const capabilities2_msgs::msg::Capability &src)
Set the source object.
Definition event_node.hpp:123
void set_event_emitter(std::shared_ptr< EventBase > event_emitter)
event emitter for this event node
Definition event_node.hpp:135
bool is_event_emitter_set() const
event emitter is set on this node
Definition event_node.hpp:278
void emit_event(const std::string &bond_id, const std::string &instance_id, const uint8_t &event_type, capabilities2_events::EventParameters parameters=capabilities2_events::EventParameters())
emit an event from this event node to all matching connections
Definition event_node.hpp:64
base class for all runners
Definition runner_base.hpp:74
const std::string get_first_service_name() const
Get the first service name.
Definition runner_base.hpp:394
virtual capabilities2_events::EventParameters param_on_success()
Update on_success event parameters with new data if available.
Definition runner_base.hpp:271
void emit_succeeded(const std::string &bond_id, const std::string &instance_id, capabilities2_events::EventParameters parameters=capabilities2_events::EventParameters())
emit SUCCEEDED event
Definition runner_base.hpp:444
virtual void stop(const std::string &bond_id, const std::string &instance_id="")=0
stop the runner
void emit_failed(const std::string &bond_id, const std::string &instance_id, capabilities2_events::EventParameters parameters=capabilities2_events::EventParameters())
emit FAILED event
Definition runner_base.hpp:457
const std::string & get_pid() const
get the pid of the runner.
Definition runner_base.hpp:208
const std::string get_first_resource_name(const std::string &resource_type) const
get first name of a given resource
Definition runner_base.hpp:356
virtual capabilities2_events::EventParameters param_on_started()
Update on_started event parameters with new data if available.
Definition runner_base.hpp:226
const std::string get_first_action_name() const
Get the first action name.
Definition runner_base.hpp:404
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
void add_connection(const std::string &connection_id, const capabilities2_msgs::msg::CapabilityEventCode &type, const capabilities2_msgs::msg::Capability &target, std::function< void(const std::string &, const std::string &, const std::string &, capabilities2_events::EventParameters)> callback)
make EventNode::add_connection public method on runner api.
Definition runner_base.hpp:164
const std::string get_parameter_name_by_type(const std::string &param_type) const
Get a parameter name by type.
Definition runner_base.hpp:311
const std::string get_resource_name_by_type(const std::string &resource_type, const std::string &msg_type) const
Get a resource name by data type from the config.
Definition runner_base.hpp:289
const std::string get_topic_name_by_type(const std::string &topic_type) const
Get a topic name by type.
Definition runner_base.hpp:322
virtual capabilities2_events::EventParameters param_on_stopped()
Update on_stopped event parameters with new data if available.
Definition runner_base.hpp:241
const std::string get_first_parameter_name() const
Get the first parameter name.
Definition runner_base.hpp:374
runner_opts run_config_
run_config_ runner configuration
Definition runner_base.hpp:474
virtual capabilities2_events::EventParameters param_on_failure()
Update on_failure event parameters with new data if available.
Definition runner_base.hpp:256
const std::string & get_interface() const
get the interface of the runner.
Definition runner_base.hpp:184
const std::string get_package_name()
get the package name to which the runner belong to.
Definition runner_base.hpp:176
void enable_events(std::shared_ptr< capabilities2_events::EventBase > events)
enable events system for this runner
Definition runner_base.hpp:144
rclcpp::Node::SharedPtr node_
shared pointer to the capabilities node Allows to use ros node related functionalities
Definition runner_base.hpp:469
const std::string & get_started_by() const
get the starter of the runner.
Definition runner_base.hpp:200
const std::string get_first_topic_name() const
Get the first topic name.
Definition runner_base.hpp:384
void emit_started(const std::string &bond_id, const std::string &instance_id, capabilities2_events::EventParameters parameters=capabilities2_events::EventParameters())
emit STARTED event
Definition runner_base.hpp:418
const std::string get_action_name_by_type(const std::string &action_type) const
Get the action name by type object.
Definition runner_base.hpp:344
RunnerBase()
Definition runner_base.hpp:76
virtual void trigger(capabilities2_events::EventParameters &parameters, const std::string &bond_id, const std::string &instance_id)=0
Trigger the runner.
const std::string & get_provider() const
get the provider of the runner.
Definition runner_base.hpp:192
virtual void start(rclcpp::Node::SharedPtr node, const runner_opts &run_config, const std::string &bond_id)=0
start the runner
const std::string get_service_name_by_type(const std::string &srv_type) const
Get a service name by type.
Definition runner_base.hpp:333
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
Definition action_runner.hpp:19
capability options for a capability runner given by interface and provider
Definition event_parameters.hpp:167
resource definition
Definition runner_base.hpp:42
std::string msg_type
Definition runner_base.hpp:45
std::string name
Definition runner_base.hpp:43
std::string resource_type
Definition runner_base.hpp:44
runner exception
Definition runner_base.hpp:24
virtual const char * what() const noexcept override
Definition runner_base.hpp:31
runner_exception(const std::string &what)
Definition runner_base.hpp:27
runner options
Definition runner_base.hpp:56
std::string pid
Definition runner_base.hpp:63
int input_count
Definition runner_base.hpp:64
std::string started_by
Definition runner_base.hpp:62
std::string interface
Definition runner_base.hpp:57
std::string global_namespace
Definition runner_base.hpp:60
std::string runner
Definition runner_base.hpp:61
std::string provider
Definition runner_base.hpp:58
std::vector< resource > resources
Definition runner_base.hpp:59