Kagome
Polkadot Runtime Engine in C++17
jrpc_server_impl.cpp
Go to the documentation of this file.
1 
7 
10 
13  switch (e) {
14  case E::JSON_FORMAT_FAILED:
15  return "Json format failed";
16  }
17  return "Unknown error";
18 }
19 
20 namespace {
21  constexpr auto rpcRequestsCountMetricName = "kagome_rpc_requests_count";
22 }
23 
24 namespace kagome::api {
25 
27  // register json format handler
28  jsonrpc_handler_.RegisterFormatHandler(format_handler_);
29 
30  // Register metrics
31  metrics_registry_->registerCounterFamily(rpcRequestsCountMetricName,
32  "Block height info of the chain");
33 
35  metrics_registry_->registerCounterMetric(rpcRequestsCountMetricName);
36  }
37 
38  void JRpcServerImpl::registerHandler(const std::string &name, Method method) {
39  auto &dispatcher = jsonrpc_handler_.GetDispatcher();
40  dispatcher.AddMethod(name, std::move(method));
41  }
42 
43  std::vector<std::string> JRpcServerImpl::getHandlerNames() {
44  auto &dispatcher = jsonrpc_handler_.GetDispatcher();
45  return dispatcher.GetMethodNames();
46  }
47 
48  void JRpcServerImpl::processJsonData(std::string method_name,
49  const jsonrpc::Request::Parameters &from,
50  const FormatterHandler &cb) {
51  /*
52  * We need this because of spec format
53  * https://github.com/w3f/PSPs/blob/psp-rpc-api/psp-002.md#state_subscribestorage-pubsub
54  */
55  using Response = jsonrpc::Request;
56  using Fault = jsonrpc::Fault;
57 
58  JsonWriter writer;
59  try {
66  constexpr bool id = false;
67  Response response(std::move(method_name), from, id);
68  response.Write(writer);
69 
70  auto &&formatted_response = writer.GetData();
71  cb(std::string_view(formatted_response->GetData(),
72  formatted_response->GetSize()));
73 
74  } catch (const Fault &ex) {
75  cb(outcome::failure(Error::JSON_FORMAT_FAILED));
76  }
77 
79  }
80 
81  void JRpcServerImpl::processData(std::string_view request,
82  const ResponseHandler &cb) {
83  JrpcHandleBatch response(jsonrpc_handler_, request);
84  cb(response.response());
85  }
86 
87 } // namespace kagome::api
std::string_view response() const
jsonrpc::Server jsonrpc_handler_
json rpc server instance
std::function< void(outcome::result< std::string_view >)> FormatterHandler
Definition: jrpc_server.hpp:46
OUTCOME_CPP_DEFINE_CATEGORY(kagome::api, JRpcServerImpl::Error, e)
void processJsonData(std::string method_name, const jsonrpc::Request::Parameters &from, const FormatterHandler &cb) override
creates a valid jsonrpc response and passes it to
virtual void inc()=0
Increment the counter by 1.
void registerHandler(const std::string &name, Method method) override
registers rpc request handler lambda
std::function< void(std::string_view)> ResponseHandler
Definition: jrpc_server.hpp:44
jsonrpc::JsonFormatHandler format_handler_
format handler instance
metrics::Counter * metric_rpc_requests_count_
std::vector< std::string > getHandlerNames() override
void processData(std::string_view request, const ResponseHandler &cb) override
handles decoded network message
jsonrpc::MethodWrapper::Method Method
Definition: jrpc_server.hpp:25
metrics::RegistryPtr metrics_registry_
std::shared_ptr< jsonrpc::FormattedData > GetData() override