BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
state.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <fc/variant.hpp>
3 #include <functional>
4 #include <fc/thread/future.hpp>
5 
6 namespace fc { namespace rpc {
7  struct request
8  {
10  std::string method;
13  };
14 
15  struct error_object
16  {
17  int64_t code;
18  std::string message;
20  };
21 
22  struct response
23  {
24  response() {}
25  response( const optional<variant>& _id, const variant& _result,
26  const optional<string>& version = optional<string>() )
27  : id(_id), jsonrpc(version), result(_result) {}
29  const optional<string>& version = optional<string>() )
30  : id(_id), jsonrpc(version), error(error) {}
35  };
36 
37  class state
38  {
39  public:
40  typedef std::function<variant(const variants&)> method;
41  ~state();
42 
43  void add_method( const std::string& name, method m );
44  void remove_method( const std::string& name );
45 
46  variant local_call( const string& method_name, const variants& args );
47  void handle_reply( const response& response );
48 
49  request start_remote_call( const string& method_name, variants args );
50  variant wait_for_response( const variant& request_id );
51 
52  void close();
53 
54  void on_unhandled( const std::function<variant(const string&,const variants&)>& unhandled );
55 
56  private:
57  uint64_t _next_id = 1;
58  std::map<variant, fc::promise<variant>::ptr> _awaiting;
59  std::unordered_map<std::string, method> _methods;
60  std::function<variant(const string&,const variants&)> _unhandled;
61  };
62 } } // namespace fc::rpc
63 
64 FC_REFLECT( fc::rpc::request, (id)(method)(params)(jsonrpc) );
65 FC_REFLECT( fc::rpc::error_object, (code)(message)(data) )
66 FC_REFLECT( fc::rpc::response, (id)(jsonrpc)(result)(error) )
fc::rpc::request::params
variants params
Definition: state.hpp:11
fc::rpc::state::handle_reply
void handle_reply(const response &response)
Definition: state.cpp:30
fc::rpc::state::add_method
void add_method(const std::string &name, method m)
Definition: state.cpp:11
fc::rpc::response::id
optional< variant > id
Definition: state.hpp:31
fc::rpc::request::id
optional< variant > id
Definition: state.hpp:9
fc::rpc::response::response
response(const optional< variant > &_id, const variant &_result, const optional< string > &version=optional< string >())
Definition: state.hpp:25
fc::rpc::state::wait_for_response
variant wait_for_response(const variant &request_id)
Definition: state.cpp:52
fc::rpc::request
Definition: state.hpp:7
fc::rpc::error_object::code
int64_t code
Definition: state.hpp:17
fc
Definition: api.hpp:15
fc::rpc::request::jsonrpc
optional< std::string > jsonrpc
Definition: state.hpp:12
fc::rpc::state::on_unhandled
void on_unhandled(const std::function< variant(const string &, const variants &)> &unhandled)
Definition: state.cpp:64
fc::rpc::error_object::data
optional< variant > data
Definition: state.hpp:19
fc::rpc::request::method
std::string method
Definition: state.hpp:10
fc::rpc::response
Definition: state.hpp:22
fc::rpc::error_object::message
std::string message
Definition: state.hpp:18
fc::rpc::state::close
void close()
Definition: state.cpp:58
fc::rpc::state::~state
~state()
Definition: state.cpp:6
fc::variants
std::vector< variant > variants
Definition: variant.hpp:170
fc::rpc::state::method
std::function< variant(const variants &)> method
Definition: state.hpp:40
fc::rpc::state::start_remote_call
request start_remote_call(const string &method_name, variants args)
Definition: state.cpp:46
fc::rpc::response::jsonrpc
optional< std::string > jsonrpc
Definition: state.hpp:32
fc::rpc::response::result
optional< fc::variant > result
Definition: state.hpp:33
FC_REFLECT
FC_REFLECT(fc::rpc::request,(id)(method)(params)(jsonrpc))
fc::rpc::state::remove_method
void remove_method(const std::string &name)
Definition: state.cpp:16
future.hpp
fc::variant
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition: variant.hpp:198
fc::rpc::state
Definition: state.hpp:37
fc::rpc::state::local_call
variant local_call(const string &method_name, const variants &args)
Definition: state.cpp:21
fc::rpc::response::response
response()
Definition: state.hpp:24
variant.hpp
fc::optional< variant >
fc::rpc::response::error
optional< error_object > error
Definition: state.hpp:34
fc::rpc::error_object
Definition: state.hpp:15
fc::rpc::response::response
response(const optional< variant > &_id, const error_object &error, const optional< string > &version=optional< string >())
Definition: state.hpp:28