BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
state.cpp
Go to the documentation of this file.
1 #include <fc/rpc/state.hpp>
2 #include <fc/thread/thread.hpp>
3 #include <fc/reflect/variant.hpp>
4 
5 namespace fc { namespace rpc {
7 {
8  close();
9 }
10 
11 void state::add_method( const std::string& name, method m )
12 {
13  _methods.emplace(std::pair<std::string,method>(name,std::move(m)));
14 }
15 
16 void state::remove_method( const std::string& name )
17 {
18  _methods.erase(name);
19 }
20 
21 variant state::local_call( const string& method_name, const variants& args )
22 {
23  auto method_itr = _methods.find(method_name);
24  if( method_itr == _methods.end() && _unhandled )
25  return _unhandled( method_name, args );
26  FC_ASSERT( method_itr != _methods.end(), "Unknown Method: ${name}", ("name",method_name) );
27  return method_itr->second(args);
28 }
29 
31 {
32  FC_ASSERT( response.id, "Response without ID: ${response}", ("response",response) );
33  auto await = _awaiting.find( *response.id );
34  FC_ASSERT( await != _awaiting.end(), "Unknown Response ID: ${id}", ("id",response.id)("response",response) );
35  if( response.result )
36  await->second->set_value( *response.result );
37  else if( response.error )
38  {
39  await->second->set_exception( exception_ptr(new FC_EXCEPTION( exception, "${error}", ("error",response.error->message)("data",response) ) ) );
40  }
41  else
42  await->second->set_value( fc::variant() );
43  _awaiting.erase(await);
44 }
45 
46 request state::start_remote_call( const string& method_name, variants args )
47 {
48  request request{ _next_id++, method_name, std::move(args) };
49  _awaiting[*request.id] = fc::promise<variant>::create("json_connection::async_call");
50  return request;
51 }
53 {
54  auto itr = _awaiting.find(request_id);
55  FC_ASSERT( itr != _awaiting.end() );
56  return fc::future<variant>( itr->second ).wait();
57 }
59 {
60  for( auto item : _awaiting )
61  item.second->set_exception( fc::exception_ptr(new FC_EXCEPTION( eof_exception, "connection closed" )) );
62  _awaiting.clear();
63 }
64 void state::on_unhandled( const std::function<variant(const string&, const variants&)>& unhandled )
65 {
66  _unhandled = unhandled;
67 }
68 
69 } } // namespace fc::rpc
fc::rpc::state::handle_reply
void handle_reply(const response &response)
Definition: state.cpp:30
fc::future
a placeholder for the result of an asynchronous operation.
Definition: future.hpp:211
fc::rpc::state::add_method
void add_method(const std::string &name, method m)
Definition: state.cpp:11
FC_EXCEPTION
#define FC_EXCEPTION(EXCEPTION_TYPE, FORMAT,...)
Definition: exception.hpp:371
fc::rpc::response::id
optional< variant > id
Definition: state.hpp:31
fc::rpc::request::id
optional< variant > id
Definition: state.hpp:9
fc::exception
Used to generate a useful error report when an exception is thrown.
Definition: exception.hpp:56
variant.hpp
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
Definition: api.hpp:15
fc::rpc::state::on_unhandled
void on_unhandled(const std::function< variant(const string &, const variants &)> &unhandled)
Definition: state.cpp:64
fc::promise::create
static ptr create(const char *desc FC_TASK_NAME_DEFAULT_ARG)
Definition: future.hpp:114
fc::exception_ptr
std::shared_ptr< exception > exception_ptr
Definition: exception.hpp:131
fc::rpc::response
Definition: state.hpp:22
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
thread.hpp
fc::rpc::state::start_remote_call
request start_remote_call(const string &method_name, variants args)
Definition: state.cpp:46
fc::rpc::response::result
optional< fc::variant > result
Definition: state.hpp:33
fc::rpc::state::remove_method
void remove_method(const std::string &name)
Definition: state.cpp:16
FC_ASSERT
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition: exception.hpp:345
fc::variant
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition: variant.hpp:198
fc::future::wait
const T & wait(const microseconds &timeout=microseconds::maximum()) const
Definition: future.hpp:228
fc::rpc::state::local_call
variant local_call(const string &method_name, const variants &args)
Definition: state.cpp:21
state.hpp
fc::rpc::response::error
optional< error_object > error
Definition: state.hpp:34