Configuration: added the ping protocol fallback count.
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Wed, 7 Mar 2012 10:14:19 +0000 (07:14 -0300)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Wed, 7 Mar 2012 10:14:19 +0000 (07:14 -0300)
- This item tells how many times it must ping using one protocol before use the other.

src/config/host.cpp
src/config/host.h
src/config/option/hostpingprotocoloption.cpp
src/config/option/hostpingprotocoloption.h

index 5bb0bd9..15b8179 100644 (file)
@@ -38,6 +38,7 @@ Host::Host( string address ) :
     Address( address ),
     Port( 0 ),
     ProtocolList(),
+    ProtocolFallbackCount( 0 ),
     IntervalInSec( 0 )
 {
 }
@@ -106,6 +107,24 @@ void Host::set_ping_protocol_list( const PingProtocolList &ping_protocol_list )
 }
 
 /**
+ * @return The amount of pings to perform before change the protocol.
+ */
+int Host::get_ping_protocol_fallback_count() const
+{
+    return ProtocolFallbackCount;
+}
+
+/**
+ * @param protocol_fallback_count The amount of pings to perform before change the protocol.
+ */
+void Host::set_ping_protocol_fallback_count( const int protocol_fallback_count )
+{
+    BOOST_ASSERT( ( 0 <= protocol_fallback_count ) && ( protocol_fallback_count < numeric_limits<int>::max() ) );
+
+    this->ProtocolFallbackCount = protocol_fallback_count;
+}
+
+/**
  * @return The interval between each ping to the host.
  */
 int Host::get_interval_in_sec() const
index 8e5969f..58586b1 100644 (file)
@@ -51,6 +51,9 @@ public:
     PingProtocolList get_ping_protocol_list() const;
     void set_ping_protocol_list( const PingProtocolList &ping_protocol );
 
+    int get_ping_protocol_fallback_count() const;
+    void set_ping_protocol_fallback_count( const int protocol_fallback_count );
+
     int get_interval_in_sec() const;
     void set_interval_in_sec( const int interval_in_sec );
 
@@ -61,6 +64,8 @@ private:
     uint16_t Port;
     /// The list of protocols used to ping
     PingProtocolList ProtocolList;
+    /// The amount of pings to perform before change the protocol.
+    int ProtocolFallbackCount;
     /// The interval between each ping to the host
     int IntervalInSec;
 
index aac7025..6f1e08d 100644 (file)
@@ -26,6 +26,7 @@
 
 #include <boost/assert.hpp>
 #include <boost/foreach.hpp>
+#include <boost/lexical_cast.hpp>
 
 #include <logfunc.hpp>
 
@@ -42,7 +43,7 @@ HostPingProtocolOption::HostPingProtocolOption() :
     HostConfigurationOption(
         "host.ping-protocol",
         value< vector<string> >(),
-        "Defines which protocol will be used to ping the destination."
+        "Defines which protocols will be used to ping the destination."
     )
 {
 }
@@ -77,8 +78,11 @@ bool HostPingProtocolOption::parse(
             BOOST_ASSERT( !protocols_string.empty() );
 
             HostItem host_item = *hosts_list_iterator;
-            PingProtocolList host_protocols = parse_protocol_list( protocols_string );
+            PingProtocolList host_protocols;
+            int protocol_fallback_count = 0;
+            parse_protocol_list( protocols_string, &host_protocols, &protocol_fallback_count );
             host_item->set_ping_protocol_list( host_protocols );
+            host_item->set_ping_protocol_fallback_count( protocol_fallback_count );
             ++hosts_list_iterator;
 
             GlobalLogger.info() << get_command_string() << "="
@@ -95,13 +99,16 @@ bool HostPingProtocolOption::parse(
     return parsed_success;
 }
 
-PingProtocolList HostPingProtocolOption::parse_protocol_list(
-        const std::string &protocol_list_string
+void HostPingProtocolOption::parse_protocol_list(
+        const std::string &protocol_list_string,
+        PingProtocolList *protocol_list,
+        int *protocol_fallback_count
+
 ) const
 {
     BOOST_ASSERT( !protocol_list_string.empty() );
 
-    PingProtocolList protocol_list;
+    size_t protocol_index = 0;
     string protocol_string;
     istringstream iss( protocol_list_string );
 
@@ -109,15 +116,31 @@ PingProtocolList HostPingProtocolOption::parse_protocol_list(
     {
         BOOST_ASSERT( !protocol_string.empty());
 
-        PingProtocol protocol = get_ping_protocol_from_string( protocol_string );
-        protocol_list.push_back( protocol );
+        // The second item in the list is the amount of pings to perform before change from the
+        // first protocol to the second (e.g. ICMP,10,ICMPv6).
+        if ( protocol_index == 1 )
+        {
+            try
+            {
+                *protocol_fallback_count = boost::lexical_cast< int >( protocol_string );
+            }
+            catch ( boost::bad_lexical_cast const &ex )
+            {
+                GlobalLogger.error() << "Error: input string was not valid" << endl;
+            }
+        }
+        else
+        {
+            PingProtocol protocol = get_ping_protocol_from_string( protocol_string );
+            protocol_list->push_back( protocol );
 
-        GlobalLogger.debug() << "- " << protocol_string;
+            GlobalLogger.debug() << "- " << protocol_string;
 
-        BOOST_ASSERT( ( PingProtocol_First <= protocol ) && ( protocol <= PingProtocol_Last ) );
-    }
+            BOOST_ASSERT( ( PingProtocol_First <= protocol ) && ( protocol <= PingProtocol_Last ) );
+        }
 
-    BOOST_ASSERT( 0 < protocol_list.size() );
+        protocol_index++;
+    }
 
-    return protocol_list;
+    BOOST_ASSERT( 0 < protocol_list->size() );
 }
index a920318..3171992 100644 (file)
@@ -45,8 +45,10 @@ public:
     );
 
 private:
-    PingProtocolList parse_protocol_list(
-            const std::string &protocol_list_string
+    void parse_protocol_list(
+            const std::string &protocol_list_string,
+            PingProtocolList *protocol_list,
+            int *protocol_fallback_count
     ) const;
 
 };