From 3fd74a53a7c44a74808f0caee066dcbbeb5e7dbe Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Thu, 14 Jul 2011 23:36:08 -0300 Subject: [PATCH] Moving the PingProtocol enumeration from PingerFactory to its own file - Added a method to convert from string to the enumeration --- src/CMakeLists.txt | 1 + src/host/pingerfactory.h | 9 ++------- src/host/pingprotocol.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ src/host/pingprotocol.h | 36 ++++++++++++++++++++++++++++++++++++ src/host/pingscheduler.cpp | 2 +- src/host/pingscheduler.h | 3 ++- src/main.cpp | 3 ++- 7 files changed, 84 insertions(+), 10 deletions(-) create mode 100644 src/host/pingprotocol.cpp create mode 100644 src/host/pingprotocol.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9418434..eaa04f8 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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 diff --git a/src/host/pingerfactory.h b/src/host/pingerfactory.h index fc93cf0..c708ab3 100644 --- a/src/host/pingerfactory.h +++ b/src/host/pingerfactory.h @@ -21,6 +21,8 @@ #ifndef PINGERFACTORY_H #define PINGERFACTORY_H +#include "host/pingprotocol.h" + //----------------------------------------------------------------------------- // PingerFactory //----------------------------------------------------------------------------- @@ -28,13 +30,6 @@ 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 index 0000000..79ff9c5 --- /dev/null +++ b/src/host/pingprotocol.cpp @@ -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 + +#include + +using namespace std; + +map 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 index 0000000..ebbcccc --- /dev/null +++ b/src/host/pingprotocol.h @@ -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 + +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 */ diff --git a/src/host/pingscheduler.cpp b/src/host/pingscheduler.cpp index f550bc6..c5250b9 100644 --- a/src/host/pingscheduler.cpp +++ b/src/host/pingscheduler.cpp @@ -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, diff --git a/src/host/pingscheduler.h b/src/host/pingscheduler.h index 52f036f..84384d6 100644 --- a/src/host/pingscheduler.h +++ b/src/host/pingscheduler.h @@ -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, diff --git a/src/main.cpp b/src/main.cpp index 0f0dc55..281d754 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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, -- 1.7.1