From 7008d1419abe5841925314b509f0b3efb07d2a16 Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Thu, 26 Mar 2015 18:38:43 -0400 Subject: [PATCH] adding websocket api stub --- include/fc/rpc/api_connection.hpp | 6 ++- include/fc/rpc/state.hpp | 5 ++- include/fc/rpc/websocket_api.hpp | 64 +++++++++++++++++++++++++++++++ tests/websocket.cpp | 1 + 4 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 include/fc/rpc/websocket_api.hpp diff --git a/include/fc/rpc/api_connection.hpp b/include/fc/rpc/api_connection.hpp index 02b7bea..8975e77 100644 --- a/include/fc/rpc/api_connection.hpp +++ b/include/fc/rpc/api_connection.hpp @@ -7,7 +7,7 @@ #include #include #include -#include +//#include namespace fc { class api_connection; @@ -111,7 +111,7 @@ namespace fc { } /** makes calls to the remote server */ - virtual variant send_call( api_id_type api_id, const string& method_name, const variants& args = variants() )const + virtual variant send_call( api_id_type api_id, const string& method_name, const variants& args = variants() ) { FC_ASSERT( _remote_connection ); return _remote_connection->receive_call( api_id, method_name, args ); @@ -217,6 +217,7 @@ namespace fc { }; } + /* class json_api_connection : public api_connection { public: @@ -248,5 +249,6 @@ namespace fc { std::shared_ptr _json_con; }; + */ } // fc diff --git a/include/fc/rpc/state.hpp b/include/fc/rpc/state.hpp index e42439c..f870750 100644 --- a/include/fc/rpc/state.hpp +++ b/include/fc/rpc/state.hpp @@ -13,13 +13,16 @@ namespace fc { namespace rpc { struct error_object { - int64_t code = 0; + int64_t code; std::string message; optional data; }; struct response { + response(){} + response( int64_t i, fc::variant r ):id(i),result(r){} + response( int64_t i, error_object r ):id(i),error(r){} int64_t id = 0; optional result; optional error; diff --git a/include/fc/rpc/websocket_api.hpp b/include/fc/rpc/websocket_api.hpp new file mode 100644 index 0000000..b275391 --- /dev/null +++ b/include/fc/rpc/websocket_api.hpp @@ -0,0 +1,64 @@ +#pragma once +#include +#include +#include +#include +#include + +namespace fc { namespace rpc { + + class websocket_api : public api_connection, public fc::rpc::state, public fc::http::websocket_session + { + public: + websocket_api( fc::http::websocket_connection_ptr c ) + :fc::http::websocket_session(c) + { + add_method( "call", [this]( const variants& args ) -> variant { + FC_ASSERT( args.size() == 3 && args[2].is_array() ); + return this->receive_call( args[0].as_uint64(), + args[1].as_string(), + args[2].get_array() ); + }); + } + + virtual variant send_call( api_id_type api_id, + const string& method_name, + const variants& args = variants() ) override + { + auto request = this->start_remote_call( "call", {api_id, method_name, args} ); + send_message( fc::json::to_string(request) ); + return wait_for_response( *request.id ); + } + + protected: + virtual void on_message( const std::string& message ) + { + auto var = fc::json::from_string(message); + const auto& var_obj = var.get_object(); + if( var_obj.contains( "method" ) ) + { + auto call = var.as(); + try { + auto result = local_call( call.method, call.params ); + if( call.id ) + { + send_message( fc::json::to_string( response( *call.id, result ) ) ); + } + } + catch ( const fc::exception& e ) + { + if( call.id ) + { + send_message( fc::json::to_string( response( *call.id, error_object{ 1, e.to_detail_string(), fc::variant(e)} ) ) ); + } + } + } + else + { + auto reply = var.as(); + handle_reply( reply ); + } + } + }; + +} } // namespace fc::rpc diff --git a/tests/websocket.cpp b/tests/websocket.cpp index 766d699..5f55c19 100644 --- a/tests/websocket.cpp +++ b/tests/websocket.cpp @@ -1,6 +1,7 @@ #include #include #include +#include using namespace fc::http;