32#include <boost/thread/mutex.hpp>
33#include <boost/thread/thread.hpp>
44#define EXIT_ON_ERROR(cond) { if (!(cond)) { LOG_PRINT_L0("ERROR: " << #cond); exit(1); } else {} }
48 struct srv_levin_commands_handler :
public test_levin_commands_handler
51 : m_tcp_server(tcp_server)
52 , m_open_close_test_conn_id(boost::uuids::nil_uuid())
59 context.m_closed =
false;
63 boost::unique_lock<boost::mutex> lock(m_open_close_test_mutex);
64 if (!m_open_close_test_conn_id.is_nil())
66 EXIT_ON_ERROR(m_open_close_test_helper->handle_new_connection(context.m_connection_id,
true));
74 boost::unique_lock<boost::mutex> lock(m_open_close_test_mutex);
75 if (context.m_connection_id == m_open_close_test_conn_id)
78 m_open_close_test_conn_id = boost::uuids::nil_uuid();
79 m_open_close_test_helper.reset(0);
97 close_connections(context.m_connection_id);
112 m_new_connection_counter.reset();
113 m_new_connection_counter.inc();
114 m_close_connection_counter.reset();
120 boost::unique_lock<boost::mutex> lock(m_open_close_test_mutex);
121 if (0 == m_open_close_test_helper.get())
123 LOG_PRINT_L0(
"Start open/close test (" << req.open_request_target <<
", " << req.max_opened_conn_count <<
")");
125 m_open_close_test_conn_id = context.m_connection_id;
126 m_open_close_test_helper.reset(
new open_close_test_helper(m_tcp_server, req.open_request_target, req.max_opened_conn_count));
138 m_tcp_server.send_stop_signal();
144 boost::uuids::uuid cmd_conn_id = context.m_connection_id;
149 req2.
data.resize(req.request_size);
155 LOG_PRINT_L0(
"Failed to invoke CMD_DATA_REQUEST. code = " << code);
168 void close_connections(boost::uuids::uuid cmd_conn_id)
170 LOG_PRINT_L0(
"Closing connections. Number of opened connections: " << m_tcp_server.get_config_object().get_connections_count());
193 boost::shared_ptr<boost::asio::deadline_timer> sh_deadline(
new boost::asio::deadline_timer(m_tcp_server.get_io_service(), boost::posix_time::seconds(7)));
194 sh_deadline->async_wait([=](
const boost::system::error_code& ec)
196 boost::shared_ptr<boost::asio::deadline_timer> t = sh_deadline;
199 close_connections(cmd_conn_id);
203 LOG_PRINT_L0(
"ERROR: " << ec.message() <<
':' << ec.value());
212 boost::uuids::uuid m_open_close_test_conn_id;
213 boost::mutex m_open_close_test_mutex;
214 std::unique_ptr<open_close_test_helper> m_open_close_test_helper;
225 size_t thread_count = (std::max)(
min_thread_count, boost::thread::hardware_concurrency() / 2);
231 srv_levin_commands_handler *commands_handler =
new srv_levin_commands_handler(tcp_server);
236 if (!tcp_server.
run_server(thread_count,
true))
void set_handler(levin_commands_handler< t_connection_context > *handler, void(*destroy)(levin_commands_handler< t_connection_context > *)=NULL)
uint64_t m_invoke_timeout
bool init_server(uint32_t port, const std::string address="0.0.0.0", ssl_options_t ssl_options=ssl_support_t::e_ssl_support_autodetect)
t_protocol_handler::config_type & get_config_object()
bool run_server(size_t threads_count, bool wait=true, const boost::thread::attributes &attrs=boost::thread::attributes())
Run the server's io_service loop.
#define END_INVOKE_MAP2()
#define HANDLE_NOTIFY_T2(NOTIFY, func)
#define CHAIN_LEVIN_NOTIFY_MAP2(context_type)
#define CHAIN_LEVIN_INVOKE_MAP2(context_type)
#define BEGIN_INVOKE_MAP2(owner_type)
#define HANDLE_INVOKE_T2(COMMAND, func)
void mlog_configure(const std::string &filename_base, bool console, const std::size_t max_log_file_size=MAX_LOG_FILE_SIZE, const std::size_t max_log_files=MAX_LOG_FILES)
std::string mlog_get_default_log_path(const char *default_filename)
#define CATCH_ENTRY_L0(lacation, return_val)
bool async_invoke_remote_command2(boost::uuids::uuid conn_id, int command, const t_arg &out_struct, t_transport &transport, const callback_t &cb, size_t inv_timeout=LEVIN_DEFAULT_TIMEOUT_PRECONFIGURED)
const std::string srv_port("36231")
const unsigned int min_thread_count
epee::net_utils::boosted_tcp_server< test_levin_protocol_handler > test_tcp_server
#define EXIT_ON_ERROR(cond)
const boost::uuids::uuid m_connection_id
uint64_t new_connection_counter
uint64_t opened_connections_count
std::string to_string() const
uint64_t close_connection_counter
virtual void on_connection_close(test_connection_context &context)
virtual void on_connection_new(test_connection_context &context)