SMACC2
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Private Attributes | List of all members
cl_http::ClHttp Class Reference

#include <cl_http_client.hpp>

Inheritance diagram for cl_http::ClHttp:
Inheritance graph
Collaboration diagram for cl_http::ClHttp:
Collaboration graph

Classes

class  Server
 

Public Types

enum class  kHttpRequestMethod { GET = static_cast<int>(boost::beast::http::verb::get) , POST = static_cast<int>(boost::beast::http::verb::post) , PUT = static_cast<int>(boost::beast::http::verb::put) }
 
using TResponse = http_session_base::TResponse
 

Public Member Functions

template<typename T >
boost::signals2::connection onResponseReceived (void(T::*callback)(const TResponse &), T *object)
 
 ClHttp (const std::string &server, const int &timeout=1500)
 
virtual ~ClHttp ()
 
void onInitialize () override
 
void makeRequest (const kHttpRequestMethod http_method, const std::string &path="/", const std::string &body="", const std::unordered_map< std::string, std::string > &headers={})
 
- Public Member Functions inherited from smacc2::ISmaccClient
 ISmaccClient ()
 
virtual ~ISmaccClient ()
 
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 ()
 

Private Attributes

const int HTTP_VERSION = 11
 
bool initialized_
 
bool is_ssl_
 
int timeout_
 
Server server_
 
boost::asio::io_context io_context_
 
boost::asio::executor_work_guard< decltype(io_context_)::executor_type > worker_guard_
 
std::thread tcp_connection_runner_
 
boost::asio::ssl::context ssl_context_
 
smacc2::SmaccSignal< void(const TResponse &)> onResponseReceived_
 
std::function< void(TResponse)> callbackHandler
 

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 ()
 
- Protected Attributes inherited from smacc2::ISmaccClient
std::map< ComponentKey, std::shared_ptr< smacc2::ISmaccComponent > > components_
 

Detailed Description

Definition at line 38 of file cl_http_client.hpp.

Member Typedef Documentation

◆ TResponse

Definition at line 76 of file cl_http_client.hpp.

Member Enumeration Documentation

◆ kHttpRequestMethod

Enumerator
GET 
POST 
PUT 

Definition at line 69 of file cl_http_client.hpp.

70 {
71 GET = static_cast<int>(boost::beast::http::verb::get),
72 POST = static_cast<int>(boost::beast::http::verb::post),
73 PUT = static_cast<int>(boost::beast::http::verb::put),
74 };

Constructor & Destructor Documentation

◆ ClHttp()

cl_http::ClHttp::ClHttp ( const std::string &  server,
const int &  timeout = 1500 
)
explicit

Definition at line 25 of file cl_http_client.cpp.

26: initialized_{false},
27 timeout_{timeout},
28 server_{server_name},
29 worker_guard_{boost::asio::make_work_guard(io_context_.get_executor())},
30 ssl_context_{boost::asio::ssl::context::tlsv13_client}
31{
32 ssl_context_.set_default_verify_paths();
33 ssl_context_.set_verify_mode(boost::asio::ssl::verify_peer);
34}
boost::asio::io_context io_context_
boost::asio::executor_work_guard< decltype(io_context_)::executor_type > worker_guard_
boost::asio::ssl::context ssl_context_

References ssl_context_.

◆ ~ClHttp()

cl_http::ClHttp::~ClHttp ( )
virtual

Definition at line 36 of file cl_http_client.cpp.

37{
38 worker_guard_.reset();
40}
std::thread tcp_connection_runner_

References tcp_connection_runner_, and worker_guard_.

Member Function Documentation

◆ makeRequest()

void cl_http::ClHttp::makeRequest ( const kHttpRequestMethod  http_method,
const std::string &  path = "/",
const std::string &  body = "",
const std::unordered_map< std::string, std::string > &  headers = {} 
)

Definition at line 51 of file cl_http_client.cpp.

