From 61b79747a45d79ad6d9d7b6cced966e35d70db16 Mon Sep 17 00:00:00 2001 From: Francesco Salvestrini Date: Sat, 27 Sep 2008 00:26:45 +0200 Subject: [PATCH] Rearranged configuration file processing --- src/client/distribute.cxx | 84 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 67 insertions(+), 17 deletions(-) diff --git a/src/client/distribute.cxx b/src/client/distribute.cxx index 64984b8..247dfdd 100644 --- a/src/client/distribute.cxx +++ b/src/client/distribute.cxx @@ -42,19 +42,21 @@ void version(void) } +#define DEFAULT_TIMEOUT 10 + void help(void) { std::cout - << "Usage: " << PROGRAM_NAME << " [OPTION]... "<< std::endl - << std::endl - << "Options: " << std::endl - << " -t, --time-out=TIME start time-out (seconds)" << std::endl - << " -j, --job=FILE use FILE as job to distribute" << std::endl - << " -d, --debug enable debugging traces" << std::endl - << " -h, --help print this help, then exit" << std::endl - << " -V, --version print version number, then exit" << std::endl - << std::endl - << "Report bugs to <" << PACKAGE_BUGREPORT << ">" << std::endl; + << "Usage: " << PROGRAM_NAME << " [OPTION]... "<< std::endl + << std::endl + << "Options: " << std::endl + << " -t, --time-out=TIME time-out in secs (default " << DEFAULT_TIMEOUT << ")" << std::endl + << " -j, --job=FILE use FILE as job to distribute" << std::endl + << " -d, --debug enable debugging traces" << std::endl + << " -h, --help print this help, then exit" << std::endl + << " -V, --version print version number, then exit" << std::endl + << std::endl + << "Report bugs to <" << PACKAGE_BUGREPORT << ">" << std::endl; } void hint(const std::string & message) @@ -72,9 +74,11 @@ int main(int argc, char * argv[]) TR_CONFIG_PFX(PROGRAM_NAME); try { - std::string conffile = ""; - int time_out = 0; // XXX FIXME: Change to a constant - std::string job = ""; // XXX FIXME: Change to a constant + std::string conffile = ""; + + int time_out = DEFAULT_TIMEOUT; + bool time_out_set = false; + std::string job = ""; int c; //int digit_optind = 0; @@ -115,6 +119,7 @@ int main(int argc, char * argv[]) hint("Wrong time-out value"); return 1; } + time_out_set = true; break; case 'j': job = optarg; @@ -138,7 +143,10 @@ int main(int argc, char * argv[]) } } - // XXX FIXME: Add options related checks here + TR_DBG("Initial values:\n"); + TR_DBG(" Time out: '%d'\n", time_out); + TR_DBG(" Job: '%s'\n", job.c_str()); + TR_DBG(" Configuration file: '%s'\n", conffile.c_str()); // Build configuration file path if (conffile.size() == 0) { @@ -156,10 +164,52 @@ int main(int argc, char * argv[]) TR_DBG("Configuration file overridden\n"); } + BUG_ON(conffile.size() == 0); + + // Read configuration file (if available) + try { + TR_DBG("Reading configuration file from '%s'\n", + conffile.c_str()); + + Configuration::File config; + std::ifstream instream(conffile.c_str()); + + instream >> config; + + int conf_timeout; + + if (config.get(conf_timeout, "time-out")) { + TR_DBG("Found 'timeout' key, value '%d'\n", + conf_timeout); + + if (!time_out_set) { + TR_DBG("Updating 'timeout' key\n"); + + // Configuration value not specified + // in command line ... + time_out = conf_timeout; + } + } + } catch (std::exception & e) { + TR_ERR("%s\n", e.what()); + } catch (...) { + BUG(); + } + + // Check gathered configuration + if (time_out < 0) { + hint("Wrong time-out value"); + return 1; + } + // Dump (acquired and derived) infos - TR_DBG("Time out: '%d'\n", time_out); - TR_DBG("Job: '%s'\n", job.c_str()); - TR_DBG("Configuration file: '%s'\n", conffile.c_str()); + TR_DBG("Final values:\n"); + TR_DBG(" Time out: '%d'\n", time_out); + TR_DBG(" Job: '%s'\n", job.c_str()); + TR_DBG(" Configuration file: '%s'\n", conffile.c_str()); + + // Assert what is needed to be asserted + BUG_ON(time_out < 0); // Do the supposed work MISSING_CODE(); -- 2.11.4.GIT