0MQ basic server in the works

This commit is contained in:
Oran Juice 2014-12-08 22:46:47 +05:30
parent 7f670dc77d
commit 9eea54bfa6
No known key found for this signature in database
GPG Key ID: 71C5AF46CCB28124
4 changed files with 238 additions and 2 deletions

View File

@ -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 <iostream>
namespace IPC
{
Daemon_ipc_server::Daemon_ipc_server(const std::string &ip, const std::string &port,
nodetool::node_server<cryptonote::t_cryptonote_protocol_handler<cryptonote::core> > *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);
}
}
}

View File

@ -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<cryptonote::t_cryptonote_protocol_handler<cryptonote::core> > *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

View File

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

View File

@ -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 */