Code improvement:
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Wed, 29 Feb 2012 00:12:01 +0000 (21:12 -0300)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Wed, 29 Feb 2012 00:13:39 +0000 (21:13 -0300)
- Replaced io_service references by shared pointers, because
  this ensures that this object is shared.

src/host/pingerfactory.cpp
src/host/pingerfactory.h
src/host/pingrotate.cpp
src/host/pingrotate.h
src/host/pingscheduler.cpp
src/icmp/icmppinger.cpp
src/icmp/icmppinger.h
src/tcp/tcppinger.cpp
src/tcp/tcppinger.h

index c8c17c8..a48a1af 100644 (file)
@@ -67,7 +67,7 @@ PingerFactory::~PingerFactory()
  */
 PingerItem PingerFactory::createPinger(
         const PingProtocol protocol,
-        io_service &io_serv,
+        const shared_ptr<io_service> 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_service> io_serv,
         const string &network_interface
 )
 {
index 234e541..bf9ef8b 100644 (file)
@@ -38,13 +38,13 @@ class PingerFactory
 public:
     static PingerItem createPinger(
             const PingProtocol protocol,
-            boost::asio::io_service &io_serv,
+            const boost::shared_ptr<boost::asio::io_service> io_serv,
             const std::string &network_interface
     );
 
     static PingerItem createPinger(
             const PingProtocolList &protocol_list,
-            boost::asio::io_service &io_serv,
+            const boost::shared_ptr<boost::asio::io_service> io_serv,
             const std::string &network_interface
     );
 
index 9e9acc0..9a2354d 100644 (file)
@@ -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_service> io_serv,
         const string &network_interface,
         PingProtocolList protocol_rotation_list
 ) :
index 42a3563..3e2ab58 100644 (file)
@@ -27,6 +27,7 @@
 
 #include <boost/asio.hpp>
 #include <boost/function.hpp>
+#include <boost/shared_ptr.hpp>
 
 #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<boost::asio::io_service> 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<boost::asio::io_service> IoService;
     /// The network interface name
     std::string NetworkInterfaceName;
     /// The list of protocols to ping
index 67fa690..cd31aa4 100644 (file)
@@ -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_service> 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();
 }
index 720959d..07c8415 100644 (file)
@@ -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_service> 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() ),
index f92b33b..80033e4 100644 (file)
@@ -13,6 +13,7 @@
 
 #include <boost/asio.hpp>
 #include <boost/function.hpp>
+#include <boost/shared_ptr.hpp>
 
 #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<boost::asio::io_service> 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<boost::asio::io_service> IoService;
     /// The destination host
     boost::asio::ip::icmp::endpoint DestinationEndpoint;
     /// Network layer protocol used to ping, IPv4 or IPv6
index 388a762..f8dc685 100644 (file)
@@ -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_service> 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() ),
index 96247b0..127857c 100644 (file)
@@ -27,6 +27,7 @@ on this file might be covered by the GNU General Public License.
 #include <boost/asio.hpp>
 #include <boost/asio/ip/tcp_raw_protocol.hpp>
 #include <boost/function.hpp>
+#include <boost/shared_ptr.hpp>
 
 #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<boost::asio::io_service> 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<boost::asio::io_service> 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;