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 ()
 
virtual void executeOnEntry ()
 
virtual void executeOnExit ()
 

Protected Member Functions

void postSuccessEvent ()
 
void postFailureEvent ()
 
virtual void dispose () override
 
bool isShutdownRequested ()
 
- 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 void dispose ()
 
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 149 of file smacc_client_async_behavior.cpp.

149{}

Member Function Documentation

◆ dispose()

void smacc2::SmaccAsyncClientBehavior::dispose ( )
overrideprotectedvirtual

Reimplemented from smacc2::ISmaccClientBehavior.

Definition at line 120 of file smacc_client_async_behavior.cpp.

121{
122 RCLCPP_DEBUG_STREAM(
123 getLogger(), "[" << getName()
124 << "] Destroying client behavior- Waiting finishing of asynchronous onExit "
125 "thread");
126 try
127 {
128 if (this->onExitThread_)
129 {
130 // this->onExitThread_->get();
131 this->onExitThread_->get();
132 }
133 //this->onExitThread_->get();
134 }
135 catch (...)
136 {
137 RCLCPP_DEBUG(
138 getLogger(),
139 "[SmaccAsyncClientBehavior] trying to Join onExit function, but it was already "
140 "finished.");
141 }
142
143 RCLCPP_DEBUG_STREAM(
144 getLogger(), "[" << getName()
145 << "] Destroying client behavior- onExit thread finished. Proccedding "
146 "destruction.");
147}
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 103 of file smacc_client_async_behavior.cpp.

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

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 ( )
inlineprotected

◆ onFailure() [1/2]

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

◆ onFailure() [2/2]

template<typename TCallbackMethod , typename T >
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]

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

◆ onOrthogonalAllocation()

template<typename TOrthogonal , typename TSourceObject >
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]

template<typename TCallbackMethod , typename T >
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 151 of file smacc_client_async_behavior.cpp.

References postSuccessEventFn_.

Referenced by cl_move_group_interface::CbMoveEndEffectorTrajectory::executeJointSpaceTrajectory(), cl_move_group_interface::CbMoveJoints::moveJoints(), cl_move_group_interface::CbMoveEndEffectorRelative::moveRelative(), cl_move_group_interface::CbMoveCartesianRelative::moveRelativeCartesian(), cl_move_group_interface::CbMoveEndEffector::moveToAbsolutePose(), smacc2::client_behaviors::CbServiceCall< ServiceType >::onEntry(), smacc2::client_behaviors::CbSequence::onEntry(), smacc2::client_behaviors::CbWaitActionServer::onEntry(), smacc2::client_behaviors::CbWaitNode::onEntry(), cl_move_group_interface::CbAttachObject::onEntry(), cl_move_group_interface::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 155 of file smacc_client_async_behavior.cpp.

156{
157 RCLCPP_INFO_STREAM_THROTTLE(
158 getLogger(), *(getNode()->get_clock()), 1000, "[" << getName() << "] requestForceFinish");
160}
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 {
55 // done
56 threadfut->get();
57 break;
58 }
59 else
60 {
61 // in progress
62 RCLCPP_INFO_STREAM(
63 getLogger(),
64 "[" << getName()
65 << "] fut valid but waiting for asynchronous onEntry thread to finish: state: "
66 << (int)status);
67 }
68 }
69 else
70 {
71 RCLCPP_INFO_STREAM(
72 getLogger(),
73 "[" << getName()
74 << "] waiting future onEntryThread. It was not even created. Skipping wait.");
75 break;
76 }
77
78 // r.sleep();
79 rclcpp::sleep_for(100ms);
80 // rclcpp::spin_some(getNode());
81 RCLCPP_WARN_THROTTLE(
82 getLogger(), *(getNode()->get_clock()), 1000,
83 "[%s] waiting for finishing client behavior, before leaving the state. Is the client "
84 "behavior stuck? requesting force finish",
85 this->getName().c_str());
86
87 if (requestFinish) requestForceFinish();
88 }
89 }
90 catch (const std::exception & e)
91 {
92 RCLCPP_DEBUG(
93 getLogger(),
94 "[SmaccAsyncClientBehavior] trying to join function, but it was already finished.");
95 }
96}

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 98 of file smacc_client_async_behavior.cpp.

99{
100 waitFutureIfNotFinished(this->onEntryThread_, requestFinish);
101}
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 92 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 93 of file smacc_asynchronous_client_behavior.hpp.

Referenced by dispose(), and executeOnExit().

◆ onFailure_

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

Definition at line 101 of file smacc_asynchronous_client_behavior.hpp.

Referenced by onFailure(), and onOrthogonalAllocation().

◆ onFinished_

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

Definition at line 99 of file smacc_asynchronous_client_behavior.hpp.

Referenced by onFinished(), and onOrthogonalAllocation().

◆ onSuccess_

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

Definition at line 100 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: