Added config load from files(main/service).
authorBjoern Sikora <bjoern.sikora@intra2net.com>
Mon, 27 Jul 2009 15:37:29 +0000 (17:37 +0200)
committerBjoern Sikora <bjoern.sikora@intra2net.com>
Mon, 27 Jul 2009 15:37:29 +0000 (17:37 +0200)
CMakeLists.txt
dhs.service [deleted file]
src/config.cpp
src/config.h

index e20c49f..2c0b51f 100644 (file)
@@ -20,3 +20,21 @@ IF(BOOST_PROGRAM_OPTIONS)
 ELSE(BOOST_PROGRAM_OPTIONS)
     MESSAGE(FATAL_ERROR "Could not find boost/program_options")
 ENDIF(BOOST_PROGRAM_OPTIONS)
+
+# find boost_filesystem and link to bpdyndnsd
+FIND_LIBRARY( BOOST_FILESYSTEM libboost_filesystem.so )
+IF(BOOST_FILESYSTEM)
+    MESSAGE(STATUS "Found boost/filesystem as ${BOOST_FILESYSTEM}")
+    TARGET_LINK_LIBRARIES(bin/bpdyndnsd ${BOOST_FILESYSTEM})
+ELSE(BOOST_FILESYSTEM)
+    MESSAGE(FATAL_ERROR "Could not find boost/filesystem")
+ENDIF(BOOST_FILESYSTEM)
+
+# find boost_filesystem and link to bpdyndnsd
+FIND_LIBRARY( BOOST_REGEX libboost_regex.so )
+IF(BOOST_REGEX)
+    MESSAGE(STATUS "Found boost/regex as ${BOOST_REGEX}")
+    TARGET_LINK_LIBRARIES(bin/bpdyndnsd ${BOOST_REGEX})
+ELSE(BOOST_REGEX)
+    MESSAGE(FATAL_ERROR "Could not find boost/regex")
+ENDIF(BOOST_REGEX)
diff --git a/dhs.service b/dhs.service
deleted file mode 100644 (file)
index d254944..0000000
+++ /dev/null
@@ -1,6 +0,0 @@
-[service]
-protocol=dhs
-host=hostname
-login=maxmuster
-password=secret
-
index 7bab10d..cd71bcd 100644 (file)
@@ -89,17 +89,7 @@ int Config::parse_cmd_line(int argc, char *argv[])
 
             //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
         {
@@ -117,83 +107,128 @@ int Config::parse_cmd_line(int argc, char *argv[])
 
 
 /**
+ * 
+ * @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;
 }
 
index 40d2bc4..481abc0 100644 (file)
@@ -13,6 +13,7 @@
 #include <boost/program_options.hpp>
 #include <boost/foreach.hpp>
 #include <boost/filesystem.hpp>
+#include <boost/regex.hpp>
 
 #include <string>
 #include <iostream>
@@ -41,6 +42,8 @@ private:
     string Logfile;
     int Loglevel;
     bool Syslog;
+
+    Service * create_service(string,string,string,string);
 public:
     Config();