Reads the host port from configuration file
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Tue, 23 Aug 2011 01:30:39 +0000 (22:30 -0300)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Tue, 23 Aug 2011 02:26:35 +0000 (23:26 -0300)
src/config/configurationreader.cpp
src/config/configurationreader.h

index 3928b28..9eee6fd 100644 (file)
@@ -21,6 +21,7 @@ on this file might be covered by the GNU General Public License.
 
 #include <fstream>
 #include <iostream>
+#include <limits>
 
 #include <boost/assert.hpp>
 #include <boost/foreach.hpp>
@@ -74,6 +75,9 @@ ConfigurationReader::ConfigurationReader() :
     LinkDownIntervalCmdDesc( "How long the link must be offline in order to consider it down." ),
     HostNameCmdStr( "host.name" ),
     HostNameCmdDesc( "Host address" ),
+    DefaultHostPort( 80 ), // HTTP port
+    HostPortCmdStr( "host.port" ),
+    HostPortCmdDesc( "Host port number" ),
     DefaultHostIntervalInSec( 60 ), // 60 seconds
     HostIntervalCmdStr( "host.interval" ),
     HostIntervalCmdDesc( "Interval between each ping to the host" )
@@ -216,6 +220,7 @@ options_description ConfigurationReader::get_configuration_options() const
         ( LinkUpIntervalCmdStr.c_str(), value<int>()->default_value( DefaultLinkUpIntervalInMin ), LinkUpIntervalCmdDesc.c_str() )
         ( LinkDownIntervalCmdStr.c_str(), value<int>()->default_value( DefaultLinkDownIntervalInMin ), LinkDownIntervalCmdDesc.c_str() )
         ( HostNameCmdStr.c_str(), value< vector<string> >(), HostNameCmdDesc.c_str() )
+        ( HostPortCmdStr.c_str(), value< vector<int> >(), HostPortCmdDesc.c_str() )
         ( HostIntervalCmdStr.c_str(), value< vector<int> >(), HostIntervalCmdDesc.c_str() )
     ;
 
@@ -316,6 +321,8 @@ bool ConfigurationReader::parse_configuration_options( const variables_map &vm )
         vector<string> hosts_names = vm[ HostNameCmdStr ].as< vector<string> > ();
         BOOST_FOREACH( string host_name, hosts_names )
         {
+            BOOST_ASSERT( !host_name.empty() );
+
             HostItem host_item( new Host( host_name ) );
             hosts_list.push_back( host_item );
 
@@ -330,6 +337,28 @@ bool ConfigurationReader::parse_configuration_options( const variables_map &vm )
         BOOST_ASSERT( hosts_names_count >= static_cast<size_t>( host_down_limit ) );
     }
 
+    // [host] port
+    size_t host_port_count = 0;
+    if ( vm.count( HostPortCmdStr ) > 0 )
+    {
+        HostList hosts_list = Config.get_hosts();
+        HostList::iterator hosts_it = hosts_list.begin();
+
+        vector<int> hosts_ports = vm[ HostPortCmdStr ].as< vector<int> >();
+        BOOST_FOREACH( int host_port, hosts_ports )
+        {
+            BOOST_ASSERT( ( 0 <= host_port ) && ( host_port <= numeric_limits<uint16_t>::max() ) );
+
+            HostItem host_item = *hosts_it;
+            host_item->set_port( static_cast<uint16_t>(host_port) );
+            ++hosts_it;
+
+            GlobalLogger.info() <<  HostPortCmdStr << "=" << host_port << endl;
+        }
+
+        host_port_count = hosts_ports.size();
+    }
+
     // [host] interval
     size_t hosts_interval_count = 0;
     if ( vm.count( HostIntervalCmdStr ) > 0 )
@@ -337,9 +366,11 @@ bool ConfigurationReader::parse_configuration_options( const variables_map &vm )
         HostList hosts_list = Config.get_hosts();
         HostList::iterator hosts_it = hosts_list.begin();
 
-        vector<int> hosts_intervals = vm[ HostIntervalCmdStr ].as< vector<int> > ();
+        vector<int> hosts_intervals = vm[ HostIntervalCmdStr ].as< vector<int> >();
         BOOST_FOREACH( int host_interval_in_sec, hosts_intervals )
         {
+            BOOST_ASSERT( 0 < host_interval_in_sec );
+
             HostItem host_item = *hosts_it;
             host_item->set_interval_in_sec( host_interval_in_sec );
             ++hosts_it;
@@ -359,6 +390,7 @@ bool ConfigurationReader::parse_configuration_options( const variables_map &vm )
         return false;
     }
 
+    BOOST_ASSERT( hosts_names_count == host_port_count );
     BOOST_ASSERT( hosts_names_count == hosts_interval_count );
 
     return true;
index 338db85..3365ce6 100644 (file)
@@ -107,6 +107,9 @@ private:
     const std::string LinkDownIntervalCmdDesc;
     const std::string HostNameCmdStr;
     const std::string HostNameCmdDesc;
+    const int DefaultHostPort;
+    const std::string HostPortCmdStr;
+    const std::string HostPortCmdDesc;
     const int DefaultHostIntervalInSec;
     const std::string HostIntervalCmdStr;
     const std::string HostIntervalCmdDesc;