net_skeleton server made usable with daemon

This commit is contained in:
Oran Juice 2014-11-02 20:45:26 +05:30
parent ac662269cf
commit fedf1eb473
No known key found for this signature in database
GPG Key ID: 71C5AF46CCB28124
6 changed files with 43 additions and 34 deletions

View File

@ -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)

View File

@ -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})

View File

@ -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 <crtdbg.h>
@ -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 );

View File

@ -0,0 +1,33 @@
#include "net_skeleton/net_skeleton.h"
#include <iostream>
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;
}
}
}

View File

@ -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;
}
}
}

View File

@ -2,8 +2,6 @@
#include <boost/thread.hpp>
#include <string>
#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));
};
}