CCEngineServer/CMakeProject1/CMakeProject1.cpp
2021-08-31 05:28:39 +02:00

128 lines
4.4 KiB
C++

#include "CCServer.h"
#include "LicenseClient.h"
#include "SystemParamsProvider.h"
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/program_options.hpp>
#include <boost/asio/signal_set.hpp>
#include <iostream>
#include <memory>
#include <chrono>
#include <thread>
namespace po = boost::program_options;
namespace net = boost::asio; // from <boost/asio.hpp>
namespace logging = boost::log;
namespace expr = boost::log::expressions;
namespace keywords = boost::log::keywords;
volatile bool signalCaught = false;
void signalHandler(int signum) {
std::cout << "Interrupt signal (" << signum << ") received.\n";
signalCaught = true;
}
void initLogging(std::optional<std::string> logFolder, bool debug = false)
{
if (logFolder.has_value()) {
logging::add_file_log(
keywords::file_name = (boost::filesystem::path(logFolder.value()) / "CCEngineServer_%N.log").native(),
keywords::open_mode = std::ios_base::app,
keywords::rotation_size = 10 * 1024 * 1024,
keywords::max_files = 10,
keywords::format = (
expr::stream
<< expr::format_date_time<boost::posix_time::ptime>("TimeStamp", "%Y-%m-%d %H:%M:%S")
<< ": <" << logging::trivial::severity
<< "> " << expr::smessage)
);
}
logging::core::get()->set_filter
(
logging::trivial::severity >= ((!debug) ? logging::trivial::info : logging::trivial::debug)
);
logging::add_common_attributes();
}
int main(int argc, const char* argv[])
{
// Declare the supported options.
po::options_description generalOptions("General options");
generalOptions.add_options()
("help", "produce help message")
("bind_ip", po::value<std::string>()->default_value("0.0.0.0"), "ip address to listen for connections")
("port", po::value<uint16_t>()->default_value(8080), "port")
("data_root", po::value<std::string>()->default_value("."), "root directory with data")
("threads", po::value<int>()->default_value(4), "port")
("log_folder", po::value<std::string>(), "folder to store logs")
;
po::options_description debugOptions("Debug options");
debugOptions.add_options()
("debug", "Debugging logs")
;
po::options_description allOptions("Allowed options");
allOptions.add(generalOptions).add(debugOptions);
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, allOptions), vm);
po::notify(vm);
if (vm.count("help")) {
std::cout << generalOptions << "\n";
return 1;
}
std::optional<std::string> logFolder;
if (vm.count("log_folder")) {
logFolder = vm["log_folder"].as<std::string>();
}
initLogging(logFolder, vm.count("debug"));
BOOST_LOG_TRIVIAL(info) << "Starting application...";
BOOST_LOG_TRIVIAL(debug) << "Initializing params provider...";
SystemParamsProvider systemParamsProvider;
LicenseClient licenseClient(systemParamsProvider, "license.dat");
licenseClient.init();
if (!licenseClient.isActivated()) {
const std::string msg = "Application is not activated. Please contact Catalogue of Currencies support.";
BOOST_LOG_TRIVIAL(error) << msg << " Exiting...";
std::cout << msg << std::endl;
std::cout << "Activation request: " << std::endl << licenseClient.buildActivationRequest() << std::endl;
return -2;
}
auto addressToListen = vm["bind_ip"].as<std::string>();
auto portToListen = vm["port"].as<uint16_t>();
auto docRoot = vm["data_root"].as<std::string>();
auto threads = vm["threads"].as<int>();
BOOST_LOG_TRIVIAL(debug) << "Will listen on: " << addressToListen << ":" << portToListen;
BOOST_LOG_TRIVIAL(debug) << "Will serve data from : " << docRoot;
BOOST_LOG_TRIVIAL(debug) << "Number of serving threads : " << threads;
auto ccServer = std::make_unique<CCServer>(addressToListen, portToListen, docRoot, threads);
ccServer->run();
while (!signalCaught)
{
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
BOOST_LOG_TRIVIAL(info) << "Caught signal. Shutting down...";
ccServer->shutdown();
return 0;
}