From 38060291a5c7d6e93a5868b0e8c96dfb327c8357 Mon Sep 17 00:00:00 2001 From: Bjoern Sikora Date: Wed, 29 Jul 2009 18:10:42 +0200 Subject: [PATCH] Moved some logic out of main into updater. --- src/config.cpp | 23 +++++++++++++++++++++++ src/config.h | 8 ++++++++ src/main.cpp | 37 +++++++------------------------------ src/updater.cpp | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/updater.h | 4 ++++ 5 files changed, 94 insertions(+), 30 deletions(-) diff --git a/src/config.cpp b/src/config.cpp index bcb99a9..cd33509 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -319,3 +319,26 @@ void Config::print_version(const string& version) { cout << "Bullet proof dynamic dns daemon.\nbpdyndnsd " << version << endl; } + +/** + * Prints out the successful parsing of the command line options. + */ +void Config::print_cmd_parsed() +{ + cout << "Command line arguments successful parsed." << endl; +} + +void Config::print_conf_loaded() +{ + cout << "Config files successful loaded." << endl; +} + +void Config::print_unknown_conf_option() +{ + cout << "See manpage for config file structure." << endl; +} + +void Config::print_error_opening() +{ + cout << "Error opening file." << endl; +} \ No newline at end of file diff --git a/src/config.h b/src/config.h index 456fe4d..bc12eb7 100644 --- a/src/config.h +++ b/src/config.h @@ -62,6 +62,14 @@ public: void print_version(const string&); + void print_cmd_parsed(); + + void print_conf_loaded(); + + void print_error_opening(); + + void print_unknown_conf_option(); + list get_services(); }; diff --git a/src/main.cpp b/src/main.cpp index b60e073..fbab021 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -42,41 +42,18 @@ typedef boost::shared_ptr UpdaterPtr; */ int main(int argc, char *argv[]) { - // Initialize Config class and get the command line parameters + // initialize Config ConfigPtr config(new Config); - int ret_val = config->parse_cmd_line(argc,argv); - if(ret_val == 1) - { - // usage - config->print_usage(); - return 0; - } - else if(ret_val == 2) - { - // version - ostringstream version_string; - version_string << VERSION << "." << REVISION << "." << RELEASE; - config->print_version(version_string.str()); - 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; + // initialize Updater + UpdaterPtr updater(new Updater(config)); + + if ( updater->init_config_from_cmd(argc,argv) != 0 ) return 0; - } - else if(ret_val == 4) - { - // error opening + + if ( updater->init_config_from_files() != 0 ) return 0; - } - // initialize Updater - UpdaterPtr updater(new Updater(config)); updater->update_services(); return 0; diff --git a/src/updater.cpp b/src/updater.cpp index 35811e6..37557e0 100644 --- a/src/updater.cpp +++ b/src/updater.cpp @@ -40,6 +40,58 @@ Updater::~Updater() /** + * + * @param argc + * @param argv[] + * @return + */ +const int Updater::init_config_from_cmd(int argc, char *argv[]) +{ + // Load the command line parameters + int ret_val = Conf->parse_cmd_line(argc,argv); + if(ret_val == 1) + { + // usage + Conf->print_usage(); + return ret_val; + } + else if(ret_val == 2) + { + // version + ostringstream version_string; + version_string << VERSION << "." << REVISION << "." << RELEASE; + Conf->print_version(version_string.str()); + return ret_val; + } + // successful parsed + Conf->print_cmd_parsed(); + return 0; +} + +const int Updater::init_config_from_files() +{ + // 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 + int ret_val = Conf->load_config_from_files(config_path); + if(ret_val == 3) + { + // unknown option + Conf->print_unknown_conf_option(); + return ret_val; + } + else if(ret_val == 4) + { + // error opening + Conf->print_error_opening(); + return ret_val; + } + // successful loaded + Conf->print_conf_loaded(); + return 0; +} + + +/** * Setter for member Conf. * @param _conf */ diff --git a/src/updater.h b/src/updater.h index edb6548..dfc7528 100644 --- a/src/updater.h +++ b/src/updater.h @@ -31,6 +31,10 @@ public: ConfigPtr get_config(); void update_services(); + + const int init_config_from_cmd(int, char **); + + const int init_config_from_files(); }; #endif -- 1.7.1