SMACC2
Loading...
Searching...
No Matches
smacc2::client_bases::ClRosLaunch2 Class Reference

#include <smacc_ros_launch_client_2.hpp>

Inheritance diagram for smacc2::client_bases::ClRosLaunch2:
Inheritance graph
Collaboration diagram for smacc2::client_bases::ClRosLaunch2:
Collaboration graph

Public Member Functions

 ClRosLaunch2 ()
 
 ClRosLaunch2 (std::string packageName, std::string launchFilename)
 
 ~ClRosLaunch2 () override
 
void launch ()
 
void stop ()
 
- Public Member Functions inherited from smacc2::ISmaccClient
 ISmaccClient ()
 
virtual ~ISmaccClient ()
 
 ISmaccClient (const ISmaccClient &)=delete
 
ISmaccClient & operator= (const ISmaccClient &)=delete
 
 ISmaccClient (ISmaccClient &&)=delete
 
ISmaccClient & operator= (ISmaccClient &&)=delete
 
virtual void onInitialize ()
 
virtual std::string getName () const
 
template<typename TComponent >
TComponent * getComponent ()
 
template<typename TComponent >
TComponent * getComponent (const std::string &name)
 
template<typename TComponent >
TComponent * getComponent (int index)
 
virtual smacc2::introspection::TypeInfo::Ptr getType ()
 
ISmaccStateMachine * getStateMachine ()
 
template<typename TSmaccSignal , typename T >
void connectSignal (TSmaccSignal &signal, void(T::*callback)(), T *object)
 
template<typename SmaccClientType >
void requiresClient (SmaccClientType *&storage)
 
void getComponents (std::vector< std::shared_ptr< ISmaccComponent > > &components)
 
const std::vector< std::shared_ptr< ISmaccComponent > > & iterateComponents () const
 
template<typename EventType >
void postEvent (const EventType &ev)
 
template<typename EventType >
void postEvent ()
 

Static Public Member Functions

static std::future< std::string > executeRosLaunch (const std::string &packageName, const std::string &launchFileName, std::function< bool()> cancelCondition, ClRosLaunch2 *client=nullptr)
 

Public Attributes

std::string packageName_
 
std::string launchFileName_
 
pid_t launchPid_ = -1
 

Protected Types

typedef std::function< void > cancelCallback
 

Protected Attributes

std::future< std::string > result_
 
std::atomic< bool > cancellationToken_ = ATOMIC_VAR_INIT(false)
 
- Protected Attributes inherited from smacc2::ISmaccClient
std::map< ComponentKey, std::shared_ptr< smacc2::ISmaccComponent > > components_
 

Static Protected Attributes

static std::map< std::future< void >, cancelCallback > detached_futures_
 

Additional Inherited Members

- Protected Member Functions inherited from smacc2::ISmaccClient
template<typename TOrthogonal , typename TClient >
void onComponentInitialization ()
 
template<typename TOrthogonal , typename TSourceObject >
void onStateOrthogonalAllocation ()
 
template<typename SmaccComponentType , typename TOrthogonal , typename TClient , typename... TArgs>
SmaccComponentType * createComponent (const TArgs &... targs)
 
template<typename SmaccComponentType , typename TOrthogonal , typename TClient , typename... TArgs>
SmaccComponentType * createNamedComponent (const std::string &name, const TArgs &... targs)
 
rclcpp::Node::SharedPtr getNode ()
 
rclcpp::Logger getLogger ()
 

Detailed Description

Definition at line 46 of file smacc_ros_launch_client_2.hpp.

Member Typedef Documentation

◆ cancelCallback

Definition at line 76 of file smacc_ros_launch_client_2.hpp.

Constructor & Destructor Documentation

◆ ClRosLaunch2() [1/2]

smacc2::client_bases::ClRosLaunch2::ClRosLaunch2 ( )

Definition at line 41 of file smacc_ros_launch_client_2.cpp.

◆ ClRosLaunch2() [2/2]

smacc2::client_bases::ClRosLaunch2::ClRosLaunch2 ( std::string packageName,
std::string launchFilename )

◆ ~ClRosLaunch2()

smacc2::client_bases::ClRosLaunch2::~ClRosLaunch2 ( )
override

Definition at line 48 of file smacc_ros_launch_client_2.cpp.

48{}

Member Function Documentation

◆ executeRosLaunch()

std::future< std::string > smacc2::client_bases::ClRosLaunch2::executeRosLaunch ( const std::string & packageName,
const std::string & launchFileName,
std::function< bool()> cancelCondition,
ClRosLaunch2 * client = nullptr )
static

Definition at line 64 of file smacc_ros_launch_client_2.cpp.

