summaryrefslogtreecommitdiffhomepage
path: root/server.cpp
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2020-04-25 18:36:54 +0200
committerRoland Reichwein <mail@reichwein.it>2020-04-25 18:36:54 +0200
commit683d2cb48c4f3620fc3be68ba97b2d3bb5a40e30 (patch)
tree8c0ae6438f6ec706a170eb41f683d6c91dc4a800 /server.cpp
parentb2c34d03399978a3d0838ee7ed92c760e7908721 (diff)
Added statistics
Diffstat (limited to 'server.cpp')
-rw-r--r--server.cpp38
1 files changed, 29 insertions, 9 deletions
diff --git a/server.cpp b/server.cpp
index 6e15466..71f39ac 100644
--- a/server.cpp
+++ b/server.cpp
@@ -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;
+}