diff --git a/smacc2_client_library/http_client/include/http_client/http_client.hpp b/smacc2_client_library/http_client/include/http_client/http_client.hpp index 7c74fd089..744d80c7d 100644 --- a/smacc2_client_library/http_client/include/http_client/http_client.hpp +++ b/smacc2_client_library/http_client/include/http_client/http_client.hpp @@ -25,13 +25,11 @@ #include #include #include - -#include -#include - #include #include #include +#include +#include #include #include #include @@ -41,12 +39,12 @@ class ClHttp : public smacc2::ISmaccClient { class http_session : public std::enable_shared_from_this { public: using TResponse = - const boost::beast::http::response &; + boost::beast::http::response; // Objects are constructed with a strand to // ensure that handlers do not execute concurrently. http_session(boost::asio::io_context &ioc, - const std::function response); + const std::function response); // Start the asynchronous operation void run(const std::string &host, const std::string &target, @@ -64,9 +62,7 @@ class ClHttp : public smacc2::ISmaccClient { void on_write(boost::beast::error_code ec, std::size_t bytes_transferred); void on_read(boost::beast::error_code ec, std::size_t bytes_transferred); - std::function &response)> - onResponse; + std::function onResponse; boost::asio::ip::tcp::resolver resolver_; boost::beast::tcp_stream stream_; @@ -79,11 +75,14 @@ class ClHttp : public smacc2::ISmaccClient { enum class kHttpRequestMethod { GET = static_cast(boost::beast::http::verb::get), POST = static_cast(boost::beast::http::verb::post), + PUT = static_cast(boost::beast::http::verb::put), }; + using TResponse = http_session::TResponse; + template boost::signals2::connection onResponseReceived( - void (T::*callback)(const std::string &), T *object) { + void (T::*callback)(const TResponse&), T *object) { return this->getStateMachine()->createSignalConnection(onResponseReceived_, callback, object); } @@ -92,7 +91,8 @@ class ClHttp : public smacc2::ISmaccClient { virtual ~ClHttp(); - void configure(); + void onInitialize() override; + void makeRequest(const kHttpRequestMethod http_method, const std::string &path = "/"); @@ -109,8 +109,10 @@ class ClHttp : public smacc2::ISmaccClient { worker_guard_; std::thread tcp_connection_runner_; - std::function callbackHandler; + smacc2::SmaccSignal onResponseReceived_; - smacc2::SmaccSignal onResponseReceived_; + std::function callbackHandler = [&](const TResponse& res) { + onResponseReceived_(res); + }; }; } // namespace cl_http diff --git a/smacc2_client_library/http_client/src/http_client/http_client.cpp b/smacc2_client_library/http_client/src/http_client/http_client.cpp index 9ddc59336..c46dc1f91 100644 --- a/smacc2_client_library/http_client/src/http_client/http_client.cpp +++ b/smacc2_client_library/http_client/src/http_client/http_client.cpp @@ -18,22 +18,21 @@ * ******************************************************************************************************************/ - #include namespace cl_http { /////////////////////////// // ClHttp implementation // -// //////////////////////// +/////////////////////////// ClHttp::ClHttp(const std::string &server, const int &timeout) : initialized_{false}, timeout_{timeout}, server_name_{server}, - worker_guard_{boost::asio::make_work_guard(io_context_)} { + worker_guard_{boost::asio::make_work_guard(io_context_.get_executor())} { // User explicitly using http - is_ssl_ = server.substr(0, 5).compare("https") ? true : false; + is_ssl_ = server.substr(0, 8).compare("https://") ? false : true; } ClHttp::~ClHttp() { @@ -41,7 +40,7 @@ ClHttp::~ClHttp() { tcp_connection_runner_.join(); } -void ClHttp::configure() { +void ClHttp::onInitialize() { if (!initialized_) { tcp_connection_runner_ = std::thread{[&]() { io_context_.run(); }}; this->initialized_ = true; @@ -50,6 +49,7 @@ void ClHttp::configure() { void ClHttp::makeRequest(const kHttpRequestMethod http_method, const std::string &path) { + RCLCPP_INFO(this->getLogger(), "SSL? %d", is_ssl_); std::make_shared(io_context_, callbackHandler) ->run(server_name_, path, is_ssl_ ? "443" : "80", static_cast(http_method), HTTP_VERSION); @@ -60,10 +60,11 @@ void ClHttp::makeRequest(const kHttpRequestMethod http_method, ///////////////////////////////// ClHttp::http_session::http_session( - boost::asio::io_context &ioc, const std::function response) + boost::asio::io_context &ioc, + const std::function response) : onResponse{response}, - resolver_(boost::asio::make_strand(ioc)), - stream_(boost::asio::make_strand(ioc)) {} + resolver_{boost::asio::make_strand(ioc)}, + stream_{boost::asio::make_strand(ioc)} {} void ClHttp::http_session::run(const std::string &host, const std::string &target, @@ -89,7 +90,7 @@ void ClHttp::http_session::on_resolve( if (ec) return fail(ec, "resolve"); // Set a timeout on the operation - stream_.expires_after(std::chrono::seconds(30)); + stream_.expires_after(std::chrono::seconds(1)); // Make the connection on the IP address we get from a lookup stream_.async_connect( @@ -97,6 +98,7 @@ void ClHttp::http_session::on_resolve( shared_from_this())); } void ClHttp::http_session::fail(boost::beast::error_code ec, char const *what) { + std::cout << "Failure!..." << std::endl; std::cerr << what << ": " << ec.message() << "\n"; res_.result(boost::beast::http::status::bad_request); res_.reason() = ec.message(); @@ -109,7 +111,7 @@ void ClHttp::http_session::on_connect( if (ec) return fail(ec, "connect"); // Set a timeout on the operation - stream_.expires_after(std::chrono::seconds(30)); + stream_.expires_after(std::chrono::seconds(1)); // Send the HTTP request to the remote host boost::beast::http::async_write( diff --git a/smacc2_sm_reference_library/sm_atomic_http/include/sm_atomic_http/clients/client_behaviors/cb_http_request.hpp b/smacc2_sm_reference_library/sm_atomic_http/include/sm_atomic_http/clients/client_behaviors/cb_http_request.hpp index 254fc28d7..6ec5fbb10 100644 --- a/smacc2_sm_reference_library/sm_atomic_http/include/sm_atomic_http/clients/client_behaviors/cb_http_request.hpp +++ b/smacc2_sm_reference_library/sm_atomic_http/include/sm_atomic_http/clients/client_behaviors/cb_http_request.hpp @@ -45,16 +45,20 @@ class CbHttpRequest : public smacc2::SmaccClientBehavior { cl_http_->onResponseReceived(&CbHttpRequest::onResponseReceived, this); } - void onResponseReceived(const std::string& response) { triggerTranstition(); } + void onResponseReceived(const cl_http::ClHttp::TResponse& response) { + RCLCPP_INFO_STREAM(this->getLogger(), "ON RESPONSE"); + // RCLCPP_INFO_STREAM(this->getLogger(), response); + triggerTranstition(); + } void onEntry() override { - RCLCPP_INFO(getLogger(), "On Entry!"); + RCLCPP_INFO(this->getLogger(), "On Entry!"); cl_http_->makeRequest( cl_http::ClHttp::kHttpRequestMethod::GET); } - void onExit() override { RCLCPP_INFO(getLogger(), "Cb on exit!"); } + void onExit() override { RCLCPP_INFO(this->getLogger(), "Cb on exit!"); } private: cl_http::ClHttp* cl_http_; diff --git a/smacc2_sm_reference_library/sm_atomic_http/include/sm_atomic_http/states/st_state_2.hpp b/smacc2_sm_reference_library/sm_atomic_http/include/sm_atomic_http/states/st_state_2.hpp index d96d4378d..af4b9934e 100644 --- a/smacc2_sm_reference_library/sm_atomic_http/include/sm_atomic_http/states/st_state_2.hpp +++ b/smacc2_sm_reference_library/sm_atomic_http/include/sm_atomic_http/states/st_state_2.hpp @@ -21,12 +21,11 @@ struct State2 : smacc2::SmaccState { // TRANSITION TABLE typedef mpl::list< - Transition, State1, SUCCESS> > + Transition, State1, SUCCESS> > reactions; // STATE FUNCTIONS static void staticConfigure() { - // EvTimer triggers once at 10 client ticks configure_orthogonal(); }