Reading host name from file. This first attempt pings just one host
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Tue, 22 Feb 2011 16:17:50 +0000 (17:17 +0100)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Tue, 22 Feb 2011 16:17:50 +0000 (17:17 +0100)
config/pingcheck.cfg
src/config/configuration.cpp
src/config/configuration.h
src/config/configurationreader.cpp
src/config/configurationreader.h
src/main.cpp

index 052cb22..245388d 100644 (file)
@@ -1 +1,4 @@
-limit-to-notify=5
\ No newline at end of file
+limit-to-notify=5
+
+[host]
+name=www.intra2net.com
index c378daf..db81454 100644 (file)
@@ -11,6 +11,7 @@ using namespace std;
 Configuration::Configuration() :
     config_file_name( "" ),
     limit_to_notify( 0 ),
+    host( "" ),
     MIN_LIMIT_TO_NOTIFY( 0 ),
     MAX_LIMIT_TO_NOTIFY( 50 )
 {
@@ -25,7 +26,7 @@ string Configuration::get_config_file_name() const
     return config_file_name;
 }
 
-void Configuration::set_config_file_name( std::string config_file_name )
+void Configuration::set_config_file_name( const std::string& config_file_name )
 {
     BOOST_ASSERT( !config_file_name.empty() );
 
@@ -37,9 +38,20 @@ uint32_t Configuration::get_limit_to_notify() const
     return limit_to_notify;
 }
 
-void Configuration::set_limit_to_notify( uint32_t limit_to_notify )
+void Configuration::set_limit_to_notify( const uint32_t limit_to_notify )
 {
     BOOST_ASSERT( ( MIN_LIMIT_TO_NOTIFY <= limit_to_notify ) && ( limit_to_notify <= MAX_LIMIT_TO_NOTIFY) );
 
     this->limit_to_notify = limit_to_notify;
 }
+
+Host Configuration::get_host() const
+{
+    return host;
+}
+
+void Configuration::set_host( const Host& host )
+{
+    this->host = host;
+}
+
index 63c78fb..464d95d 100644 (file)
@@ -4,6 +4,8 @@
 #include <stdint.h>
 #include <string>
 
+#include "host.h"
+
 //-----------------------------------------------------------------------------
 // Configuration
 //-----------------------------------------------------------------------------
@@ -15,14 +17,18 @@ public:
     virtual ~Configuration();
 
     std::string get_config_file_name() const;
-    void set_config_file_name( std::string config_file_name );
+    void set_config_file_name( const std::string& config_file_name );
 
     uint32_t get_limit_to_notify() const;
-    void set_limit_to_notify( uint32_t limit_to_notify );
+    void set_limit_to_notify( const uint32_t limit_to_notify );
+
+    Host get_host() const;
+    void set_host( const Host& host );
 
 private:
     std::string config_file_name;
     uint32_t limit_to_notify;
+    Host host;
 
     const uint32_t MIN_LIMIT_TO_NOTIFY;
     const uint32_t MAX_LIMIT_TO_NOTIFY;
index 9dc0365..b34480e 100644 (file)
@@ -24,8 +24,8 @@ ConfigurationReader::ConfigurationReader() :
     DEFAULT_LIMIT_TO_NOTIFY( 4 ),
     LIMIT_TO_NOTIFY_CMD_STR( "limit-to-notify" ),
     LIMIT_TO_NOTIFY_CMD_DESC( "Limit of host that have to be down in order to notify." ),
-    HOST_CMD_STR( "host" ),
-    HOST_CMD_DESC( "Host address" )
+    HOST_NAME_CMD_STR( "host.name" ),
+    HOST_NAME_CMD_DESC( "Host address" )
 {
 }
 
@@ -49,7 +49,7 @@ options_description ConfigurationReader::get_configuration_options() const
     options_description options( "Configuration" );
     options.add_options()
         ( LIMIT_TO_NOTIFY_CMD_STR.c_str(), value<int>()->default_value( DEFAULT_LIMIT_TO_NOTIFY ), LIMIT_TO_NOTIFY_CMD_DESC.c_str() )
-        ( HOST_CMD_STR.c_str(), value< vector<string> >(), HOST_CMD_DESC.c_str() );
+        ( HOST_NAME_CMD_STR.c_str(), value< string >(), HOST_NAME_CMD_DESC.c_str() );
 
     return options;
 }
@@ -72,7 +72,7 @@ bool ConfigurationReader::parse_command_line(
         visible.add( generic ).add( config );
 
         positional_options_description p;
-        p.add( HOST_CMD_STR.c_str(), -1 );
+        p.add( HOST_NAME_CMD_STR.c_str(), -1 );
 
         store( command_line_parser( argc, argv ).
                 options( cmdline_options ).
@@ -141,7 +141,7 @@ bool ConfigurationReader::parse(
         char* argv[]
 )
 {
-    BOOST_ASSERT( argc > 1 );
+    BOOST_ASSERT( argc > 0 );
     BOOST_ASSERT( argv != NULL );
 
     variables_map vm;
@@ -175,6 +175,16 @@ bool ConfigurationReader::fill_configuration( const variables_map& vm )
         cout << LIMIT_TO_NOTIFY_CMD_STR << "=" << limit_to_notify << endl;
     }
 
+    if ( vm.count( HOST_NAME_CMD_STR ) )
+    {
+        string host_name = vm[ HOST_NAME_CMD_STR ].as<string> ();
+        Host host = configuration.get_host();
+        host.set_address( host_name );
+        configuration.set_host( host );
+
+        cout << HOST_NAME_CMD_STR << "=" << host_name << endl;
+    }
+
     return true;
 }
 
@@ -182,4 +192,3 @@ Configuration ConfigurationReader::get_configuration() const
 {
     return configuration;
 }
-
index f8ff57c..3a75d22 100644 (file)
@@ -51,8 +51,8 @@ private:
     const uint32_t DEFAULT_LIMIT_TO_NOTIFY;
     const std::string LIMIT_TO_NOTIFY_CMD_STR;
     const std::string LIMIT_TO_NOTIFY_CMD_DESC;
-    const std::string HOST_CMD_STR;
-    const std::string HOST_CMD_DESC;
+    const std::string HOST_NAME_CMD_STR;
+    const std::string HOST_NAME_CMD_DESC;
 
 };
 
index 526587d..5ce79f6 100644 (file)
@@ -8,6 +8,8 @@
 #include "host.h"
 #include "boostpinger.h"
 
+using namespace std;
+
 int main( int argc, char* argv[] )
 {
     // sends the program command line to be parsed by the configuration reader
@@ -19,9 +21,8 @@ int main( int argc, char* argv[] )
         {
             boost::asio::io_service io_service;
             BoostPinger p( io_service );
-//            Configuration configuration = config_reader.getConfiguration();
-//            Host host( configuration.get_hosts().at(0) ); // TODO pass Configuration object
-            Host host( argv[1] );
+            Configuration config = config_reader.get_configuration();
+            Host host = config.get_host();
             p.ping( host );
             io_service.run();
         }