67{
68 return std::async(
69 std::launch::async,
70 [packageName, launchFileName, cancelCondition, client]()
71 {
72 RCLCPP_WARN_STREAM(rclcpp::get_logger("smacc2"), "[ClRosLaunch2] Starting ros launch thread");
73
74 std::stringstream cmd;
75 cmd << "ros2 launch " << packageName << " " << launchFileName;
76 std::array<char, 128> buffer{};
77 std::string result;
78
79 auto child = runProcess(cmd.str().c_str());
80
81 if (!child.pipe)
82 {
83 // Logged, not thrown: nothing reads this future in LAUNCH_CLIENT_BEHAVIOR_LIFETIME mode.
84 RCLCPP_ERROR_STREAM(
85 rclcpp::get_logger("smacc2"), "[ClRosLaunch2] could not start: " << cmd.str());
86 return result;
87 }
88 if (client != nullptr)
89 {
90 client->launchPid_ = child.pid;
91 }
92
93 int fd = fileno(child.pipe.get());
94 bool cancelled = false;
95
96 // poll() with a 100 ms timeout is also the cancel-check cadence. read() returning 0 is the
97 // launch tree's end of file, which the former non-blocking fread() loop took for "no data yet".
98 while (!cancelled)
99 {
100 cancelled = cancelCondition();
101 pollfd pfd{fd, POLLIN, 0};
102 int ready = poll(&pfd, 1, 100);
103 if (ready < 0)
104 {
105 if (errno == EINTR) continue;
106 RCLCPP_ERROR_STREAM(
107 rclcpp::get_logger("smacc2"),
108 "[ClRosLaunch2] poll() failed: " << std::generic_category().message(errno));
109 break;
110 }
111 if (ready == 0) continue;
112
113 ssize_t bytesRead = read(fd, buffer.data(), buffer.size());
114 if (bytesRead > 0)
115 {
116 result.append(buffer.data(), static_cast<size_t>(bytesRead));
117 }
118 else if (bytesRead == 0)
119 {
120 RCLCPP_INFO(
121 rclcpp::get_logger("smacc2"), "[ClRosLaunch2] launch process closed its output");
122 break;
123 }
124 else if (errno != EINTR && errno != EAGAIN)
125 {
126 RCLCPP_ERROR_STREAM(
127 rclcpp::get_logger("smacc2"),
128 "[ClRosLaunch2] read() failed: " << std::generic_category().message(errno));
129 break;
130 }
131 }
132
133 rclcpp::sleep_for(2s);
134 if (child.pid > 0)
135 {
136 killGrandchildren(child.pid);
137 rclcpp::sleep_for(2s);
138 }
139
140 int status = 0;
141 pid_t child_pid = child.pid;
142 if (waitpid(child_pid, &status, 0) != -1)
143 {
144 if (WIFEXITED(status))
145 {
146 int exit_status = WEXITSTATUS(status);
147 RCLCPP_INFO(
148 rclcpp::get_logger("smacc2"), "Child process exited with status: %d", exit_status);
149 }
150 else if (WIFSIGNALED(status))
151 {
152 int term_signal = WTERMSIG(status);
153 RCLCPP_WARN(
154 rclcpp::get_logger("smacc2"), "Child process terminated by signal: %d", term_signal);
155 }
156 }
157 else
158 {
159 RCLCPP_ERROR(rclcpp::get_logger("smacc2"), "Error waiting for child process.");
160 }
161
162 // The stream came from fdopen() on our own pipe and the child is reaped: fclose, through
163 // the pointer's deleter, closes the descriptor and frees the FILE. Not pclose().
164 child.pipe.reset();
165
166 RCLCPP_WARN_STREAM(rclcpp::get_logger("smacc2"), "[ClRosLaunch2] RESULT:\n" << result);
167
168 return result;
169 });
170}
void killGrandchildren(pid_t originalPid)
ProcessInfo runProcess(const char *command)

References smacc2::client_bases::killGrandchildren(), launchPid_, and smacc2::client_bases::runProcess().

Referenced by launch(), and smacc2::client_behaviors::CbRosLaunch2::onEntry().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ launch()

void smacc2::client_bases::ClRosLaunch2::launch ( )

Definition at line 50 of file smacc_ros_launch_client_2.cpp.

51{
52 cancellationToken_.store(false);
53 // Start the launch execution thread
54 this->result_ =
56}
static std::future< std::string > executeRosLaunch(const std::string &packageName, const std::string &launchFileName, std::function< bool()> cancelCondition, ClRosLaunch2 *client=nullptr)

References cancellationToken_, executeRosLaunch(), launchFileName_, packageName_, and result_.

Referenced by smacc2::client_behaviors::CbRosLaunch2::onEntry().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ stop()

void smacc2::client_bases::ClRosLaunch2::stop ( )

Definition at line 58 of file smacc_ros_launch_client_2.cpp.

59{
60 // Set the cancellation flag
61 cancellationToken_.store(true);
62}

References cancellationToken_.

Referenced by smacc2::client_behaviors::CbRosStop2::onEntry().

Here is the caller graph for this function:

Member Data Documentation

◆ cancellationToken_

std::atomic<bool> smacc2::client_bases::ClRosLaunch2::cancellationToken_ = ATOMIC_VAR_INIT(false)
protected

Definition at line 80 of file smacc_ros_launch_client_2.hpp.

Referenced by launch(), and stop().

◆ detached_futures_

std::map<std::future<void>, cancelCallback> smacc2::client_bases::ClRosLaunch2::detached_futures_
staticprotected

Definition at line 78 of file smacc_ros_launch_client_2.hpp.

◆ launchFileName_

std::string smacc2::client_bases::ClRosLaunch2::launchFileName_

◆ launchPid_

pid_t smacc2::client_bases::ClRosLaunch2::launchPid_ = -1

Definition at line 71 of file smacc_ros_launch_client_2.hpp.

Referenced by executeRosLaunch().

◆ packageName_

std::string smacc2::client_bases::ClRosLaunch2::packageName_

◆ result_

std::future<std::string> smacc2::client_bases::ClRosLaunch2::result_
protected

Definition at line 74 of file smacc_ros_launch_client_2.hpp.

Referenced by launch().


The documentation for this class was generated from the following files: