SMACC2
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
smacc2::SmaccAsyncClientBehavior Class Reference

#include <smacc_asynchronous_client_behavior.hpp>

Inheritance diagram for smacc2::SmaccAsyncClientBehavior:
Inheritance graph
Collaboration diagram for smacc2::SmaccAsyncClientBehavior:
Collaboration graph

Public Member Functions

template<typename TOrthogonal , typename TSourceObject >
void onOrthogonalAllocation ()
 
virtual ~SmaccAsyncClientBehavior ()
 
template<typename TCallback , typename T >
boost::signals2::connection onSuccess (TCallback callback, T *object)
 
template<typename TCallback , typename T >
boost::signals2::connection onFinished (TCallback callback, T *object)
 
template<typename TCallback , typename T >
boost::signals2::connection onFailure (TCallback callback, T *object)
 
void requestForceFinish ()
 
void executeOnEntry () override
 
void executeOnExit () override
 
void waitOnEntryThread (bool requestFinish)
 
template<typename TCallbackMethod , typename T >
boost::signals2::connection onSuccess (TCallbackMethod callback, T *object)
 
template<typename TCallbackMethod , typename T >
boost::signals2::connection onFinished (TCallbackMethod callback, T *object)
 
template<typename TCallbackMethod , typename T >
boost::signals2::connection onFailure (TCallbackMethod callback, T *object)
 
- Public Member Functions inherited from smacc2::ISmaccClientBehavior
 ISmaccClientBehavior ()
 
virtual ~ISmaccClientBehavior ()
 
ISmaccStateMachinegetStateMachine ()
 
std::string getName () const
 
template<typename SmaccClientType >
void requiresClient (SmaccClientType *&storage)
 
template<typename SmaccComponentType >
void requiresComponent (SmaccComponentType *&storage, bool throwExceptionIfNotExist=false)
 
virtual void onEntry ()
 
virtual void onExit ()
 

Protected Member Functions

void postSuccessEvent ()
 
void postFailureEvent ()
 
virtual void dispose () override
 
bool isShutdownRequested ()
 onEntry is executed in a new thread. However the current state cannot be left until the onEntry thread finishes. This flag can be checked from the onEntry thread to force finishing the thread.
 
- Protected Member Functions inherited from smacc2::ISmaccClientBehavior
virtual void runtimeConfigure ()
 
template<typename EventType >
void postEvent (const EventType &ev)
 
template<typename EventType >
void postEvent ()
 
ISmaccStategetCurrentState ()
 
virtual rclcpp::Node::SharedPtr getNode () const
 
virtual rclcpp::Logger getLogger () const
 

Private Member Functions

void waitFutureIfNotFinished (std::optional< std::future< int > > &threadfut, bool requestFinish)
 

Private Attributes

std::optional< std::future< int > > onEntryThread_
 
std::optional< std::future< int > > onExitThread_
 
std::function< void()> postFinishEventFn_
 
std::function< void()> postSuccessEventFn_
 
std::function< void()> postFailureEventFn_
 
SmaccSignal< void()> onFinished_
 
SmaccSignal< void()> onSuccess_
 
SmaccSignal< void()> onFailure_
 
bool isShutdownRequested_ = false
 

Detailed Description

Definition at line 55 of file smacc_asynchronous_client_behavior.hpp.

Constructor & Destructor Documentation

◆ ~SmaccAsyncClientBehavior()

smacc2::SmaccAsyncClientBehavior::~SmaccAsyncClientBehavior ( )
virtual

Definition at line 151 of file smacc_client_async_behavior.cpp.

151{}

Member Function Documentation

◆ dispose()

void smacc2::SmaccAsyncClientBehavior::dispose ( )
overrideprotectedvirtual

Reimplemented from smacc2::ISmaccClientBehavior.

Definition at line 122 of file smacc_client_async_behavior.cpp.

123{
124 RCLCPP_DEBUG_STREAM(
125 getLogger(), "[" << getName()
126 << "] Destroying client behavior- Waiting finishing of asynchronous onExit "
127 "thread");
128 try
129 {
130 if (this->onExitThread_)
131 {
132 // this->onExitThread_->get();
133 this->onExitThread_->get();
134 }
135 //this->onExitThread_->get();
136 }
137 catch (...)
138 {
139 RCLCPP_DEBUG(
140 getLogger(),
141 "[SmaccAsyncClientBehavior] trying to Join onExit function, but it was already "
142 "finished.");
143 }
144
145 RCLCPP_DEBUG_STREAM(
146 getLogger(), "[" << getName()
147 << "] Destroying client behavior- onExit thread finished. Proccedding "
148 "destruction.");
149}
virtual rclcpp::Logger getLogger() const
std::optional< std::future< int > > onExitThread_

