/**
+ * 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>();
+ string host = vm["service.host"].as<string>();
+ string login = vm["service.login"].as<string>();
+ string password = vm["service.password"].as<string>();
+
+ // 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<bool>();
+ Logfile = vm["main.logfile"].as<string>();
+ Loglevel = vm["main.loglevel"].as<int>();
+ Syslog = vm["main.syslog"].as<bool>();
+ }
+ }
+ 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.
{
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<bool>();
- Logfile = vm["main.logfile"].as<string>();
- Loglevel = vm["main.loglevel"].as<int>();
- Syslog = vm["main.syslog"].as<bool>();
- }
- }
- 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>();
- string host = vm["service.host"].as<string>();
- string login = vm["service.login"].as<string>();
- string password = vm["service.password"].as<string>();
-
- // 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;
}
}
}
}
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;