JSON-RPC 2.0
JSON-RPC 2.0 Modern C++ Library
Loading...
Searching...
No Matches
jsonrpc::server::Dispatcher Class Reference

Dispatcher for JSON-RPC requests. More...

#include <dispatcher.hpp>

Collaboration diagram for jsonrpc::server::Dispatcher:

Public Member Functions

 Dispatcher (bool enable_multithreading=true, size_t num_threads=std::thread::hardware_concurrency())
 Constructs a Dispatcher.
 
 Dispatcher (const Dispatcher &)=delete
 
 Dispatcher (Dispatcher &&)=delete
 
auto operator= (const Dispatcher &) -> Dispatcher &=delete
 
auto operator= (Dispatcher &&) -> Dispatcher &=delete
 
virtual ~Dispatcher ()=default
 Destructor.
 
auto DispatchRequest (const std::string &request) -> std::optional< std::string >
 Processes a JSON-RPC request.
 
void RegisterMethodCall (const std::string &method, const MethodCallHandler &handler)
 Registers a method call handler.
 
void RegisterNotification (const std::string &method, const NotificationHandler &handler)
 Registers a notification handler.
 

Detailed Description

Dispatcher for JSON-RPC requests.

Dispatcher manages the registration and execution of method call and notification handlers for JSON-RPC requests. It can operate in single-threaded or multi-threaded mode.

Definition at line 24 of file dispatcher.hpp.

Constructor & Destructor Documentation

◆ Dispatcher() [1/3]

jsonrpc::server::Dispatcher::Dispatcher ( bool enable_multithreading = true,
size_t num_threads = std::thread::hardware_concurrency() )
explicit

Constructs a Dispatcher.

Parameters
enableMultithreadingEnable multi-threading support.
numThreadsNumber of threads to use if multi-threading is enabled.

Definition at line 7 of file dispatcher.cpp.

8 : enable_multithreading_(enable_multithreading),
9 thread_pool_(enable_multithreading ? num_threads : 0) {
10 // Optionally log or perform additional setup if needed
11 if (enable_multithreading_) {
12 spdlog::info(
13 "Dispatcher initialized with multithreading support using {} threads",
14 num_threads);
15 } else {
16 spdlog::info("Dispatcher initialized without multithreading support");
17 }
18}

◆ Dispatcher() [2/3]

jsonrpc::server::Dispatcher::Dispatcher ( const Dispatcher & )
delete

◆ Dispatcher() [3/3]

jsonrpc::server::Dispatcher::Dispatcher ( Dispatcher && )
delete

◆ ~Dispatcher()

virtual jsonrpc::server::Dispatcher::~Dispatcher ( )
virtualdefault

Destructor.

Member Function Documentation

◆ DispatchRequest()

auto jsonrpc::server::Dispatcher::DispatchRequest ( const std::string & request) -> std::optional<std::string>

Processes a JSON-RPC request.

Dispatches the request to the appropriate handler.

Parameters
requestThe JSON-RPC request as a string.
Returns
The response from the handler as a JSON string, or std::nullopt if no response is needed.

Definition at line 20 of file dispatcher.cpp.

21 {
22 auto request_json = ParseAndValidateJson(request_str);
23 if (!request_json.has_value()) {
25 }
26
27 if (request_json->is_array()) {
28 return DispatchBatchRequest(*request_json);
29 }
30
31 return DispatchSingleRequest(*request_json);
32}
static auto CreateLibError(LibErrorKind error_kind, const std::optional< nlohmann::json > &id=std::nullopt) -> Response
Creates a Response object for a library error.
Definition response.cpp:67

References jsonrpc::server::Response::CreateLibError(), and jsonrpc::server::kParseError.

Here is the call graph for this function:

◆ operator=() [1/2]

auto jsonrpc::server::Dispatcher::operator= ( const Dispatcher & ) -> Dispatcher &=delete
delete

◆ operator=() [2/2]

auto jsonrpc::server::Dispatcher::operator= ( Dispatcher && ) -> Dispatcher &=delete
delete

◆ RegisterMethodCall()

void jsonrpc::server::Dispatcher::RegisterMethodCall ( const std::string & method,
const MethodCallHandler & handler )

Registers a method call handler.

Parameters
methodThe name of the RPC method.
handlerThe handler function for this method.

Definition at line 208 of file dispatcher.cpp.

209 {
210 handlers_[method] = handler;
211 spdlog::info("Dispatcher registered method call: {}", method);
212}

◆ RegisterNotification()

void jsonrpc::server::Dispatcher::RegisterNotification ( const std::string & method,
const NotificationHandler & handler )

Registers a notification handler.

Parameters
methodThe name of the RPC notification.
handlerThe handler function for this notification.

Definition at line 214 of file dispatcher.cpp.

215 {
216 handlers_[method] = handler;
217 spdlog::info("Dispatcher registered notification: {}", method);
218}

The documentation for this class was generated from the following files: