Skip to content

Commit

Permalink
refactor: Co_MessageProcessingTask uses HttpRequestParams
Browse files Browse the repository at this point in the history
The token still needs to be set since it will be updated,
when it expires. For the moment we still pass it as a reference.
  • Loading branch information
jr0me committed Aug 8, 2024
1 parent 562ae99 commit 64f30bb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
7 changes: 2 additions & 5 deletions src/agent/communicator/include/http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,8 @@ namespace http_client
boost::beast::http::request<boost::beast::http::string_body>& req,
boost::beast::error_code& ec);

boost::asio::awaitable<void> Co_MessageProcessingTask(const boost::beast::http::verb method,
const std::string host,
const std::string port,
const std::string endpoint,
const std::string& token,
boost::asio::awaitable<void> Co_MessageProcessingTask(const std::string& token,
HttpRequestParams params,
std::function<std::string()> messageGetter);

boost::beast::http::response<boost::beast::http::dynamic_body> SendHttpRequest(const HttpRequestParams& params);
Expand Down
15 changes: 9 additions & 6 deletions src/agent/communicator/src/communicator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ namespace communicator

boost::asio::awaitable<void> Communicator::GetCommandsFromManager()
{
co_await http_client::Co_MessageProcessingTask(
boost::beast::http::verb::get, m_managerIp, m_port, "/commands", m_token, {});
const auto reqParams =
http_client::HttpRequestParams(boost::beast::http::verb::get, m_managerIp, m_port, "/commands");
co_await http_client::Co_MessageProcessingTask(m_token, reqParams, {});
}

boost::asio::awaitable<void> Communicator::WaitForTokenExpirationAndAuthenticate()
Expand Down Expand Up @@ -118,13 +119,15 @@ namespace communicator

boost::asio::awaitable<void> Communicator::StatefulMessageProcessingTask(std::queue<std::string>& messageQueue)
{
co_await http_client::Co_MessageProcessingTask(
boost::beast::http::verb::post, m_managerIp, m_port, "/stateful", m_token, {});
const auto reqParams =
http_client::HttpRequestParams(boost::beast::http::verb::post, m_managerIp, m_port, "/stateful");
co_await http_client::Co_MessageProcessingTask(m_token, reqParams, {});
}

boost::asio::awaitable<void> Communicator::StatelessMessageProcessingTask(std::queue<std::string>& messageQueue)
{
co_await http_client::Co_MessageProcessingTask(
boost::beast::http::verb::post, m_managerIp, m_port, "/stateless", m_token, {});
const auto reqParams =
http_client::HttpRequestParams(boost::beast::http::verb::post, m_managerIp, m_port, "/stateless");
co_await http_client::Co_MessageProcessingTask(m_token, reqParams, {});
}
} // namespace communicator
14 changes: 6 additions & 8 deletions src/agent/communicator/src/http_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,8 @@ namespace http_client
std::cout << "Response body: " << boost::beast::buffers_to_string(res.body().data()) << std::endl;
}

boost::asio::awaitable<void> Co_MessageProcessingTask(const boost::beast::http::verb method,
const std::string host,
const std::string port,
const std::string endpoint,
const std::string& token,
boost::asio::awaitable<void> Co_MessageProcessingTask(const std::string& token,
HttpRequestParams reqParams,
std::function<std::string()> messageGetter)
{
using namespace std::chrono_literals;
Expand All @@ -78,7 +75,8 @@ namespace http_client
{
boost::asio::ip::tcp::socket socket(executor);

const auto results = co_await resolver.async_resolve(host, port, boost::asio::use_awaitable);
const auto results =
co_await resolver.async_resolve(reqParams.host, reqParams.port, boost::asio::use_awaitable);

boost::system::error_code code;
co_await boost::asio::async_connect(
Expand All @@ -92,8 +90,8 @@ namespace http_client
continue;
}

const auto message = messageGetter ? messageGetter() : "";
const auto reqParams = HttpRequestParams(method, host, port, endpoint, token, "", message);
reqParams.body = messageGetter ? messageGetter() : "";
reqParams.token = token;
auto req = CreateHttpRequest(reqParams);

boost::beast::error_code ec;
Expand Down

0 comments on commit 64f30bb

Please sign in to comment.