adb4a97744d8f55e5713ecbbecc267c153dbf77f
[pingcheck] / src / host / pingscheduler.h
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/pingprotocol.h"
37 #include "dns/resolverbase.h"
38
39 typedef std::vector<PingerItem> pinger_vec;
40
41 //-----------------------------------------------------------------------------
42 // PingScheduler
43 //-----------------------------------------------------------------------------
44
45 /**
46  * @brief This class is responsible to control whether to ping a host. It
47  * schedules periodic pings to a host.
48  * Scope: one object per host.
49  */
50 class PingScheduler
51 {
52 public:
53     PingScheduler(
54             const IoServiceItem io_service,
55             const std::string &network_interface,
56             const std::string &destination_address,
57             const uint16_t destination_port,
58             const PingProtocolList &ping_protocol_list,
59             const long ping_interval_in_sec,
60             const int ping_fail_percentage_limit,
61             const int ping_congestion_percentage_limit,
62             const int ping_congestion_duration_thresh,
63             const int ping_reply_timeout,
64             LinkStatusItem link_analyzer,
65             const int first_delay,
66             const int n_parallel_pings,
67             const int parallel_ping_delay
68     );
69     ~PingScheduler();
70
71     void start_pinging();
72     void stop_pinging();
73
74 private:
75     //
76     // Methods
77     //
78
79     void ping(const boost::system::error_code &error);
80     void ping_done_handler(const PingStatus &result,
81                            const long ping_duration_us);
82     void update_ping_interval();
83
84     void init_ping_protocol();
85     void update_ping_protocol();
86     void get_next_ping_protocol();
87     bool can_change_ping_protocol() const;
88
89     void prepare_next_ping();
90     void update_dns_resolver( PingProtocol current_protocol );
91
92     void ping_when_ready();
93     void delayed_ping( const boost::system::error_code &error,
94                        const boost::asio::ip::address &ip,
95                        const int pinger_index );
96     void dns_resolve_callback(const bool was_success,
97                               const int recursion_count);
98     void start_resolving_ping_address();
99
100     void update_log_prefix();
101     void cancel_resolve(const bool force_cancel);
102     void clear_pingers();
103
104     //
105     // Attributes
106     //
107
108     /// The IO service object, which has the loop event
109     IoServiceItem IoService;
110     /// The network interface name
111     std::string NetworkInterfaceName;
112     /// The address to ping
113     std::string DestinationAddress;
114     /// The port to ping at destination host (same for all protocols)
115     uint16_t DestinationPort;
116     /// The list of protocols to ping and iterator over them
117     PingProtocolList Protocols;
118     PingProtocolList::const_iterator ProtocolIter;
119     /// Interval between each ping to the same host
120     PingInterval PingIntervalInSec;
121     /// delay for very first ping to avoid lots of simultaneous pings at startup
122     int FirstDelay;
123     /// Timer to trigger the next ping
124     boost::asio::deadline_timer NextPingTimer;
125     /// Keeps track of the time when the last ping was send
126     boost::posix_time::ptime TimeSentLastPing;
127     /// time threshold for ping
128     const int PingReplyTimeout;
129     /// Object responsible to evaluate the status of the host
130     HostStatus HostAnalyzer;
131     /// The Dns resolver
132     ResolverItem Resolver;
133     /// vector of pingers
134     pinger_vec Pingers;
135     /// number of pingers that work in parallel
136     int NPingers;
137     /// number of results from pingers
138     int NPingersDone;
139     /// delay (in ms) between pings to same IP
140     int ParallelPingDelay;
141     /// timers for delayed pings
142     boost::asio::deadline_timer DelayedPingTimer;
143     /// a flag whether we should ping as soon as dns is ready
144     bool WantToPing;
145     /// Prefix to log messages for quicker analysis/debugging
146     std::string LogPrefix;
147     /// Flag whether DNS resolution has failed so we have to run on outdated IPs
148     bool ContinueOnOutdatedIps;
149 };
150
151 //-----------------------------------------------------------------------------
152 // PingSchedulerItem
153 //-----------------------------------------------------------------------------
154
155 typedef boost::shared_ptr<PingScheduler> PingSchedulerItem;
156
157 //-----------------------------------------------------------------------------
158 // PingSchedulerList
159 //-----------------------------------------------------------------------------
160
161 typedef std::vector< PingSchedulerItem > PingSchedulerList;
162
163 #endif // PING_SCHEDULER_H