SMACC2
Loading...
Searching...
No Matches
cl_gcalcli::CbWaitConnection Class Reference

Async behavior that waits for gcalcli connection. More...

#include <cb_wait_connection.hpp>

Inheritance diagram for cl_gcalcli::CbWaitConnection:
Inheritance graph
Collaboration diagram for cl_gcalcli::CbWaitConnection:
Collaboration graph

Public Member Functions

 CbWaitConnection (std::chrono::seconds timeout=std::chrono::seconds{30})
 Construct with optional timeout.
 
virtual ~CbWaitConnection ()=default
 
void onEntry () override
 
- Public Member Functions inherited from smacc2::SmaccAsyncClientBehavior
template<typename TOrthogonal , typename TSourceObject >
void onStateOrthogonalAllocation ()
 
virtual ~SmaccAsyncClientBehavior ()
 
template<typename TCallback , typename T >
smacc2::SmaccSignalConnection onSuccess (TCallback callback, T *object)
 
template<typename TCallback , typename T >
smacc2::SmaccSignalConnection onFinished (TCallback callback, T *object)
 
template<typename TCallback , typename T >
smacc2::SmaccSignalConnection onFailure (TCallback callback, T *object)
 
void requestForceFinish ()
 
void executeOnEntry () override
 
void executeOnExit () override
 
void waitOnEntryThread (bool requestFinish)
 
template<typename TCallbackMethod , typename T >
smacc2::SmaccSignalConnection onSuccess (TCallbackMethod callback, T *object)
 
template<typename TCallbackMethod , typename T >
smacc2::SmaccSignalConnection onFinished (TCallbackMethod callback, T *object)
 
template<typename TCallbackMethod , typename T >
smacc2::SmaccSignalConnection 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, ComponentRequirement requirementType=ComponentRequirement::SOFT)
 
virtual void onExit ()
 

Private Attributes

std::chrono::seconds timeout_
 
ClGcalcliclient_
 

Additional Inherited Members

- Protected Member Functions inherited from smacc2::SmaccAsyncClientBehavior
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
 

Detailed Description

Async behavior that waits for gcalcli connection.

This behavior attempts to establish a connection to Google Calendar via gcalcli and posts success when connected or failure on timeout.

Definition at line 32 of file cb_wait_connection.hpp.

Constructor & Destructor Documentation

◆ CbWaitConnection()

cl_gcalcli::CbWaitConnection::CbWaitConnection ( std::chrono::seconds timeout = std::chrono::seconds{30})
explicit

Construct with optional timeout.

Parameters
timeoutMaximum time to wait for connection

Definition at line 22 of file cb_wait_connection.cpp.

23: timeout_(timeout), client_(nullptr)
24{
25}

◆ ~CbWaitConnection()

virtual cl_gcalcli::CbWaitConnection::~CbWaitConnection ( )
virtualdefault

Member Function Documentation

◆ onEntry()

void cl_gcalcli::CbWaitConnection::onEntry ( )
overridevirtual

Reimplemented from smacc2::ISmaccClientBehavior.

Definition at line 27 of file cb_wait_connection.cpp.

28{
29 RCLCPP_INFO(
30 getLogger(), "[CbWaitConnection] Waiting for gcalcli connection (timeout: %lds)",
31 timeout_.count());
32
34
35 if (!client_)
36 {
37 RCLCPP_ERROR(getLogger(), "[CbWaitConnection] ClGcalcli client not available");
38 this->postFailureEvent();
39 return;
40 }
41
42 auto * connection = client_->getConnection();
43 if (!connection)
44 {
45 RCLCPP_ERROR(getLogger(), "[CbWaitConnection] CpGcalcliConnection not available");
46 this->postFailureEvent();
47 return;
48 }
49
50 auto start = std::chrono::steady_clock::now();
51 auto deadline = start + timeout_;
52
53 // Poll for connection with increasing backoff
54 std::chrono::milliseconds poll_interval{500};
55 const std::chrono::milliseconds max_interval{5000};
56
57 while (std::chrono::steady_clock::now() < deadline)
58 {
59 // Check if we should force finish
60 if (this->isShutdownRequested())
61 {
62 RCLCPP_INFO(getLogger(), "[CbWaitConnection] Shutdown requested");
63 return;
64 }
65
66 if (connection->checkConnection())
67 {
68 RCLCPP_INFO(getLogger(), "[CbWaitConnection] Connection established");
69 this->postSuccessEvent();
70 return;
71 }
72
73 // Wait before next attempt
74 std::this_thread::sleep_for(poll_interval);
75
76 // Increase interval with backoff
77 poll_interval = std::min(poll_interval * 2, max_interval);
78 }
79
80 RCLCPP_ERROR(getLogger(), "[CbWaitConnection] Connection timeout after %lds", timeout_.count());
81 this->postFailureEvent();
82}
CpGcalcliConnection * getConnection()
virtual rclcpp::Logger getLogger() const
void requiresClient(SmaccClientType *&storage)
bool isShutdownRequested()
onEntry is executed in a new thread. However the current state cannot be left until the onEntry threa...

References client_, cl_gcalcli::ClGcalcli::getConnection(), smacc2::ISmaccClientBehavior::getLogger(), smacc2::SmaccAsyncClientBehavior::isShutdownRequested(), smacc2::SmaccAsyncClientBehavior::postFailureEvent(), smacc2::SmaccAsyncClientBehavior::postSuccessEvent(), smacc2::ISmaccClientBehavior::requiresClient(), and timeout_.

Here is the call graph for this function:

Member Data Documentation

◆ client_

ClGcalcli* cl_gcalcli::CbWaitConnection::client_
private

Definition at line 47 of file cb_wait_connection.hpp.

Referenced by onEntry().

◆ timeout_

std::chrono::seconds cl_gcalcli::CbWaitConnection::timeout_
private

Definition at line 46 of file cb_wait_connection.hpp.

Referenced by onEntry().


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