Plotly.cpp 0.1.0
A C++ plotting library for expressive, interactive, real-time & streaming data visualization
Loading...
Searching...
No Matches
json_rpc.hpp
Go to the documentation of this file.
1
5
6#ifndef PLOTLY_DETAILS_JSON_RPC_HPP
7#define PLOTLY_DETAILS_JSON_RPC_HPP
8
10#include <nlohmann/json.hpp>
11
12#include <cstdint>
13#include <functional>
14#include <future>
15#include <memory>
16#include <mutex>
17#include <optional>
18#include <string>
19#include <unordered_set>
20#include <utility>
21
22namespace plotly::detail {
23
27enum class JsonRpcErrorCode : std::int16_t {
28 PARSE_ERROR = -32700,
29 INVALID_REQUEST = -32600,
31 INVALID_PARAMS = -32602,
32 INTERNAL_ERROR = -32603,
33 SERVER_ERROR = -32000
34};
35
40 int code;
41 std::string message;
42 nlohmann::json data;
43};
44
49 std::optional<nlohmann::json> id;
50 std::optional<nlohmann::json>
52 std::optional<JsonRpcError> error;
53 std::string jsonrpc = "2.0";
54
59 [[nodiscard]] auto toJson() const -> nlohmann::json;
60};
61
65class JsonRpc {
66public:
70 explicit JsonRpc(std::unique_ptr<WebsocketEndpointInterface> wsEndpoint);
71
75 ~JsonRpc();
76
77 // Delete copy constructor and assignment operator
78 JsonRpc(const JsonRpc &) = delete;
79 auto operator=(const JsonRpc &) -> JsonRpc & = delete;
80
81 // Move constructor and assignment operator
82 JsonRpc(JsonRpc &&other) noexcept;
83 auto operator=(JsonRpc &&other) noexcept -> JsonRpc &;
84
91 void registerHandler(
92 const std::string &method,
93 const std::function<nlohmann::json(const nlohmann::json &)> &handler);
94
95 void
96 registerNotification(const std::string &method,
97 std::function<void(const nlohmann::json &)> handler);
98
99 void unregisterHandler(const std::string &method);
100
108 const std::string &callbackName,
109 std::function<void(const std::string_view)> callback);
110
115 void unregisterCallbackFromWebsocket(const std::string &callbackName);
116
121
129 auto call(const std::string &method, const nlohmann::json &params)
130 -> std::pair<std::future<nlohmann::json>, std::function<void()>>;
131
137 void notify(const std::string &method, const nlohmann::json &params);
138
143 [[nodiscard]] auto getWebsocketEndpoint() const
145
146private:
147 std::unique_ptr<WebsocketEndpointInterface> _wsEndpoint;
148
149 void handleIncomingMessage(const std::string_view &message);
150 void sendSuccessResponse(const nlohmann::json &requestId,
151 const nlohmann::json &result);
152 void sendErrorResponse(const nlohmann::json &requestId,
153 JsonRpcErrorCode errorCode,
154 const std::string &message);
155
156 std::unordered_map<std::string,
157 std::function<nlohmann::json(const nlohmann::json &)>>
158 _handlers;
159 std::unordered_map<std::string, std::function<void(const nlohmann::json &)>>
160 _notifications;
161 std::unordered_set<std::string> _registeredCallbacks;
162 mutable std::mutex _registeredCallbacksMutex;
163};
164
165} // namespace plotly::detail
166#endif // PLOTLY_DETAILS_JSON_RPC_HPP
void registerCallbackWithWebsocket(const std::string &callbackName, std::function< void(const std::string_view)> callback)
Registers a callback function for the WebSocket endpoint.
Definition json_rpc.cpp:100
auto operator=(const JsonRpc &) -> JsonRpc &=delete
void unregisterAllCallbacksFromWebsockets()
Unregisters all callbacks from the WebSocket endpoint.
Definition json_rpc.cpp:122
auto call(const std::string &method, const nlohmann::json &params) -> std::pair< std::future< nlohmann::json >, std::function< void()> >
Makes an asynchronous JSON-RPC call to the connected client.
Definition json_rpc.cpp:130
void unregisterCallbackFromWebsocket(const std::string &callbackName)
Unregisters a callback function from the WebSocket endpoint.
Definition json_rpc.cpp:112
void registerHandler(const std::string &method, const std::function< nlohmann::json(const nlohmann::json &)> &handler)
Registers a handler function for a specific RPC method.
Definition json_rpc.cpp:82
void registerNotification(const std::string &method, std::function< void(const nlohmann::json &)> handler)
Definition json_rpc.cpp:88
JsonRpc(const JsonRpc &)=delete
void unregisterHandler(const std::string &method)
Definition json_rpc.cpp:96
void notify(const std::string &method, const nlohmann::json &params)
Sends a notification to all connected clients.
Definition json_rpc.cpp:171
auto getWebsocketEndpoint() const -> WebsocketEndpointInterface *
Gets a raw pointer to the underlying websocket endpoint.
Definition json_rpc.cpp:179
JsonRpc(std::unique_ptr< WebsocketEndpointInterface > wsEndpoint)
Constructs a new JSON-RPC server.
Definition json_rpc.cpp:39
Abstract interface for WebSocket endpoints.
Definition websockets_endpoint.hpp:37
Definition browser.cpp:27
JsonRpcErrorCode
Enumeration of standard JSON-RPC 2.0 error codes.
Definition json_rpc.hpp:27
@ INVALID_PARAMS
Invalid method parameter(s)
Definition json_rpc.hpp:31
@ SERVER_ERROR
Server error.
Definition json_rpc.hpp:33
@ PARSE_ERROR
Invalid JSON was received by the server.
Definition json_rpc.hpp:28
@ METHOD_NOT_FOUND
The method does not exist / is not available.
Definition json_rpc.hpp:30
@ INVALID_REQUEST
The JSON sent is not a valid Request object.
Definition json_rpc.hpp:29
@ INTERNAL_ERROR
Internal JSON-RPC error.
Definition json_rpc.hpp:32
Structure representing a JSON-RPC error object.
Definition json_rpc.hpp:39
nlohmann::json data
Additional error data.
Definition json_rpc.hpp:42
int code
The error code.
Definition json_rpc.hpp:40
std::string message
The error message.
Definition json_rpc.hpp:41
Structure representing a JSON-RPC response object.
Definition json_rpc.hpp:48
auto toJson() const -> nlohmann::json
Converts the response to a JSON object.
Definition json_rpc.cpp:19
std::string jsonrpc
JSON-RPC version string.
Definition json_rpc.hpp:53
std::optional< nlohmann::json > id
Request ID (null for notifications)
Definition json_rpc.hpp:49
std::optional< nlohmann::json > result
The result of the request (if successful)
Definition json_rpc.hpp:51
std::optional< JsonRpcError > error
The error object (if request failed)
Definition json_rpc.hpp:52
WebSocket endpoint interface and implementation for plotly.cpp.