75598b91fa4f24b4432859c40bac1b479b5df237
[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/pinger.h"
34 #include "host/pinginterval.h"
35 #include "host/pingprotocol.h"
36 #include "host/pingrotate.h"
37
38 //-----------------------------------------------------------------------------
39 // PingScheduler
40 //-----------------------------------------------------------------------------
41
42 /**
43  * @brief This class is responsible to control whether to ping a host. It
44  * schedules periodic pings to a host.
45  * Scope: one object per host.
46  */
47 class PingScheduler
48 {
49 public:
50     PingScheduler(
51             const IoServiceItem io_service,
52             const std::string &network_interface,
53             const std::string &destination_address,
54             const uint16_t destination_port,
55             const PingProtocolList &ping_protocol_list,
56             const long ping_interval_in_sec,
57             const int ping_fail_percentage_limit,
58             const int ping_reply_timeout,
59             const int resolved_ip_ttl_threshold,
60             LinkStatusItem link_analyzer,
61             const int first_delay
62     );
63     ~PingScheduler();
64
65     void start_pinging();
66     void stop_pinging();
67
68 private:
69     //
70     // Methods
71     //
72
73     void resolve_and_ping(const boost::system::error_code &error);
74
75     void ping();
76     void ping_done_handler(const bool ping_success);
77     void update_ping_interval();
78     void update_ping_elapsed_time();
79
80     //
81     // Attributes
82     //
83
84     /// Timer to trigger the next ping
85     boost::asio::deadline_timer NextPingTimer;
86     /// Timer to trigger the next attempt to resolve addresses
87     boost::asio::deadline_timer NextAddressTimer;
88     /// Keeps track of the time when the last ping was send
89     boost::posix_time::ptime TimeSentLastPing;
90     /// Interval between each ping to the same host
91     PingInterval PingIntervalInSec;
92     /// Interval between attempts to resolve host address
93     PingInterval AddressResolveIntervalInSec;
94     /// Object responsible to evaluate the status of the host
95     HostStatus HostAnalyzer;
96     /// delay for very first ping to avoid lots of simultaneous pings
97     int  FirstDelay;
98     /// number of attempts at Address Resolution
99     int AddressResolutionAttempts;
100     /// flag whether any DNS lookup succeeded
101     //  (comparing get_ip_count to 0 might violate assertion in dnsresolver.cpp)
102     bool EverHadAnyIP;
103     /// Internal boost pinger object
104     PingRotateItem Ping;
105 };
106
107 //-----------------------------------------------------------------------------
108 // PingSchedulerItem
109 //-----------------------------------------------------------------------------
110
111 typedef boost::shared_ptr<PingScheduler> PingSchedulerItem;
112
113 //-----------------------------------------------------------------------------
114 // PingSchedulerList
115 //-----------------------------------------------------------------------------
116
117 typedef std::vector< PingSchedulerItem > PingSchedulerList;
118
119 #endif // PING_SCHEDULER_H