--- /dev/null
+#include "ping/pinganalyzer.h"
+
+#include <iostream>
+
+#include <boost/assert.hpp>
+
+using namespace std;
+
+//-----------------------------------------------------------------------------
+// PingAnalyzer
+//-----------------------------------------------------------------------------
+
+PingAnalyzer::PingAnalyzer(
+ const string &host_address,
+ const int ping_fail_threshold_percentage
+) :
+ Notifier( host_address ),
+ PingFailThresholdPercentage( ping_fail_threshold_percentage ),
+ ResolvedIpCount( 0 ),
+ PingsPerformedCount( 0 ),
+ PingsFailedCount( 0 )
+{
+ BOOST_ASSERT( !host_address.empty() );
+ BOOST_ASSERT( ( 0 <= ping_fail_threshold_percentage ) && ( ping_fail_threshold_percentage <= 100 ) );
+}
+
+PingAnalyzer::~PingAnalyzer()
+{
+}
+
+void PingAnalyzer::set_resolved_ip_count( const int resolved_ip_count )
+{
+ BOOST_ASSERT( 1 <= resolved_ip_count );
+
+ ResolvedIpCount = resolved_ip_count;
+}
+
+void PingAnalyzer::update_ping_statistics( bool ping_success )
+{
+ BOOST_ASSERT( 1 <= ResolvedIpCount );
+ BOOST_ASSERT( 0 <= PingsPerformedCount );
+ BOOST_ASSERT( PingsFailedCount <= PingsPerformedCount );
+
+ ++PingsPerformedCount;
+
+ if ( !ping_success )
+ {
+ ++PingsFailedCount;
+ }
+
+ if ( PingsPerformedCount >= ResolvedIpCount )
+ {
+ analyze();
+
+ reset_ping_counter();
+ }
+
+ BOOST_ASSERT( PingsFailedCount <= PingsPerformedCount );
+}
+
+void PingAnalyzer::analyze()
+{
+ BOOST_ASSERT( ( 0 <= PingFailThresholdPercentage ) && ( PingFailThresholdPercentage <= 100 ) );
+ BOOST_ASSERT( ( 0 <= PingsFailedCount ) && ( PingsFailedCount <= PingsPerformedCount ) );
+ BOOST_ASSERT( PingsPerformedCount == ResolvedIpCount );
+
+ int ping_fail_threshold_absolute = PingFailThresholdPercentage / 100; // TODO possible number loss, check with care
+
+ if ( PingsFailedCount > ping_fail_threshold_absolute )
+ {
+ Notifier.alert_host_down();
+ }
+ else
+ {
+ Notifier.alert_host_up();
+ }
+
+}
+
+void PingAnalyzer::reset_ping_counter()
+{
+ PingsPerformedCount = 0;
+ PingsFailedCount = 0;
+}
--- /dev/null
+#ifndef PINGANALYZER_H
+#define PINGANALYZER_H
+
+#include <string>
+
+#include "ping/pingstatusnotifier.h"
+
+//-----------------------------------------------------------------------------
+// PingAnalyzer
+//-----------------------------------------------------------------------------
+
+class PingAnalyzer
+{
+public:
+ PingAnalyzer(
+ const std::string &host_address,
+ const int ping_fail_threshold_percentage
+ );
+ virtual ~PingAnalyzer();
+
+ void set_resolved_ip_count( const int resolved_ip_count );
+ void update_ping_statistics( bool ping_success );
+
+private:
+ void analyze();
+ void reset_ping_counter();
+
+private:
+ PingStatusNotifier Notifier;
+ int PingFailThresholdPercentage;
+ int ResolvedIpCount;
+ int PingsPerformedCount;
+ int PingsFailedCount;
+
+};
+
+#endif /* PINGANALYZER_H */
--- /dev/null
+#include "ping/pingstatusnotifier.h"
+
+#include <iostream>
+
+using namespace std;
+
+//-----------------------------------------------------------------------------
+// PingStatusNotifier
+//-----------------------------------------------------------------------------
+
+PingStatusNotifier::PingStatusNotifier( const string &host_address ) :
+ HostAddress( host_address )
+{
+}
+
+PingStatusNotifier::~PingStatusNotifier()
+{
+}
+
+void PingStatusNotifier::alert_host_up() const
+{
+ // TODO call script
+ cerr << "- " << HostAddress << " - LINK IS UP /\\ " << endl;
+}
+
+void PingStatusNotifier::alert_host_down() const
+{
+ // TODO call script
+ cerr << "- " << HostAddress << " - LINK IS DOWN \\/" << endl;
+}
--- /dev/null
+#ifndef PINGSTATUSNOTIFIER_H
+#define PINGSTATUSNOTIFIER_H
+
+#include <string>
+
+//-----------------------------------------------------------------------------
+// PingStatusNotifier
+//-----------------------------------------------------------------------------
+
+class PingStatusNotifier
+{
+public:
+ PingStatusNotifier( const std::string &host_address );
+ virtual ~PingStatusNotifier();
+
+ void alert_host_up() const;
+ void alert_host_down() const;
+
+private:
+ const std::string HostAddress;
+
+};
+
+#endif /* PINGSTATUSNOTIFIER_H */