From 9eea54bfa6f58a9a5b5cc29b55ea80f88218c1a4 Mon Sep 17 00:00:00 2001 From: Oran Juice Date: Mon, 8 Dec 2014 22:46:47 +0530 Subject: [PATCH] 0MQ basic server in the works --- src/rpc/daemon_ipc_server.cpp | 135 +++++++++++++++++++++++++++++++ src/rpc/daemon_ipc_server.h | 97 ++++++++++++++++++++++ src/rpc/json_rpc_http_server.cpp | 6 +- src/rpc/json_rpc_http_server.h | 2 +- 4 files changed, 238 insertions(+), 2 deletions(-) create mode 100644 src/rpc/daemon_ipc_server.cpp create mode 100644 src/rpc/daemon_ipc_server.h diff --git a/src/rpc/daemon_ipc_server.cpp b/src/rpc/daemon_ipc_server.cpp new file mode 100644 index 000000000..54f4232da --- /dev/null +++ b/src/rpc/daemon_ipc_server.cpp @@ -0,0 +1,135 @@ +// Copyright (c) 2014, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers + +#include "json_rpc_http_server.h" + +#include + +namespace IPC +{ + Daemon_ipc_server::Daemon_ipc_server(const std::string &ip, const std::string &port, + nodetool::node_server > *p_p2p, + cryptonote::core *p_core) + { + if (m_is_running) + { + return false; + } + p2p = p_p2p; + core = p_core; + m_ip = ip; + m_port = port; + zmq::context_t context(1); + socket = new zmq::socket_t(context, ZMQ_REQ); + return true; + } + + void Daemon_ipc_server::start() + { + socket->bind(std::string("tcp://" + m_ip + ":" + m_port); + m_is_running = true; + // Start a new thread so it doesn't block. + server_thread = new boost::thread(&Daemon_ipc_server::poll, this); + } + + Daemon_ipc_server::~Daemon_ipc_server() + { + stop(); + } + + bool Daemon_ipc_server::check_core_busy() + { + if (p2p->get_payload_object().get_core().get_blockchain_storage().is_storing_blockchain()) + { + return false; + } + return true; + } + + /*! + * \brief Stops the server + */ + void Daemon_ipc_server::stop() + { + m_is_running = false; + server_thread->join(); + delete server_thread; + } + + void Daemon_ipc_server::getblocks(std::string &response) + { + + } + void Daemon_ipc_server::sendtransactions(std::string &response); + void Daemon_ipc_server::get_o_indexes(std::string &response); + + void handle_request(const std::string &request_string, std::string &response) + { + if (check_core_busy()) + { + response = ""; + } + if (request_string == "getblocks") + { + getblocks(response); + } + else if (request_string == "sendrawtransaction") + { + sendrawtransaction(response); + } + else if (request_string == "get_o_indexes") + { + get_o_indexes(response); + } + else + { + response = ""; + } + } + + /*! + * \brief Repeatedly loops processing requests if any. + */ + void Daemon_ipc_server::poll() + { + // Loop until the server is running and poll. + while (m_is_running) { + zmq::message_t request; + // Wait for next request from client + socket->recv(&request); + std::string request_string((char*)request.data(), request.size()); + std::string response; + handle_request(request_string, response); + zmq::message_t reply(response.length()); + memcpy((void*)reply.data(), response.c_str(), response.length()); + socket->send(reply); + } + } +} diff --git a/src/rpc/daemon_ipc_server.h b/src/rpc/daemon_ipc_server.h new file mode 100644 index 000000000..876e5b3ec --- /dev/null +++ b/src/rpc/daemon_ipc_server.h @@ -0,0 +1,97 @@ +// Copyright (c) 2014, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers + +#ifndef JSON_RPC_HTTP_SERVER_H +#define JSON_RPC_HTTP_SERVER_H + +#include "include_base_utils.h" +using namespace epee; + +#include "wallet_rpc_server.h" +#include "common/command_line.h" +#include "cryptonote_core/cryptonote_format_utils.h" +#include "cryptonote_core/account.h" +#include "wallet_rpc_server_commands_defs.h" +#include "misc_language.h" +#include "string_tools.h" +#include "crypto/hash.h" +#include "zmq.hpp" + +namespace IPC +{ + class Daemon_ipc_server + { + boost::thread *server_thread; /*!< Server runs on this thread */ + /*! + * \brief Repeatedly loops processing requests if any. + */ + void poll(); + void handle_request(const std::string &request_string, std::string &response); + bool check_core_busy(); + + void getblocks(std::string &response); + void sendtransactions(std::string &response); + void get_o_indexes(std::string &response); + std::string m_ip; /*!< IP address where its listening */ + std::string m_port; /*!< Port where its listening */ + bool m_is_running; /*!< Whether the server is currently running */ + zmq::socket_t *socket; /*!< 0MQ Socket pointer */ + cryptonote::core *core; /*!< Pointer to the core */ + nodetool::node_server > *p2p; + /*!< Pointer to p2p node server */ + + public: + /** + * \brief Constructor + * \param ip IP address to bind + * \param port Port number to bind + * \param ev_handler Event handler function pointer + */ + Daemon_ipc_server(const std::string &ip, const std::string &port); + + /** + * \brief Destructor + */ + ~Daemon_ipc_server(); + + /*! + * \brief Starts the server + * \return True if start was successful + */ + bool start(); + + /*! + * \brief Stops the server + */ + void stop(); + }; +} + +#endif diff --git a/src/rpc/json_rpc_http_server.cpp b/src/rpc/json_rpc_http_server.cpp index a27dd49d4..86edc7d10 100644 --- a/src/rpc/json_rpc_http_server.cpp +++ b/src/rpc/json_rpc_http_server.cpp @@ -47,6 +47,10 @@ namespace RPC */ bool Json_rpc_http_server::start() { + if (m_is_running) + { + return false; + } m_is_running = true; ns_mgr_init(&mgr, NULL); nc = ns_bind(&mgr, (m_ip + ":" + m_port).c_str(), m_ev_handler); @@ -61,7 +65,7 @@ namespace RPC } /*! - * \brief Repeated loops processing requests if any. + * \brief Repeatedly loops processing requests if any. */ void Json_rpc_http_server::poll() { diff --git a/src/rpc/json_rpc_http_server.h b/src/rpc/json_rpc_http_server.h index b69a2f574..a262f6061 100644 --- a/src/rpc/json_rpc_http_server.h +++ b/src/rpc/json_rpc_http_server.h @@ -28,7 +28,7 @@ namespace RPC struct ns_connection *nc; /*!< Connection pointer */ boost::thread *server_thread; /*!< Server runs on this thread */ /*! - * \brief Repeated loops processing requests if any. + * \brief Repeatedly loops processing requests if any. */ void poll(); std::string m_ip; /*!< IP address where its listening */