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)
 
virtual ~ClRosLaunch2 ()
 
void launch ()
 
void stop ()
 
- Public Member Functions inherited from smacc2::ISmaccClient
 ISmaccClient ()
 
virtual ~ISmaccClient ()
 
virtual void onInitialize ()
 
virtual std::string getName () const
 
template<typename TComponent >
TComponent * getComponent ()
 
template<typename TComponent >
TComponent * getComponent (std::string name)
 
template<typename TComponent >
TComponent * getComponent (int index)
 
virtual smacc2::introspection::TypeInfo::Ptr getType ()
 
ISmaccStateMachinegetStateMachine ()
 
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 (std::string packageName, std::string launchFilename, std::function< bool()> cancelCondition, ClRosLaunch2 *client=nullptr)
 

Public Attributes

std::string packageName_
 
std::string launchFileName_
 
pid_t launchPid_
 

Protected Types

typedef std::function< void > cancelCallback
 

Protected Attributes

std::future< std::string > result_
 
std::atomic< boolcancellationToken_ = 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 >, cancelCallbackdetached_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 (TArgs... targs)
 
template<typename SmaccComponentType , typename TOrthogonal , typename TClient , typename... TArgs>
SmaccComponentType * createNamedComponent (std::string name, TArgs... targs)
 
rclcpp::Node::SharedPtr getNode ()
 
rclcpp::Logger getLogger ()
 

Detailed Description

Definition at line 43 of file smacc_ros_launch_client_2.hpp.

Member Typedef Documentation

◆ cancelCallback

Definition at line 72 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 ( )
virtual

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 ( std::string packageName,
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 throw std::runtime_error("popen() failed!");
84 }
85 if (client != nullptr)
86 {
87 client->launchPid_ = child.pid;
88 }
89
90 int fd = fileno(child.pipe);
91
92 int flags = fcntl(fd, F_GETFL, 0);
93 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
94
95 bool cancelled = false;
96
97 // while (!cancelCondition())
98 while (!cancelled)
99 {
100 cancelled = cancelCondition();
101 size_t bytesRead = fread(buffer.data(), 1, buffer.size(), /*data*/ child.pipe);
102
103 if (bytesRead > 0)
104 {
105 result.append(buffer.data(), bytesRead);
106 }
107 else if (bytesRead == 0)
108 {
109 // No data available yet
110 std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Wait before retrying
111 }
112 else
113 {
114 // Read error
115 RCLCPP_ERROR(rclcpp::get_logger("smacc2"), "Error reading from pipe");
116 break;
117 }
118 }
119
120 rclcpp::sleep_for(2s);
121 if (child.pid > 0)
122 {
123 killGrandchildren(child.pid);
124 rclcpp::sleep_for(2s);
125 }
126
127 int status;
128 pid_t child_pid = child.pid;
129 if (waitpid(child_pid, &status, 0) != -1)
130 {
131 if (WIFEXITED(status))
132 {
133 int exit_status = WEXITSTATUS(status);
134 RCLCPP_INFO(
135 rclcpp::get_logger("smacc2"), "Child process exited with status: %d", exit_status);
136 }
137 else if (WIFSIGNALED(status))
138 {
139 int term_signal = WTERMSIG(status);
140 RCLCPP_WARN(
141 rclcpp::get_logger("smacc2"), "Child process terminated by signal: %d", term_signal);
142 }
143 }
144 else
145 {
146 RCLCPP_ERROR(rclcpp::get_logger("smacc2"), "Error waiting for child process.");
147 }
148
149 pclose(child.pipe);
150 close(child.pipe->_fileno); // Close pipe file descriptor but not processes
151
152 RCLCPP_WARN_STREAM(rclcpp::get_logger("smacc2"), "[ClRosLaunch2] RESULT:\n" << result);
153
154 return result;
155 });
156}
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(std::string packageName, 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 76 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 74 of file smacc_ros_launch_client_2.hpp.

◆ launchFileName_

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

◆ launchPid_

pid_t smacc2::client_bases::ClRosLaunch2::launchPid_

Definition at line 67 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 70 of file smacc_ros_launch_client_2.hpp.

Referenced by launch().


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