Skip to content

Commit

Permalink
Run through formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
yassiezar committed Jun 9, 2024
1 parent 761f809 commit dfba4ea
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
#include <boost/beast/http.hpp>
#include <boost/beast/ssl.hpp>
#include <boost/beast/version.hpp>
#include <http_client/ssl_http_session.hpp>
#include <http_client/http_session.hpp>
#include <http_client/ssl_http_session.hpp>
#include <iostream>
#include <memory>
#include <optional>
Expand All @@ -37,16 +37,22 @@
#include <thread>
#include <unordered_map>

namespace cl_http {
class ClHttp : public smacc2::ISmaccClient {
class Server {
public:
explicit Server(const std::string &server_name)
: server_name_{server_name}, ssl_{true} {
if (!server_name_.substr(0, 7).compare("http://")) {
namespace cl_http
{
class ClHttp : public smacc2::ISmaccClient
{
class Server
{
public:
explicit Server(const std::string & server_name) : server_name_{server_name}, ssl_{true}
{
if (!server_name_.substr(0, 7).compare("http://"))
{
ssl_ = false;
server_name_.erase(0, 7);
} else if (!server_name_.substr(0, 8).compare("https://")) {
}
else if (!server_name_.substr(0, 8).compare("https://"))
{
server_name_.erase(0, 8);
ssl_ = true;
}
Expand All @@ -58,13 +64,14 @@ class ClHttp : public smacc2::ISmaccClient {

std::string getServerName() const { return server_name_; }

private:
private:
std::string server_name_;
bool ssl_;
};

public:
enum class kHttpRequestMethod {
public:
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),
Expand All @@ -73,22 +80,20 @@ class ClHttp : public smacc2::ISmaccClient {
using TResponse = http_session_base::TResponse;

template <typename T>
boost::signals2::connection onResponseReceived(
void (T::*callback)(const TResponse &), T *object) {
return this->getStateMachine()->createSignalConnection(onResponseReceived_,
callback, object);
boost::signals2::connection onResponseReceived(void (T::*callback)(const TResponse &), T * object)
{
return this->getStateMachine()->createSignalConnection(onResponseReceived_, callback, object);
}

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

virtual ~ClHttp();

void onInitialize() override;

void makeRequest(const kHttpRequestMethod http_method,
const std::string &path = "/");
void makeRequest(const kHttpRequestMethod http_method, const std::string & path = "/");

private:
private:
const int HTTP_VERSION = 11;

bool initialized_;
Expand All @@ -98,16 +103,14 @@ class ClHttp : public smacc2::ISmaccClient {
Server server_;

boost::asio::io_context io_context_;
boost::asio::executor_work_guard<decltype(io_context_)::executor_type>
worker_guard_;
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 = [&](const TResponse &res) {
onResponseReceived_(res);
};
std::function<void(TResponse)> callbackHandler = [&](const TResponse & res)
{ onResponseReceived_(res); };
};
} // namespace cl_http
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,36 @@
#include <iostream>
#include <string>

namespace cl_http {
class http_session : public std::enable_shared_from_this<http_session>,
public http_session_base {
public:
namespace cl_http
{
class http_session : public std::enable_shared_from_this<http_session>, public http_session_base
{
public:
// Objects are constructed with a strand to
// ensure that handlers do not execute concurrently.
http_session(boost::asio::any_io_executor ioc,
const std::function<void(const TResponse &)> response);
http_session(
boost::asio::any_io_executor ioc, const std::function<void(const TResponse &)> response);

virtual ~http_session() {}

// Start the asynchronous operation
void run(const std::string &host, const std::string &target,
const boost::beast::http::verb http_method,
const int &version) override;
void run(
const std::string & host, const std::string & target,
const boost::beast::http::verb http_method, const int & version) override;

std::string getPort() override { return kPort; }

private:
private:
const std::string kPort = "80";

void on_resolve(
boost::beast::error_code ec,
boost::asio::ip::tcp::resolver::results_type results) override;
void fail(boost::beast::error_code ec, const char *what) override;
boost::beast::error_code ec, boost::asio::ip::tcp::resolver::results_type results) override;
void fail(boost::beast::error_code ec, const char * what) override;
void on_connect(
boost::beast::error_code ec,
boost::asio::ip::tcp::resolver::results_type::endpoint_type) override;
void on_write(boost::beast::error_code ec,
std::size_t bytes_transferred) override;
void on_read(boost::beast::error_code ec,
std::size_t bytes_transferred) override;
boost::beast::error_code ec,
boost::asio::ip::tcp::resolver::results_type::endpoint_type) override;
void on_write(boost::beast::error_code ec, std::size_t bytes_transferred) override;
void on_read(boost::beast::error_code ec, std::size_t bytes_transferred) override;

std::function<void(const TResponse &)> onResponse;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,30 @@
#include <boost/beast/version.hpp>
#include <string>

namespace cl_http {
class http_session_base {
public:
namespace cl_http
{
class http_session_base
{
public:
virtual ~http_session_base() {}

using TResponse =
boost::beast::http::response<boost::beast::http::string_body>;
using TResponse = boost::beast::http::response<boost::beast::http::string_body>;

// Start the asynchronous operation
virtual void run(const std::string &host, const std::string &target,
const boost::beast::http::verb http_method,
const int &version) = 0;
virtual void run(
const std::string & host, const std::string & target,
const boost::beast::http::verb http_method, const int & version) = 0;

virtual std::string getPort() = 0;

protected:
protected:
virtual void on_resolve(
boost::beast::error_code ec,
boost::asio::ip::tcp::resolver::results_type results) = 0;
virtual void fail(boost::beast::error_code ec, const char *what) = 0;
boost::beast::error_code ec, boost::asio::ip::tcp::resolver::results_type results) = 0;
virtual void fail(boost::beast::error_code ec, const char * what) = 0;
virtual void on_connect(
boost::beast::error_code ec,
boost::asio::ip::tcp::resolver::results_type::endpoint_type) = 0;
virtual void on_write(boost::beast::error_code ec,
std::size_t bytes_transferred) = 0;
virtual void on_read(boost::beast::error_code ec,
std::size_t bytes_transferred) = 0;
boost::beast::error_code ec, boost::asio::ip::tcp::resolver::results_type::endpoint_type) = 0;
virtual void on_write(boost::beast::error_code ec, std::size_t bytes_transferred) = 0;
virtual void on_read(boost::beast::error_code ec, std::size_t bytes_transferred) = 0;

// Optional, needed for SSL connections
virtual void on_handshake(boost::beast::error_code ec) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,39 @@
#include <iostream>
#include <string>

namespace cl_http {
namespace cl_http
{
class ssl_http_session : public std::enable_shared_from_this<ssl_http_session>,
public http_session_base {
public:
public http_session_base
{
public:
// Objects are constructed with a strand to
// ensure that handlers do not execute concurrently.
ssl_http_session(boost::asio::any_io_executor ioc,
boost::asio::ssl::context &ssl_context,
const std::function<void(const TResponse &)> response);
ssl_http_session(
boost::asio::any_io_executor ioc, boost::asio::ssl::context & ssl_context,
const std::function<void(const TResponse &)> response);

virtual ~ssl_http_session() {}

// Start the asynchronous operation
void run(const std::string &host, const std::string &target,
const boost::beast::http::verb http_method,
const int &version) override;
void run(
const std::string & host, const std::string & target,
const boost::beast::http::verb http_method, const int & version) override;

std::string getPort() override { return kPort; }

private:
private:
const std::string kPort = "443";

void on_resolve(
boost::beast::error_code ec,
boost::asio::ip::tcp::resolver::results_type results) override;
void fail(boost::beast::error_code ec, const char *what) override;
boost::beast::error_code ec, boost::asio::ip::tcp::resolver::results_type results) override;
void fail(boost::beast::error_code ec, const char * what) override;
void on_connect(
boost::beast::error_code ec,
boost::asio::ip::tcp::resolver::results_type::endpoint_type) override;
boost::beast::error_code ec,
boost::asio::ip::tcp::resolver::results_type::endpoint_type) override;
void on_handshake(boost::beast::error_code ec) override;
void on_write(boost::beast::error_code ec,
std::size_t bytes_transferred) override;
void on_read(boost::beast::error_code ec,
std::size_t bytes_transferred) override;
void on_write(boost::beast::error_code ec, std::size_t bytes_transferred) override;
void on_read(boost::beast::error_code ec, std::size_t bytes_transferred) override;
void on_shutdown(boost::beast::error_code ec) override;

std::function<void(const TResponse &)> onResponse;
Expand Down
56 changes: 33 additions & 23 deletions smacc2_client_library/http_client/src/http_client/http_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,39 @@

#include <http_client/http_client.hpp>

namespace cl_http {
ClHttp::ClHttp(const std::string &server_name, const int &timeout)
: initialized_{false},
timeout_{timeout},
server_{server_name},
worker_guard_{boost::asio::make_work_guard(io_context_.get_executor())},
ssl_context_{boost::asio::ssl::context::tlsv12_client} {
namespace cl_http
{
ClHttp::ClHttp(const std::string & server_name, const int & timeout)
: initialized_{false},
timeout_{timeout},
server_{server_name},
worker_guard_{boost::asio::make_work_guard(io_context_.get_executor())},
ssl_context_{boost::asio::ssl::context::tlsv12_client}
{
ssl_context_.set_default_verify_paths();
ssl_context_.set_verify_mode(boost::asio::ssl::verify_peer);
}

ClHttp::~ClHttp() {
ClHttp::~ClHttp()
{
worker_guard_.reset();
tcp_connection_runner_.join();
}

void ClHttp::onInitialize() {
if (!initialized_) {
void ClHttp::onInitialize()
{
if (!initialized_)
{
tcp_connection_runner_ = std::thread{[&]() { io_context_.run(); }};
this->initialized_ = true;
}
}

void ClHttp::makeRequest(const kHttpRequestMethod http_method,
const std::string &path) {
void ClHttp::makeRequest(const kHttpRequestMethod http_method, const std::string & path)
{
auto path_used = path;
if (path[0] != '/') {
if (path[0] != '/')
{
std::reverse(path_used.begin(), path_used.end());
path_used += '/';
std::reverse(path_used.begin(), path_used.end());
Expand All @@ -57,16 +63,20 @@ void ClHttp::makeRequest(const kHttpRequestMethod http_method,
RCLCPP_INFO(this->getLogger(), "Path %s", path_used.c_str());
RCLCPP_INFO(this->getLogger(), "Port %s", server_.getPort().c_str());

if (server_.isSSL()) {
std::make_shared<ssl_http_session>(boost::asio::make_strand(io_context_),
ssl_context_, callbackHandler)
->run(server_.getServerName(), path_used,
static_cast<boost::beast::http::verb>(http_method), HTTP_VERSION);
} else {
std::make_shared<http_session>(boost::asio::make_strand(io_context_),
callbackHandler)
->run(server_.getServerName(), path_used,
static_cast<boost::beast::http::verb>(http_method), HTTP_VERSION);
if (server_.isSSL())
{
std::make_shared<ssl_http_session>(
boost::asio::make_strand(io_context_), ssl_context_, callbackHandler)
->run(
server_.getServerName(), path_used, static_cast<boost::beast::http::verb>(http_method),
HTTP_VERSION);
}
else
{
std::make_shared<http_session>(boost::asio::make_strand(io_context_), callbackHandler)
->run(
server_.getServerName(), path_used, static_cast<boost::beast::http::verb>(http_method),
HTTP_VERSION);
}
}
} // namespace cl_http
Loading

0 comments on commit dfba4ea

Please sign in to comment.