OpenCBDC Transaction Processor
Loading...
Searching...
No Matches
json_rpc_http_client.hpp
Go to the documentation of this file.
1// Copyright (c) 2022 MIT Digital Currency Initiative,
2// Federal Reserve Bank of Boston
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6#ifndef OPENCBDC_TX_SRC_RPC_JSON_RPC_HTTP_CLIENT_H_
7#define OPENCBDC_TX_SRC_RPC_JSON_RPC_HTTP_CLIENT_H_
8
9#include "event_handler.hpp"
11
12#include <curl/curl.h>
13#include <functional>
14#include <json/json.h>
15#include <optional>
16#include <queue>
17#include <set>
18#include <unordered_map>
19
20namespace cbdc::rpc {
23 public:
28 };
29
32 static curl_initializer curl_init = curl_initializer();
33
37 public:
42 json_rpc_http_client(std::vector<std::string> endpoints,
43 long timeout,
44 std::shared_ptr<logging::log> log);
47
50 -> json_rpc_http_client& = delete;
53 -> json_rpc_http_client& = delete;
54
56 using callback_type = std::function<void(std::optional<Json::Value>)>;
57
63 void call(const std::string& method,
64 Json::Value params,
65 callback_type result_fn);
66
70 [[nodiscard]] auto pump() -> bool;
71
72 private:
73 std::vector<std::string> m_endpoints;
74 long m_timeout;
75 std::unique_ptr<event_handler> m_ev_handler;
76
77 Json::StreamWriterBuilder m_builder;
78
79 CURLM* m_multi_handle{};
80
81 std::queue<CURL*> m_handles;
82
83 struct transfer {
84 std::stringstream m_result;
85 callback_type m_cb;
86 std::string m_payload;
87 };
88
89 std::unordered_map<CURL*, std::unique_ptr<transfer>> m_transfers;
90
91 curl_slist* m_headers{};
92 Json::Value m_payload;
93
94 size_t m_lb_idx{};
95
96 // size_t m_requests_started{};
97
98 static auto
99 write_data(void* ptr, size_t size, size_t nmemb, struct transfer* t)
100 -> size_t;
101
102 static auto socket_callback(CURL* handle,
103 curl_socket_t s,
104 int what,
106 void* socketp) -> int;
107
108 static auto timer_callback(CURLM* multi_handle,
109 long timeout_ms,
110 json_rpc_http_client* c) -> int;
111
112 protected:
113 std::shared_ptr<cbdc::logging::log> m_log;
114 };
115}
116
117#endif
Class for performing libcurl global initialization.
~curl_initializer()
Deinitializes libcurl.
curl_initializer()
Initializes libcurl.
Asynchronous HTTP JSON-RPC client implemented using libcurl.
void call(const std::string &method, Json::Value params, callback_type result_fn)
Calls the requested JSON-RPC method with the given parameters and returns the response asynchronously...
std::function< void(std::optional< Json::Value >)> callback_type
Type alias for the response callback function.
json_rpc_http_client(json_rpc_http_client &&)=delete
std::shared_ptr< cbdc::logging::log > m_log
json_rpc_http_client(const json_rpc_http_client &)=delete
auto operator=(json_rpc_http_client &&) -> json_rpc_http_client &=delete
~json_rpc_http_client()
Cancels any existing requests and stops the client.
auto pump() -> bool
Process events raised by the underlying libcurl implementation.
json_rpc_http_client(std::vector< std::string > endpoints, long timeout, std::shared_ptr< logging::log > log)
Construct a new client.
auto operator=(const json_rpc_http_client &) -> json_rpc_http_client &=delete