/* 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 PING_SCHEDULER_H #define PING_SCHEDULER_H #include #include #include #include #include #include "link/linkstatus.h" #include "host/hoststatus.h" #include "host/pingstatus.h" #include "host/pinger.h" #include "host/pinginterval.h" #include "host/pingnumber.h" #include "host/pingprotocol.h" #include "dns/resolverbase.h" typedef std::vector pinger_vec; //----------------------------------------------------------------------------- // PingScheduler //----------------------------------------------------------------------------- /** * @brief This class is responsible to control whether to ping a host. It * schedules periodic pings to a host. * Scope: one object per host. */ class PingScheduler { public: PingScheduler( const IoServiceItem io_service, const std::string &network_interface, const std::string &destination_address, const uint16_t destination_port, const PingProtocolList &ping_protocol_list, const long ping_interval_in_sec, const int ping_fail_percentage_limit, const int ping_congestion_percentage_limit, const int congest_caused_by_fail_percentage_limit, const int ping_congestion_duration_thresh, const int ping_reply_timeout, LinkStatusItem link_analyzer, const int first_delay, const int n_parallel_pings, const int parallel_ping_delay, const int ping_timeout_factor ); ~PingScheduler(); void start_pinging(); void stop_pinging(); private: // // Methods // void ping(const boost::system::error_code &error); void ping_done_handler(const PingStatus &result, const long ping_duration_us); void update_ping_interval(); void update_ping_number(); void init_ping_protocol(); void update_ping_protocol(); void get_next_ping_protocol(); bool can_change_ping_protocol() const; void prepare_next_ping(); void update_dns_resolver(); void ping_when_ready(); void delayed_ping( const boost::system::error_code &error, const boost::asio::ip::address ip, const int pinger_index ); void dns_resolve_callback(const bool was_success, const int recursion_count); void start_resolving_ping_address(); void update_log_prefix(); void cancel_resolve(const bool force_cancel); void clear_pingers(); // // Attributes // /// The IO service object, which has the loop event IoServiceItem IoService; /// The network interface name std::string NetworkInterfaceName; /// The address to ping std::string DestinationAddress; /// The port to ping at destination host (same for all protocols) uint16_t DestinationPort; /// The list of protocols to ping and iterator over them PingProtocolList Protocols; PingProtocolList::const_iterator ProtocolIter; /// Interval between each ping to the same host PingInterval PingIntervalInSec; /// number of pingers that work in parallel PingNumber NPingers; /// delay for very first ping to avoid lots of simultaneous pings at startup int FirstDelay; /// Timer to trigger the next ping boost::asio::deadline_timer NextPingTimer; /// Keeps track of the time when the last ping was send boost::posix_time::ptime TimeSentLastPing; /// time threshold for ping int PingReplyTimeout; /// time threshold for ping -- original value const int PingReplyTimeoutOrig; /// Object responsible to evaluate the status of the host HostStatus HostAnalyzer; /// The Dns resolver ResolverItem Resolver; /// vector of pingers pinger_vec Pingers; /// number of results from pingers int NPingersDone; /// delay (in ms) between pings to same IP int ParallelPingDelay; /// timers for delayed pings boost::asio::deadline_timer DelayedPingTimer; /// a flag whether we should ping as soon as dns is ready bool WantToPing; /// Prefix to log messages for quicker analysis/debugging std::string LogPrefix; /// Flag whether DNS resolution has failed so we have to run on outdated IPs bool ContinueOnOutdatedIps; int PingTimeoutFactor; }; //----------------------------------------------------------------------------- // PingSchedulerItem //----------------------------------------------------------------------------- typedef boost::shared_ptr PingSchedulerItem; //----------------------------------------------------------------------------- // PingSchedulerList //----------------------------------------------------------------------------- typedef std::vector< PingSchedulerItem > PingSchedulerList; #endif // PING_SCHEDULER_H