The program now pings in configured intervals
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Wed, 2 Mar 2011 16:53:00 +0000 (17:53 +0100)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Wed, 2 Mar 2011 16:53:00 +0000 (17:53 +0100)
- the code is functional, but requires improvements!

src/CMakeLists.txt
src/main.cpp

index 19c48e7..0e536c7 100644 (file)
@@ -14,9 +14,7 @@ set( SOURCES
     ping/icmpheader.cpp
     ping/icmppacket.cpp
     ping/ipv4header.cpp
-    ping/pingcheck.cpp
     ping/pinger.cpp
-    ping/pingmanager.cpp
     main.cpp
 )
 
index 7fc2099..64da9f9 100644 (file)
 
 using namespace std;
 
-int main( int argc, char* argv[] )
+class PingCheck
 {
-    // sends the program command line to be parsed by the configuration reader
-    ConfigurationReader config_reader;
-    bool read_success = config_reader.parse( argc, argv );
-    if ( read_success )
+public:
+    PingCheck( Host host ) :
+        Host_( host )
     {
+    }
+
+    void start_pinging()
+    {
+        cout << "[start_pinging]" << endl;
+
+        string destination = Host_.get_address();
+        size_t ping_set_total = 100; // TODO configurable:
+        size_t ping_set_count = 0;
+        while ( ping_set_count < ping_set_total )
+        {
+            ping_schedule( destination );
+
+            ping_set_count++;
+        }
+    }
+
+private:
+
+    void ping_schedule( const string &destination )
+    {
+        cout << "[ping_schedule]" << endl;
+
         try
         {
-            boost::asio::io_service io_service;
-            BoostPinger p( io_service );
-            Configuration config = config_reader.get_configuration();
-            Host host = config.get_host();
-            string destination = host.get_address();
-            p.ping( destination, 50 );
+            ping( destination );
         }
         catch ( std::exception& e )
         {
             std::cerr << "Exception: " << e.what() << std::endl;
-            return -1;
+            return;
         }
+
+        wait();
+    }
+
+    void ping( const string &destination ) throw ( std::exception& )
+    {
+        cout << "[ping]" << endl;
+
+        size_t times_to_ping = 3; // TODO configurable: this must be automatically selected
+        boost::asio::io_service io_service;
+        BoostPinger pinger( io_service );
+        pinger.ping( destination, times_to_ping );
+    }
+
+    void wait()
+    {
+        cout << "[wait]" << endl;
+
+        boost::asio::io_service io_service;
+        deadline_timer timer( io_service );
+        std::size_t interval = Host_.get_interval(); // TODO configurable:
+        timer.expires_from_now( boost::posix_time::seconds( interval ) );
+        timer.wait();
+    }
+
+private:
+    Host Host_;
+
+};
+
+int main( int argc, char* argv[] )
+{
+    // sends the program command line to be parsed by the configuration reader
+    ConfigurationReader config_reader;
+    bool read_success = config_reader.parse( argc, argv );
+    if ( read_success )
+    {
+        Configuration config = config_reader.get_configuration();
+        Host host = config.get_host();
+
+        PingCheck check( host );
+        check.start_pinging();
     }
 
     return 0;