From: Thomas Jarosch Date: Wed, 4 May 2011 13:39:50 +0000 (+0200) Subject: Reuse BoostPinger object X-Git-Tag: v1.0~26 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=e1e27b3cf92ef262c72c20b37cb793029b8b072a;p=pingcheck Reuse BoostPinger object --- diff --git a/src/host/boostpinger.cpp b/src/host/boostpinger.cpp index c8af357..a67378c 100644 --- a/src/host/boostpinger.cpp +++ b/src/host/boostpinger.cpp @@ -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 diff --git a/src/host/boostpinger.h b/src/host/boostpinger.h index 4beadc9..127655b 100644 --- a/src/host/boostpinger.h +++ b/src/host/boostpinger.h @@ -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 diff --git a/src/host/pingscheduler.cpp b/src/host/pingscheduler.cpp index 75bb927..3375cf8 100644 --- a/src/host/pingscheduler.cpp +++ b/src/host/pingscheduler.cpp @@ -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() diff --git a/src/host/pingscheduler.h b/src/host/pingscheduler.h index 5337b40..77e3af4 100644 --- a/src/host/pingscheduler.h +++ b/src/host/pingscheduler.h @@ -8,6 +8,7 @@ #include #include +#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;