//TODO: convert protocol option to lowercase
- // Create corresponding Service object TODO: swap to extra method
- if(protocol == "dhs")
- {
- Service * service = new DHS(host,login,password);
- this->Services.push_back(service);
- }
- else if(protocol == "ods")
- {
- Service * service = new ODS(host,login,password);
- this->Services.push_back(service);
- }
+ Services.push_back(create_service(protocol,host,login,password));
}
else
{
/**
+ *
+ * @param protocol
+ * @param host
+ * @param login
+ * @param password
+ * @return A pointer to the created Service object.
+ */
+Service * Config::create_service(string protocol,string host, string login, string password)
+{
+ Service * service;
+ if(protocol == "dhs")
+ {
+ service = new DHS(host,login,password);
+ }
+ else if(protocol == "ods")
+ {
+ service = new ODS(host,login,password);
+ }
+ return service;
+}
+
+
+/**
* 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.
*/
int Config::load_config_from_files(string config_path)
{
-/* fs::path full_config_path = fs::system_complete(fs::path(config_path));
+ fs::path full_config_path = fs::system_complete(fs::path(config_path));
- if( fs::exists(full_config_path) && fs::is_directory(full_config_path))
+ if ( fs::exists(full_config_path) && fs::is_directory(full_config_path) )
{
fs::directory_iterator end_iter;
- for(fs::directory_iterator dir_itr(full_config_path); dir_itr != end_iter; ++dir_itr)
+ for ( fs::directory_iterator dir_itr(full_config_path) ; dir_itr != end_iter ; ++dir_itr )
{
- if( fs::is_regular_file(dir_itr->status()))
+ if( fs::is_regular_file( dir_itr->status() ) )
{
- string filename = dir_itr->path().filename();
- if(filename == "bpdyndnsd.conf")
+ 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" )
{
- cout << filename << endl;
- }
- }
- }
+ 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);
- }
- else
- {
- cout << "Path doesn't exist or is not a directory" << endl;
- return 4;
- }
-
-*/
-
-
-
-
-/*
-
- // first load the main config file bpdyndnsd.conf
- string main_conf_file = config_path.append("/bpdyndnsd.conf");
- ifstream config_file (main_conf_file.c_str(),ifstream::in);
- if(config_file.is_open())
- {
- try
- {
- // parse the config file
- po::variables_map vm;
- po::parsed_options parsed = po::parse_config_file(config_file,*this->opt_desc_conf_main,true);
- po::store(parsed,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;
+ }
+ }
+ // 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("daemon_mode") && vm.count("logfile") && vm.count("loglevel") && vm.count("syslog"))
- {
- // Get the options from main section
- this->daemon_mode = vm["main.daemon_mode"].as<bool>();
- this->logfile = vm["main.logfile"].as<string>();
- this->loglevel = vm["main.loglevel"].as<int>();
- this->syslog = vm["main.syslog"].as<bool>();
+ if(vm.count("service.protocol") && vm.count("service.host") && vm.count("service.login") && vm.count("service.password"))
+ {
+ // create the corresponding service
+ Services.push_back(create_service(vm["service.protocol"].as<string>(),vm["service.host"].as<string>(),vm["service.login"].as<string>(),vm["service.password"].as<string>()));
+ }
+ }
+ 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;
+ }
+ }
}
}
- catch(po::unknown_option e)
- {
- // unknown option in config file detected
- config_file.close();
- cout << "Unknown option in config file detected!" << endl;
- return 3;
- }
- config_file.close();
}
else
{
- cout << "Can't open main config file for reading: " << main_conf_file << endl;
+ cout << "Path doesn't exist or is not a directory" << endl;
return 4;
}
-*/
- // then load all service definition files in config path
-
- // TODO: code to load service definition files in config path
return 0;
}