dns/timetolive.cpp
host/hoststatusanalyzer.cpp
host/pinger.cpp
+ host/pingerfactory.cpp
host/pinginterval.cpp
host/pingprotocol.cpp
host/pingscheduler.cpp
#include "host/pingerfactory.h"
+#include "icmp/icmppinger.h"
+#include "tcp/tcppinger.h"
+
+using namespace std;
+using boost::shared_ptr;
+using boost::asio::io_service;
+
//-----------------------------------------------------------------------------
// PingerFactory
//-----------------------------------------------------------------------------
{
}
-void PingerFactory::createPinger( PingProtocol type )
+/**
+ * @brief Create a Pinger object suitable to the given protocol.
+ *
+ * @param protocol One of the available ping protocols.
+ * @param io_serv The io_service object.
+ * @param network_interface The network interface name from where the ping
+ * packet will be sent.
+ * @return a Pinger object to able to ping using the given protocol.
+ */
+shared_ptr<Pinger> PingerFactory::createPinger(
+ const PingProtocol protocol,
+ io_service &io_serv,
+ const string &network_interface
+)
{
- // TODO
+ BOOST_ASSERT( ( PingProtocol_First <= protocol ) && ( protocol <= PingProtocol_Last ) );
+ BOOST_ASSERT( !network_interface.empty() );
+
+ // Ping reply timeout. Could be made a configuration variable
+ const int ping_reply_timeout_in_sec = 30; // TODO
+
+ switch ( protocol )
+ {
+ case PingProtocol_ICMP:
+ return shared_ptr<Pinger>(
+ new IcmpPinger( io_serv, network_interface, ping_reply_timeout_in_sec )
+ );
+ case PingProtocol_TCP:
+ return shared_ptr<Pinger>(
+ new TcpPinger( io_serv, network_interface, ping_reply_timeout_in_sec )
+ );
+ default:
+ BOOST_ASSERT( false );
+ return shared_ptr<Pinger>();
+ }
}
#ifndef PINGERFACTORY_H
#define PINGERFACTORY_H
+#include <string>
+
+#include <boost/asio.hpp>
+#include <boost/shared_ptr.hpp>
+
+#include "host/pinger.h"
#include "host/pingprotocol.h"
//-----------------------------------------------------------------------------
PingerFactory();
virtual ~PingerFactory();
- void createPinger( PingProtocol type );
+ static boost::shared_ptr<Pinger> createPinger(
+ const PingProtocol protocol,
+ boost::asio::io_service &io_serv,
+ const std::string &network_interface
+ );
};
#endif /* PINGERFACTORY_H */
// PingScheduler
//-----------------------------------------------------------------------------
-/// Ping reply timeout. Could be made a configuration variable
-const int PingReplyTimeout = 30;
-
PingScheduler::PingScheduler(
const string &network_interface,
const string &destination_address,
- const PingProtocol /*ping_protocol*/,
+ const PingProtocol protocol,
const long ping_interval_in_sec,
const int ping_fail_percentage_limit,
const string &nameserver,
Ping(),
Thread()
{
-#if 0
- // TODO read the Factory Design Pattern from Head First
- Ping = PingerFactory::create( ping_protocol );
-#endif
- Ping = shared_ptr<Pinger>(
- new IcmpPinger( IoService, LocalNetworkInterfaceName, PingReplyTimeout )
- );
+ BOOST_ASSERT( !network_interface.empty() );
+ BOOST_ASSERT( !destination_address.empty() );
+
+ Ping = PingerFactory::createPinger( protocol, IoService, network_interface );
}
PingScheduler::~PingScheduler()
PingScheduler(
const std::string &network_interface,
const std::string &destination_address,
- const PingProtocol ping_protocol,
+ const PingProtocol protocol,
const long ping_interval_in_sec,
const int ping_fail_percentage_limit,
const std::string &nameserver,