References smacc2::ISmaccClientBehavior::getLogger(), smacc2::ISmaccClientBehavior::getName(), and onExitThread_.

Here is the call graph for this function:

◆ executeOnEntry()

void smacc2::SmaccAsyncClientBehavior::executeOnEntry ( )
overridevirtual

Reimplemented from smacc2::ISmaccClientBehavior.

Definition at line 27 of file smacc_client_async_behavior.cpp.

28{
29 RCLCPP_INFO_STREAM(getLogger(), "[" << getName() << "] asynchronous onEntry thread started");
30 this->onEntryThread_ = std::async(
31 std::launch::async,
32 [=]
33 {
34 this->onEntry();
35 this->postFinishEventFn_();
36 RCLCPP_INFO_STREAM(getLogger(), "[" << getName() << "] asynchronous onEntry thread finished");
37 return 0;
38 });
39}
std::optional< std::future< int > > onEntryThread_

References smacc2::ISmaccClientBehavior::getLogger(), smacc2::ISmaccClientBehavior::getName(), smacc2::ISmaccClientBehavior::onEntry(), onEntryThread_, and postFinishEventFn_.

Here is the call graph for this function:

◆ executeOnExit()

void smacc2::SmaccAsyncClientBehavior::executeOnExit ( )
overridevirtual

Reimplemented from smacc2::ISmaccClientBehavior.

Definition at line 105 of file smacc_client_async_behavior.cpp.

106{
107 RCLCPP_INFO_STREAM(getLogger(), "[" << getName() << "] onExit - join async onEntry thread");
108 this->waitOnEntryThread(true);
109
110 RCLCPP_INFO_STREAM(
111 getLogger(), "[" << getName() << "] onExit - Creating asynchronous onExit thread");
112 this->onExitThread_ = std::async(
113 std::launch::async,
114 [=]
115 {
116 this->onExit();
117 RCLCPP_INFO_STREAM(getLogger(), "[" << getName() << "] asynchronous onExit done.");
118 return 0;
119 });
120}

References smacc2::ISmaccClientBehavior::getLogger(), smacc2::ISmaccClientBehavior::getName(), smacc2::ISmaccClientBehavior::onExit(), onExitThread_, and waitOnEntryThread().

Here is the call graph for this function:

◆ isShutdownRequested()

bool smacc2::SmaccAsyncClientBehavior::isShutdownRequested ( )
protected

onEntry is executed in a new thread. However the current state cannot be left until the onEntry thread finishes. This flag can be checked from the onEntry thread to force finishing the thread.

Definition at line 165 of file smacc_client_async_behavior.cpp.

166{
167 std::string shut = "";
169 {
170 shut = "True";
171 }
172 else
173 {
174 shut = "False";
175 }
176 // RCLCPP_FATAL_STREAM_THROTTLE(
177 // getLogger(), *(getNode()->get_clock()), 1000, "[" << getName() << "] " << ((uint64_t) this ) << " Is requestForceFinish active? " << shut );
179}

References isShutdownRequested_.

Referenced by cl_nav2z::CbNav2ZClientBehaviorBase::isOwnActionResponse(), smacc2::client_behaviors::CbServiceCall< ServiceType >::onEntry(), smacc2::client_behaviors::CbRosLaunch::onEntry(), smacc2::client_behaviors::CbRosLaunch2::onEntry(), smacc2::client_behaviors::CbSequence::onEntry(), smacc2::client_behaviors::CbSleepFor::onEntry(), smacc2::client_behaviors::CbWaitActionServer::onEntry(), smacc2::client_behaviors::CbWaitNode::onEntry(), and smacc2::client_behaviors::CbWaitTopic::onEntry().

Here is the caller graph for this function:

◆ onFailure() [1/2]

template<typename TCallback , typename T >
boost::signals2::connection smacc2::SmaccAsyncClientBehavior::onFailure ( TCallback  callback,
T object 
)

◆ onFailure() [2/2]

boost::signals2::connection smacc2::SmaccAsyncClientBehavior::onFailure ( TCallbackMethod  callback,
T object 
)

Definition at line 64 of file smacc_asynchronous_client_behavior_impl.hpp.

66{
67 return this->getStateMachine()->createSignalConnection(onFailure_, callback, object);
68}
boost::signals2::connection createSignalConnection(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object)

References smacc2::ISmaccStateMachine::createSignalConnection(), smacc2::ISmaccClientBehavior::getStateMachine(), and onFailure_.

Here is the call graph for this function:

◆ onFinished() [1/2]

template<typename TCallback , typename T >
boost::signals2::connection smacc2::SmaccAsyncClientBehavior::onFinished ( TCallback  callback,
T object 
)

◆ onFinished() [2/2]

boost::signals2::connection smacc2::SmaccAsyncClientBehavior::onFinished ( TCallbackMethod  callback,
T object 
)

◆ onOrthogonalAllocation()

void smacc2::SmaccAsyncClientBehavior::onOrthogonalAllocation ( )

Definition at line 28 of file smacc_asynchronous_client_behavior_impl.hpp.

29{
30 postFinishEventFn_ = [this]
31 {
32 this->onFinished_();
33 this->postEvent<EvCbFinished<TSourceObject, TOrthogonal>>();
34 };
35
36 postSuccessEventFn_ = [this]
37 {
38 this->onSuccess_();
39 this->postEvent<EvCbSuccess<TSourceObject, TOrthogonal>>();
40 };
41
42 postFailureEventFn_ = [this]
43 {
44 this->onFailure_();
45 this->postEvent<EvCbFailure<TSourceObject, TOrthogonal>>();
46 };
47}

References onFailure_, onFinished_, onSuccess_, postFailureEventFn_, postFinishEventFn_, and postSuccessEventFn_.

◆ onSuccess() [1/2]

template<typename TCallback , typename T >
boost::signals2::connection smacc2::SmaccAsyncClientBehavior::onSuccess ( TCallback  callback,
T object 
)

◆ onSuccess() [2/2]

boost::signals2::connection smacc2::SmaccAsyncClientBehavior::onSuccess ( TCallbackMethod  callback,
T object 
)

Definition at line 50 of file smacc_asynchronous_client_behavior_impl.hpp.

52{
53 return this->getStateMachine()->createSignalConnection(onSuccess_, callback, object);
54}

References smacc2::ISmaccStateMachine::createSignalConnection(), smacc2::ISmaccClientBehavior::getStateMachine(), and onSuccess_.

Here is the call graph for this function:

◆ postFailureEvent()

void smacc2::SmaccAsyncClientBehavior::postFailureEvent ( )
protected

◆ postSuccessEvent()

void smacc2::SmaccAsyncClientBehavior::postSuccessEvent ( )
protected

Definition at line 153 of file smacc_client_async_behavior.cpp.

References postSuccessEventFn_.

Referenced by cl_moveit2z::CbMoveEndEffectorTrajectory::executeJointSpaceTrajectory(), cl_moveit2z::CbMoveJoints::moveJoints(), cl_moveit2z::CbMoveEndEffectorRelative::moveRelative(), cl_moveit2z::CbMoveCartesianRelative::moveRelativeCartesian(), cl_moveit2z::CbMoveEndEffector::moveToAbsolutePose(), smacc2::client_behaviors::CbServiceCall< ServiceType >::onEntry(), smacc2::client_behaviors::CbSequence::onEntry(), smacc2::client_behaviors::CbSleepFor::onEntry(), smacc2::client_behaviors::CbWaitActionServer::onEntry(), smacc2::client_behaviors::CbWaitNode::onEntry(), smacc2::client_behaviors::CbWaitTopic::onEntry(), cl_moveit2z::CbAttachObject::onEntry(), cl_moveit2z::CbDetachObject::onEntry(), cl_nav2z::CbAbortNavigation::onEntry(), cl_nav2z::CbWaitPose::onEntry(), cl_nav2z::CbWaitTransform::onEntry(), cl_nav2z::CbWaitNav2Nodes::onMessageReceived(), smacc2::client_behaviors::CbWaitTopicMessage< TMessage >::onMessageReceived(), cl_nav2z::CbNav2ZClientBehaviorBase::onNavigationActionSuccess(), cl_nav2z::CbNavigateNextWaypointUntilReached::onNavigationActionSuccess(), cl_lifecyclenode::CbActivate::onOrthogonalAllocation(), cl_lifecyclenode::CbCleanup::onOrthogonalAllocation(), cl_lifecyclenode::CbConfigure::onOrthogonalAllocation(), cl_lifecyclenode::CbDeactivate::onOrthogonalAllocation(), cl_lifecyclenode::CbDestroy::onOrthogonalAllocation(), and cl_lifecyclenode::CbShutdown::onOrthogonalAllocation().

Here is the caller graph for this function:

◆ requestForceFinish()

void smacc2::SmaccAsyncClientBehavior::requestForceFinish ( )

Definition at line 157 of file smacc_client_async_behavior.cpp.

158{
159 RCLCPP_FATAL_STREAM_THROTTLE(
160 getLogger(), *(getNode()->get_clock()), 1000,
161 "[" << getName() << "] " << ((uint64_t)this) << " requestForceFinish");
163}
virtual rclcpp::Node::SharedPtr getNode() const

References smacc2::ISmaccClientBehavior::getLogger(), smacc2::ISmaccClientBehavior::getName(), smacc2::ISmaccClientBehavior::getNode(), and isShutdownRequested_.

