SMACC2
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Public Attributes | Protected Types | Protected Attributes | Static Protected Attributes | List of all members
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 >
TComponentgetComponent ()
 
template<typename TComponent >
TComponentgetComponent (std::string name)
 
template<typename TComponent >
TComponentgetComponent (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< voidcancelCallback
 

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 TSourceObject >
void onOrthogonalAllocation ()
 
template<typename SmaccComponentType , typename TOrthogonal , typename TClient , typename... TArgs>
SmaccComponentTypecreateComponent (TArgs... targs)
 
template<typename SmaccComponentType , typename TOrthogonal , typename TClient , typename... TArgs>
SmaccComponentTypecreateNamedComponent (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 73 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.

42: /*packageName_(std::nullopt), launchFileName_(std::nullopt),*/ cancellationToken_(false)
43{
44}

◆ ClRosLaunch2() [2/2]

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

◆ ~ClRosLaunch2()

smacc2::client_bases::ClRosLaunch2::~ClRosLaunch2 ( )
virtual

Definition at line 51 of file smacc_ros_launch_client_2.cpp.

51{}

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 69 of file smacc_ros_launch_client_2.cpp.

73{
74 return std::async(
75 std::launch::async,
76 [packageName, launchFileName, cancelCondition, client]()
77 {
78 RCLCPP_WARN_STREAM(rclcpp::get_logger("smacc2"), "[ClRosLaunch2] Starting ros launch thread");
79
80 std::stringstream cmd;
81 cmd << "ros2 launch " << packageName << " " << launchFileName;
82 std::array<char, 128> buffer;
83 std::string result;
84
85 auto child = runProcess(cmd.str().c_str());
86
87 if (!child.pipe)
88 {
89 throw std::runtime_error("popen() failed!");
90 }
91 if (client != nullptr)
92 {
93 client->launchPid_ = child.pid;
94 }
95
96 int fd = fileno(child.pipe);
97
98 int flags = fcntl(fd, F_GETFL, 0);
99 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
100
101 bool cancelled = false;
102
103 // while (!cancelCondition())
104 while (!cancelled)
105 {
106 cancelled = cancelCondition();
107 size_t bytesRead = fread(buffer.data(), 1, buffer.size(), /*data*/ child.pipe);
108
109 if (bytesRead > 0)
110 {
111 result.append(buffer.data(), bytesRead);
112 }
113 else if (bytesRead == 0)
114 {
115 // No se han leído más datos
116 std::this_thread::sleep_for(
117 std::chrono::milliseconds(100)); // Espera antes de intentar nuevamente
118 }
119 else
120 {
121 // Error de lectura
122 RCLCPP_ERROR(rclcpp::get_logger("smacc2"), "Error de lectura en pipe");
123 break;
124 }
125 }
126
127 rclcpp::sleep_for(2s);
128 if (child.pid > 0)
129 {
130 killGrandchildren(child.pid);
131 rclcpp::sleep_for(2s);
132 }
133
134 int status;
135 pid_t child_pid = child.pid;
136 if (waitpid(child_pid, &status, 0) != -1)
137 {
138 if (WIFEXITED(status))
139 {
140 int exit_status = WEXITSTATUS(status);
141 RCLCPP_INFO(
142 rclcpp::get_logger("smacc2"), "Child process exited with status: %d", exit_status);
143 }
144 else if (WIFSIGNALED(status))
145 {
146 int term_signal = WTERMSIG(status);
147 RCLCPP_WARN(
148 rclcpp::get_logger("smacc2"), "Child process terminated by signal: %d", term_signal);
149 }
150 }
151 else
152 {
153 RCLCPP_ERROR(rclcpp::get_logger("smacc2"), "Error waiting for child process.");
154 }
155
156 pclose(child.pipe);
157 close(child.pipe->_fileno); // Close pipe file descriptor but not processes
158
159 RCLCPP_WARN_STREAM(rclcpp::get_logger("smacc2"), "[ClRosLaunch2] RESULT:\n" << result);
160
161 // Devuelve una std::future con el resultado
162 // return std::async(std::launch::async, [result]() { return result; });
163 return result;
164 });
165}
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 53 of file smacc_ros_launch_client_2.cpp.

54{
55 cancellationToken_.store(false);
56 // Iniciar el hilo para la ejecución del lanzamiento
57 this->result_ = /*std::async([this]()*/
58 // {
60 // });
61}
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 63 of file smacc_ros_launch_client_2.cpp.

64{
65 // Establecer la bandera de cancelación
66 cancellationToken_.store(true);
67}

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 77 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 75 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 71 of file smacc_ros_launch_client_2.hpp.

Referenced by launch().


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