From 683d2cb48c4f3620fc3be68ba97b2d3bb5a40e30 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sat, 25 Apr 2020 18:36:54 +0200 Subject: Added statistics --- server.cpp | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) (limited to 'server.cpp') diff --git a/server.cpp b/server.cpp index 6e15466..71f39ac 100644 --- a/server.cpp +++ b/server.cpp @@ -15,6 +15,7 @@ #include #endif #include +#include #include #include @@ -28,6 +29,7 @@ #include "http.h" #include "https.h" #include "privileges.h" +#include "statistics.h" namespace beast = boost::beast; // from namespace http = beast::http; // from @@ -37,11 +39,12 @@ using tcp = boost::asio::ip::tcp; // from const std::string Server::VersionString{ "Reichwein.IT Webserver "s + std::string{VERSION} }; -Server::Server(Config& config, boost::asio::io_context& ioc, const Socket& socket, plugins_container_type& plugins) +Server::Server(Config& config, boost::asio::io_context& ioc, const Socket& socket, plugins_container_type& plugins, Statistics& statistics) : m_config(config) , m_ioc(ioc) , m_socket(socket) , m_plugins(plugins) + , m_statistics(statistics) { } @@ -51,18 +54,26 @@ Server::~Server() int run_server(Config& config, plugins_container_type& plugins) { + Statistics stats; + auto const threads = std::max(1, config.Threads()); boost::asio::io_context ioc{threads}; + boost::asio::signal_set signals(ioc, SIGINT, SIGTERM); + signals.async_wait([&](const boost::system::error_code& error, int signal_number){ + std::cout << "Terminating via signal " << signal_number << std::endl; + ioc.stop(); + }); + std::vector> servers; const auto& sockets {config.Sockets()}; for (const auto& socket: sockets) { if (socket.protocol == SocketProtocol::HTTP) { - servers.push_back(std::make_shared(config, ioc, socket, plugins)); + servers.push_back(std::make_shared(config, ioc, socket, plugins, stats)); } else { - servers.push_back(std::make_shared(config, ioc, socket, plugins)); + servers.push_back(std::make_shared(config, ioc, socket, plugins, stats)); } servers.back()->start(); } @@ -73,14 +84,19 @@ int run_server(Config& config, plugins_container_type& plugins) // Run the I/O service on the requested number of threads std::vector v; v.reserve(threads - 1); - for(auto i = threads - 1; i > 0; --i) - v.emplace_back( - [&ioc] - { - ioc.run(); - }); + for (auto i = threads - 1; i > 0; --i) { + v.emplace_back( + [&ioc] + { + ioc.run(); + }); + } ioc.run(); + for (auto& t: v) { + t.join(); + } + return EXIT_SUCCESS; } @@ -107,3 +123,7 @@ plugin_type Server::GetPlugin(const std::string& name) } } +Statistics& Server::GetStatistics() +{ + return m_statistics; +} -- cgit v1.2.3