The app reads a list of hosts instead of just one
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Fri, 4 Mar 2011 15:14:59 +0000 (16:14 +0100)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Fri, 4 Mar 2011 15:14:59 +0000 (16:14 +0100)
Caveat: it pings the host sequentially, this is not the desirable behavior

conf/pingcheck.conf
src/config/configuration.cpp
src/config/configuration.h
src/config/configurationreader.cpp
src/main.cpp
src/ping/host.h
src/ping/pingcheck.cpp
src/ping/pingcheck.h

index 08edd6f..d9ed170 100644 (file)
@@ -2,4 +2,12 @@ limit-to-notify=5
 
 [host]
 name=www.intra2net.com
-interval=2
\ No newline at end of file
+interval=4
+
+[host]
+name=209.85.148.105
+interval=2
+
+[host]
+name=www.ufsc.br
+interval=3
index be75d80..654a384 100644 (file)
@@ -11,7 +11,7 @@ using namespace std;
 Configuration::Configuration() :
     ConfigFileName( "" ),
     LimitToNotify( 0 ),
-    Host_( "" ),
+    Hosts(),
     MinLimitToNotify( 0 ),
     MaxLimitToNotify( 50 )
 {
@@ -45,13 +45,13 @@ void Configuration::set_limit_to_notify( const std::size_t limit_to_notify )
     this->LimitToNotify = limit_to_notify;
 }
 
-Host Configuration::get_host() const
+vector< HostItem > Configuration::get_hosts() const
 {
-    return Host_;
+    return Hosts;
 }
 
-void Configuration::set_host( const Host& host )
+void Configuration::set_hosts( const vector< HostItem > &hosts_list )
 {
-    this->Host_ = host;
+    this->Hosts = hosts_list;
 }
 
index e56004c..229e747 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <stdint.h>
 #include <string>
+#include <vector>
 
 #include "host.h"
 
@@ -22,13 +23,13 @@ public:
     std::size_t get_limit_to_notify() const;
     void set_limit_to_notify( const std::size_t limit_to_notify );
 
-    Host get_host() const;
-    void set_host( const Host& host );
+    std::vector<HostItem> get_hosts() const;
+    void set_hosts( const std::vector<HostItem> &hosts_list );
 
 private:
     std::string ConfigFileName;
     std::size_t LimitToNotify;
-    Host Host_;
+    std::vector<HostItem> Hosts;
 
     const std::size_t MinLimitToNotify;
     const std::size_t MaxLimitToNotify;
index adf9525..a60481a 100644 (file)
@@ -1,11 +1,12 @@
 #include <boost/assert.hpp>
+#include <boost/foreach.hpp>
 #include <fstream>
 #include <iostream>
-#include <iterator>
 
 #include "configurationreader.h"
 
 using namespace std;
+using namespace boost;
 using namespace boost::program_options;
 
 //-----------------------------------------------------------------------------
@@ -108,8 +109,8 @@ options_description ConfigurationReader::get_configuration_options() const
     options_description options( "Configuration" );
     options.add_options()
         ( LimitToNotifyCmdStr.c_str(), value<size_t>()->default_value( DefaultLimitToNotify ), LimitToNotifyCmdDesc.c_str() )
-        ( HostNameCmdStr.c_str(), value< string >(), HostNameCmdDesc.c_str() )
-        ( HostIntervalCmdStr.c_str(), value<size_t>()->default_value( DefaultHostInterval ), HostIntervalCmdDesc.c_str() )
+        ( HostNameCmdStr.c_str(), value< vector<string> >(), HostNameCmdDesc.c_str() )
+        ( HostIntervalCmdStr.c_str(), value< vector<size_t> >(), HostIntervalCmdDesc.c_str() )
     ;
 
     return options;
@@ -133,26 +134,47 @@ bool ConfigurationReader::parse_configuration_options( const variables_map &vm )
         cout << LimitToNotifyCmdStr << "=" << limit_to_notify << endl;
     }
 
+    size_t hosts_names_total = 0;
     if ( vm.count( HostNameCmdStr ) )
     {
-        string host_name = vm[ HostNameCmdStr ].as<string> ();
-        Host host = Config.get_host();
-        host.set_address( host_name );
-        Config.set_host( host );
+        vector<HostItem> hosts_list;
 
-        cout << HostNameCmdStr << "=" << host_name << endl;
+        vector<string> hosts_names = vm[ HostNameCmdStr ].as< vector<string> > ();
+        BOOST_FOREACH( string host_name, hosts_names )
+        {
+            HostItem host_item( new Host( host_name ) );
+            hosts_list.push_back( host_item );
+
+            cout << HostNameCmdStr << "=" << host_name << endl;
+        }
+
+        Config.set_hosts( hosts_list );
+
+        hosts_names_total = hosts_names.size();
     }
 
+    size_t hosts_interval_total = 0;
     if ( vm.count( HostIntervalCmdStr ) )
     {
-        size_t host_interval = vm[ HostIntervalCmdStr ].as<size_t> ();
-        Host host = Config.get_host();
-        host.set_interval( host_interval );
-        Config.set_host( host );
+        vector<HostItem> hosts_list = Config.get_hosts();
+        vector<HostItem>::iterator hosts_it = hosts_list.begin();
 
-        cout << HostIntervalCmdStr << "=" << host_interval << endl;
+        vector<size_t> hosts_intervals = vm[ HostIntervalCmdStr ].as< vector<size_t> > ();
+        BOOST_FOREACH( size_t host_interval, hosts_intervals )
+        {
+            HostItem host_item = *hosts_it;
+            host_item->set_interval( host_interval );
+            hosts_it++;
+
+            cout << HostIntervalCmdStr << "=" << host_interval << endl;
+        }
+
+        hosts_interval_total = hosts_intervals.size();
     }
 
+    // TODO deal when interval for a given host was not set. use DefaultHostInterval
+    BOOST_ASSERT(hosts_names_total == hosts_interval_total);
+
     return true;
 }
 
@@ -190,7 +212,7 @@ bool ConfigurationReader::process_command_line(
         }
 
     }
-    catch ( exception& e )
+    catch ( std::exception &e )
     {
         cout << e.what() << endl;
         return false;
index 6dbf200..40bd258 100644 (file)
@@ -1,8 +1,10 @@
 #include <boost/asio.hpp>
 #include <boost/bind.hpp>
+#include <boost/foreach.hpp>
 #include <istream>
 #include <iostream>
 #include <ostream>
+#include <vector>
 
 #include "configurationreader.h"
 #include "host.h"
@@ -17,10 +19,12 @@ int main( int argc, char* argv[] )
     if ( read_success )
     {
         Configuration config = config_reader.get_configuration();
-        Host host = config.get_host();
-
-        PingCheck check( host );
-        check.start_pinging();
+        vector<HostItem> hosts = config.get_hosts();
+        BOOST_FOREACH( HostItem host, hosts )
+        {
+            PingCheck check( *host );
+            check.start_pinging();
+        }
     }
 
     return 0;
index d684c5e..08c1c56 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef HOST_H
 #define HOST_H
 
+#include <boost/shared_ptr.hpp>
 #include <stdint.h>
 #include <string>
 #include <vector>
@@ -35,4 +36,10 @@ private:
 
 };
 
+//-----------------------------------------------------------------------------
+// HostItem
+//-----------------------------------------------------------------------------
+
+typedef boost::shared_ptr<Host> HostItem;
+
 #endif /* HOST_H */
index 6f1d0a8..18013f7 100644 (file)
@@ -12,7 +12,7 @@ using namespace boost::asio;
 // PingCheck
 //-----------------------------------------------------------------------------
 
-PingCheck::PingCheck( Host host ) :
+PingCheck::PingCheck( const Host &host ) :
     Host_( host )
 {
 }
index 652af6e..6c940e2 100644 (file)
@@ -12,7 +12,7 @@
 class PingCheck
 {
 public:
-    PingCheck( Host host );
+    PingCheck( const Host &host );
     virtual ~PingCheck();
 
     void start_pinging();