From: Bjoern Sikora Date: Wed, 29 Jul 2009 15:18:12 +0000 (+0200) Subject: Divided load_config_from_files into smaller ones. X-Git-Tag: v1.1~267 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=1cf4e2b52b7733c1128a307b552d1526ce4930c2;p=bpdyndnsd Divided load_config_from_files into smaller ones. --- diff --git a/src/config.cpp b/src/config.cpp index 4b3fa8c..bcb99a9 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -142,6 +142,104 @@ ServicePtr Config::create_service(const string &protocol,const string &host, con /** + * Loads a service config file, invoked by load_config_from_files. + * @param full_filename Filename of the service config file to load. + * @return 0 if all is fine, 3 if an unknown option was detected, 4 if the service file could not be opened for reading. + */ +const int Config::load_service_config_file(const string& full_filename) +{ + cout << "Loading service config file: " << full_filename << endl; + ifstream service_config_file(full_filename.c_str(),ifstream::in); + if(service_config_file.is_open()) + { + try + { + po::variables_map vm; + po::parsed_options parsed_service_options = po::parse_config_file(service_config_file,*this->Opt_desc_conf_service,true); + po::store(parsed_service_options,vm); + po::notify(vm); + + if(vm.count("service.protocol") && vm.count("service.host") && vm.count("service.login") && vm.count("service.password")) + { + // create the corresponding service + string protocol = vm["service.protocol"].as(); + string host = vm["service.host"].as(); + string login = vm["service.login"].as(); + string password = vm["service.password"].as(); + + // TODO: convert protocol to lowercase + //protocol = tolower(protocol.c_str()); + + ServicePtr service = create_service(protocol,host,login,password); + if ( service != NULL ) + { + Services.push_back(service); + } + } + } + catch ( po::unknown_option e ) + { + // unknown option in config file detected + service_config_file.close(); + cout << "Unknown option in service config file detected!" << endl; + return 3; + } + service_config_file.close(); + } + else + { + cout << "Can't open service config file for reading: " << service_config_file << endl; + return 4; + } + return 0; +} + +/** + * Loads the main config file, invoked by load_config_from_files + * @param full_filename The full filename of the main config file to load + * @return 0 if all is fine. 3 if unknown option was detected, 4 if main config file could not be opened for reading + */ +const int Config::load_main_config_file(const string& full_filename) +{ + // load main config file + cout << "Loading main config file: " << full_filename << endl; + ifstream main_config_file(full_filename.c_str(),ifstream::in); + if(main_config_file.is_open()) + { + try + { + po::variables_map vm; + po::parsed_options parsed_main_options = po::parse_config_file(main_config_file,*this->Opt_desc_conf_main,true); + po::store(parsed_main_options,vm); + po::notify(vm); + + if(vm.count("main.daemon_mode") && vm.count("main.logfile") && vm.count("main.loglevel") && vm.count("main.syslog")) + { + Daemon_mode = vm["main.daemon_mode"].as(); + Logfile = vm["main.logfile"].as(); + Loglevel = vm["main.loglevel"].as(); + Syslog = vm["main.syslog"].as(); + } + } + catch ( po::unknown_option e ) + { + // unknown option in config file detected + main_config_file.close(); + cout << "Unknown option in main config file detected!" << endl; + return 3; + } + main_config_file.close(); + } + else + { + cout << "Can't open main config file for reading: " << main_config_file << endl; + return 4; + } + return 0; +} + +// TODO: Seperate into smaller methods +/** * Loads the main and the service config file and does the needed action. * @param config_path The path to the config directory. * @return 0 if all is fine. @@ -159,90 +257,26 @@ const int Config::load_config_from_files(const string& config_path) { string actual_file = dir_itr->path().filename(); boost::regex expr(".*\.conf?"); + // If it is the main config file do the following if ( actual_file == "bpdyndnsd.conf" ) { + // Load the main config file string full_filename = dir_itr->path().string(); - cout << "Loading main config file: " << full_filename << endl; - ifstream main_config_file(full_filename.c_str(),ifstream::in); - if(main_config_file.is_open()) - { - try - { - po::variables_map vm; - po::parsed_options parsed_main_options = po::parse_config_file(main_config_file,*this->Opt_desc_conf_main,true); - po::store(parsed_main_options,vm); - po::notify(vm); - - if(vm.count("main.daemon_mode") && vm.count("main.logfile") && vm.count("main.loglevel") && vm.count("main.syslog")) - { - Daemon_mode = vm["main.daemon_mode"].as(); - Logfile = vm["main.logfile"].as(); - Loglevel = vm["main.loglevel"].as(); - Syslog = vm["main.syslog"].as(); - } - } - catch ( po::unknown_option e ) - { - // unknown option in config file detected - main_config_file.close(); - cout << "Unknown option in main config file detected!" << endl; - return 3; - } - main_config_file.close(); - } - else + int ret_val = load_main_config_file(full_filename); + if ( ret_val != 0 ) { - cout << "Can't open main config file for reading: " << main_config_file << endl; - return 4; + return ret_val; } } // If it is a service definition file *.conf, parse it and generate the corresponding service else if ( boost::regex_search( actual_file,expr ) ) { string full_filename = dir_itr->path().string(); - cout << "Loading service config file: " << full_filename << endl; - ifstream service_config_file(full_filename.c_str(),ifstream::in); - if(service_config_file.is_open()) - { - try - { - po::variables_map vm; - po::parsed_options parsed_service_options = po::parse_config_file(service_config_file,*this->Opt_desc_conf_service,true); - po::store(parsed_service_options,vm); - po::notify(vm); - - if(vm.count("service.protocol") && vm.count("service.host") && vm.count("service.login") && vm.count("service.password")) - { - // create the corresponding service - string protocol = vm["service.protocol"].as(); - string host = vm["service.host"].as(); - string login = vm["service.login"].as(); - string password = vm["service.password"].as(); - - // TODO: convert protocol to lowercase - protocol = tolower(protocol.c_str()); - - ServicePtr service = create_service(protocol,host,login,password); - if ( service != NULL ) - { - Services.push_back(service); - } - } - } - catch ( po::unknown_option e ) - { - // unknown option in config file detected - service_config_file.close(); - cout << "Unknown option in service config file detected!" << endl; - return 3; - } - service_config_file.close(); - } - else + int ret_val = load_service_config_file(full_filename); + if ( ret_val != 0 ) { - cout << "Can't open service config file for reading: " << service_config_file << endl; - return 4; + return ret_val; } } } @@ -250,7 +284,7 @@ const int Config::load_config_from_files(const string& config_path) } else { - cout << "Path doesn't exist or is not a directory" << endl; + cout << "Config path doesn't exist or is not a directory" << endl; return 4; } return 0; diff --git a/src/config.h b/src/config.h index 08f9723..456fe4d 100644 --- a/src/config.h +++ b/src/config.h @@ -47,6 +47,8 @@ private: bool Syslog; ServicePtr create_service(const string&,const string&,const string&,const string&); + const int load_main_config_file(const string&); + const int load_service_config_file(const string&); public: Config(); diff --git a/src/main.cpp b/src/main.cpp index 80218b9..b60e073 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -60,14 +60,18 @@ int main(int argc, char *argv[]) return 0; } + // Load the main config and the service files string config_path = "/home/bjoern/bpdyndnsd"; // TODO: standard config path should be /etc/bpdyndnsd if not specified other on command line ret_val = config->load_config_from_files(config_path); if(ret_val == 3) { + // unknown option cout << "See manpage for config file structure." << endl; + return 0; } else if(ret_val == 4) { + // error opening return 0; }