OpenCBDC Transaction Processor
Loading...
Searching...
No Matches
json_rpc_http_server.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_SERVER_H_
7#define OPENCBDC_TX_SRC_RPC_JSON_RPC_HTTP_SERVER_H_
8
10
11#include <atomic>
12#include <functional>
13#include <json/json.h>
14#include <microhttpd.h>
15#include <mutex>
16#include <optional>
17#include <sstream>
18
19namespace cbdc::rpc {
23 public:
27 = std::function<void(std::optional<Json::Value>)>;
30 using handler_callback_type = std::function<
31 bool(std::string, Json::Value, result_callback_type)>;
32
37 bool enable_cors = false);
38
47 -> json_rpc_http_server& = delete;
48
51
54 auto init() -> bool;
55
56 private:
57 struct request {
58 MHD_Connection* m_connection{};
59 std::stringstream m_request;
60 json_rpc_http_server* m_server{};
61 const char* m_origin{};
62 unsigned int m_code{};
63 };
64
65 network::ip_address m_host{};
66 uint16_t m_port{};
67 MHD_Daemon* m_daemon{};
69 Json::StreamWriterBuilder m_builder;
70
71 std::mutex m_requests_mut;
72 std::map<request*, std::unique_ptr<request>> m_requests;
73
74 std::atomic<bool> m_running{true};
75 bool m_enable_cors;
76 // std::atomic<size_t> m_requests_started{};
77
78 static auto callback(void* cls,
79 struct MHD_Connection* connection,
80 const char* url,
81 const char* method,
82 const char* version,
83 const char* upload_data,
84 size_t* upload_data_size,
85 void** con_cls) -> MHD_Result;
86
87 static auto send_cors_response(request* request_info) -> bool;
88 static auto send_response(std::string response, request* request_info)
89 -> bool;
90
91 auto handle_request(request* request_info) -> bool;
92
93 void handle_response(uint64_t id,
94 request* request_info,
95 std::optional<Json::Value> resp);
96
97 static void request_complete(void* cls,
98 struct MHD_Connection* connection,
99 void** con_cls,
100 MHD_RequestTerminationCode toe);
101 };
102}
103
104#endif
Asynchrounous HTTP JSON-RPC server implemented using libmicrohttpd and libjsoncpp.
auto operator=(const json_rpc_http_server &) -> json_rpc_http_server &=delete
json_rpc_http_server(network::endpoint_t endpoint, bool enable_cors=false)
Construct a new server.
void register_handler_callback(handler_callback_type handler_callback)
Register the application request handler function with the server.
std::function< bool(std::string, Json::Value, result_callback_type)> handler_callback_type
Callback function type provided by the application for processing requests.
std::function< void(std::optional< Json::Value >)> result_callback_type
Type alias for the callback function for returning response values to the server.
auto init() -> bool
Start listening for incoming connections and processing requests.
std::string ip_address
An IP addresses.
Definition socket.hpp:15
std::pair< ip_address, port_number_t > endpoint_t
[host name, port number].
Definition socket.hpp:19