From 11e914fbe97184e0566085948b04942a8301ac36 Mon Sep 17 00:00:00 2001 From: Scott Schurr Date: Wed, 28 Jun 2023 03:57:52 +0300 Subject: [PATCH] refactor: rename ServerHandlerImp to ServerHandler (#4516) Rename `ServerHandlerImp` to `ServerHandler`. There was no other ServerHandler definition despite the existence of a header suggesting that there was. This resolves a piece of historical confusion in the code, which was identified during a code review. The changes in the diff may look more extensive than they actually are. The contents of `impl/ServerHandlerImp.h` were merged into `ServerHandler.h`, making the latter file appear to have undergone significant modifications. However, this is a non-breaking refactor that only restructures code. --- Builds/CMake/RippledCore.cmake | 2 +- src/ripple/rpc/ServerHandler.h | 199 ++++++++++++++++- ...ServerHandlerImp.cpp => ServerHandler.cpp} | 43 ++-- src/ripple/rpc/impl/ServerHandlerImp.h | 201 ------------------ 4 files changed, 215 insertions(+), 230 deletions(-) rename src/ripple/rpc/impl/{ServerHandlerImp.cpp => ServerHandler.cpp} (98%) delete mode 100644 src/ripple/rpc/impl/ServerHandlerImp.h diff --git a/Builds/CMake/RippledCore.cmake b/Builds/CMake/RippledCore.cmake index 9f5813297b2..53a5e61a7b7 100644 --- a/Builds/CMake/RippledCore.cmake +++ b/Builds/CMake/RippledCore.cmake @@ -692,7 +692,7 @@ target_sources (rippled PRIVATE src/ripple/rpc/impl/RPCHandler.cpp src/ripple/rpc/impl/RPCHelpers.cpp src/ripple/rpc/impl/Role.cpp - src/ripple/rpc/impl/ServerHandlerImp.cpp + src/ripple/rpc/impl/ServerHandler.cpp src/ripple/rpc/impl/ShardArchiveHandler.cpp src/ripple/rpc/impl/ShardVerificationScheduler.cpp src/ripple/rpc/impl/Status.cpp diff --git a/src/ripple/rpc/ServerHandler.h b/src/ripple/rpc/ServerHandler.h index 54cccdff64e..07fb61362a0 100644 --- a/src/ripple/rpc/ServerHandler.h +++ b/src/ripple/rpc/ServerHandler.h @@ -20,21 +20,200 @@ #ifndef RIPPLE_RPC_SERVERHANDLER_H_INCLUDED #define RIPPLE_RPC_SERVERHANDLER_H_INCLUDED -#include -#include -#include +#include #include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include namespace ripple { -using ServerHandler = ServerHandlerImp; +inline bool +operator<(Port const& lhs, Port const& rhs) +{ + return lhs.name < rhs.name; +} + +class ServerHandler +{ +public: + struct Setup + { + explicit Setup() = default; + + std::vector ports; + + // Memberspace + struct client_t + { + explicit client_t() = default; + + bool secure = false; + std::string ip; + std::uint16_t port = 0; + std::string user; + std::string password; + std::string admin_user; + std::string admin_password; + }; + + // Configuration when acting in client role + client_t client; + + // Configuration for the Overlay + struct overlay_t + { + explicit overlay_t() = default; + + boost::asio::ip::address ip; + std::uint16_t port = 0; + }; + + overlay_t overlay; + + void + makeContexts(); + }; + +private: + using socket_type = boost::beast::tcp_stream; + using stream_type = boost::beast::ssl_stream; + + Application& app_; + Resource::Manager& m_resourceManager; + beast::Journal m_journal; + NetworkOPs& m_networkOPs; + std::unique_ptr m_server; + Setup setup_; + JobQueue& m_jobQueue; + beast::insight::Counter rpc_requests_; + beast::insight::Event rpc_size_; + beast::insight::Event rpc_time_; + std::mutex mutex_; + std::condition_variable condition_; + bool stopped_{false}; + std::map, int> count_; + + // A private type used to restrict access to the ServerHandler constructor. + struct ServerHandlerCreator + { + explicit ServerHandlerCreator() = default; + }; + + // Friend declaration that allows make_ServerHandler to access the + // private type that restricts access to the ServerHandler ctor. + friend std::unique_ptr + make_ServerHandler( + Application& app, + boost::asio::io_service&, + JobQueue&, + NetworkOPs&, + Resource::Manager&, + CollectorManager& cm); + +public: + // Must be public so make_unique can call it. + ServerHandler( + ServerHandlerCreator const&, + Application& app, + boost::asio::io_service& io_service, + JobQueue& jobQueue, + NetworkOPs& networkOPs, + Resource::Manager& resourceManager, + CollectorManager& cm); + + ~ServerHandler(); + + using Output = Json::Output; + + void + setup(Setup const& setup, beast::Journal journal); + + Setup const& + setup() const + { + return setup_; + } + + void + stop(); + + // + // Handler + // + + bool + onAccept(Session& session, boost::asio::ip::tcp::endpoint endpoint); + + Handoff + onHandoff( + Session& session, + std::unique_ptr&& bundle, + http_request_type&& request, + boost::asio::ip::tcp::endpoint const& remote_address); + + Handoff + onHandoff( + Session& session, + http_request_type&& request, + boost::asio::ip::tcp::endpoint const& remote_address) + { + return onHandoff( + session, + {}, + std::forward(request), + remote_address); + } + + void + onRequest(Session& session); + + void + onWSMessage( + std::shared_ptr session, + std::vector const& buffers); + + void + onClose(Session& session, boost::system::error_code const&); + + void + onStopped(Server&); + +private: + Json::Value + processSession( + std::shared_ptr const& session, + std::shared_ptr const& coro, + Json::Value const& jv); + + void + processSession( + std::shared_ptr const&, + std::shared_ptr coro); + + void + processRequest( + Port const& port, + std::string const& request, + beast::IP::Endpoint const& remoteIPAddress, + Output&&, + std::shared_ptr coro, + boost::string_view forwardedFor, + boost::string_view user); + + Handoff + statusResponse(http_request_type const& request) const; +}; ServerHandler::Setup setup_ServerHandler(Config const& c, std::ostream&& log); diff --git a/src/ripple/rpc/impl/ServerHandlerImp.cpp b/src/ripple/rpc/impl/ServerHandler.cpp similarity index 98% rename from src/ripple/rpc/impl/ServerHandlerImp.cpp rename to src/ripple/rpc/impl/ServerHandler.cpp index f269283b83a..85bff54232b 100644 --- a/src/ripple/rpc/impl/ServerHandlerImp.cpp +++ b/src/ripple/rpc/impl/ServerHandler.cpp @@ -17,6 +17,8 @@ */ //============================================================================== +#include + #include #include #include @@ -35,9 +37,7 @@ #include #include #include -#include #include -#include #include #include #include @@ -101,7 +101,8 @@ authorized(Port const& port, std::map const& h) return strUser == port.user && strPassword == port.password; } -ServerHandlerImp::ServerHandlerImp( +ServerHandler::ServerHandler( + ServerHandlerCreator const&, Application& app, boost::asio::io_service& io_service, JobQueue& jobQueue, @@ -121,13 +122,13 @@ ServerHandlerImp::ServerHandlerImp( rpc_time_ = group->make_event("time"); } -ServerHandlerImp::~ServerHandlerImp() +ServerHandler::~ServerHandler() { m_server = nullptr; } void -ServerHandlerImp::setup(Setup const& setup, beast::Journal journal) +ServerHandler::setup(Setup const& setup, beast::Journal journal) { setup_ = setup; m_server->ports(setup.ports); @@ -136,7 +137,7 @@ ServerHandlerImp::setup(Setup const& setup, beast::Journal journal) //------------------------------------------------------------------------------ void -ServerHandlerImp::stop() +ServerHandler::stop() { m_server->close(); { @@ -148,7 +149,7 @@ ServerHandlerImp::stop() //------------------------------------------------------------------------------ bool -ServerHandlerImp::onAccept( +ServerHandler::onAccept( Session& session, boost::asio::ip::tcp::endpoint endpoint) { @@ -170,7 +171,7 @@ ServerHandlerImp::onAccept( } Handoff -ServerHandlerImp::onHandoff( +ServerHandler::onHandoff( Session& session, std::unique_ptr&& bundle, http_request_type&& request, @@ -272,7 +273,7 @@ buffers_to_string(ConstBufferSequence const& bs) } void -ServerHandlerImp::onRequest(Session& session) +ServerHandler::onRequest(Session& session) { // Make sure RPC is enabled on the port if (session.port().protocol.count("http") == 0 && @@ -312,7 +313,7 @@ ServerHandlerImp::onRequest(Session& session) } void -ServerHandlerImp::onWSMessage( +ServerHandler::onWSMessage( std::shared_ptr session, std::vector const& buffers) { @@ -362,14 +363,14 @@ ServerHandlerImp::onWSMessage( } void -ServerHandlerImp::onClose(Session& session, boost::system::error_code const&) +ServerHandler::onClose(Session& session, boost::system::error_code const&) { std::lock_guard lock(mutex_); --count_[session.port()]; } void -ServerHandlerImp::onStopped(Server&) +ServerHandler::onStopped(Server&) { std::lock_guard lock(mutex_); stopped_ = true; @@ -398,7 +399,7 @@ logDuration( } Json::Value -ServerHandlerImp::processSession( +ServerHandler::processSession( std::shared_ptr const& session, std::shared_ptr const& coro, Json::Value const& jv) @@ -545,7 +546,7 @@ ServerHandlerImp::processSession( // Run as a coroutine. void -ServerHandlerImp::processSession( +ServerHandler::processSession( std::shared_ptr const& session, std::shared_ptr coro) { @@ -586,7 +587,7 @@ Json::Int constexpr forbidden = -32605; Json::Int constexpr wrong_version = -32606; void -ServerHandlerImp::processRequest( +ServerHandler::processRequest( Port const& port, std::string const& request, beast::IP::Endpoint const& remoteIPAddress, @@ -1022,7 +1023,7 @@ ServerHandlerImp::processRequest( is reported, meaning the server can accept more connections. */ Handoff -ServerHandlerImp::statusResponse(http_request_type const& request) const +ServerHandler::statusResponse(http_request_type const& request) const { using namespace boost::beast::http; Handoff handoff; @@ -1252,8 +1253,14 @@ make_ServerHandler( Resource::Manager& resourceManager, CollectorManager& cm) { - return std::make_unique( - app, io_service, jobQueue, networkOPs, resourceManager, cm); + return std::make_unique( + ServerHandler::ServerHandlerCreator(), + app, + io_service, + jobQueue, + networkOPs, + resourceManager, + cm); } } // namespace ripple diff --git a/src/ripple/rpc/impl/ServerHandlerImp.h b/src/ripple/rpc/impl/ServerHandlerImp.h deleted file mode 100644 index 7c0bf9c9ae5..00000000000 --- a/src/ripple/rpc/impl/ServerHandlerImp.h +++ /dev/null @@ -1,201 +0,0 @@ -//------------------------------------------------------------------------------ -/* - This file is part of rippled: https://github.com/ripple/rippled - Copyright (c) 2012, 2013 Ripple Labs Inc. - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ -//============================================================================== - -#ifndef RIPPLE_RPC_SERVERHANDLERIMP_H_INCLUDED -#define RIPPLE_RPC_SERVERHANDLERIMP_H_INCLUDED - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace ripple { - -inline bool -operator<(Port const& lhs, Port const& rhs) -{ - return lhs.name < rhs.name; -} - -class ServerHandlerImp -{ -public: - struct Setup - { - explicit Setup() = default; - - std::vector ports; - - // Memberspace - struct client_t - { - explicit client_t() = default; - - bool secure = false; - std::string ip; - std::uint16_t port = 0; - std::string user; - std::string password; - std::string admin_user; - std::string admin_password; - }; - - // Configuration when acting in client role - client_t client; - - // Configuration for the Overlay - struct overlay_t - { - explicit overlay_t() = default; - - boost::asio::ip::address ip; - std::uint16_t port = 0; - }; - - overlay_t overlay; - - void - makeContexts(); - }; - -private: - using socket_type = boost::beast::tcp_stream; - using stream_type = boost::beast::ssl_stream; - - Application& app_; - Resource::Manager& m_resourceManager; - beast::Journal m_journal; - NetworkOPs& m_networkOPs; - std::unique_ptr m_server; - Setup setup_; - JobQueue& m_jobQueue; - beast::insight::Counter rpc_requests_; - beast::insight::Event rpc_size_; - beast::insight::Event rpc_time_; - std::mutex mutex_; - std::condition_variable condition_; - bool stopped_{false}; - std::map, int> count_; - -public: - ServerHandlerImp( - Application& app, - boost::asio::io_service& io_service, - JobQueue& jobQueue, - NetworkOPs& networkOPs, - Resource::Manager& resourceManager, - CollectorManager& cm); - - ~ServerHandlerImp(); - - using Output = Json::Output; - - void - setup(Setup const& setup, beast::Journal journal); - - Setup const& - setup() const - { - return setup_; - } - - void - stop(); - - // - // Handler - // - - bool - onAccept(Session& session, boost::asio::ip::tcp::endpoint endpoint); - - Handoff - onHandoff( - Session& session, - std::unique_ptr&& bundle, - http_request_type&& request, - boost::asio::ip::tcp::endpoint const& remote_address); - - Handoff - onHandoff( - Session& session, - http_request_type&& request, - boost::asio::ip::tcp::endpoint const& remote_address) - { - return onHandoff( - session, - {}, - std::forward(request), - remote_address); - } - - void - onRequest(Session& session); - - void - onWSMessage( - std::shared_ptr session, - std::vector const& buffers); - - void - onClose(Session& session, boost::system::error_code const&); - - void - onStopped(Server&); - -private: - Json::Value - processSession( - std::shared_ptr const& session, - std::shared_ptr const& coro, - Json::Value const& jv); - - void - processSession( - std::shared_ptr const&, - std::shared_ptr coro); - - void - processRequest( - Port const& port, - std::string const& request, - beast::IP::Endpoint const& remoteIPAddress, - Output&&, - std::shared_ptr coro, - boost::string_view forwardedFor, - boost::string_view user); - - Handoff - statusResponse(http_request_type const& request) const; -}; - -} // namespace ripple - -#endif