From fedf1eb473a8e73cf86e9848ac3062cbe3e9279b Mon Sep 17 00:00:00 2001 From: Oran Juice Date: Sun, 2 Nov 2014 20:45:26 +0530 Subject: [PATCH] net_skeleton server made usable with daemon --- external/CMakeLists.txt | 1 + src/CMakeLists.txt | 2 +- src/daemon/daemon.cpp | 6 +++++- src/rpc/json_rpc_handlers.h | 33 ++++++++++++++++++++++++++++++++ src/rpc/json_rpc_http_server.cpp | 24 ++--------------------- src/rpc/json_rpc_http_server.h | 11 +---------- 6 files changed, 43 insertions(+), 34 deletions(-) create mode 100644 src/rpc/json_rpc_handlers.h diff --git a/external/CMakeLists.txt b/external/CMakeLists.txt index dcf56da4c..000c1edb2 100755 --- a/external/CMakeLists.txt +++ b/external/CMakeLists.txt @@ -176,4 +176,5 @@ ENDIF() set(NET_SKELETON_SRCS net_skeleton/net_skeleton.c) add_library(net_skeleton ${NET_SKELETON_SRCS}) +set(NET_SKELETON_LIBRARY "${CMAKE_CURRENT_BINARY_DIR}/libnet_skeleton.a" PARENT_SCOPE) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index aec615eb2..aa12b2747 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -65,7 +65,7 @@ add_library(mnemonics ${MNEMONICS}) add_executable(daemon ${DAEMON} ${P2P} ${CRYPTONOTE_PROTOCOL}) add_executable(connectivity_tool ${CONN_TOOL}) add_executable(simpleminer ${MINER}) -target_link_libraries(daemon rpc cryptonote_core crypto common ${UNBOUND_LIBRARY} ${UPNP_LIBRARIES} ${Boost_LIBRARIES} ${EXTRA_LIBRARIES}) +target_link_libraries(daemon rpc cryptonote_core crypto common ${UNBOUND_LIBRARY} ${UPNP_LIBRARIES} ${NET_SKELETON_LIBRARY} ${Boost_LIBRARIES} ${EXTRA_LIBRARIES}) target_link_libraries(connectivity_tool cryptonote_core crypto common ${UNBOUND_LIBRARY} ${Boost_LIBRARIES} ${EXTRA_LIBRARIES}) target_link_libraries(simpleminer cryptonote_core crypto common ${UNBOUND_LIBRARY} ${Boost_LIBRARIES} ${EXTRA_LIBRARIES}) add_library(rpc ${RPC}) diff --git a/src/daemon/daemon.cpp b/src/daemon/daemon.cpp index da09acfd4..8e3d0d28a 100644 --- a/src/daemon/daemon.cpp +++ b/src/daemon/daemon.cpp @@ -50,6 +50,8 @@ using namespace epee; #include "cryptonote_protocol/cryptonote_protocol_handler.h" #include "daemon_commands_handler.h" #include "version.h" +#include "rpc/json_rpc_handlers.h" +#include "rpc/json_rpc_http_server.h" #if defined(WIN32) #include @@ -107,7 +109,9 @@ bool command_line_preprocessor(const boost::program_options::variables_map& vm) int main(int argc, char* argv[]) { - + RPC::Json_rpc_http_server server2("127.0.0.1", "9997", &RPC::ev_handler); + if(!server2.start()) std::cout << "Couldn't start net_skeleton server\n"; + string_tools::set_module_name_and_folder(argv[0]); #ifdef WIN32 _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); diff --git a/src/rpc/json_rpc_handlers.h b/src/rpc/json_rpc_handlers.h new file mode 100644 index 000000000..062ed3867 --- /dev/null +++ b/src/rpc/json_rpc_handlers.h @@ -0,0 +1,33 @@ +#include "net_skeleton/net_skeleton.h" +#include + +namespace RPC +{ + int foo(char *buf, int len, struct ns_rpc_request *req) { + std::cout << "Method name: "; + std::cout << req->method->ptr << std::endl; + return 0; + } + + const char *method_names[] = {"foo", NULL}; + ns_rpc_handler_t handlers[] = {foo, NULL}; + + void ev_handler(struct ns_connection *nc, int ev, void *ev_data) + { + std::cout << "evenet\n\n"; + struct http_message *hm = (struct http_message *) ev_data; + char buf[100]; + switch (ev) { + case NS_HTTP_REQUEST: + ns_rpc_dispatch(hm->body.p, hm->body.len, buf, sizeof(buf), + method_names, handlers); + ns_printf(nc, "HTTP/1.0 200 OK\r\nContent-Length: %d\r\n" + "Content-Type: application/json\r\n\r\n%s", + (int) strlen(buf), buf); + nc->flags |= NSF_FINISHED_SENDING_DATA; + break; + default: + break; + } + } +} diff --git a/src/rpc/json_rpc_http_server.cpp b/src/rpc/json_rpc_http_server.cpp index fd57941f4..65eba883c 100644 --- a/src/rpc/json_rpc_http_server.cpp +++ b/src/rpc/json_rpc_http_server.cpp @@ -5,14 +5,11 @@ namespace RPC { Json_rpc_http_server::Json_rpc_http_server(const std::string &ip, const std::string &port, - void (*ev_handler)(struct ns_connection *nc, int ev, void *ev_data), - const char **method_names, ns_rpc_handler_t *handlers) : - m_method_names(method_names), m_handlers(handlers) + void (*ev_handler)(struct ns_connection *nc, int ev, void *ev_data)) { m_ip = ip; m_port = port; m_is_running = false; - m_method_count = 0; m_ev_handler = ev_handler; } @@ -32,6 +29,7 @@ namespace RPC } ns_set_protocol_http_websocket(nc); server_thread = new boost::thread(&Json_rpc_http_server::poll, this); + return true; } void Json_rpc_http_server::poll() @@ -48,22 +46,4 @@ namespace RPC delete server_thread; ns_mgr_free(&mgr); } - - void Json_rpc_http_server::ev_handler(struct ns_connection *nc, int ev, void *ev_data) - { - struct http_message *hm = (struct http_message *) ev_data; - char buf[100]; - switch (ev) { - case NS_HTTP_REQUEST: - ns_rpc_dispatch(hm->body.p, hm->body.len, buf, sizeof(buf), - m_method_names, m_handlers); - ns_printf(nc, "HTTP/1.0 200 OK\r\nContent-Length: %d\r\n" - "Content-Type: application/json\r\n\r\n%s", - (int) strlen(buf), buf); - nc->flags |= NSF_FINISHED_SENDING_DATA; - break; - default: - break; - } - } } diff --git a/src/rpc/json_rpc_http_server.h b/src/rpc/json_rpc_http_server.h index bdac9aac1..4860fccd6 100644 --- a/src/rpc/json_rpc_http_server.h +++ b/src/rpc/json_rpc_http_server.h @@ -2,8 +2,6 @@ #include #include -#define JSON_RPC_MAX_METHODS 100 - namespace RPC { class Json_rpc_http_server @@ -11,23 +9,16 @@ namespace RPC struct ns_mgr mgr; struct ns_connection *nc; boost::thread *server_thread; - void ev_handler(struct ns_connection *nc, int ev, void *ev_data); void poll(); std::string m_ip; std::string m_port; bool m_is_running; - const char **m_method_names; - ns_rpc_handler_t *m_handlers; - int m_method_count; void (*m_ev_handler)(struct ns_connection *nc, int ev, void *ev_data); public: Json_rpc_http_server(const std::string &ip, const std::string &port, - void (*ev_handler)(struct ns_connection *nc, int ev, void *ev_data), - const char **method_names, ns_rpc_handler_t *handlers); + void (*ev_handler)(struct ns_connection *nc, int ev, void *ev_data)); ~Json_rpc_http_server(); bool start(); void stop(); - bool add_handler(const std::string &method_name, - int (*hander)(char *buf, int len, struct ns_rpc_request *req)); }; }