Running the BoostPingers in parallel (multi-thread approach instead of assynchronous)
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Mon, 2 May 2011 10:21:46 +0000 (12:21 +0200)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Mon, 2 May 2011 10:24:46 +0000 (12:24 +0200)
src/CMakeLists.txt
src/host/pingscheduler.cpp
src/host/pingscheduler.h
src/main.cpp

index 7e8550b..d893a3f 100644 (file)
@@ -5,7 +5,7 @@ include(FindPkgConfig)
 set(Boost_USE_STATIC_LIBS ON)
 set(Boost_USE_MULTITHREADED ON)
 set(Boost_USE_STATIC_RUNTIME OFF)
-find_package(Boost COMPONENTS filesystem program_options system REQUIRED)
+find_package(Boost COMPONENTS filesystem program_options system thread REQUIRED)
 include_directories(${Boost_INCLUDE_DIRS})
 link_directories(${Boost_LIBRARY_DIRS})
 
index 5dca442..8b5bd55 100644 (file)
@@ -18,6 +18,7 @@ using boost::posix_time::microsec_clock;
 using boost::posix_time::ptime;
 using boost::posix_time::seconds;
 using boost::shared_ptr;
+using boost::thread;
 using I2n::Logger::GlobalLogger;
 
 //-----------------------------------------------------------------------------
@@ -25,7 +26,6 @@ using I2n::Logger::GlobalLogger;
 //-----------------------------------------------------------------------------
 
 PingScheduler::PingScheduler(
-        io_service &io_serv,
         const string &network_interface,
         const string &destination_address,
         const long ping_interval_in_sec,
@@ -34,13 +34,14 @@ PingScheduler::PingScheduler(
         shared_ptr<LinkStatusAnalyzer> link_analyzer
 
 ) :
-    IoService( io_serv ),
+    IoService(),
     LocalNetworkInterfaceName( network_interface ),
-    NextPingTimer( io_serv ),
+    NextPingTimer( IoService ),
     TimeSentLastPing( microsec_clock::universal_time() ),
     PingIntervalInSec( ping_interval_in_sec ),
     IpList( destination_address, nameserver ),
-    HostAnalyzer( destination_address, ping_fail_percentage_limit, link_analyzer )
+    HostAnalyzer( destination_address, ping_fail_percentage_limit, link_analyzer ),
+    Thread()
 {
 }
 
@@ -48,6 +49,20 @@ PingScheduler::~PingScheduler()
 {
 }
 
+bool PingScheduler::start_pinging_thread()
+{
+    Thread = thread( &PingScheduler::start_pinging, this );
+
+    return true;
+}
+
+void PingScheduler::wait_pinging_thread()
+{
+    BOOST_ASSERT( Thread.joinable() );
+
+    Thread.join();
+}
+
 bool PingScheduler::start_pinging()
 {
     bool address_resolved = resolve_ping_address();
@@ -58,6 +73,8 @@ bool PingScheduler::start_pinging()
 
     schedule_next_ping();
 
+    IoService.run();
+
     return true;
 }
 
index 5ca1a0b..4042ef9 100644 (file)
@@ -5,6 +5,7 @@
 
 #include <boost/asio.hpp>
 #include <boost/shared_ptr.hpp>
+#include <boost/thread.hpp>
 
 #include "dns/dnsresolver.h"
 #include "link/linkstatusanalyzer.h"
@@ -24,7 +25,6 @@ class PingScheduler
 {
 public:
     PingScheduler(
-            boost::asio::io_service &io_serv,
             const std::string &network_interface,
             const std::string &destination_address,
             const long ping_interval_in_sec,
@@ -34,6 +34,8 @@ public:
     );
     ~PingScheduler();
 
+    bool start_pinging_thread();
+    void wait_pinging_thread();
     bool start_pinging();
 
 private:
@@ -47,7 +49,7 @@ private:
 
 private:
     /// service object, which has the event loop
-    boost::asio::io_service &IoService;
+    boost::asio::io_service IoService;
     /// name of the network device used to send the packets
     std::string LocalNetworkInterfaceName;
     /// timer to trigger the next ping
@@ -60,6 +62,8 @@ private:
     DnsResolver IpList;
     /// object responsible to evaluate the status of the host
     HostStatusAnalyzer HostAnalyzer;
+    /// thread object
+    boost::thread Thread;
 
 };
 
index 3606bb2..d52e2fe 100644 (file)
@@ -34,8 +34,6 @@ int main( int argc, char* argv[] )
     bool read_success = config_reader.parse( argc, argv );
     if ( read_success )
     {
-        io_service io_serv;
-
         Configuration config = config_reader.get_configuration();
 
         bool daemon_mode = config.get_daemon();
@@ -69,7 +67,6 @@ int main( int argc, char* argv[] )
             int ping_interval_in_sec = (*host).get_interval_in_sec();
             PingSchedulerItem scheduler(
                     new PingScheduler(
-                            io_serv,
                             local_interface,
                             destination_address,
                             ping_interval_in_sec,
@@ -84,7 +81,7 @@ int main( int argc, char* argv[] )
         // TODO ping_loop()
         BOOST_FOREACH( PingSchedulerItem scheduler, scheduler_list )
         {
-            bool started = scheduler->start_pinging();
+            bool started = scheduler->start_pinging_thread();
             if ( !started )
             {
                 GlobalLogger.error() << "Error: could not start pinger."
@@ -93,7 +90,10 @@ int main( int argc, char* argv[] )
         }
 
         // Main loop to handle scheduled ping requests
-        (void) io_serv.run();
+        BOOST_FOREACH( PingSchedulerItem scheduler, scheduler_list )
+        {
+            scheduler->wait_pinging_thread();
+        }
     }
 
     return 0;