Alternative approach to solve:
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Wed, 4 May 2011 08:50:49 +0000 (10:50 +0200)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Wed, 4 May 2011 08:50:49 +0000 (10:50 +0200)
- If one of the "[host]" in the configuration file omit the "interval" entry,
  the application uses the next "[host]"'s "interval" entry, instead of a
  default value. The problem is specific to the configuration read classes
  that are not smart enough to verify which "[host]" have this entry and which
  don't.
  Example:
     [host]
     name=A
     [host]
     name=B
     interval=1
     [host]
     name=C
     interval=3
  Thus, the tuples will be (A,1) , (B,3), (C,?), but A should have a default
  value, instead of borrow B's value.

The approach consists of ensure the program only runs if there is an interval
for every single host.

TODO
src/config/configurationreader.cpp

diff --git a/TODO b/TODO
index 7baf1f2..79c0ee8 100644 (file)
--- a/TODO
+++ b/TODO
@@ -27,23 +27,6 @@ TODO
 Guilherme:
 - fix config parser:
 
-- If one of the "[host]" in the configuration file omit the "interval" entry,
-  the application uses the next "[host]"'s "interval" entry, instead of a 
-  default value. The problem is specific to the configuration read classes
-  that are not smart enough to verify which "[host]" have this entry and which
-  don't.
-  Example:
-     [host]
-     name=A
-     [host]
-     name=B
-     interval=1
-     [host]
-     name=C
-     interval=3
-  Thus, the tuples will be (A,1) , (B,3), (C,?), but A should have a default
-  value, instead of borrow B's value.
-
 - Perform a better treatment of missing configuration items, actually the
   application just crashes.
 
index 7be6ee6..d963fa9 100644 (file)
@@ -246,8 +246,8 @@ bool ConfigurationReader::parse_configuration_options( const variables_map &vm )
     }
 
     // [host] name
-    size_t hosts_names_count = vm.count( HostNameCmdStr );
-    if ( hosts_names_count > 0 )
+    size_t hosts_names_count = 0;
+    if ( vm.count( HostNameCmdStr ) > 0 )
     {
         HostList hosts_list;
 
@@ -263,13 +263,14 @@ bool ConfigurationReader::parse_configuration_options( const variables_map &vm )
 
         Config.set_hosts( hosts_list );
 
-        BOOST_ASSERT( hosts_names_count == hosts_names.size() );
+        hosts_names_count = hosts_names.size();
+
         BOOST_ASSERT( hosts_names_count >= static_cast<size_t>( host_down_limit ) );
     }
 
     // [host] interval
-    size_t hosts_interval_count = vm.count( HostIntervalCmdStr );
-    if ( hosts_interval_count > 0 )
+    size_t hosts_interval_count = 0;
+    if ( vm.count( HostIntervalCmdStr ) > 0 )
     {
         HostList hosts_list = Config.get_hosts();
         HostList::iterator hosts_it = hosts_list.begin();
@@ -285,10 +286,16 @@ bool ConfigurationReader::parse_configuration_options( const variables_map &vm )
                     << host_interval_in_sec << endl;
         }
 
-        BOOST_ASSERT( hosts_interval_count == hosts_intervals.size() );
+        hosts_interval_count = hosts_intervals.size();
     }
 
-    // TODO deal when interval for a given host was not set. use DefaultHostIntervalInSec
+    // make sure there is always an interval for each host
+    if ( hosts_names_count != hosts_interval_count )
+    {
+        GlobalLogger.error() << "Could not parse configuration file." <<
+                " Missing an interval entry for one of the hosts." << endl;
+        return false;
+    }
 
     BOOST_ASSERT( hosts_names_count == hosts_interval_count );