CCEngine/CCEngine/CCEngine.cpp
2018-03-18 12:36:11 +01:00

158 lines
4.6 KiB
C++

#include "SystemParamsProvider_win.h"
#include "HTTPClient.h"
#include "LicenseClient.h"
#include "CCServer.h"
#include "ModuleManager.h"
#include "JSONModuleDatabase.h"
#include <boost/filesystem.hpp>
#include <iostream>
//------------------------------------------------------------------------------
int main(int argc, char* argv[])
{
auto appDir = boost::filesystem::system_complete(argv[0]).parent_path();
SystemParamsProvider_win idProvider;
HTTPClient httpClient;
LicenseClient licenseClient(idProvider, (appDir / "license.dat").string());
licenseClient.init();
if (!licenseClient.isActivated())
{
try
{
if (!licenseClient.tryPreactivate(httpClient))
{
std::string licenseKey;
std::cout << "Your installation is not yet activated." << std::endl <<
"Please enter license key: ";
std::getline(std::cin, licenseKey);
auto validLicenseKey = validateLicenseKey(licenseKey);
if (!validLicenseKey)
{
throw std::runtime_error("Invalid license key. Please try again.");
}
else if (!licenseClient.activate(httpClient, validLicenseKey.value()))
{
throw std::runtime_error("Activation failed. Please try again.");
}
}
}
catch (CouldNotConnectException&)
{
std::cerr << "Error: The system could not be activated since your system seems offline. Please check your internet connection!" << std::endl;
throw;
}
}
JSONModuleDatabase moduleDatabase((appDir / ".inst").string());
auto currentModules = moduleDatabase.listModules();
std::vector<ModuleUpdate> moduleUpdates;
try
{
moduleUpdates = licenseClient.checkForUpdates(httpClient, currentModules);
}
catch (CouldNotConnectException&)
{
std::cout << "Warning: Could not check for udates since your system looks offlince" << std::endl;
}
bool restartRequired = false;
if (!moduleUpdates.empty())
{
char what;
std::cout << "Updates available. Do you want to update now? [yn]: ";
std::cin >> what;
if (what == 'y')
{
ModuleManager moduleManager(appDir.string(), moduleDatabase, httpClient);
for (const auto& update : moduleUpdates)
{
restartRequired = restartRequired || (update.flag & static_cast<uint32_t>(ModuleUpdateFlags::restartRequired));
moduleManager.applyUpdate(update.moduleId, update);
}
}
}
if (restartRequired)
{
std::cout << "Restart required" << std::endl;
return 0;
}
auto docRoot = appDir / "data";
CCServer server("127.0.0.1", 8080, docRoot.string(), 4);
server.run();
char c = 0;
while (c != 'q')
{
std::cin >> c;
}
server.shutdown();
/* if (!licenseManager->isActivated())
{
activate(systemId);
}
if (!licenseManager->checkActivation(systemId))
{
throw std::runtime_error("Integrity error. Please contact support.");
} */
// Check command line arguments.
/*
if (argc != 5)
{
std::cerr <<
"Usage: ccengine <address> <port> <doc_root> <threads>\n" <<
"Example:\n" <<
" advanced-server 0.0.0.0 8080 . 1\n";
return EXIT_FAILURE;
}
auto const address = boost::asio::ip::make_address(argv[1]);
auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
std::string const doc_root = argv[3];
auto const threads = std::max<int>(1, std::atoi(argv[4]));
// The io_context is required for all I/O
boost::asio::io_context ioc{ threads };
// Create and launch a listening port
std::make_shared<listener>(
ioc,
tcp::endpoint{ address, port },
doc_root)->run();
// Run the I/O service on the requested number of threads
std::vector<std::thread> v;
v.reserve(threads);
for (auto i = threads - 1; i >= 0; --i)
v.emplace_back(
[&ioc]
{
ioc.run();
});
{
HTTPClient httpClient;
std::ofstream os("testfile.txt", std::ofstream::binary);
httpClient.downloadFile("https://github.com/cpp-netlib/cpp-netlib/raw/master/libs/network/example/http_client.cpp", os);
}
{
LicenseClient licenseClient(idProvider, "license.dat");
licenseClient.init();
} */
return EXIT_SUCCESS;
}