mirror of
https://codeberg.org/anoncontributorxmr/monero.git
synced 2024-11-23 10:37:37 -07:00
Merge pull request #1597
4d6976fb
undelete epee tests.cpp (moneroexamples)0fd2508b
removing some unsed epee files (moneroexamples)
This commit is contained in:
commit
ab9c287d92
@ -1,217 +0,0 @@
|
|||||||
// Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
|
|
||||||
// All rights reserved.
|
|
||||||
//
|
|
||||||
// Redistribution and use in source and binary forms, with or without
|
|
||||||
// modification, are permitted provided that the following conditions are met:
|
|
||||||
// * Redistributions of source code must retain the above copyright
|
|
||||||
// notice, this list of conditions and the following disclaimer.
|
|
||||||
// * 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.
|
|
||||||
// * Neither the name of the Andrey N. Sabelnikov 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 OWNER 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.
|
|
||||||
//
|
|
||||||
|
|
||||||
#include "stdafx.h"
|
|
||||||
#include "console_handler.h"
|
|
||||||
#include "demo_http_server.h"
|
|
||||||
#include "net/http_client.h"
|
|
||||||
#include "storages/http_abstract_invoke.h"
|
|
||||||
|
|
||||||
|
|
||||||
template<class t_request, class t_response>
|
|
||||||
bool communicate(const std::string url, t_request& req, t_response& rsp, const std::string& ip, const std::string& port, bool use_json, bool use_jrpc = false)
|
|
||||||
{
|
|
||||||
epee::net_utils::http::http_simple_client http_client;
|
|
||||||
bool r = http_client.connect(ip, port, 1000);
|
|
||||||
CHECK_AND_ASSERT_MES(r, false, "failed to connect");
|
|
||||||
if(use_json)
|
|
||||||
{
|
|
||||||
if(use_jrpc)
|
|
||||||
{
|
|
||||||
epee::json_rpc::request<t_request> req_t = AUTO_VAL_INIT(req_t);
|
|
||||||
req_t.jsonrpc = "2.0";
|
|
||||||
req_t.id = epee::serialization::storage_entry(10);
|
|
||||||
req_t.method = "command_example_1";
|
|
||||||
req_t.params = req;
|
|
||||||
epee::json_rpc::response<t_response, std::string> resp_t = AUTO_VAL_INIT(resp_t);
|
|
||||||
if(!epee::net_utils::invoke_http_json_remote_command2("/request_json_rpc", req_t, resp_t, http_client))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
rsp = resp_t.result;
|
|
||||||
return true;
|
|
||||||
}else
|
|
||||||
return epee::net_utils::invoke_http_json_remote_command2(url, req, rsp, http_client);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
return epee::net_utils::invoke_http_bin_remote_command2(url, req, rsp, http_client);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
|
||||||
{
|
|
||||||
TRY_ENTRY();
|
|
||||||
string_tools::set_module_name_and_folder(argv[0]);
|
|
||||||
|
|
||||||
//set up logging options
|
|
||||||
log_space::get_set_log_detalisation_level(true, LOG_LEVEL_2);
|
|
||||||
log_space::log_singletone::add_logger(LOGGER_CONSOLE, NULL, NULL);
|
|
||||||
log_space::log_singletone::add_logger(LOGGER_FILE,
|
|
||||||
log_space::log_singletone::get_default_log_file().c_str(),
|
|
||||||
log_space::log_singletone::get_default_log_folder().c_str());
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
LOG_PRINT("Demo server starting ...", LOG_LEVEL_0);
|
|
||||||
|
|
||||||
|
|
||||||
demo::demo_http_server srv;
|
|
||||||
|
|
||||||
start_default_console(&srv, "#");
|
|
||||||
|
|
||||||
std::string bind_param = "0.0.0.0";
|
|
||||||
std::string port = "83";
|
|
||||||
|
|
||||||
if(!srv.init(port, bind_param))
|
|
||||||
{
|
|
||||||
LOG_ERROR("Failed to initialize srv!");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
//log loop
|
|
||||||
srv.run();
|
|
||||||
size_t count = 0;
|
|
||||||
while (!srv.is_stop())
|
|
||||||
{
|
|
||||||
|
|
||||||
demo::COMMAND_EXAMPLE_1::request req;
|
|
||||||
req.sub = demo::get_test_data();
|
|
||||||
demo::COMMAND_EXAMPLE_1::response rsp;
|
|
||||||
bool r = false;
|
|
||||||
if(count%2)
|
|
||||||
{//invoke json
|
|
||||||
r = communicate("/request_api_json_1", req, rsp, "127.0.0.1", port, true, true);
|
|
||||||
}else{
|
|
||||||
r = communicate("/request_api_bin_1", req, rsp, "127.0.0.1", port, false);
|
|
||||||
}
|
|
||||||
CHECK_AND_ASSERT_MES(r, false, "failed to invoke http request");
|
|
||||||
CHECK_AND_ASSERT_MES(rsp.m_success, false, "wrong response");
|
|
||||||
CHECK_AND_ASSERT_MES(rsp.subs.size()==1, false, "wrong response");
|
|
||||||
CHECK_AND_ASSERT_MES(rsp.subs.front() == demo::get_test_data(), false, "wrong response");
|
|
||||||
//misc_utils::sleep_no_w(1000);
|
|
||||||
++count;
|
|
||||||
}
|
|
||||||
bool r = srv.wait_stop();
|
|
||||||
CHECK_AND_ASSERT_MES(r, 1, "failed to wait server stop");
|
|
||||||
srv.deinit();
|
|
||||||
|
|
||||||
LOG_PRINT("Demo server stoped.", LOG_LEVEL_0);
|
|
||||||
return 1;
|
|
||||||
|
|
||||||
CATCH_ENTRY_L0("main", 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/************************************************************************/
|
|
||||||
/* */
|
|
||||||
/************************************************************************/
|
|
||||||
namespace demo
|
|
||||||
{
|
|
||||||
bool demo_http_server::init(const std::string& bind_port, const std::string& bind_ip)
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
//set self as callback handler
|
|
||||||
m_net_server.get_config_object().m_phandler = this;
|
|
||||||
|
|
||||||
//here set folder for hosting reqests
|
|
||||||
m_net_server.get_config_object().m_folder = "";
|
|
||||||
|
|
||||||
LOG_PRINT_L0("Binding on " << bind_ip << ":" << bind_port);
|
|
||||||
return m_net_server.init_server(bind_port, bind_ip);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool demo_http_server::run()
|
|
||||||
{
|
|
||||||
m_stop = false;
|
|
||||||
//here you can set worker threads count
|
|
||||||
int thrds_count = 4;
|
|
||||||
|
|
||||||
//go to loop
|
|
||||||
LOG_PRINT("Run net_service loop( " << thrds_count << " threads)...", LOG_LEVEL_0);
|
|
||||||
if(!m_net_server.run_server(thrds_count, false))
|
|
||||||
{
|
|
||||||
LOG_ERROR("Failed to run net tcp server!");
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool demo_http_server::deinit()
|
|
||||||
{
|
|
||||||
return m_net_server.deinit_server();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool demo_http_server::send_stop_signal()
|
|
||||||
{
|
|
||||||
m_stop = true;
|
|
||||||
m_net_server.send_stop_signal();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool demo_http_server::on_requestr_uri_1(const net_utils::http::http_request_info& query_info,
|
|
||||||
net_utils::http::http_response_info& response,
|
|
||||||
const net_utils::connection_context_base& m_conn_context)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
bool demo_http_server::on_requestr_uri_2(const net_utils::http::http_request_info& query_info,
|
|
||||||
net_utils::http::http_response_info& response,
|
|
||||||
const net_utils::connection_context_base& m_conn_context)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
bool demo_http_server::on_hosting_request( const net_utils::http::http_request_info& query_info,
|
|
||||||
net_utils::http::http_response_info& response,
|
|
||||||
const net_utils::connection_context_base& m_conn_context)
|
|
||||||
{
|
|
||||||
//read file from filesystem here
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool demo_http_server::on_request_api_1(const COMMAND_EXAMPLE_1::request& req, COMMAND_EXAMPLE_1::response& res, connection_context& ctxt)
|
|
||||||
{
|
|
||||||
CHECK_AND_ASSERT_MES(req.sub == demo::get_test_data(), false, "wrong request");
|
|
||||||
res.m_success = true;
|
|
||||||
res.subs.push_back(req.sub);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool demo_http_server::on_request_api_1_with_error(const COMMAND_EXAMPLE_1::request& req, COMMAND_EXAMPLE_1::response& res, epee::json_rpc::error& error_resp, connection_context& ctxt)
|
|
||||||
{
|
|
||||||
error_resp.code = 232432;
|
|
||||||
error_resp.message = "bla bla bla";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool demo_http_server::on_request_api_2(const COMMAND_EXAMPLE_2::request& req, COMMAND_EXAMPLE_2::response& res, connection_context& ctxt)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,103 +0,0 @@
|
|||||||
// Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
|
|
||||||
// All rights reserved.
|
|
||||||
//
|
|
||||||
// Redistribution and use in source and binary forms, with or without
|
|
||||||
// modification, are permitted provided that the following conditions are met:
|
|
||||||
// * Redistributions of source code must retain the above copyright
|
|
||||||
// notice, this list of conditions and the following disclaimer.
|
|
||||||
// * 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.
|
|
||||||
// * Neither the name of the Andrey N. Sabelnikov 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 OWNER 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.
|
|
||||||
//
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
#include <boost/thread.hpp>
|
|
||||||
#include <boost/bind.hpp>
|
|
||||||
|
|
||||||
#include "net/http_server_cp2.h"
|
|
||||||
#include "transport_defs.h"
|
|
||||||
#include "net/http_server_handlers_map2.h"
|
|
||||||
|
|
||||||
using namespace epee;
|
|
||||||
|
|
||||||
namespace demo
|
|
||||||
{
|
|
||||||
|
|
||||||
class demo_http_server: public net_utils::http::i_http_server_handler<epee::net_utils::connection_context_base>
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
typedef epee::net_utils::connection_context_base connection_context;
|
|
||||||
|
|
||||||
demo_http_server():m_stop(false){}
|
|
||||||
bool run();
|
|
||||||
bool init(const std::string& bind_port = "11112", const std::string& bind_ip = "0.0.0.0");
|
|
||||||
bool deinit();
|
|
||||||
bool send_stop_signal();
|
|
||||||
bool is_stop(){return m_stop;}
|
|
||||||
bool wait_stop(){return m_net_server.timed_wait_server_stop(100000);}
|
|
||||||
private:
|
|
||||||
|
|
||||||
|
|
||||||
CHAIN_HTTP_TO_MAP2(connection_context); //forward http requests to uri map
|
|
||||||
|
|
||||||
BEGIN_URI_MAP2()
|
|
||||||
MAP_URI2("/requestr_uri_1", on_requestr_uri_1)
|
|
||||||
MAP_URI2("/requestr_uri_2", on_requestr_uri_1)
|
|
||||||
//MAP_URI_AUTO_XML2("/request_api_xml_1", on_request_api_1, COMMAND_EXAMPLE_1)
|
|
||||||
//MAP_URI_AUTO_XML2("/request_api_xml_2", on_request_api_2, COMMAND_EXAMPLE_2)
|
|
||||||
MAP_URI_AUTO_JON2("/request_api_json_1", on_request_api_1, COMMAND_EXAMPLE_1)
|
|
||||||
MAP_URI_AUTO_JON2("/request_api_json_2", on_request_api_2, COMMAND_EXAMPLE_2)
|
|
||||||
MAP_URI_AUTO_BIN2("/request_api_bin_1", on_request_api_1, COMMAND_EXAMPLE_1)
|
|
||||||
MAP_URI_AUTO_BIN2("/request_api_bin_2", on_request_api_2, COMMAND_EXAMPLE_2)
|
|
||||||
BEGIN_JSON_RPC_MAP("/request_json_rpc")
|
|
||||||
MAP_JON_RPC("command_example_1", on_request_api_1, COMMAND_EXAMPLE_1)
|
|
||||||
MAP_JON_RPC("command_example_2", on_request_api_2, COMMAND_EXAMPLE_2)
|
|
||||||
MAP_JON_RPC_WE("command_example_1_we", on_request_api_1_with_error, COMMAND_EXAMPLE_1)
|
|
||||||
END_JSON_RPC_MAP()
|
|
||||||
CHAIN_URI_MAP2(on_hosting_request)
|
|
||||||
END_URI_MAP2()
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
bool on_requestr_uri_1(const net_utils::http::http_request_info& query_info,
|
|
||||||
net_utils::http::http_response_info& response,
|
|
||||||
const net_utils::connection_context_base& m_conn_context);
|
|
||||||
|
|
||||||
|
|
||||||
bool on_requestr_uri_2(const net_utils::http::http_request_info& query_info,
|
|
||||||
net_utils::http::http_response_info& response,
|
|
||||||
const net_utils::connection_context_base& m_conn_context);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
bool on_hosting_request( const net_utils::http::http_request_info& query_info,
|
|
||||||
net_utils::http::http_response_info& response,
|
|
||||||
const net_utils::connection_context_base& m_conn_context);
|
|
||||||
|
|
||||||
bool on_request_api_1(const COMMAND_EXAMPLE_1::request& req, COMMAND_EXAMPLE_1::response& res, connection_context& ctxt);
|
|
||||||
bool on_request_api_2(const COMMAND_EXAMPLE_2::request& req, COMMAND_EXAMPLE_2::response& res, connection_context& ctxt);
|
|
||||||
|
|
||||||
bool on_request_api_1_with_error(const COMMAND_EXAMPLE_1::request& req, COMMAND_EXAMPLE_1::response& res, epee::json_rpc::error& error_resp, connection_context& ctxt);
|
|
||||||
|
|
||||||
net_utils::boosted_http_server_custum_handling m_net_server;
|
|
||||||
std::atomic<bool> m_stop;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
@ -1,200 +0,0 @@
|
|||||||
// Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
|
|
||||||
// All rights reserved.
|
|
||||||
//
|
|
||||||
// Redistribution and use in source and binary forms, with or without
|
|
||||||
// modification, are permitted provided that the following conditions are met:
|
|
||||||
// * Redistributions of source code must retain the above copyright
|
|
||||||
// notice, this list of conditions and the following disclaimer.
|
|
||||||
// * 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.
|
|
||||||
// * Neither the name of the Andrey N. Sabelnikov 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 OWNER 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.
|
|
||||||
//
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#include "stdafx.h"
|
|
||||||
#include "demo_levin_server.h"
|
|
||||||
#include "console_handler.h"
|
|
||||||
|
|
||||||
|
|
||||||
template<class t_request>
|
|
||||||
bool communicate(net_utils::boosted_levin_async_server& transport, int id, t_request& req, const std::string& ip, const std::string& port, bool use_async)
|
|
||||||
{
|
|
||||||
if(use_async)
|
|
||||||
{
|
|
||||||
//IMPORTANT: do not pass local parameters from stack by reference! connect_async returns immediately, and callback will call in any thread later
|
|
||||||
transport.connect_async(ip, port, 10000, [&transport, id, req, ip, port](net_utils::connection_context_base& ctx, const boost::system::error_code& ec_)
|
|
||||||
{
|
|
||||||
if(!!ec_)
|
|
||||||
{
|
|
||||||
LOG_ERROR("Failed to connect to " << ip << ":" << port);
|
|
||||||
}else
|
|
||||||
{//connected ok!
|
|
||||||
|
|
||||||
epee::net_utils::async_invoke_remote_command2<demo::COMMAND_EXAMPLE_1::response>(ctx.m_connection_id, id, req, transport.get_config_object(), [&transport, ip, port](int res_code, demo::COMMAND_EXAMPLE_1::response& rsp, net_utils::connection_context_base& ctx)
|
|
||||||
{
|
|
||||||
if(res_code < 0)
|
|
||||||
{
|
|
||||||
LOG_ERROR("Failed to invoke to " << ip << ":" << port);
|
|
||||||
}else
|
|
||||||
{//invoked ok
|
|
||||||
CHECK_AND_ASSERT_MES(rsp.m_success, false, "wrong response");
|
|
||||||
CHECK_AND_ASSERT_MES(rsp.subs.size()==1, false, "wrong response");
|
|
||||||
CHECK_AND_ASSERT_MES(rsp.subs.front() == demo::get_test_data(), false, "wrong response");
|
|
||||||
LOG_PRINT_GREEN("Client COMMAND_EXAMPLE_1 async invoked ok", LOG_LEVEL_0);
|
|
||||||
}
|
|
||||||
transport.get_config_object().close(ctx.m_connection_id);
|
|
||||||
return true;
|
|
||||||
});
|
|
||||||
LOG_PRINT_GREEN("Client COMMAND_EXAMPLE_1 async invoke requested", LOG_LEVEL_0);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}else
|
|
||||||
{
|
|
||||||
net_utils::connection_context_base ctx = AUTO_VAL_INIT(ctx);
|
|
||||||
bool r = transport.connect(ip, port, 10000, ctx);
|
|
||||||
CHECK_AND_ASSERT_MES(r, false, "failed to connect to " << ip << ":" << port);
|
|
||||||
demo::COMMAND_EXAMPLE_1::response rsp = AUTO_VAL_INIT(rsp);
|
|
||||||
LOG_PRINT_GREEN("Client COMMAND_EXAMPLE_1 sync invoke requested", LOG_LEVEL_0);
|
|
||||||
r = epee::net_utils::invoke_remote_command2(ctx.m_connection_id, id, req, rsp, transport.get_config_object());
|
|
||||||
CHECK_AND_ASSERT_MES(r, false, "failed to invoke levin request");
|
|
||||||
CHECK_AND_ASSERT_MES(rsp.m_success, false, "wrong response");
|
|
||||||
CHECK_AND_ASSERT_MES(rsp.subs.size()==1, false, "wrong response");
|
|
||||||
CHECK_AND_ASSERT_MES(rsp.subs.front() == demo::get_test_data(), false, "wrong response");
|
|
||||||
transport.get_config_object().close(ctx.m_connection_id);
|
|
||||||
LOG_PRINT_GREEN("Client COMMAND_EXAMPLE_1 sync invoked ok", LOG_LEVEL_0);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
|
||||||
{
|
|
||||||
TRY_ENTRY();
|
|
||||||
string_tools::set_module_name_and_folder(argv[0]);
|
|
||||||
|
|
||||||
//set up logging options
|
|
||||||
log_space::get_set_log_detalisation_level(true, LOG_LEVEL_2);
|
|
||||||
log_space::log_singletone::add_logger(LOGGER_CONSOLE, NULL, NULL);
|
|
||||||
log_space::log_singletone::add_logger(LOGGER_FILE,
|
|
||||||
log_space::log_singletone::get_default_log_file().c_str(),
|
|
||||||
log_space::log_singletone::get_default_log_folder().c_str());
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
LOG_PRINT("Demo server starting ...", LOG_LEVEL_0);
|
|
||||||
|
|
||||||
|
|
||||||
demo::demo_levin_server srv;
|
|
||||||
|
|
||||||
start_default_console(&srv, "#");
|
|
||||||
|
|
||||||
std::string bind_param = "0.0.0.0";
|
|
||||||
std::string port = "12345";
|
|
||||||
|
|
||||||
if(!srv.init(port, bind_param))
|
|
||||||
{
|
|
||||||
LOG_ERROR("Failed to initialize srv!");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
srv.run();
|
|
||||||
|
|
||||||
size_t c = 1;
|
|
||||||
while (!srv.is_stop())
|
|
||||||
{
|
|
||||||
|
|
||||||
demo::COMMAND_EXAMPLE_1::request req;
|
|
||||||
req.sub = demo::get_test_data();
|
|
||||||
bool r = communicate(srv.get_server(), demo::COMMAND_EXAMPLE_1::ID, req, "127.0.0.1", port, (c%2 == 0));
|
|
||||||
misc_utils::sleep_no_w(1000);
|
|
||||||
++c;
|
|
||||||
}
|
|
||||||
bool r = srv.wait_stop();
|
|
||||||
CHECK_AND_ASSERT_MES(r, 1, "failed to wait server stop");
|
|
||||||
|
|
||||||
|
|
||||||
srv.deinit();
|
|
||||||
|
|
||||||
LOG_PRINT("Demo server stoped.", LOG_LEVEL_0);
|
|
||||||
return 1;
|
|
||||||
|
|
||||||
CATCH_ENTRY_L0("main", 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/************************************************************************/
|
|
||||||
/* */
|
|
||||||
/************************************************************************/
|
|
||||||
namespace demo
|
|
||||||
{
|
|
||||||
bool demo_levin_server::init(const std::string& bind_port, const std::string& bind_ip)
|
|
||||||
{
|
|
||||||
m_net_server.get_config_object().m_pcommands_handler = this;
|
|
||||||
LOG_PRINT_L0("Binding on " << bind_ip << ":" << bind_port);
|
|
||||||
return m_net_server.init_server(bind_port, bind_ip);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool demo_levin_server::run()
|
|
||||||
{
|
|
||||||
m_stop = false;
|
|
||||||
//here you can set worker threads count
|
|
||||||
int thrds_count = 4;
|
|
||||||
m_net_server.get_config_object().m_invoke_timeout = 10000;
|
|
||||||
m_net_server.get_config_object().m_pcommands_handler = this;
|
|
||||||
|
|
||||||
//go to loop
|
|
||||||
LOG_PRINT("Run net_service loop( " << thrds_count << " threads)...", LOG_LEVEL_0);
|
|
||||||
if(!m_net_server.run_server(thrds_count, false))
|
|
||||||
{
|
|
||||||
LOG_ERROR("Failed to run net tcp server!");
|
|
||||||
}
|
|
||||||
|
|
||||||
LOG_PRINT("net_service loop stopped.", LOG_LEVEL_0);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool demo_levin_server::deinit()
|
|
||||||
{
|
|
||||||
return m_net_server.deinit_server();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool demo_levin_server::send_stop_signal()
|
|
||||||
{
|
|
||||||
m_net_server.send_stop_signal();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
int demo_levin_server::handle_command_1(int command, COMMAND_EXAMPLE_1::request& arg, COMMAND_EXAMPLE_1::response& rsp, const net_utils::connection_context_base& context)
|
|
||||||
{
|
|
||||||
CHECK_AND_ASSERT_MES(arg.sub == demo::get_test_data(), false, "wrong request");
|
|
||||||
rsp.m_success = true;
|
|
||||||
rsp.subs.push_back(arg.sub);
|
|
||||||
LOG_PRINT_GREEN("Server COMMAND_EXAMPLE_1 ok", LOG_LEVEL_0);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
int demo_levin_server::handle_command_2(int command, COMMAND_EXAMPLE_2::request& arg, COMMAND_EXAMPLE_2::response& rsp, const net_utils::connection_context_base& context)
|
|
||||||
{
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
int demo_levin_server::handle_notify_1(int command, COMMAND_EXAMPLE_1::request& arg, const net_utils::connection_context_base& context)
|
|
||||||
{
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
int demo_levin_server::handle_notify_2(int command, COMMAND_EXAMPLE_2::request& arg, const net_utils::connection_context_base& context)
|
|
||||||
{
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,76 +0,0 @@
|
|||||||
// Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
|
|
||||||
// All rights reserved.
|
|
||||||
//
|
|
||||||
// Redistribution and use in source and binary forms, with or without
|
|
||||||
// modification, are permitted provided that the following conditions are met:
|
|
||||||
// * Redistributions of source code must retain the above copyright
|
|
||||||
// notice, this list of conditions and the following disclaimer.
|
|
||||||
// * 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.
|
|
||||||
// * Neither the name of the Andrey N. Sabelnikov 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 OWNER 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.
|
|
||||||
//
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
#include <boost/thread.hpp>
|
|
||||||
#include <boost/bind.hpp>
|
|
||||||
|
|
||||||
#include "net/levin_server_cp2.h"
|
|
||||||
#include "transport_defs.h"
|
|
||||||
#include "storages/levin_abstract_invoke2.h"
|
|
||||||
|
|
||||||
using namespace epee;
|
|
||||||
|
|
||||||
namespace demo
|
|
||||||
{
|
|
||||||
|
|
||||||
class demo_levin_server: public levin::levin_commands_handler<>
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
bool run();
|
|
||||||
bool init(const std::string& bind_port = "11112", const std::string& bind_ip = "0.0.0.0");
|
|
||||||
bool deinit();
|
|
||||||
bool send_stop_signal();
|
|
||||||
bool is_stop(){return m_stop;}
|
|
||||||
bool wait_stop(){return m_net_server.timed_wait_server_stop(100000);}
|
|
||||||
net_utils::boosted_levin_async_server& get_server(){return m_net_server;}
|
|
||||||
private:
|
|
||||||
|
|
||||||
|
|
||||||
CHAIN_LEVIN_INVOKE_MAP(); //move levin_commands_handler interface invoke(...) callbacks into invoke map
|
|
||||||
CHAIN_LEVIN_NOTIFY_STUB(); //move levin_commands_handler interface notify(...) callbacks into nothing
|
|
||||||
|
|
||||||
BEGIN_INVOKE_MAP2(demo_levin_server)
|
|
||||||
HANDLE_INVOKE_T2(COMMAND_EXAMPLE_1, &demo_levin_server::handle_command_1)
|
|
||||||
HANDLE_INVOKE_T2(COMMAND_EXAMPLE_2, &demo_levin_server::handle_command_2)
|
|
||||||
HANDLE_NOTIFY_T2(COMMAND_EXAMPLE_1, &demo_levin_server::handle_notify_1)
|
|
||||||
HANDLE_NOTIFY_T2(COMMAND_EXAMPLE_2, &demo_levin_server::handle_notify_2)
|
|
||||||
END_INVOKE_MAP2()
|
|
||||||
|
|
||||||
//----------------- commands handlers ----------------------------------------------
|
|
||||||
int handle_command_1(int command, COMMAND_EXAMPLE_1::request& arg, COMMAND_EXAMPLE_1::response& rsp, const net_utils::connection_context_base& context);
|
|
||||||
int handle_command_2(int command, COMMAND_EXAMPLE_2::request& arg, COMMAND_EXAMPLE_2::response& rsp, const net_utils::connection_context_base& context);
|
|
||||||
int handle_notify_1(int command, COMMAND_EXAMPLE_1::request& arg, const net_utils::connection_context_base& context);
|
|
||||||
int handle_notify_2(int command, COMMAND_EXAMPLE_2::request& arg, const net_utils::connection_context_base& context);
|
|
||||||
//----------------------------------------------------------------------------------
|
|
||||||
net_utils::boosted_levin_async_server m_net_server;
|
|
||||||
std::atomic<bool> m_stop;
|
|
||||||
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
@ -1,132 +0,0 @@
|
|||||||
// Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
|
|
||||||
// All rights reserved.
|
|
||||||
//
|
|
||||||
// Redistribution and use in source and binary forms, with or without
|
|
||||||
// modification, are permitted provided that the following conditions are met:
|
|
||||||
// * Redistributions of source code must retain the above copyright
|
|
||||||
// notice, this list of conditions and the following disclaimer.
|
|
||||||
// * 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.
|
|
||||||
// * Neither the name of the Andrey N. Sabelnikov 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 OWNER 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.
|
|
||||||
//
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "inmemtoxml.h"
|
|
||||||
|
|
||||||
//#include "levin/levin_server.h"
|
|
||||||
|
|
||||||
namespace epee
|
|
||||||
{
|
|
||||||
|
|
||||||
class activity_printer_base
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
activity_printer_base(){}
|
|
||||||
virtual ~activity_printer_base(){}
|
|
||||||
};
|
|
||||||
|
|
||||||
template<class A>
|
|
||||||
class notify_activity_printer: public activity_printer_base
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
notify_activity_printer(int level, A& arg, bool is_notify_mode = true):m_ref_arg(arg), m_level(level), m_is_notify_mode(is_notify_mode)
|
|
||||||
{
|
|
||||||
m_command_name = typeid(m_ref_arg).name();
|
|
||||||
m_command_name.erase(0, 7);
|
|
||||||
m_command_name.erase(m_command_name.size()-10, m_command_name.size()-1);
|
|
||||||
if(level == log_space::get_set_log_detalisation_level())
|
|
||||||
{
|
|
||||||
LOG_PRINT(m_command_name, level);
|
|
||||||
}
|
|
||||||
else if(level+1 == log_space::get_set_log_detalisation_level())
|
|
||||||
{
|
|
||||||
LOG_PRINT(" -->>" << m_command_name, level);
|
|
||||||
}
|
|
||||||
else if(level+2 == log_space::get_set_log_detalisation_level())
|
|
||||||
{
|
|
||||||
LOG_PRINT(" -->>" << m_command_name << "\n" << StorageNamed::xml::get_t_as_xml(m_ref_arg), level);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual ~notify_activity_printer()
|
|
||||||
{
|
|
||||||
if(m_is_notify_mode)
|
|
||||||
{
|
|
||||||
if(m_level+1 == log_space::get_set_log_detalisation_level())
|
|
||||||
{
|
|
||||||
LOG_PRINT(" <<--" << m_command_name, m_level);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
protected:
|
|
||||||
std::string m_command_name;
|
|
||||||
A& m_ref_arg;
|
|
||||||
int m_level;
|
|
||||||
bool m_is_notify_mode;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<class A, class R>
|
|
||||||
class command_activity_printer: public notify_activity_printer<A>
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
command_activity_printer(int level, A& arg, R& rsp):notify_activity_printer(level, arg, false), m_ref_rsp(rsp)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual ~command_activity_printer()
|
|
||||||
{
|
|
||||||
if(m_level+1 == log_space::get_set_log_detalisation_level())
|
|
||||||
{
|
|
||||||
LOG_PRINT(" <<--" << m_command_name, m_level);
|
|
||||||
}
|
|
||||||
else if(m_level+2 == log_space::get_set_log_detalisation_level())
|
|
||||||
{
|
|
||||||
LOG_PRINT(" <<--" << m_command_name << "\n" << StorageNamed::trace_as_xml(m_ref_rsp), m_level);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
private:
|
|
||||||
R& m_ref_rsp;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<class A, class R>
|
|
||||||
activity_printer_base* create_activity_printer(int level, A& arg, R& rsp)
|
|
||||||
{
|
|
||||||
return new command_activity_printer<A, R>(level, arg, rsp);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class A>
|
|
||||||
activity_printer_base* create_activity_printer(int level, A& arg)
|
|
||||||
{
|
|
||||||
return new notify_activity_printer<A>(level, arg);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#define PRINT_COMMAND_ACTIVITY(level) boost::shared_ptr<activity_printer_base> local_activity_printer(create_activity_printer(level, in_struct, out_struct));
|
|
||||||
#define PRINT_NOTIFY_ACTIVITY(level) boost::shared_ptr<activity_printer_base> local_activity_printer(create_activity_printer(level, in_struct));
|
|
||||||
|
|
||||||
#define PRINT_ACTIVITY(level) \
|
|
||||||
{std::string some_str = typeid(in_struct).name(); \
|
|
||||||
some_str.erase(0, 7); \
|
|
||||||
some_str.erase(some_str.size()-10, some_str.size()-1); \
|
|
||||||
LOG_PRINT(some_str, level);}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user