2015-03-26 22:38:43 +00:00
|
|
|
#pragma once
|
|
|
|
|
#include <fc/rpc/api_connection.hpp>
|
|
|
|
|
#include <fc/rpc/state.hpp>
|
|
|
|
|
#include <fc/network/http/websocket.hpp>
|
|
|
|
|
#include <fc/io/json.hpp>
|
|
|
|
|
#include <fc/reflect/variant.hpp>
|
|
|
|
|
|
|
|
|
|
namespace fc { namespace rpc {
|
|
|
|
|
|
2015-03-27 20:29:33 +00:00
|
|
|
class websocket_api_connection : public api_connection
|
2015-03-26 22:38:43 +00:00
|
|
|
{
|
|
|
|
|
public:
|
2015-03-30 22:34:04 +00:00
|
|
|
websocket_api_connection( fc::http::websocket_connection& c )
|
2015-03-27 20:29:33 +00:00
|
|
|
:_connection(c)
|
2015-03-26 22:38:43 +00:00
|
|
|
{
|
2015-03-27 20:29:33 +00:00
|
|
|
_rpc_state.add_method( "call", [this]( const variants& args ) -> variant {
|
2015-03-26 22:38:43 +00:00
|
|
|
FC_ASSERT( args.size() == 3 && args[2].is_array() );
|
|
|
|
|
return this->receive_call( args[0].as_uint64(),
|
2015-03-27 20:29:33 +00:00
|
|
|
args[1].as_string(),
|
|
|
|
|
args[2].get_array() );
|
2015-03-26 22:38:43 +00:00
|
|
|
});
|
2015-03-31 21:45:01 +00:00
|
|
|
|
2015-03-31 15:31:56 +00:00
|
|
|
_rpc_state.add_method( "notice", [this]( const variants& args ) -> variant {
|
|
|
|
|
FC_ASSERT( args.size() == 2 && args[1].is_array() );
|
|
|
|
|
this->receive_notice( args[0].as_uint64(), args[1].get_array() );
|
|
|
|
|
return variant();
|
|
|
|
|
});
|
2015-03-31 21:45:01 +00:00
|
|
|
|
2015-03-31 15:31:56 +00:00
|
|
|
_rpc_state.add_method( "callback", [this]( const variants& args ) -> variant {
|
|
|
|
|
FC_ASSERT( args.size() == 2 && args[1].is_array() );
|
|
|
|
|
this->receive_callback( args[0].as_uint64(), args[1].get_array() );
|
|
|
|
|
return variant();
|
|
|
|
|
});
|
2015-03-31 21:45:01 +00:00
|
|
|
|
|
|
|
|
_rpc_state.on_unhandled( [&]( const std::string& method_name, const variants& args ){
|
|
|
|
|
return this->receive_call( 0, method_name, args );
|
|
|
|
|
});
|
|
|
|
|
|
2015-03-30 22:34:04 +00:00
|
|
|
_connection.on_message_handler( [&]( const std::string& msg ){ on_message(msg); } );
|
2015-03-26 22:38:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual variant send_call( api_id_type api_id,
|
2015-03-31 15:31:56 +00:00
|
|
|
string method_name,
|
|
|
|
|
variants args = variants() ) override
|
2015-03-26 22:38:43 +00:00
|
|
|
{
|
2015-03-31 15:31:56 +00:00
|
|
|
auto request = _rpc_state.start_remote_call( "call", {api_id, std::move(method_name), std::move(args) } );
|
2015-03-30 22:34:04 +00:00
|
|
|
_connection.send_message( fc::json::to_string(request) );
|
2015-03-27 20:29:33 +00:00
|
|
|
return _rpc_state.wait_for_response( *request.id );
|
2015-03-26 22:38:43 +00:00
|
|
|
}
|
2015-03-31 15:31:56 +00:00
|
|
|
virtual variant send_callback( uint64_t callback_id, variants args = variants() ) override
|
|
|
|
|
{
|
|
|
|
|
auto request = _rpc_state.start_remote_call( "callback", {callback_id, std::move(args) } );
|
|
|
|
|
_connection.send_message( fc::json::to_string(request) );
|
|
|
|
|
return _rpc_state.wait_for_response( *request.id );
|
|
|
|
|
}
|
|
|
|
|
virtual void send_notice( uint64_t callback_id, variants args = variants() ) override
|
|
|
|
|
{
|
|
|
|
|
fc::rpc::request req{ optional<uint64_t>(), "notice", {callback_id, std::move(args)}};
|
|
|
|
|
_connection.send_message( fc::json::to_string(req) );
|
|
|
|
|
}
|
2015-03-26 22:38:43 +00:00
|
|
|
|
|
|
|
|
protected:
|
2015-03-27 20:29:33 +00:00
|
|
|
void on_message( const std::string& message )
|
2015-03-31 15:31:56 +00:00
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
auto var = fc::json::from_string(message);
|
|
|
|
|
const auto& var_obj = var.get_object();
|
|
|
|
|
if( var_obj.contains( "method" ) )
|
|
|
|
|
{
|
|
|
|
|
auto call = var.as<fc::rpc::request>();
|
|
|
|
|
try {
|
|
|
|
|
auto result = _rpc_state.local_call( call.method, call.params );
|
|
|
|
|
if( call.id )
|
|
|
|
|
{
|
|
|
|
|
_connection.send_message( fc::json::to_string( response( *call.id, result ) ) );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch ( const fc::exception& e )
|
2015-03-26 22:38:43 +00:00
|
|
|
{
|
2015-03-31 15:31:56 +00:00
|
|
|
if( call.id )
|
|
|
|
|
{
|
|
|
|
|
_connection.send_message( fc::json::to_string( response( *call.id, error_object{ 1, e.to_detail_string(), fc::variant(e)} ) ) );
|
|
|
|
|
}
|
2015-03-26 22:38:43 +00:00
|
|
|
}
|
|
|
|
|
}
|
2015-03-31 15:31:56 +00:00
|
|
|
else
|
2015-03-26 22:38:43 +00:00
|
|
|
{
|
2015-03-31 15:31:56 +00:00
|
|
|
auto reply = var.as<fc::rpc::response>();
|
|
|
|
|
_rpc_state.handle_reply( reply );
|
2015-03-26 22:38:43 +00:00
|
|
|
}
|
2015-03-31 15:31:56 +00:00
|
|
|
} catch ( const fc::exception& e ) {
|
|
|
|
|
wdump((e.to_detail_string()));
|
2015-03-26 22:38:43 +00:00
|
|
|
}
|
|
|
|
|
}
|
2015-03-30 22:34:04 +00:00
|
|
|
fc::http::websocket_connection& _connection;
|
|
|
|
|
fc::rpc::state _rpc_state;
|
2015-03-26 22:38:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} } // namespace fc::rpc
|