From 72e54d1c70a595e4b690d532ab4be6ee6149b442 Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Wed, 25 Jan 2012 22:39:46 -0200 Subject: [PATCH] Renamed LinkStatusAnalyzer to LinkStatus. - This is a stronger noun name; - See "The Gravitational Pull of Functional Decomposition" at http://www.eetimes.com/design/embedded/4006522/Using-object-oriented-methods-to-achieve-C--s-promise-Part-3 --- src/CMakeLists.txt | 2 +- src/host/hoststatus.cpp | 2 +- src/host/hoststatus.h | 6 +- src/host/pingscheduler.cpp | 4 +- src/host/pingscheduler.h | 4 +- src/link/linkstatus.cpp | 256 +++++++++++++++++++++++++++++++++++++++ src/link/linkstatus.h | 113 +++++++++++++++++ src/link/linkstatusanalyzer.cpp | 256 --------------------------------------- src/link/linkstatusanalyzer.h | 113 ----------------- src/main.cpp | 12 +- test/mock_linkstatus.h | 71 +++++++++++ test/mock_linkstatusanalyzer.h | 71 ----------- test/test_hoststatus.cpp | 8 +- 13 files changed, 459 insertions(+), 459 deletions(-) create mode 100644 src/link/linkstatus.cpp create mode 100644 src/link/linkstatus.h delete mode 100644 src/link/linkstatusanalyzer.cpp delete mode 100644 src/link/linkstatusanalyzer.h create mode 100644 test/mock_linkstatus.h delete mode 100644 test/mock_linkstatusanalyzer.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b0fd7ab..d578b39 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -79,7 +79,7 @@ set(SOURCES icmp/icmppinger.cpp ip/ipv4header.cpp ip/ipv6header.cpp - link/linkstatusanalyzer.cpp + link/linkstatus.cpp link/statusnotifiercommand.cpp tcp/tcpheader.cpp tcp/tcppinger.cpp diff --git a/src/host/hoststatus.cpp b/src/host/hoststatus.cpp index dbe9e96..d7634ee 100644 --- a/src/host/hoststatus.cpp +++ b/src/host/hoststatus.cpp @@ -39,7 +39,7 @@ using boost::shared_ptr; HostStatus::HostStatus( const string &host_address, const int ping_fail_limit_percentage, - const shared_ptr link_analyzer + const shared_ptr link_analyzer ) : HostAddress( host_address ), LinkAnalyzer( link_analyzer ), diff --git a/src/host/hoststatus.h b/src/host/hoststatus.h index 491fc39..e01deb2 100644 --- a/src/host/hoststatus.h +++ b/src/host/hoststatus.h @@ -24,7 +24,7 @@ on this file might be covered by the GNU General Public License. #include -#include "link/linkstatusanalyzer.h" +#include "link/linkstatus.h" //----------------------------------------------------------------------------- // HostStatus @@ -41,7 +41,7 @@ public: HostStatus( const std::string &host_address, const int ping_fail_limit_percentage, - const boost::shared_ptr link_analyzer + const boost::shared_ptr link_analyzer ); ~HostStatus(); @@ -61,7 +61,7 @@ private: /// the DNS address of the host to analyze std::string HostAddress; /// the object responsible to analyze the link - const boost::shared_ptr LinkAnalyzer; + const boost::shared_ptr LinkAnalyzer; /// the maximum amount of pings that can fail without warning int PingFailLimitPercentage; /// the amount of IPs that are aliases to the host DNS diff --git a/src/host/pingscheduler.cpp b/src/host/pingscheduler.cpp index 93bfb93..da3b742 100644 --- a/src/host/pingscheduler.cpp +++ b/src/host/pingscheduler.cpp @@ -28,7 +28,7 @@ on this file might be covered by the GNU General Public License. #include "dns/dnsresolver.h" #include "icmp/icmppinger.h" -#include "link/linkstatusanalyzer.h" +#include "link/linkstatus.h" using namespace std; using boost::asio::io_service; @@ -53,7 +53,7 @@ PingScheduler::PingScheduler( const long ping_interval_in_sec, const int ping_fail_percentage_limit, const string &nameserver, - shared_ptr link_analyzer + shared_ptr link_analyzer ) : IoService(), diff --git a/src/host/pingscheduler.h b/src/host/pingscheduler.h index 3f7f038..f916565 100644 --- a/src/host/pingscheduler.h +++ b/src/host/pingscheduler.h @@ -28,7 +28,7 @@ on this file might be covered by the GNU General Public License. #include #include "dns/dnsresolver.h" -#include "link/linkstatusanalyzer.h" +#include "link/linkstatus.h" #include "host/hoststatus.h" #include "host/pinger.h" #include "host/pingerfactory.h" @@ -56,7 +56,7 @@ public: const long ping_interval_in_sec, const int ping_fail_percentage_limit, const std::string &nameserver, - boost::shared_ptr link_analyzer + boost::shared_ptr link_analyzer ); ~PingScheduler(); diff --git a/src/link/linkstatus.cpp b/src/link/linkstatus.cpp new file mode 100644 index 0000000..3c5bab9 --- /dev/null +++ b/src/link/linkstatus.cpp @@ -0,0 +1,256 @@ +/* +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 "link/linkstatus.h" + +#include + +#include + +#include + +using namespace std; +using boost::lock_guard; +using boost::mutex; +using boost::posix_time::microsec_clock; +using boost::posix_time::ptime; +using I2n::Logger::GlobalLogger; + +typedef lock_guard mutex_lock_guard; + +//----------------------------------------------------------------------------- +// LinkStatus +//----------------------------------------------------------------------------- + +/** + * @brief Creates a link status object. + * + * @param hosts_down_limit The maximum amount of different hosts that can be + * down before the system take any action. + * @param link_up_interval_in_min The amount of time required to the link to + * stay up before notify. + * @param link_down_interval_in_min The amount of time required to the link to + * stay down before notify. + * @param status_notifier_cmd The command used to notify about link status + * changes. + */ +LinkStatus::LinkStatus( + const int hosts_down_limit, + const int link_up_interval_in_min, + const int link_down_interval_in_min, + const string &status_notifier_cmd +) : + HostsDownLimit( hosts_down_limit ), + HostsDownList(), + LinkUpIntervalInMin( link_up_interval_in_min ), + LinkDownIntervalInMin( link_down_interval_in_min ), + CurrentLinkStatus( Status_Down ), + CurrentNotificationStatus( NotificationStatus_NotReported ), + TimeLinkStatusChanged( microsec_clock::universal_time() ), + StatusNotifierCmd( status_notifier_cmd ), + Mutex() +{ + BOOST_ASSERT( 0 <= hosts_down_limit ); + BOOST_ASSERT( 0 <= link_up_interval_in_min ); + BOOST_ASSERT( 0 <= link_down_interval_in_min ); +} + +LinkStatus::~LinkStatus() +{ +} + +/** + * @brief Notify the system that a given host is up. The object takes an + * appropriated action to deal with that. + * Note: this object does not resolves IPs, thus you have to send the same host + * address in order to the object to consider the same host. + * + * @param host_address the DNS/IP address of the host that is up. + */ +void LinkStatus::notify_host_up( const string &host_address ) +{ + BOOST_ASSERT( !host_address.empty() ); + + mutex_lock_guard lock( Mutex ); + + GlobalLogger.info() << "- Host up: " << host_address << endl; + + add_host_up( host_address ); + + if ( !exceeded_host_down_limit() ) + { + notify_link_up(); + } + + // removed from the list? + BOOST_ASSERT( HostsDownList.count( host_address ) == 0 ); +} + +/** + * @brief Notify the system that a given host is down. The object takes an + * appropriated action to deal with that. + * Note: this object does not resolves IPs, thus you have to send the same host + * address in order to the object to consider the same host. + * + * @param host_address The DNS/IP address of the host that is down. + */ +void LinkStatus::notify_host_down( const string &host_address ) +{ + BOOST_ASSERT( !host_address.empty() ); + + mutex_lock_guard lock( Mutex ); + + GlobalLogger.info() << "- Host down: " << host_address << endl; + + add_host_down( host_address ); + + if ( exceeded_host_down_limit() ) + { + notify_link_down(); + } + + // inserted in the list? + BOOST_ASSERT( HostsDownList.count( host_address ) == 1 ); +} + +void LinkStatus::add_host_up( const string &host_address ) +{ + if ( HostsDownList.count( host_address ) > 0 ) + { + size_t erased_host_count = HostsDownList.erase( host_address ); + + BOOST_ASSERT( erased_host_count == 1 ); + } +} + +void LinkStatus::add_host_down( const string &host_address ) +{ + (void) HostsDownList.insert( host_address ); +} + +bool LinkStatus::exceeded_host_down_limit() const +{ + int host_down_count = static_cast( HostsDownList.size() ); + + return ( host_down_count > HostsDownLimit ); +} + +void LinkStatus::notify_link_up() +{ + set_link_status( Status_Up ); + + // report the link status only if: it is up longer than a configured amount + // of time, and if we haven't reported the new status yet + if ( is_link_up_enough_time() && can_report_link_status() ) + { + BOOST_ASSERT( CurrentLinkStatus == Status_Up ); + BOOST_ASSERT( CurrentNotificationStatus == NotificationStatus_NotReported ); + + StatusNotifierCmd.set_token_value( + StatusNotifierCommand::StatusToken, + "up" + ); //lint !e534 + + bool executed = StatusNotifierCmd.execute(); + + if ( executed ) + { + CurrentNotificationStatus = NotificationStatus_Reported; + } + } +} + +void LinkStatus::notify_link_down() +{ + set_link_status( Status_Down ); + + // report the link status only if: it is down longer than a configured amount + // of time, and if we haven't reported the new status yet + if ( is_link_down_enough_time() && can_report_link_status() ) + { + BOOST_ASSERT( CurrentLinkStatus == Status_Down ); + BOOST_ASSERT( CurrentNotificationStatus == NotificationStatus_NotReported ); + + StatusNotifierCmd.set_token_value( + StatusNotifierCommand::StatusToken, + "down" + ); //lint !e534 + + bool executed = StatusNotifierCmd.execute(); + + if ( executed ) + { + CurrentNotificationStatus = NotificationStatus_Reported; + } + } +} + +bool LinkStatus::is_link_up_enough_time() const +{ + if ( CurrentLinkStatus == Status_Up ) + { + ptime now = microsec_clock::universal_time(); + long amount_time_link_is_up = (now - TimeLinkStatusChanged).total_seconds(); + long link_up_interval_in_sec = LinkUpIntervalInMin * 60; + + if ( amount_time_link_is_up >= link_up_interval_in_sec ) + { + return true; + } + } + + return false; +} + +bool LinkStatus::is_link_down_enough_time() const +{ + if ( CurrentLinkStatus == Status_Down ) + { + ptime now = microsec_clock::universal_time(); + long amount_time_link_is_down = (now - TimeLinkStatusChanged).total_seconds(); + long link_down_interval_in_sec = LinkDownIntervalInMin * 60; + + if ( amount_time_link_is_down >= link_down_interval_in_sec ) + { + return true; + } + } + + return false; +} + +bool LinkStatus::can_report_link_status() const +{ + return ( CurrentNotificationStatus == NotificationStatus_NotReported ); +} + +void LinkStatus::set_link_status( + const LinkStatus::Status new_link_status +) +{ + // only reset the control flags if the link status has changed + if ( new_link_status != CurrentLinkStatus ) + { + CurrentLinkStatus = new_link_status; + TimeLinkStatusChanged = microsec_clock::universal_time(); + + // have to report the link status change + CurrentNotificationStatus = NotificationStatus_NotReported; + } +} diff --git a/src/link/linkstatus.h b/src/link/linkstatus.h new file mode 100644 index 0000000..dcd30e0 --- /dev/null +++ b/src/link/linkstatus.h @@ -0,0 +1,113 @@ +/* +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 LINK_STATUS_H +#define LINK_STATUS_H + +#include +#include + +#include +#include +#include + +#include "link/statusnotifiercommand.h" + +//----------------------------------------------------------------------------- +// LinkStatus +//----------------------------------------------------------------------------- + +/** + * @brief This class analyzes and notifies the link status, through keeping + * track of the amount of hosts down. + * Scope: one object for many hosts. + */ +class LinkStatus +{ +public: + LinkStatus( + const int hosts_down_limit, + const int link_up_interval_in_min, + const int link_down_interval_in_min, + const std::string &status_notifier_cmd + ); + ~LinkStatus(); + + void notify_host_up( const std::string &host_address ); + void notify_host_down( const std::string &host_address ); + +private: + enum Status + { + Status_Up, + Status_Down + }; + + enum NotificationStatus + { + NotificationStatus_NotReported, + NotificationStatus_Reported + }; + +private: + void add_host_up( const std::string &host_address ); + void add_host_down( const std::string &host_address ); + + bool exceeded_host_down_limit() const; + + void notify_link_up(); + void notify_link_down(); + + bool is_link_up_enough_time() const; + bool is_link_down_enough_time() const; + + bool can_report_link_status() const; + void set_link_status( + const LinkStatus::Status new_link_status + ); + +private: + /// The maximum amount of hosts which can be down before sound the alarm + const int HostsDownLimit; + /// List of hosts down (obvious isn't it?) + std::set HostsDownList; + /// Interval the link have to be stable in order to consider it is functional + const int LinkUpIntervalInMin; + /// Interval the link have to be down in order to consider it is non-functional + const int LinkDownIntervalInMin; + /// Keep track of the actual link status + LinkStatus::Status CurrentLinkStatus; + /// Indicates if the last link status change was notified + LinkStatus::NotificationStatus CurrentNotificationStatus; + /// When was the last time the status changed + boost::posix_time::ptime TimeLinkStatusChanged; + /// Command used to notify the status of the link + StatusNotifierCommand StatusNotifierCmd; + /// Mutual exclusion variable to avoid data races + boost::mutex Mutex; + +}; + +//----------------------------------------------------------------------------- +// LinkStatusItem +//----------------------------------------------------------------------------- + +typedef boost::shared_ptr LinkStatusItem; + +#endif // LINK_STATUS_H diff --git a/src/link/linkstatusanalyzer.cpp b/src/link/linkstatusanalyzer.cpp deleted file mode 100644 index 78c6864..0000000 --- a/src/link/linkstatusanalyzer.cpp +++ /dev/null @@ -1,256 +0,0 @@ -/* -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 "link/linkstatusanalyzer.h" - -#include - -#include - -#include - -using namespace std; -using boost::lock_guard; -using boost::mutex; -using boost::posix_time::microsec_clock; -using boost::posix_time::ptime; -using I2n::Logger::GlobalLogger; - -typedef lock_guard mutex_lock_guard; - -//----------------------------------------------------------------------------- -// LinkStatusAnalyzer -//----------------------------------------------------------------------------- - -/** - * @brief Creates a link status object. - * - * @param hosts_down_limit the maximum amount of different hosts that can be - * down before the system take any action. - * @param link_up_interval_in_min the amount of time required to the link to - * stay up before notify. - * @param link_down_interval_in_min the amount of time required to the link to - * stay down before notify. - * @param status_notifier_cmd the command used to notify about link status - * changes. - */ -LinkStatusAnalyzer::LinkStatusAnalyzer( - const int hosts_down_limit, - const int link_up_interval_in_min, - const int link_down_interval_in_min, - const string &status_notifier_cmd -) : - HostsDownLimit( hosts_down_limit ), - HostsDownList(), - LinkUpIntervalInMin( link_up_interval_in_min ), - LinkDownIntervalInMin( link_down_interval_in_min ), - CurrentLinkStatus( LinkStatus_Down ), - CurrentNotificationStatus( NotificationStatus_NotReported ), - TimeLinkStatusChanged( microsec_clock::universal_time() ), - StatusNotifierCmd( status_notifier_cmd ), - Mutex() -{ - BOOST_ASSERT( 0 <= hosts_down_limit ); - BOOST_ASSERT( 0 <= link_up_interval_in_min ); - BOOST_ASSERT( 0 <= link_down_interval_in_min ); -} - -LinkStatusAnalyzer::~LinkStatusAnalyzer() -{ -} - -/** - * @brief Notify the system that a given host is up. The object takes an - * appropriated action to deal with that. - * Note: this object does not resolves IPs, thus you have to send the same host - * address in order to the object to consider the same host. - * - * @param host_address the DNS/IP address of the host that is up. - */ -void LinkStatusAnalyzer::notify_host_up( const string &host_address ) -{ - BOOST_ASSERT( !host_address.empty() ); - - mutex_lock_guard lock( Mutex ); - - GlobalLogger.info() << "- Host up: " << host_address << endl; - - add_host_up( host_address ); - - if ( !exceeded_host_down_limit() ) - { - notify_link_up(); - } - - // removed from the list? - BOOST_ASSERT( HostsDownList.count( host_address ) == 0 ); -} - -/** - * @brief Notify the system that a given host is down. The object takes an - * appropriated action to deal with that. - * Note: this object does not resolves IPs, thus you have to send the same host - * address in order to the object to consider the same host. - * - * @param host_address the DNS/IP address of the host that is down. - */ -void LinkStatusAnalyzer::notify_host_down( const string &host_address ) -{ - BOOST_ASSERT( !host_address.empty() ); - - mutex_lock_guard lock( Mutex ); - - GlobalLogger.info() << "- Host down: " << host_address << endl; - - add_host_down( host_address ); - - if ( exceeded_host_down_limit() ) - { - notify_link_down(); - } - - // inserted in the list? - BOOST_ASSERT( HostsDownList.count( host_address ) == 1 ); -} - -void LinkStatusAnalyzer::add_host_up( const string &host_address ) -{ - if ( HostsDownList.count( host_address ) > 0 ) - { - size_t erased_host_count = HostsDownList.erase( host_address ); - - BOOST_ASSERT( erased_host_count == 1 ); - } -} - -void LinkStatusAnalyzer::add_host_down( const string &host_address ) -{ - (void) HostsDownList.insert( host_address ); -} - -bool LinkStatusAnalyzer::exceeded_host_down_limit() const -{ - int host_down_count = static_cast( HostsDownList.size() ); - - return ( host_down_count > HostsDownLimit ); -} - -void LinkStatusAnalyzer::notify_link_up() -{ - set_link_status( LinkStatus_Up ); - - // report the link status only if: it is up longer than a configured amount - // of time, and if we haven't reported the new status yet - if ( is_link_up_enough_time() && can_report_link_status() ) - { - BOOST_ASSERT( CurrentLinkStatus == LinkStatus_Up ); - BOOST_ASSERT( CurrentNotificationStatus == NotificationStatus_NotReported ); - - StatusNotifierCmd.set_token_value( - StatusNotifierCommand::StatusToken, - "up" - ); //lint !e534 - - bool executed = StatusNotifierCmd.execute(); - - if ( executed ) - { - CurrentNotificationStatus = NotificationStatus_Reported; - } - } -} - -void LinkStatusAnalyzer::notify_link_down() -{ - set_link_status( LinkStatus_Down ); - - // report the link status only if: it is down longer than a configured amount - // of time, and if we haven't reported the new status yet - if ( is_link_down_enough_time() && can_report_link_status() ) - { - BOOST_ASSERT( CurrentLinkStatus == LinkStatus_Down ); - BOOST_ASSERT( CurrentNotificationStatus == NotificationStatus_NotReported ); - - StatusNotifierCmd.set_token_value( - StatusNotifierCommand::StatusToken, - "down" - ); //lint !e534 - - bool executed = StatusNotifierCmd.execute(); - - if ( executed ) - { - CurrentNotificationStatus = NotificationStatus_Reported; - } - } -} - -bool LinkStatusAnalyzer::is_link_up_enough_time() const -{ - if ( CurrentLinkStatus == LinkStatus_Up ) - { - ptime now = microsec_clock::universal_time(); - long amount_time_link_is_up = (now - TimeLinkStatusChanged).total_seconds(); - long link_up_interval_in_sec = LinkUpIntervalInMin * 60; - - if ( amount_time_link_is_up >= link_up_interval_in_sec ) - { - return true; - } - } - - return false; -} - -bool LinkStatusAnalyzer::is_link_down_enough_time() const -{ - if ( CurrentLinkStatus == LinkStatus_Down ) - { - ptime now = microsec_clock::universal_time(); - long amount_time_link_is_down = (now - TimeLinkStatusChanged).total_seconds(); - long link_down_interval_in_sec = LinkDownIntervalInMin * 60; - - if ( amount_time_link_is_down >= link_down_interval_in_sec ) - { - return true; - } - } - - return false; -} - -bool LinkStatusAnalyzer::can_report_link_status() const -{ - return ( CurrentNotificationStatus == NotificationStatus_NotReported ); -} - -void LinkStatusAnalyzer::set_link_status( - const LinkStatusAnalyzer::LinkStatus new_link_status -) -{ - // only reset the control flags if the link status has changed - if ( new_link_status != CurrentLinkStatus ) - { - CurrentLinkStatus = new_link_status; - TimeLinkStatusChanged = microsec_clock::universal_time(); - - // have to report the link status change - CurrentNotificationStatus = NotificationStatus_NotReported; - } -} diff --git a/src/link/linkstatusanalyzer.h b/src/link/linkstatusanalyzer.h deleted file mode 100644 index ca327ae..0000000 --- a/src/link/linkstatusanalyzer.h +++ /dev/null @@ -1,113 +0,0 @@ -/* -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 LINK_STATUS_ANALYZER_H -#define LINK_STATUS_ANALYZER_H - -#include -#include - -#include -#include -#include - -#include "link/statusnotifiercommand.h" - -//----------------------------------------------------------------------------- -// LinkStatusAnalyzer -//----------------------------------------------------------------------------- - -/** - * @brief This class analyzes and notifies the link status, through keeping - * track of the amount of hosts down. - * Scope: one object for many hosts. - */ -class LinkStatusAnalyzer -{ -public: - LinkStatusAnalyzer( - const int hosts_down_limit, - const int link_up_interval_in_min, - const int link_down_interval_in_min, - const std::string &status_notifier_cmd - ); - ~LinkStatusAnalyzer(); - - void notify_host_up( const std::string &host_address ); - void notify_host_down( const std::string &host_address ); - -private: - enum LinkStatus - { - LinkStatus_Up, - LinkStatus_Down - }; - - enum NotificationStatus - { - NotificationStatus_NotReported, - NotificationStatus_Reported - }; - -private: - void add_host_up( const std::string &host_address ); - void add_host_down( const std::string &host_address ); - - bool exceeded_host_down_limit() const; - - void notify_link_up(); - void notify_link_down(); - - bool is_link_up_enough_time() const; - bool is_link_down_enough_time() const; - - bool can_report_link_status() const; - void set_link_status( - const LinkStatusAnalyzer::LinkStatus new_link_status - ); - -private: - /// the maximum amount of hosts which can be down before sound the alarm - const int HostsDownLimit; - /// list of hosts down (obvious isn't it?) - std::set HostsDownList; - /// interval the link have to be stable in order to consider it is functional - const int LinkUpIntervalInMin; - /// interval the link have to be down in order to consider it is non-functional - const int LinkDownIntervalInMin; - /// keep track of the actual link status - LinkStatusAnalyzer::LinkStatus CurrentLinkStatus; - /// indicates if the last link status change was notified - LinkStatusAnalyzer::NotificationStatus CurrentNotificationStatus; - /// when was the last time the status changed - boost::posix_time::ptime TimeLinkStatusChanged; - /// command used to notify the status of the link - StatusNotifierCommand StatusNotifierCmd; - /// mutual exclusion variable to avoid data races - boost::mutex Mutex; - -}; - -//----------------------------------------------------------------------------- -// LinkStatusAnalyzerItem -//----------------------------------------------------------------------------- - -typedef boost::shared_ptr LinkStatusAnalyzerItem; - -#endif // LINK_STATUS_ANALYZER_H diff --git a/src/main.cpp b/src/main.cpp index e4e4b55..3c2b76c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,7 +31,7 @@ on this file might be covered by the GNU General Public License. #include "config/configurationreader.h" #include "config/host.h" -#include "link/linkstatusanalyzer.h" +#include "link/linkstatus.h" #include "host/pingerfactory.h" #include "host/pingprotocol.h" #include "host/pingscheduler.h" @@ -62,7 +62,7 @@ ConfigurationItem get_configuration( return configuration; } -LinkStatusAnalyzerItem get_status_notifier( +LinkStatusItem get_status_notifier( const ConfigurationItem &configuration ) { @@ -70,8 +70,8 @@ LinkStatusAnalyzerItem get_status_notifier( int link_up_interval_in_min = configuration->get_link_up_interval_in_min(); int link_down_interval_in_min = configuration->get_link_down_interval_in_min(); string status_notifier_cmd = configuration->get_status_notifier_cmd(); - LinkStatusAnalyzerItem link_analyzer( - new LinkStatusAnalyzer( + LinkStatusItem link_analyzer( + new LinkStatus( hosts_down_limit, link_up_interval_in_min, link_down_interval_in_min, @@ -90,7 +90,7 @@ void init_logger() void init_pingers( const ConfigurationItem &configuration, - const LinkStatusAnalyzerItem &status_notifier, + const LinkStatusItem &status_notifier, PingSchedulerList *scheduler_list ) { @@ -238,7 +238,7 @@ int main( int argc, char *argv[] ) I2n::Daemon::daemonize(); } - LinkStatusAnalyzerItem status_notifier = get_status_notifier( configuration ); + LinkStatusItem status_notifier = get_status_notifier( configuration ); PingSchedulerList scheduler_list; init_pingers( configuration, status_notifier, &scheduler_list ); diff --git a/test/mock_linkstatus.h b/test/mock_linkstatus.h new file mode 100644 index 0000000..c80e226 --- /dev/null +++ b/test/mock_linkstatus.h @@ -0,0 +1,71 @@ +/* +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 LINK_STATUS_H +#define LINK_STATUS_H + +#include + +#include + +//----------------------------------------------------------------------------- +// LinkStatus +//----------------------------------------------------------------------------- + +/** + * @brief This is a fake link status class. + * Scope: one object for many hosts. + */ +class LinkStatus +{ +public: + LinkStatus(); + ~LinkStatus(); + + void notify_host_up( const std::string &host_address ); + void notify_host_down( const std::string &host_address ); + +}; + +//----------------------------------------------------------------------------- + +LinkStatus::LinkStatus() +{ +} + +LinkStatus::~LinkStatus() +{ +} + +void LinkStatus::notify_host_up( const std::string &host_address ) +{ +} + +void LinkStatus::notify_host_down( const std::string &host_address ) +{ +} + +//----------------------------------------------------------------------------- +// LinkStatusItem +//----------------------------------------------------------------------------- + +typedef boost::shared_ptr LinkStatusItem; + +#endif // LINK_STATUS_H diff --git a/test/mock_linkstatusanalyzer.h b/test/mock_linkstatusanalyzer.h deleted file mode 100644 index 1d55dc8..0000000 --- a/test/mock_linkstatusanalyzer.h +++ /dev/null @@ -1,71 +0,0 @@ -/* -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 LINK_STATUS_ANALYZER_H -#define LINK_STATUS_ANALYZER_H - -#include - -#include - -//----------------------------------------------------------------------------- -// LinkStatusAnalyzer -//----------------------------------------------------------------------------- - -/** - * @brief This is a fake link status class. - * Scope: one object for many hosts. - */ -class LinkStatusAnalyzer -{ -public: - LinkStatusAnalyzer(); - ~LinkStatusAnalyzer(); - - void notify_host_up( const std::string &host_address ); - void notify_host_down( const std::string &host_address ); - -}; - -//----------------------------------------------------------------------------- - -LinkStatusAnalyzer::LinkStatusAnalyzer() -{ -} - -LinkStatusAnalyzer::~LinkStatusAnalyzer() -{ -} - -void LinkStatusAnalyzer::notify_host_up( const std::string &host_address ) -{ -} - -void LinkStatusAnalyzer::notify_host_down( const std::string &host_address ) -{ -} - -//----------------------------------------------------------------------------- -// LinkStatusAnalyzerItem -//----------------------------------------------------------------------------- - -typedef boost::shared_ptr LinkStatusAnalyzerItem; - -#endif // LINK_STATUS_ANALYZER_H diff --git a/test/test_hoststatus.cpp b/test/test_hoststatus.cpp index 920c2dc..a97f92c 100644 --- a/test/test_hoststatus.cpp +++ b/test/test_hoststatus.cpp @@ -26,7 +26,7 @@ on this file might be covered by the GNU General Public License. #include #include -#include "mock_linkstatusanalyzer.h" +#include "mock_linkstatus.h" #include "host/hoststatus.h" @@ -37,7 +37,7 @@ BOOST_AUTO_TEST_CASE( fail_percentage_10 ) int ping_fail_percentage_limit = 10; int resolved_ip_count = 10; - LinkStatusAnalyzerItem link_status( new LinkStatusAnalyzer ); + LinkStatusItem link_status( new LinkStatus ); HostStatus host_status( "localhost", ping_fail_percentage_limit, link_status ); host_status.set_resolved_ip_count( resolved_ip_count ); @@ -77,7 +77,7 @@ BOOST_AUTO_TEST_CASE( fail_percentage_50 ) int ping_fail_percentage_limit = 50; int resolved_ip_count = 10; - LinkStatusAnalyzerItem link_status( new LinkStatusAnalyzer ); + LinkStatusItem link_status( new LinkStatus ); HostStatus host_status( "localhost", ping_fail_percentage_limit, link_status ); host_status.set_resolved_ip_count( resolved_ip_count ); @@ -117,7 +117,7 @@ BOOST_AUTO_TEST_CASE( fail_percentage_80 ) int ping_fail_percentage_limit = 80; int resolved_ip_count = 10; - LinkStatusAnalyzerItem link_status( new LinkStatusAnalyzer ); + LinkStatusItem link_status( new LinkStatus ); HostStatus host_status( "localhost", ping_fail_percentage_limit, link_status ); host_status.set_resolved_ip_count( resolved_ip_count ); -- 1.7.1