| 1 | /* |
| 2 | The software in this package is distributed under the GNU General |
| 3 | Public License version 2 (with a special exception described below). |
| 4 | |
| 5 | A copy of GNU General Public License (GPL) is included in this distribution, |
| 6 | in the file COPYING.GPL. |
| 7 | |
| 8 | As a special exception, if other files instantiate templates or use macros |
| 9 | or inline functions from this file, or you compile this file and link it |
| 10 | with other works to produce a work based on this file, this file |
| 11 | does not by itself cause the resulting work to be covered |
| 12 | by the GNU General Public License. |
| 13 | |
| 14 | However the source code for this file must still be made available |
| 15 | in accordance with section (3) of the GNU General Public License. |
| 16 | |
| 17 | This exception does not invalidate any other reasons why a work based |
| 18 | on this file might be covered by the GNU General Public License. |
| 19 | */ |
| 20 | #ifndef PING_SCHEDULER_H |
| 21 | #define PING_SCHEDULER_H |
| 22 | |
| 23 | #include <stdint.h> |
| 24 | |
| 25 | #include <string> |
| 26 | #include <vector> |
| 27 | |
| 28 | #include <boost/asio.hpp> |
| 29 | #include <boost/shared_ptr.hpp> |
| 30 | |
| 31 | #include "link/linkstatus.h" |
| 32 | #include "host/hoststatus.h" |
| 33 | #include "host/pingstatus.h" |
| 34 | #include "host/pinger.h" |
| 35 | #include "host/pinginterval.h" |
| 36 | #include "host/pingnumber.h" |
| 37 | #include "host/pingprotocol.h" |
| 38 | #include "dns/resolverbase.h" |
| 39 | |
| 40 | typedef std::vector<PingerItem> pinger_vec; |
| 41 | |
| 42 | //----------------------------------------------------------------------------- |
| 43 | // PingScheduler |
| 44 | //----------------------------------------------------------------------------- |
| 45 | |
| 46 | /** |
| 47 | * @brief This class is responsible to control whether to ping a host. It |
| 48 | * schedules periodic pings to a host. |
| 49 | * Scope: one object per host. |
| 50 | */ |
| 51 | class PingScheduler |
| 52 | { |
| 53 | public: |
| 54 | PingScheduler( |
| 55 | const IoServiceItem io_service, |
| 56 | const std::string &network_interface, |
| 57 | const std::string &destination_address, |
| 58 | const uint16_t destination_port, |
| 59 | const PingProtocolList &ping_protocol_list, |
| 60 | const long ping_interval_in_sec, |
| 61 | const int ping_fail_percentage_limit, |
| 62 | const int ping_congestion_percentage_limit, |
| 63 | const int congest_caused_by_fail_percentage_limit, |
| 64 | const int ping_congestion_duration_thresh, |
| 65 | const int ping_reply_timeout, |
| 66 | LinkStatusItem link_analyzer, |
| 67 | const int first_delay, |
| 68 | const int n_parallel_pings, |
| 69 | const int parallel_ping_delay, |
| 70 | const int ping_timeout_factor |
| 71 | ); |
| 72 | ~PingScheduler(); |
| 73 | |
| 74 | void start_pinging(); |
| 75 | void stop_pinging(); |
| 76 | |
| 77 | private: |
| 78 | // |
| 79 | // Methods |
| 80 | // |
| 81 | |
| 82 | void ping(const boost::system::error_code &error); |
| 83 | void ping_done_handler(const PingStatus &result, |
| 84 | const long ping_duration_us); |
| 85 | void update_ping_interval(); |
| 86 | void update_ping_number(); |
| 87 | |
| 88 | void init_ping_protocol(); |
| 89 | void update_ping_protocol(); |
| 90 | void get_next_ping_protocol(); |
| 91 | bool can_change_ping_protocol() const; |
| 92 | |
| 93 | void prepare_next_ping(); |
| 94 | void update_dns_resolver(); |
| 95 | |
| 96 | void ping_when_ready(); |
| 97 | void delayed_ping( const boost::system::error_code &error, |
| 98 | const boost::asio::ip::address ip, |
| 99 | const int pinger_index ); |
| 100 | void dns_resolve_callback(const bool was_success, |
| 101 | const int recursion_count); |
| 102 | void start_resolving_ping_address(); |
| 103 | |
| 104 | void update_log_prefix(); |
| 105 | void cancel_resolve(const bool force_cancel); |
| 106 | void clear_pingers(); |
| 107 | |
| 108 | // |
| 109 | // Attributes |
| 110 | // |
| 111 | |
| 112 | /// The IO service object, which has the loop event |
| 113 | IoServiceItem IoService; |
| 114 | /// The network interface name |
| 115 | std::string NetworkInterfaceName; |
| 116 | /// The address to ping |
| 117 | std::string DestinationAddress; |
| 118 | /// The port to ping at destination host (same for all protocols) |
| 119 | uint16_t DestinationPort; |
| 120 | /// The list of protocols to ping and iterator over them |
| 121 | PingProtocolList Protocols; |
| 122 | PingProtocolList::const_iterator ProtocolIter; |
| 123 | /// Interval between each ping to the same host |
| 124 | PingInterval PingIntervalInSec; |
| 125 | /// number of pingers that work in parallel |
| 126 | PingNumber NPingers; |
| 127 | /// delay for very first ping to avoid lots of simultaneous pings at startup |
| 128 | int FirstDelay; |
| 129 | /// Timer to trigger the next ping |
| 130 | boost::asio::deadline_timer NextPingTimer; |
| 131 | /// Keeps track of the time when the last ping was send |
| 132 | boost::posix_time::ptime TimeSentLastPing; |
| 133 | /// time threshold for ping |
| 134 | int PingReplyTimeout; |
| 135 | /// time threshold for ping -- original value |
| 136 | const int PingReplyTimeoutOrig; |
| 137 | /// Object responsible to evaluate the status of the host |
| 138 | HostStatus HostAnalyzer; |
| 139 | /// The Dns resolver |
| 140 | ResolverItem Resolver; |
| 141 | /// vector of pingers |
| 142 | pinger_vec Pingers; |
| 143 | /// number of results from pingers |
| 144 | int NPingersDone; |
| 145 | /// delay (in ms) between pings to same IP |
| 146 | int ParallelPingDelay; |
| 147 | /// timers for delayed pings |
| 148 | boost::asio::deadline_timer DelayedPingTimer; |
| 149 | /// a flag whether we should ping as soon as dns is ready |
| 150 | bool WantToPing; |
| 151 | /// Prefix to log messages for quicker analysis/debugging |
| 152 | std::string LogPrefix; |
| 153 | /// Flag whether DNS resolution has failed so we have to run on outdated IPs |
| 154 | bool ContinueOnOutdatedIps; |
| 155 | int PingTimeoutFactor; |
| 156 | }; |
| 157 | |
| 158 | //----------------------------------------------------------------------------- |
| 159 | // PingSchedulerItem |
| 160 | //----------------------------------------------------------------------------- |
| 161 | |
| 162 | typedef boost::shared_ptr<PingScheduler> PingSchedulerItem; |
| 163 | |
| 164 | //----------------------------------------------------------------------------- |
| 165 | // PingSchedulerList |
| 166 | //----------------------------------------------------------------------------- |
| 167 | |
| 168 | typedef std::vector< PingSchedulerItem > PingSchedulerList; |
| 169 | |
| 170 | #endif // PING_SCHEDULER_H |