From e58d750735048b76721ade950d3b9e0c75d201f7 Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Tue, 28 Feb 2012 21:12:01 -0300 Subject: [PATCH] Code improvement: - Replaced io_service references by shared pointers, because this ensures that this object is shared. --- src/host/pingerfactory.cpp | 4 ++-- src/host/pingerfactory.h | 4 ++-- src/host/pingrotate.cpp | 3 ++- src/host/pingrotate.h | 5 +++-- src/host/pingscheduler.cpp | 21 ++++++++++++--------- src/icmp/icmppinger.cpp | 7 ++++--- src/icmp/icmppinger.h | 5 +++-- src/tcp/tcppinger.cpp | 7 ++++--- src/tcp/tcppinger.h | 13 +++++++------ 9 files changed, 39 insertions(+), 30 deletions(-) diff --git a/src/host/pingerfactory.cpp b/src/host/pingerfactory.cpp index c8c17c8..a48a1af 100644 --- a/src/host/pingerfactory.cpp +++ b/src/host/pingerfactory.cpp @@ -67,7 +67,7 @@ PingerFactory::~PingerFactory() */ PingerItem PingerFactory::createPinger( const PingProtocol protocol, - io_service &io_serv, + const shared_ptr io_serv, const string &network_interface ) { @@ -124,7 +124,7 @@ PingerItem PingerFactory::createPinger( */ PingerItem PingerFactory::createPinger( const PingProtocolList &protocol_list, - io_service &io_serv, + const shared_ptr io_serv, const string &network_interface ) { diff --git a/src/host/pingerfactory.h b/src/host/pingerfactory.h index 234e541..bf9ef8b 100644 --- a/src/host/pingerfactory.h +++ b/src/host/pingerfactory.h @@ -38,13 +38,13 @@ class PingerFactory public: static PingerItem createPinger( const PingProtocol protocol, - boost::asio::io_service &io_serv, + const boost::shared_ptr io_serv, const std::string &network_interface ); static PingerItem createPinger( const PingProtocolList &protocol_list, - boost::asio::io_service &io_serv, + const boost::shared_ptr io_serv, const std::string &network_interface ); diff --git a/src/host/pingrotate.cpp b/src/host/pingrotate.cpp index 9e9acc0..9a2354d 100644 --- a/src/host/pingrotate.cpp +++ b/src/host/pingrotate.cpp @@ -30,6 +30,7 @@ using namespace std; using boost::asio::io_service; using boost::function; +using boost::shared_ptr; //----------------------------------------------------------------------------- // PingRotate @@ -45,7 +46,7 @@ using boost::function; * host. The protocols will be used in the order they are in the list. */ PingRotate::PingRotate( - io_service &io_serv, + const shared_ptr io_serv, const string &network_interface, PingProtocolList protocol_rotation_list ) : diff --git a/src/host/pingrotate.h b/src/host/pingrotate.h index 42a3563..3e2ab58 100644 --- a/src/host/pingrotate.h +++ b/src/host/pingrotate.h @@ -27,6 +27,7 @@ #include #include +#include #include "host/pinger.h" #include "host/pingprotocol.h" @@ -44,7 +45,7 @@ class PingRotate : public Pinger { public: PingRotate( - boost::asio::io_service &io_serv, + const boost::shared_ptr io_serv, const std::string &network_interface, PingProtocolList protocol_rotation_list ); @@ -71,7 +72,7 @@ private: // /// The IO service object, which has the loop event - boost::asio::io_service &IoService; + boost::shared_ptr IoService; /// The network interface name std::string NetworkInterfaceName; /// The list of protocols to ping diff --git a/src/host/pingscheduler.cpp b/src/host/pingscheduler.cpp index 67fa690..cd31aa4 100644 --- a/src/host/pingscheduler.cpp +++ b/src/host/pingscheduler.cpp @@ -39,6 +39,7 @@ using boost::posix_time::microsec_clock; using boost::posix_time::ptime; using boost::posix_time::seconds; using boost::thread; +using boost::shared_ptr; using I2n::Logger::GlobalLogger; //----------------------------------------------------------------------------- @@ -86,7 +87,9 @@ PingScheduler::PingScheduler( BOOST_ASSERT( (0 <= ping_fail_percentage_limit) && (ping_fail_percentage_limit <= 100) ); BOOST_ASSERT( !nameserver.empty() ); - Ping = PingerFactory::createPinger( ping_protocol_list, IoService, network_interface ); + shared_ptr io_serv( &IoService ); + + Ping = PingerFactory::createPinger( ping_protocol_list, io_serv, network_interface ); } /** @@ -187,12 +190,13 @@ void PingScheduler::setup_next_ping() bool address_resolved = resolve_ping_address(); if ( !address_resolved ) { - GlobalLogger.error() << "Error: could not update host " - "IP, may use outdated address" << endl; + GlobalLogger.error() << "Error: could not update host IP, may use outdated address" + << endl; } } string destination_ip = IpList.get_next_ip(); + ping( destination_ip, DestinationPort ); } @@ -230,15 +234,15 @@ void PingScheduler::update_ping_interval() { PingIntervalInSec.speed_up(); - GlobalLogger.info() << "- Speeding up ping interval to: " - << PingIntervalInSec << "s" << endl; + GlobalLogger.info() << "- Speeding up ping interval to: " << PingIntervalInSec << "s" + << endl; } else { PingIntervalInSec.back_to_original(); - GlobalLogger.info() << "- Stick to the original ping interval: " - << PingIntervalInSec << "s" << endl; + GlobalLogger.info() << "- Stick to the original ping interval: " << PingIntervalInSec << "s" + << endl; } } @@ -247,8 +251,7 @@ void PingScheduler::update_ping_elapsed_time() ptime now = microsec_clock::universal_time(); time_resolution_traits_adapted64_impl::int_type elapsed_time_in_sec = (now - TimeSentLastPing).total_seconds(); - GlobalLogger.info() << "- Time elapsed since last ping: " - << elapsed_time_in_sec << "s" << endl; + GlobalLogger.info() << "- Time elapsed since last ping: " << elapsed_time_in_sec << "s" << endl; TimeSentLastPing = microsec_clock::universal_time(); } diff --git a/src/icmp/icmppinger.cpp b/src/icmp/icmppinger.cpp index 720959d..07c8415 100644 --- a/src/icmp/icmppinger.cpp +++ b/src/icmp/icmppinger.cpp @@ -31,6 +31,7 @@ using boost::asio::ip::icmp; using boost::function; using boost::posix_time::microsec_clock; using boost::posix_time::seconds; +using boost::shared_ptr; using I2n::Logger::GlobalLogger; //----------------------------------------------------------------------------- @@ -47,7 +48,7 @@ using I2n::Logger::GlobalLogger; * @param echo_reply_timeout_in_sec The amount of time to wait for a reply. */ IcmpPinger::IcmpPinger( - io_service &io_serv, + const shared_ptr io_serv, const icmp::socket::protocol_type &protocol, const string &source_network_interface, const int echo_reply_timeout_in_sec @@ -55,9 +56,9 @@ IcmpPinger::IcmpPinger( IoService( io_serv ), DestinationEndpoint(), Protocol( protocol ), - Socket( IoService, Protocol ), + Socket( *IoService, Protocol ), NetInterface( source_network_interface, Socket ), - IcmpPacketReceiveTimer( IoService ), + IcmpPacketReceiveTimer( *IoService ), Identifier( 0 ), SequenceNumber( 0 ), TimeSent( microsec_clock::universal_time() ), diff --git a/src/icmp/icmppinger.h b/src/icmp/icmppinger.h index f92b33b..80033e4 100644 --- a/src/icmp/icmppinger.h +++ b/src/icmp/icmppinger.h @@ -13,6 +13,7 @@ #include #include +#include #include "host/networkinterface.hpp" #include "host/pinger.h" @@ -31,7 +32,7 @@ class IcmpPinger : public Pinger { public: IcmpPinger( - boost::asio::io_service &io_serv, + const boost::shared_ptr io_serv, const boost::asio::ip::icmp::socket::protocol_type &protocol, const std::string &source_network_interface, const int echo_reply_timeout_in_sec @@ -59,7 +60,7 @@ private: private: /// The IO service object, which has the loop event - boost::asio::io_service &IoService; + boost::shared_ptr IoService; /// The destination host boost::asio::ip::icmp::endpoint DestinationEndpoint; /// Network layer protocol used to ping, IPv4 or IPv6 diff --git a/src/tcp/tcppinger.cpp b/src/tcp/tcppinger.cpp index 388a762..f8dc685 100644 --- a/src/tcp/tcppinger.cpp +++ b/src/tcp/tcppinger.cpp @@ -49,6 +49,7 @@ using boost::function; using boost::posix_time::microsec_clock; using boost::posix_time::ptime; using boost::posix_time::seconds; +using boost::shared_ptr; using I2n::Logger::GlobalLogger; //----------------------------------------------------------------------------- @@ -65,7 +66,7 @@ using I2n::Logger::GlobalLogger; * @param echo_reply_timeout_in_sec The amount of time to wait for a reply. */ TcpPinger::TcpPinger( - io_service &io_serv, + const shared_ptr io_serv, const tcp_raw_protocol::socket::protocol_type &protocol, const string &source_network_interface_name, const int rst_reply_timeout_in_sec @@ -73,9 +74,9 @@ TcpPinger::TcpPinger( IoService( io_serv ), DestinationEndpoint(), Protocol( protocol ), - Socket( IoService, Protocol ), + Socket( *IoService, Protocol ), NetInterface( source_network_interface_name, Socket ), - TcpSegmentReceiveTimer( IoService ), + TcpSegmentReceiveTimer( *IoService ), Identifier( 0 ), SequenceNumber( 0 ), TimeSent( microsec_clock::universal_time() ), diff --git a/src/tcp/tcppinger.h b/src/tcp/tcppinger.h index 96247b0..127857c 100644 --- a/src/tcp/tcppinger.h +++ b/src/tcp/tcppinger.h @@ -27,6 +27,7 @@ on this file might be covered by the GNU General Public License. #include #include #include +#include #include "host/networkinterface.hpp" #include "host/pinger.h" @@ -45,7 +46,7 @@ class TcpPinger : public Pinger { public: TcpPinger( - boost::asio::io_service &io_service, + const boost::shared_ptr io_serv, const boost::asio::ip::tcp_raw_protocol::socket::protocol_type &protocol, const std::string &source_network_interface_name, const int rst_reply_timeout_in_sec @@ -81,9 +82,9 @@ private: void set_ping_status( PingStatus ping_status ); private: - /// io service object, which has the loop event - boost::asio::io_service &IoService; - /// the destination host + /// IO service object, which has the loop event + boost::shared_ptr IoService; + /// The destination host boost::asio::ip::tcp_raw_protocol::endpoint DestinationEndpoint; /// Network layer protocol used to ping, IPv4 or IPv6 boost::asio::ip::tcp_raw_protocol::socket::protocol_type Protocol; @@ -104,9 +105,9 @@ private: boost::asio::streambuf ReplyBuffer; /// A flag to indicate if we got a reply or not bool ReceivedReply; - /// the amount of time to wait for the reply + /// The amount of time to wait for the reply int RstReplyTimeoutInSec; - /// the status of the pinger + /// The status of the pinger PingStatus PingerStatus; /// Callback to notify when the ping is done (got reply/timeout) boost::function< void(bool) > PingDoneCallback; -- 1.7.1