Referenced by smacc2::client_behaviors::CbSequence::onSubNodeAbort(), and waitFutureIfNotFinished().

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

◆ waitFutureIfNotFinished()

void smacc2::SmaccAsyncClientBehavior::waitFutureIfNotFinished ( std::optional< std::future< int > > &  threadfut,
bool  requestFinish 
)
private

Definition at line 41 of file smacc_client_async_behavior.cpp.

43{
44 try
45 {
46 rclcpp::Rate r(100);
47 while (rclcpp::ok())
48 {
49 //bool valid = threadfut.valid();
50 if (threadfut && threadfut->valid())
51 {
52 auto status = threadfut->wait_for(std::chrono::milliseconds(20));
53 if (status == std::future_status::ready)
54 // auto status = threadfut->wait();
55 // if(status)
56 {
57 // done
58 threadfut->get();
59 break;
60 }
61 else
62 {
63 // in progress
64 RCLCPP_INFO_STREAM(
65 getLogger(),
66 "[" << getName()
67 << "] fut valid but waiting for asynchronous onEntry thread to finish: state: "
68 << (int)status);
69 }
70 }
71 else
72 {
73 RCLCPP_INFO_STREAM(
74 getLogger(),
75 "[" << getName()
76 << "] waiting future onEntryThread. It was not even created. Skipping wait.");
77 break;
78 }
79
80 // r.sleep();
81 rclcpp::sleep_for(100ms);
82 // rclcpp::spin_some(getNode());
83 RCLCPP_WARN_THROTTLE(
84 getLogger(), *(getNode()->get_clock()), 1000,
85 "[%s] waiting for finishing client behavior, before leaving the state. Is the client "
86 "behavior stuck? requesting force finish",
87 this->getName().c_str());
88
89 if (requestFinish) requestForceFinish();
90 }
91 }
92 catch (const std::exception & e)
93 {
94 RCLCPP_DEBUG(
95 getLogger(),
96 "[SmaccAsyncClientBehavior] trying to join function, but it was already finished.");
97 }
98}

References smacc2::ISmaccClientBehavior::getLogger(), smacc2::ISmaccClientBehavior::getName(), smacc2::ISmaccClientBehavior::getNode(), and requestForceFinish().

Referenced by waitOnEntryThread().

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

◆ waitOnEntryThread()

void smacc2::SmaccAsyncClientBehavior::waitOnEntryThread ( bool  requestFinish)

Definition at line 100 of file smacc_client_async_behavior.cpp.

101{
102 waitFutureIfNotFinished(this->onEntryThread_, requestFinish);
103}
void waitFutureIfNotFinished(std::optional< std::future< int > > &threadfut, bool requestFinish)

References onEntryThread_, and waitFutureIfNotFinished().

Referenced by executeOnExit().

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

Member Data Documentation

◆ isShutdownRequested_

bool smacc2::SmaccAsyncClientBehavior::isShutdownRequested_ = false
private

◆ onEntryThread_

std::optional<std::future<int> > smacc2::SmaccAsyncClientBehavior::onEntryThread_
private

Definition at line 98 of file smacc_asynchronous_client_behavior.hpp.

Referenced by executeOnEntry(), and waitOnEntryThread().

◆ onExitThread_

std::optional<std::future<int> > smacc2::SmaccAsyncClientBehavior::onExitThread_
private

Definition at line 99 of file smacc_asynchronous_client_behavior.hpp.

Referenced by dispose(), and executeOnExit().

◆ onFailure_

SmaccSignal<void()> smacc2::SmaccAsyncClientBehavior::onFailure_
private

Definition at line 107 of file smacc_asynchronous_client_behavior.hpp.

Referenced by onFailure(), and onOrthogonalAllocation().

◆ onFinished_

SmaccSignal<void()> smacc2::SmaccAsyncClientBehavior::onFinished_
private

Definition at line 105 of file smacc_asynchronous_client_behavior.hpp.

Referenced by onFinished(), and onOrthogonalAllocation().

◆ onSuccess_

SmaccSignal<void()> smacc2::SmaccAsyncClientBehavior::onSuccess_
private

Definition at line 106 of file smacc_asynchronous_client_behavior.hpp.

Referenced by onOrthogonalAllocation(), and onSuccess().

◆ postFailureEventFn_

std::function<void()> smacc2::SmaccAsyncClientBehavior::postFailureEventFn_
private

◆ postFinishEventFn_

std::function<void()> smacc2::SmaccAsyncClientBehavior::postFinishEventFn_
private

◆ postSuccessEventFn_

std::function<void()> smacc2::SmaccAsyncClientBehavior::postSuccessEventFn_
private

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