/*
Copyright © 2015 Gluzskiy Alexandr (sss)
This file is part of Unknown Download Manager (UDM).
UDM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
UDM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with UDM. If not, see .
*/
#include
#include
#include
#include
#include "api_module_metadata_storage.h"
#include "modules_handler.h"
#include "utilities.h"
#include "server.h"
#include "main.h"
#include "config.h"
core_api *module_api = nullptr;
modules_handler *modules = nullptr;
namespace bpo = boost::program_options;
runtime_config_s runtime_config;
server *serv = nullptr;
//TODO: "core" config section architecture, define base settings
int main(int argc, char *argv[])
{
bpo::options_description desc("Available commands and options");
desc.add_options()
("help,h", "this message")
("daemon,d", "fork to background (must be combined with \"--run\", does nothing alone)")
("config,c", bpo::value(), "use specified config file instead of \"~/.config/udm/udm.conf\"")
("list-modules", "list all installed modules (you can set verbosity level, accepted values from 0 to 1, default = 0)")
("verbosity,v", bpo::value()->default_value(0), "set global verbosity level")
("run", "start UDM server")
;
bool daemon = false, run = false;
std::string config_path = "~/.config/udm/udm.conf", parsed_config_path;
try{
bpo::variables_map vm;
bpo::store(bpo::command_line_parser(argc, argv).options(desc).style(bpo::command_line_style::unix_style | bpo::command_line_style::allow_long_disguise).run(), vm);
bpo::notify(vm);
//load config first, as it may be needed for other commands
if(vm.count("config"))
config_path = vm["config"].as();
{
parsed_config_path = replace_home_var(config_path);
if(boost::filesystem::exists(parsed_config_path) && boost::filesystem::is_regular(parsed_config_path))
bpt::read_info(parsed_config_path, runtime_config.config_file); //TODO: finish this
else
std::cerr<<"failed to load config: \"" + config_path + "\", file does not exists or is not regular file\n";
}
//load all config variables here
runtime_config.settings.verbosity = runtime_config.config_file.get("server.verbosity", 0);
daemon = runtime_config.config_file.get("server.daemon", false);
//override config from command line here
if(vm.count("verbosity"))
{
runtime_config.settings.verbosity = vm["verbosity"].as();
}
if(vm.count("help"))
{
std::cout<load_modules();
}
std::cout<list_modules();
bpt::write_info(parsed_config_path, runtime_config.config_file); //save config on exit
return 0;
}
if(vm.count("run"))
{
run = true;
}
}
catch(std::exception &e)
{
std::cerr<<"error: "<load_modules();
}
std::string default_metadata_module_name = runtime_config.config_file.get("server.default_metadata_module", "");
if(default_metadata_module_name == "")
{
if(!modules->get_metadata_modules().empty())
runtime_config.default_metadata_storage = static_cast(*(modules->get_metadata_modules().begin()));
else
std::cerr<<"Error: Metadata storage modules not installed'n";
}
else
{
for(auto i = modules->get_metadata_modules().begin(), end = modules->get_metadata_modules().end(); i != end; ++i)
{
if((*i)->get_module_info().name == default_metadata_module_name)
runtime_config.default_metadata_storage = static_cast(*i);
}
if(!runtime_config.default_metadata_storage)
{
std::cerr<<"Error: Failed to load metadata storage module named \""<on_modules_loaded(); //call second initialization stage in modules
if(daemon)
{
//TODO: fork here
}
boost::asio::io_service io_service_server;
try{
//TODO: server options (ssl, ...)
serv = new server(io_service_server, 6613);
}
catch(std::exception &e)
{
//TODO:
}
catch(...)
{
//TODO:
}
boost::system::error_code ec;
io_service_server.run(ec);
if(ec)
{
//TODO:
}
//TODO: run here
return 0; //stub for now
}
std::cerr<<"error: no command specified"<