Reuse BoostPinger object
authorThomas Jarosch <thomas.jarosch@intra2net.com>
Wed, 4 May 2011 13:39:50 +0000 (15:39 +0200)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Wed, 4 May 2011 13:39:50 +0000 (15:39 +0200)
src/host/boostpinger.cpp
src/host/boostpinger.h
src/host/pingscheduler.cpp
src/host/pingscheduler.h

index c8af357..a67378c 100644 (file)
@@ -36,14 +36,13 @@ using I2n::Logger::GlobalLogger;
 //-----------------------------------------------------------------------------
 
 BoostPinger::BoostPinger(
-        io_service &io_serv,
         string source_network_interface,
         const int echo_reply_timeout_in_sec
 ) :
-    IoService( io_serv ),
+    IoService(),
     DestinationEndpoint(),
-    Socket( io_serv, icmp::v4() ),
-    IcmpPacketReceiveTimer( io_serv ),
+    Socket( IoService, icmp::v4() ),
+    IcmpPacketReceiveTimer( IoService ),
     Identifier(0),
     SequenceNumber( 0 ),
     TimeSent( microsec_clock::universal_time() ),
@@ -86,6 +85,10 @@ bool BoostPinger::ping( const string &destination_ip )
 {
     BOOST_ASSERT( !destination_ip.empty() );
 
+    // Prepare ping
+    IoService.reset();
+    PingerStatus = PingStatus_NotSent;
+
     set_destination_endpoint( destination_ip );
 
     // if start_pinger() does not block, the PingerStatus will be equal to
index 4beadc9..127655b 100644 (file)
@@ -17,7 +17,6 @@ class BoostPinger
 {
 public:
     BoostPinger(
-            boost::asio::io_service &io_serv,
             std::string source_network_interface,
             const int echo_reply_timeout_in_sec
     );
@@ -68,7 +67,7 @@ private:
 
 private:
     /// io service object, which has the loop event
-    boost::asio::io_service &IoService;
+    boost::asio::io_service IoService;
     /// the destination host
     boost::asio::ip::icmp::endpoint DestinationEndpoint;
     /// the socket object
index 75bb927..3375cf8 100644 (file)
@@ -41,6 +41,7 @@ PingScheduler::PingScheduler(
     PingIntervalInSec( ping_interval_in_sec ),
     IpList( destination_address, nameserver ),
     HostAnalyzer( destination_address, ping_fail_percentage_limit, link_analyzer ),
+    Pinger(LocalNetworkInterfaceName, 5),
     Thread()
 {
 }
@@ -114,19 +115,11 @@ bool PingScheduler::resolve_ping_address()
     return true;
 }
 
-bool PingScheduler::ping( const string &destination_ip ) const
+bool PingScheduler::ping( const string &destination_ip )
 {
     BOOST_ASSERT( !destination_ip.empty() );
 
-    io_service io_serv;
-    int echo_reply_timeout_in_sec = 5; // TODO configurable: this is the timeout to WAIT FOR the ping before considering a timeout
-
-    BoostPinger pinger(
-            io_serv,
-            LocalNetworkInterfaceName,
-            echo_reply_timeout_in_sec
-    );
-    return pinger.ping( destination_ip );
+    return Pinger.ping( destination_ip );
 }
 
 void PingScheduler::setup_next_ping()
index 5337b40..77e3af4 100644 (file)
@@ -8,6 +8,7 @@
 #include <boost/shared_ptr.hpp>
 #include <boost/thread.hpp>
 
+#include "host/boostpinger.h"
 #include "dns/dnsresolver.h"
 #include "link/linkstatusanalyzer.h"
 #include "host/hoststatusanalyzer.h"
@@ -44,7 +45,7 @@ private:
     void stop_pinging();
 
     bool resolve_ping_address();
-    bool ping( const std::string &destination_ip ) const;
+    bool ping( const std::string &destination_ip );
     void setup_next_ping();
     void schedule_next_ping();
 
@@ -67,6 +68,8 @@ private:
     DnsResolver IpList;
     /// object responsible to evaluate the status of the host
     HostStatusAnalyzer HostAnalyzer;
+    /// Internal boost pinger object
+    BoostPinger Pinger;
     /// thread object
     boost::thread Thread;