diff options
Diffstat (limited to 'server.cpp')
-rw-r--r-- | server.cpp | 38 |
1 files changed, 29 insertions, 9 deletions
@@ -15,6 +15,7 @@ #include <boost/asio/ssl/stream.hpp> #endif #include <boost/asio/dispatch.hpp> +#include <boost/asio/signal_set.hpp> #include <boost/asio/strand.hpp> #include <boost/config.hpp> @@ -28,6 +29,7 @@ #include "http.h" #include "https.h" #include "privileges.h" +#include "statistics.h" namespace beast = boost::beast; // from <boost/beast.hpp> namespace http = beast::http; // from <boost/beast/http.hpp> @@ -37,11 +39,12 @@ using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp> 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<int>(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<std::shared_ptr<Server>> servers; const auto& sockets {config.Sockets()}; for (const auto& socket: sockets) { if (socket.protocol == SocketProtocol::HTTP) { - servers.push_back(std::make_shared<HTTP::Server>(config, ioc, socket, plugins)); + servers.push_back(std::make_shared<HTTP::Server>(config, ioc, socket, plugins, stats)); } else { - servers.push_back(std::make_shared<HTTPS::Server>(config, ioc, socket, plugins)); + servers.push_back(std::make_shared<HTTPS::Server>(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<std::thread> 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; +} |