BitShares-Core  5.0.0
BitShares blockchain implementation and command-line interface 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) {}
28  response( const optional<variant>& _id, const error_object& error,
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 
65 FC_REFLECT( fc::rpc::error_object, (code)(message)(data) )
66 FC_REFLECT( fc::rpc::response, (id)(jsonrpc)(result)(error) )
variants params
Definition: state.hpp:11
std::vector< variant > variants
Definition: variant.hpp:170
optional< fc::variant > result
Definition: state.hpp:33
std::function< variant(const variants &)> method
Definition: state.hpp:40
optional< variant > id
Definition: state.hpp:9
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object&#39;s.
Definition: variant.hpp:198
optional< std::string > jsonrpc
Definition: state.hpp:32
optional< std::string > jsonrpc
Definition: state.hpp:12
response(const optional< variant > &_id, const error_object &error, const optional< string > &version=optional< string >())
Definition: state.hpp:28
FC_REFLECT(fc::rpc::request,(id)(method)(params)(jsonrpc))
std::string message
Definition: state.hpp:18
Definition: api.hpp:15
optional< error_object > error
Definition: state.hpp:34
optional< variant > data
Definition: state.hpp:19
optional< variant > id
Definition: state.hpp:31
response(const optional< variant > &_id, const variant &_result, const optional< string > &version=optional< string >())
Definition: state.hpp:25
std::string method
Definition: state.hpp:10