diff options
author | Gluzskiy Alexandr <sss@sss.chaoslab.ru> | 2015-03-29 00:06:35 +0300 |
---|---|---|
committer | Gluzskiy Alexandr <sss@sss.chaoslab.ru> | 2015-03-29 00:06:35 +0300 |
commit | 42646fc776ff938a4440af5d1cc7be41160a3dbb (patch) | |
tree | fb50bf44186f150ec651015e1ca3dea25ac2fd48 /server/src | |
parent | 264fee4fb3b5ad1c4682a9f233ef781318f74cc8 (diff) |
work in progress
Diffstat (limited to 'server/src')
-rw-r--r-- | server/src/main.cpp | 46 | ||||
-rw-r--r-- | server/src/modules_handler.cpp | 43 |
2 files changed, 65 insertions, 24 deletions
diff --git a/server/src/main.cpp b/server/src/main.cpp index 08fe5b7..e8b4e2c 100644 --- a/server/src/main.cpp +++ b/server/src/main.cpp @@ -18,13 +18,13 @@ */ #include <boost/program_options.hpp> -#include <boost/property_tree/ptree.hpp> -#include <boost/property_tree/info_parser.hpp> #include <boost/filesystem.hpp> #include <iostream> -#include <api_module_metadata_storage.h> -#include <modules_handler.h> +#include "api_module_metadata_storage.h" +#include "modules_handler.h" +#include "utilities.h" +#include "main.h" core_api *module_api = nullptr; @@ -32,9 +32,10 @@ core_api *module_api = nullptr; modules_handler *modules = nullptr; namespace bpo = boost::program_options; -namespace bpt = boost::property_tree; -bpt::ptree config; + + +runtime_config_s runtime_config; @@ -42,19 +43,20 @@ int main(int argc, char *argv[]) { bpo::options_description desc("Available commands and options"); desc.add_options() - ("help", "this message") - ("daemon", "fork to background") - ("config", bpo::value<std::string>(), "use specified config file instead of \"~/.config/udm/udm.conf\"") - ("list-modules", "list all installed modules") + ("help,h", "this message") + ("daemon,d", "fork to background (must be combined with \"--run\", does nothing alone)") + ("config,c", bpo::value<std::string>(), "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<short>()->default_value(0), "set global verbosity level") ("run", "start UDM server") ; bool daemon = false, run = false; - std::string config_path = "~/.config/udm/udm.conf"; + std::string config_path = "~/.config/udm/udm.conf", parsed_config_path; try{ bpo::variables_map vm; - bpo::store(bpo::parse_command_line(argc, argv, desc), 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 @@ -62,15 +64,16 @@ int main(int argc, char *argv[]) config_path = vm["config"].as<std::string>(); { - std::string tmp_path = config_path, home = getenv("HOME"); - for(std::string::size_type p1 = tmp_path.find("~"); p1 != std::string::npos; p1 = tmp_path.find("~", p1)) - tmp_path.replace(p1, 1, home); - if(boost::filesystem::exists(tmp_path) && boost::filesystem::is_regular(tmp_path)) - bpt::read_info(tmp_path, config); //TODO: finish this + 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"; } - + if(vm.count("verbosity")) + { + runtime_config.verbosity = vm["verbosity"].as<short>(); + } if(vm.count("help")) { @@ -88,6 +91,7 @@ int main(int argc, char *argv[]) if(!modules) modules = new modules_handler; std::cout<<modules->list_modules(); + bpt::write_info(parsed_config_path, runtime_config.config_file); //save config on exit return 0; } if(vm.count("run")) @@ -112,10 +116,14 @@ int main(int argc, char *argv[]) { //TODO: fork here } + //TODO: run here } - //module_api must be created first std::cerr<<"error: no command specified"<<std::endl; std::cout<<desc<<std::endl; + bpt::write_info(parsed_config_path, runtime_config.config_file); //save config on exit + + + return 0; } diff --git a/server/src/modules_handler.cpp b/server/src/modules_handler.cpp index a6dbcef..1626d91 100644 --- a/server/src/modules_handler.cpp +++ b/server/src/modules_handler.cpp @@ -19,14 +19,23 @@ */ #include "modules_handler.h" +#include "main.h" #include <unistd.h> #include <dlfcn.h> #include <string> #include <boost/filesystem.hpp> +#include <iostream> + + + -#include <iostream> + +namespace bpt = boost::property_tree; + +extern runtime_config_s runtime_config; + extern core_api *module_api; @@ -55,6 +64,7 @@ void modules_handler::load_metadata_modules(const std::string &path) //TODO: aditional sanity checks module_metadata_storage *m = static_cast<module_metadata_storage*>(f()); m->load(module_api); + sync_module_settings(m); metadata_modules.push_back(m); } } @@ -84,6 +94,7 @@ void modules_handler::load_downloader_modules(const std::string &path) //TODO: aditional sanity checks module_metadata_storage *m = static_cast<module_metadata_storage*>(f()); m->load(module_api); + sync_module_settings(m); metadata_modules.push_back(m); } } @@ -98,16 +109,27 @@ std::string modules_handler::get_self_path() return s; } -std::string modules_handler::list_modules() +std::string modules_handler::list_modules(short verbose_level) { std::string buf; buf += "Installed metadata modules:\n"; for(auto i = metadata_modules.begin(), end = metadata_modules.end(); i != end; ++i) { - buf += "\t"; + buf += "\tName: "; buf += (*i)->get_module_info().name; - buf += " (" + (*i)->get_module_info().description + ") "; - buf += "v" + (*i)->get_module_info().version; + buf += "\n\tDescription: " +(*i)->get_module_info().description; + buf += "\n\tVersion: " + (*i)->get_module_info().version; + if(runtime_config.verbosity >= 1) + { + buf += "\n\tAvailable options:"; + for(auto i1 = (*i)->get_module_info().default_settings.begin(), end = (*i)->get_module_info().default_settings.end(); i1 != end; ++i1) + { + buf += "\n\t\t"; + buf += i1->first; + buf += " = "; + buf += i1->second; + } + } buf += "\n"; } buf += "Installed downloader modules:\n"; @@ -122,6 +144,17 @@ std::string modules_handler::list_modules() return buf; } +void modules_handler::sync_module_settings(module_base *m) +{ + for(auto i = m->get_module_info().default_settings.begin(), end = m->get_module_info().default_settings.end(); i != end; ++i) + { + std::string setting = "modules." + m->get_module_info().name + "." + i->first; + std::string current_val = runtime_config.config_file.get<std::string>(setting, "not set"), default_val = i->second; + if(current_val == "not set" || current_val != default_val) + runtime_config.config_file.put(setting, i->second); + } +} + modules_handler::modules_handler() { |