Moving the PingProtocol enumeration from PingerFactory to its own file
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Fri, 15 Jul 2011 02:36:08 +0000 (23:36 -0300)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Fri, 15 Jul 2011 02:36:08 +0000 (23:36 -0300)
- Added a method to convert from string to the enumeration

src/CMakeLists.txt
src/host/pingerfactory.h
src/host/pingprotocol.cpp [new file with mode: 0644]
src/host/pingprotocol.h [new file with mode: 0644]
src/host/pingscheduler.cpp
src/host/pingscheduler.h
src/main.cpp

index 9418434..eaa04f8 100644 (file)
@@ -37,6 +37,7 @@ set(SOURCES
     host/hoststatusanalyzer.cpp
     host/pinger.cpp
     host/pinginterval.cpp
+    host/pingprotocol.cpp
     host/pingscheduler.cpp
     icmp/icmpdestinationunreachablemessage.cpp
     icmp/icmpechoreplymessage.cpp
index fc93cf0..c708ab3 100644 (file)
@@ -21,6 +21,8 @@
 #ifndef PINGERFACTORY_H
 #define PINGERFACTORY_H
 
+#include "host/pingprotocol.h"
+
 //-----------------------------------------------------------------------------
 // PingerFactory
 //-----------------------------------------------------------------------------
 class PingerFactory
 {
 public:
-    enum PingProtocol
-    {
-        PingProtocol_ICMP,
-        PingProtocol_TCP
-    };
-
-public:
     PingerFactory();
     virtual ~PingerFactory();
 
diff --git a/src/host/pingprotocol.cpp b/src/host/pingprotocol.cpp
new file mode 100644 (file)
index 0000000..79ff9c5
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+The software in this package is distributed under the GNU General
+Public License version 2 (with a special exception described below).
+
+A copy of GNU General Public License (GPL) is included in this distribution,
+in the file COPYING.GPL.
+
+As a special exception, if other files instantiate templates or use macros
+or inline functions from this file, or you compile this file and link it
+with other works to produce a work based on this file, this file
+does not by itself cause the resulting work to be covered
+by the GNU General Public License.
+
+However the source code for this file must still be made available
+in accordance with section (3) of the GNU General Public License.
+
+This exception does not invalidate any other reasons why a work based
+on this file might be covered by the GNU General Public License.
+*/
+
+#include "host/pingprotocol.h"
+
+#include <map>
+
+#include <boost/assert.hpp>
+
+using namespace std;
+
+map<string, PingProtocol> protocol_string_map;
+
+PingProtocol get_ping_protocol_from_string( string protocol_string )
+{
+    BOOST_ASSERT( !protocol_string.empty() );
+
+    // TODO move to an init method
+    protocol_string_map[ "ICMP" ] = PingProtocol_ICMP;
+    protocol_string_map[ "TCP" ] = PingProtocol_TCP;
+
+    return protocol_string_map[ protocol_string ];
+}
diff --git a/src/host/pingprotocol.h b/src/host/pingprotocol.h
new file mode 100644 (file)
index 0000000..ebbcccc
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ The software in this package is distributed under the GNU General
+ Public License version 2 (with a special exception described below).
+
+ A copy of GNU General Public License (GPL) is included in this distribution,
+ in the file COPYING.GPL.
+
+ As a special exception, if other files instantiate templates or use macros
+ or inline functions from this file, or you compile this file and link it
+ with other works to produce a work based on this file, this file
+ does not by itself cause the resulting work to be covered
+ by the GNU General Public License.
+
+ However the source code for this file must still be made available
+ in accordance with section (3) of the GNU General Public License.
+
+ This exception does not invalidate any other reasons why a work based
+ on this file might be covered by the GNU General Public License.
+ */
+
+#ifndef PINGPROTOCOL_H
+#define PINGPROTOCOL_H
+
+#include <string>
+
+enum PingProtocol
+{
+    PingProtocol_First = 0,
+    PingProtocol_ICMP = 0,
+    PingProtocol_TCP,
+    PingProtocol_Last = PingProtocol_TCP
+};
+
+PingProtocol get_ping_protocol_from_string( std::string protocol_string );
+
+#endif /* PINGPROTOCOL_H */
index f550bc6..c5250b9 100644 (file)
@@ -50,7 +50,7 @@ const int PingReplyTimeout = 30;
 PingScheduler::PingScheduler(
         const string &network_interface,
         const string &destination_address,
-        const PingerFactory::PingProtocol /*ping_protocol*/,
+        const PingProtocol /*ping_protocol*/,
         const long ping_interval_in_sec,
         const int ping_fail_percentage_limit,
         const string &nameserver,
index 52f036f..84384d6 100644 (file)
@@ -33,6 +33,7 @@ on this file might be covered by the GNU General Public License.
 #include "host/pinger.h"
 #include "host/pingerfactory.h"
 #include "host/pinginterval.h"
+#include "host/pingprotocol.h"
 #include "icmp/icmppinger.h"
 
 //-----------------------------------------------------------------------------
@@ -50,7 +51,7 @@ public:
     PingScheduler(
             const std::string &network_interface,
             const std::string &destination_address,
-            const PingerFactory::PingProtocol ping_protocol,
+            const PingProtocol ping_protocol,
             const long ping_interval_in_sec,
             const int ping_fail_percentage_limit,
             const std::string &nameserver,
index 0f0dc55..281d754 100644 (file)
@@ -33,6 +33,7 @@ on this file might be covered by the GNU General Public License.
 #include "config/host.h"
 #include "link/linkstatusanalyzer.h"
 #include "host/pingerfactory.h"
+#include "host/pingprotocol.h"
 #include "host/pingscheduler.h"
 
 using namespace std;
@@ -93,6 +94,7 @@ void init_pingers(
         PingSchedulerList *scheduler_list
 )
 {
+    PingProtocol protocol = configuration->get_ping_protocol();
     string local_interface = configuration->get_source_network_interface();
     string nameserver = configuration->get_nameserver();
     int ping_fail_limit = configuration->get_ping_fail_limit();
@@ -102,7 +104,6 @@ void init_pingers(
     {
         string destination_address = host->get_address();
         int ping_interval_in_sec = host->get_interval_in_sec();
-        PingerFactory::PingProtocol protocol = PingerFactory::PingProtocol_ICMP;
         PingSchedulerItem scheduler(
                 new PingScheduler(
                         local_interface,