BitShares-Core  4.0.0
BitShares blockchain implementation and command-line interface 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
void close()
Definition: state.cpp:58
static ptr create(const char *desc FC_TASK_NAME_DEFAULT_ARG)
Definition: future.hpp:114
a placeholder for the result of an asynchronous operation.
Definition: future.hpp:211
T wait(boost::signals2::signal< void(T)> &sig, const microseconds &timeout_us=microseconds::maximum())
Definition: signals.hpp:38
void remove_method(const std::string &name)
Definition: state.cpp:16
variant local_call(const string &method_name, const variants &args)
Definition: state.cpp:21
variant wait_for_response(const variant &request_id)
Definition: state.cpp:52
Used to generate a useful error report when an exception is thrown.At each level in the stack where t...
Definition: exception.hpp:56
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
void on_unhandled(const std::function< variant(const string &, const variants &)> &unhandled)
Definition: state.cpp:64
std::shared_ptr< exception > exception_ptr
Definition: exception.hpp:131
request start_remote_call(const string &method_name, variants args)
Definition: state.cpp:46
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition: exception.hpp:345
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
void add_method(const std::string &name, method m)
Definition: state.cpp:11
Definition: api.hpp:15
optional< error_object > error
Definition: state.hpp:34
optional< variant > id
Definition: state.hpp:31
void handle_reply(const response &response)
Definition: state.cpp:30
#define FC_EXCEPTION(EXCEPTION_TYPE, FORMAT,...)
Definition: exception.hpp:371