Skip to content

Commit

Permalink
refactor: Rename RequestManager => Session
Browse files Browse the repository at this point in the history
  • Loading branch information
egelja committed Sep 6, 2023
1 parent f28d223 commit 312c2f0
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 55 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ FetchContent_MakeAvailable(cmake_git_version_tracking)
add_library(
raccoon_lib OBJECT
# Web
src/web/manager.cpp
src/web/session.cpp
src/web/connections/base.cpp
src/web/connections/ws.cpp

Expand Down
13 changes: 6 additions & 7 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include "git.h"
#include "storage/storage.hpp"
#include "utils/utils.hpp"
#include "web/manager.hpp"
#include "web/web.hpp"

#include <argparse/argparse.hpp>
Expand Down Expand Up @@ -127,8 +126,8 @@ main(int argc, const char** argv)
return 1;
}

// Manager testing
raccoon::web::RequestManager session;
// Create web session
raccoon::web::Session session;

// Create websocket
auto data_cb = [&prox](auto* conn, std::vector<uint8_t> data) {
Expand All @@ -147,16 +146,16 @@ main(int argc, const char** argv)

auto ws1 = session.ws("ws://localhost:8675", data_cb);

// Run manager
// Run session
auto err = session.run();

if (err) [[unlikely]] {
if (err) {
switch (err) {
case raccoon::web::RequestManager::STATUS_FORCED_SHUTDOWN:
case raccoon::web::Session::STATUS_FORCED_SHUTDOWN:
log_c(main, "Forced shutdown, aborting!");
abort();

case raccoon::web::RequestManager::STATUS_GRACEFUL_SHUTDOWN:
case raccoon::web::Session::STATUS_GRACEFUL_SHUTDOWN:
log_w(main, "Gracefully exiting application");
redisFree(ctx);
return 0;
Expand Down
3 changes: 0 additions & 3 deletions src/web/connections/base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

#include "logging.hpp"

#include <curl/curl.h>
#include <quill/LogLevel.h>

#include <cctype>

namespace raccoon {
Expand Down
3 changes: 1 addition & 2 deletions src/web/connections/base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include "common.hpp"
#include "utils/utils.hpp"
#include "web/connections/base.hpp"

#include <curl/curl.h>

Expand Down Expand Up @@ -77,7 +76,7 @@ class Connection {
*/
[[nodiscard]] virtual FILE* file() const noexcept = 0;

friend class RequestManager;
friend class Session;

protected:
/**
Expand Down
1 change: 0 additions & 1 deletion src/web/connections/ws.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "ws.hpp"

#include "common.hpp"
#include "logging.hpp"
#include "utils/utils.hpp"

#include <curl/curl.h>
Expand Down
3 changes: 1 addition & 2 deletions src/web/connections/ws.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include "base.hpp"
#include "common.hpp"
#include "web/connections/base.hpp"

#include <functional>

Expand Down Expand Up @@ -103,7 +102,7 @@ class WebSocketConnection : public Connection {
return nullptr;
}

friend class RequestManager;
friend class Session;

private:
/**
Expand Down
46 changes: 21 additions & 25 deletions src/web/manager.cpp → src/web/session.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#include "manager.hpp"
#include "session.hpp"

#include "common.hpp"
#include "web/manager.hpp"

#include <curl/curl.h>
#include <uv.h>

#include <csignal>

Expand All @@ -20,7 +16,7 @@ format_as(CURLMSG msg)
namespace raccoon {
namespace web {

RequestManager::RequestManager(uv_loop_t* event_loop) :
Session::Session(uv_loop_t* event_loop) :
curl_handle_(curl_multi_init()), loop_(event_loop)
{
log_bt(web, "Creating session for handle {}", fmt::ptr(curl_handle_));
Expand Down Expand Up @@ -49,7 +45,7 @@ RequestManager::RequestManager(uv_loop_t* event_loop) :
&interrupt_signal_,
[](auto* handle, int signum) {
assert(signum == SIGINT);
auto* session = static_cast<RequestManager*>(handle->data);
auto* session = static_cast<Session*>(handle->data);

if (session->status_ == STATUS_OK) {
log_w(web, "Received SIGINT, shutting down gracefully");
Expand Down Expand Up @@ -87,7 +83,7 @@ RequestManager::RequestManager(uv_loop_t* event_loop) :
&break_signal_,
[](auto* handle, int signum) {
assert(signum == SIGBREAK);
auto* session = static_cast<RequestManager*>(handle->data);
auto* session = static_cast<Session*>(handle->data);

log_w(web, "Received SIGBREAK, printing metrics");

Expand All @@ -109,13 +105,13 @@ RequestManager::RequestManager(uv_loop_t* event_loop) :
metrics_cb_.data = this;

uv_prepare_start(&metrics_cb_, [](auto* handle) {
auto* session = static_cast<RequestManager*>(handle->data);
auto* session = static_cast<Session*>(handle->data);
uv_metrics_info(session->loop_, &session->metrics_);
});
}

void
RequestManager::process_libcurl_messages_()
Session::process_libcurl_messages_()
{
log_bt(web, "Start libcurl message processing");

Expand Down Expand Up @@ -214,12 +210,12 @@ RequestManager::process_libcurl_messages_()
*****************************************************************************/

void
RequestManager::run_initializations_(uv_timer_t* handle)
Session::run_initializations_(uv_timer_t* handle)
{
log_bt(web, "Running connection initializations");

// Get manager
auto* manager = static_cast<RequestManager*>(handle->data);
auto* manager = static_cast<Session*>(handle->data);

// Run through initialization queue
auto& init_queue = manager->connections_to_init_;
Expand Down Expand Up @@ -258,7 +254,7 @@ RequestManager::run_initializations_(uv_timer_t* handle)
}

std::shared_ptr<WebSocketConnection>
RequestManager::ws(const std::string& url, WebSocketConnection::callback on_data)
Session::ws(const std::string& url, WebSocketConnection::callback on_data)
{
log_bt(web, "Create WS conn to {}", url);

Expand Down Expand Up @@ -286,13 +282,13 @@ RequestManager::ws(const std::string& url, WebSocketConnection::callback on_data
namespace detail {

CURLM*
get_handle(RequestManager* manager)
get_handle(Session* manager)
{
return manager->curl_handle_;
}

void
process_libcurl_messages(RequestManager* manager, int running_handles)
process_libcurl_messages(Session* manager, int running_handles)
{
log_bt(web, "libcurl message processing ran ({} running handles)", running_handles);
log_d(web, "{} running handles", running_handles);
Expand All @@ -314,14 +310,14 @@ struct curl_context_t {
uv_poll_t poll_handle;
curl_socket_t sock_fd;

RequestManager* manager;
Session* manager;
};

/**
* Create a new curl context.
*/
static curl_context_t*
create_curl_context(curl_socket_t sock_fd, uv_loop_t* loop, RequestManager* manager)
create_curl_context(curl_socket_t sock_fd, uv_loop_t* loop, Session* manager)
{
log_bt(web, "Create curl ctx for socket {}", sock_fd);

Expand Down Expand Up @@ -406,7 +402,7 @@ on_poll(uv_poll_t* req, int status, int uv_events)
} // namespace detail

int
RequestManager::handle_socket_(
Session::handle_socket_(
CURL* easy, // easy handle
curl_socket_t sock_fd, // socket
int action, // what curl wants us to do with the socket
Expand All @@ -418,7 +414,7 @@ RequestManager::handle_socket_(
log_bt(web, "libcurl action {} on socket {}", action, sock_fd);

// Get our request manager
auto* manager = static_cast<RequestManager*>(user_ptr);
auto* manager = static_cast<Session*>(user_ptr);

// Get our socket context
auto* curl_ctx = static_cast<detail::curl_context_t*>(socket_ptr);
Expand Down Expand Up @@ -489,17 +485,17 @@ RequestManager::handle_socket_(
}

int
RequestManager::start_timeout_( // NOLINT(*-naming)
CURLM* multi, // curl multi handle
int32_t timeout_ms, // timeout curl wants to set
void* user_ptr // pointer to user data
Session::start_timeout_( // NOLINT(*-naming)
CURLM* multi, // curl multi handle
int32_t timeout_ms, // timeout curl wants to set
void* user_ptr // pointer to user data
)
{
UNUSED(multi);
log_bt(web, "Update libcurl timeout to {}ms", timeout_ms);

// Get our request manager
auto* manager = static_cast<RequestManager*>(user_ptr);
auto* manager = static_cast<Session*>(user_ptr);

// Process timer
if (timeout_ms < 0) { // curl wants to stop the timer
Expand All @@ -518,7 +514,7 @@ RequestManager::start_timeout_( // NOLINT(*-naming)
log_t2(web, "libcurl socket timeout");

// Get manager
auto* cb_manager = static_cast<RequestManager*>(timer->data);
auto* cb_manager = static_cast<Session*>(timer->data);

// Process the request
int running_handles{};
Expand Down
21 changes: 8 additions & 13 deletions src/web/manager.hpp → src/web/session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@

#include "common.hpp"
#include "connections/connections.hpp"
#include "logging.hpp"
#include "web/connections/base.hpp"
#include "web/manager.hpp"

#include <curl/curl.h>
#include <curl/multi.h>
#include <uv.h>

#include <queue>

namespace raccoon {
namespace web {

class RequestManager;
class Session;

/**
* Implementation details.
Expand All @@ -25,19 +21,19 @@ namespace detail {
/**
* Friend function to access the handle of the request manager.
*/
CURLM* get_handle(RequestManager* manager);
CURLM* get_handle(Session* manager);

/**
* Friend function to access a private method of the request manager.
*/
void process_libcurl_messages(RequestManager* manager, int running_handles);
void process_libcurl_messages(Session* manager, int running_handles);

} // namespace detail

/**
* Class to manage web requests.
*/
class RequestManager {
class Session {
public:
/**
* Status of the request manager.
Expand Down Expand Up @@ -76,7 +72,7 @@ class RequestManager {
/**
* Create a new request manager.
*/
explicit RequestManager(uv_loop_t* event_loop = uv_default_loop());
explicit Session(uv_loop_t* event_loop = uv_default_loop());

/**
* Run the session to completion.
Expand Down Expand Up @@ -132,10 +128,9 @@ class RequestManager {
}

/* Friends */
friend CURLM* detail::get_handle(RequestManager* manager);
friend CURLM* detail::get_handle(Session* manager);

friend void
detail::process_libcurl_messages(RequestManager* manager, int running_handles);
friend void detail::process_libcurl_messages(Session* manager, int running_handles);

private:
static void run_initializations_(uv_timer_t* handle); // NOLINT(*-naming)
Expand All @@ -158,7 +153,7 @@ class RequestManager {
};

inline auto
format_as(RequestManager::Status status)
format_as(Session::Status status)
{
return fmt::underlying(status);
}
Expand Down
2 changes: 1 addition & 1 deletion src/web/web.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
// Re-exports

#include "connections/connections.hpp"
#include "manager.hpp"
#include "session.hpp"

0 comments on commit 312c2f0

Please sign in to comment.