Skip to content

Commit

Permalink
Moved HTTP client into the client_libraries folder and made separate …
Browse files Browse the repository at this point in the history
…compilation units. Modified examples to use this new client
  • Loading branch information
yassiezar committed Aug 28, 2023
1 parent ef8f247 commit d70537b
Show file tree
Hide file tree
Showing 10 changed files with 320 additions and 218 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,8 @@ class SmaccActionClientBase : public ISmaccActionClient
<< getName()
<< "] Action request "
// << rclcpp_action::to_string(this->goalHandle_->get_goal_id()) <<". Goal sent to " << this->action_endpoint_
<< "\": " << std::endl
<< goal);
<< "\": " << std::endl);
// << goal);

// if (client_->isServerConnected())
// {
Expand Down
210 changes: 0 additions & 210 deletions smacc2/include/smacc2/client_bases/smacc_http_client.hpp

This file was deleted.

41 changes: 41 additions & 0 deletions smacc2_client_library/http_client/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
cmake_minimum_required(VERSION 3.5)
project(http_client)

# Default to C++17
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 17)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(smacc2 REQUIRED)

include_directories(
include
${smacc2_INCLUDE_DIRS}
)

file(GLOB_RECURSE SRC_FILES src *.cpp)

add_library(${PROJECT_NAME}
${SRC_FILES}
)

target_link_libraries(${PROJECT_NAME} ${smacc2_LIBRARIES})
ament_target_dependencies(${PROJECT_NAME} smacc2)
ament_export_include_directories(include)
ament_export_libraries(${PROJECT_NAME})

install(
DIRECTORY include/
DESTINATION include)

install(TARGETS
${PROJECT_NAME}
DESTINATION lib/)

ament_package()
116 changes: 116 additions & 0 deletions smacc2_client_library/http_client/include/http_client/http_client.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Copyright 2023 RobosoftAI Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/*****************************************************************************************************************
*
* Authors: Jaycee Lock
*
******************************************************************************************************************/

#pragma once

#include <boost/asio/executor_work_guard.hpp>
#include <boost/asio/strand.hpp>
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/version.hpp>

#include <smacc2/smacc_client.hpp>
#include <smacc2/smacc.hpp>

#include <iostream>
#include <memory>
#include <optional>
#include <string>
#include <thread>
#include <unordered_map>

namespace cl_http {
class ClHttp : public smacc2::ISmaccClient {
class http_session : public std::enable_shared_from_this<http_session> {
public:
using TResponse =
const boost::beast::http::response<boost::beast::http::string_body> &;

// Objects are constructed with a strand to
// ensure that handlers do not execute concurrently.
http_session(boost::asio::io_context &ioc,
const std::function<void(TResponse)> response);

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

void on_resolve(boost::beast::error_code ec,
boost::asio::ip::tcp::resolver::results_type results);

private:
void fail(boost::beast::error_code ec, char const *what);
void on_connect(
boost::beast::error_code ec,
boost::asio::ip::tcp::resolver::results_type::endpoint_type);
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<void(const boost::beast::http::response<
boost::beast::http::string_body> &response)>
onResponse;

boost::asio::ip::tcp::resolver resolver_;
boost::beast::tcp_stream stream_;
boost::beast::flat_buffer buffer_; // (Must persist between reads)
boost::beast::http::request<boost::beast::http::empty_body> req_;
boost::beast::http::response<boost::beast::http::string_body> res_;
};

public:
enum class kHttpRequestMethod {
GET = static_cast<int>(boost::beast::http::verb::get),
POST = static_cast<int>(boost::beast::http::verb::post),
};

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

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

virtual ~ClHttp();

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

private:
const int HTTP_VERSION = 11;

bool initialized_;
bool is_ssl_;
int timeout_;
std::string server_name_;

boost::asio::io_context io_context_;
boost::asio::executor_work_guard<decltype(io_context_)::executor_type>
worker_guard_;
std::thread tcp_connection_runner_;

std::function<void(http_session::TResponse)> callbackHandler;

smacc2::SmaccSignal<void(const std::string &)> onResponseReceived_;
};
} // namespace cl_http
17 changes: 17 additions & 0 deletions smacc2_client_library/http_client/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>http_client</name>
<version>2.3.18</version>
<description>The http_client package</description>
<maintainer email="[email protected]">Jaycee Lock</maintainer>
<license>Apache-2.0</license>

<buildtool_depend>ament_cmake</buildtool_depend>

<depend>smacc2</depend>

<export>
<build_type>ament_cmake</build_type>
</export>
</package>
Loading

0 comments on commit d70537b

Please sign in to comment.