54{
55 auto path_used = path;
56 if (path[0] != '/')
57 {
58 std::reverse(path_used.begin(), path_used.end());
59 path_used += '/';
60 std::reverse(path_used.begin(), path_used.end());
61 }
62
63 RCLCPP_INFO(this->getLogger(), "SSL? %d", server_.isSSL());
64 RCLCPP_INFO(this->getLogger(), "Server %s", server_.getServerName().c_str());
65 RCLCPP_INFO(this->getLogger(), "Path %s", path_used.c_str());
66 RCLCPP_INFO(this->getLogger(), "Port %s", server_.getPort().c_str());
67
68 std::shared_ptr<http_session_base> http_session_ptr;
69
70 if (server_.isSSL())
71 {
72 http_session_ptr = std::make_shared<ssl_http_session>(
73 boost::asio::make_strand(io_context_), ssl_context_, callbackHandler);
74 }
75 else
76 {
77 http_session_ptr =
78 std::make_shared<http_session>(boost::asio::make_strand(io_context_), callbackHandler);
79 }
80
81 http_session_ptr->setBody(body);
82 http_session_ptr->setHeaders(headers);
83 http_session_ptr->run(
84 server_.getServerName(), path_used, static_cast<boost::beast::http::verb>(http_method),
86}
std::string getServerName() const
std::string getPort() const
std::function< void(TResponse)> callbackHandler
rclcpp::Logger getLogger()

References callbackHandler, smacc2::ISmaccClient::getLogger(), cl_http::ClHttp::Server::getPort(), cl_http::ClHttp::Server::getServerName(), HTTP_VERSION, io_context_, cl_http::ClHttp::Server::isSSL(), server_, and ssl_context_.

Referenced by cl_http::CbHttpRequestBase::onEntry().

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

◆ onInitialize()

void cl_http::ClHttp::onInitialize ( )
overridevirtual

Reimplemented from smacc2::ISmaccClient.

Definition at line 42 of file cl_http_client.cpp.

43{
44 if (!initialized_)
45 {
46 tcp_connection_runner_ = std::thread{[&]() { io_context_.run(); }};
47 this->initialized_ = true;
48 }
49}

References initialized_, io_context_, and tcp_connection_runner_.

◆ onResponseReceived()

template<typename T >
boost::signals2::connection cl_http::ClHttp::onResponseReceived ( void(T::*)(const TResponse &)  callback,
T *  object 
)
inline

Definition at line 79 of file cl_http_client.hpp.

80 {
81 return this->getStateMachine()->createSignalConnection(onResponseReceived_, callback, object);
82 }
smacc2::SmaccSignal< void(const TResponse &)> onResponseReceived_
ISmaccStateMachine * getStateMachine()
boost::signals2::connection createSignalConnection(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object)

References smacc2::ISmaccStateMachine::createSignalConnection(), smacc2::ISmaccClient::getStateMachine(), and onResponseReceived_.

Referenced by cl_http::CbHttpRequestBase::runtimeConfigure().

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

Member Data Documentation

◆ callbackHandler

std::function<void(TResponse)> cl_http::ClHttp::callbackHandler
private
Initial value:
= [&](const TResponse & res)
http_session_base::TResponse TResponse

Definition at line 112 of file cl_http_client.hpp.

113 { onResponseReceived_(res); };

Referenced by makeRequest().

◆ HTTP_VERSION

const int cl_http::ClHttp::HTTP_VERSION = 11
private

Definition at line 96 of file cl_http_client.hpp.

Referenced by makeRequest().

◆ initialized_

bool cl_http::ClHttp::initialized_
private

Definition at line 98 of file cl_http_client.hpp.

Referenced by onInitialize().

◆ io_context_

boost::asio::io_context cl_http::ClHttp::io_context_
private

Definition at line 104 of file cl_http_client.hpp.

Referenced by makeRequest(), and onInitialize().

◆ is_ssl_

bool cl_http::ClHttp::is_ssl_
private

Definition at line 99 of file cl_http_client.hpp.

◆ onResponseReceived_

smacc2::SmaccSignal<void(const TResponse &)> cl_http::ClHttp::onResponseReceived_
private

Definition at line 110 of file cl_http_client.hpp.

Referenced by onResponseReceived().

◆ server_

Server cl_http::ClHttp::server_
private

Definition at line 102 of file cl_http_client.hpp.

Referenced by makeRequest().

◆ ssl_context_

boost::asio::ssl::context cl_http::ClHttp::ssl_context_
private

Definition at line 108 of file cl_http_client.hpp.

Referenced by ClHttp(), and makeRequest().

◆ tcp_connection_runner_

std::thread cl_http::ClHttp::tcp_connection_runner_
private

Definition at line 106 of file cl_http_client.hpp.

Referenced by onInitialize(), and ~ClHttp().

◆ timeout_

int cl_http::ClHttp::timeout_
private

Definition at line 100 of file cl_http_client.hpp.

◆ worker_guard_

boost::asio::executor_work_guard<decltype(io_context_)::executor_type> cl_http::ClHttp::worker_guard_
private

Definition at line 105 of file cl_http_client.hpp.

Referenced by ~ClHttp().


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