merged PingRotate into PingScheduler; fixed save/load of cache to/from file; started...
[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 "dns/resolverbase.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             LinkStatusItem link_analyzer,
60             const int first_delay
61     );
62     ~PingScheduler();
63
64     void start_pinging();
65     void stop_pinging();
66
67 private:
68     //
69     // Methods
70     //
71
72     void ping(const boost::system::error_code &error);
73     void ping_done_handler(const bool ping_success);
74     void update_ping_interval();
75     void update_ping_elapsed_time();
76
77     void init_ping_protocol();
78     void update_ping_protocol();
79     void get_next_ping_protocol();
80     bool can_change_ping_protocol() const;
81
82     void update_dns_resolver( PingProtocol current_protocol );
83
84     void try_to_ping();
85     void dns_resolve_callback(const bool was_success, const int cname_count);
86     void start_resolving_ping_address();
87
88     void update_log_prefix();
89
90     //
91     // Attributes
92     //
93
94     /// The IO service object, which has the loop event
95     IoServiceItem IoService;
96     /// The network interface name
97     std::string NetworkInterfaceName;
98     /// The address to ping
99     std::string DestinationAddress;
100     /// The port to ping at destination host (same for all protocols)
101     uint16_t DestinationPort;
102     /// The list of protocols to ping and iterator over them
103     PingProtocolList Protocols;
104     PingProtocolList::const_iterator ProtocolIter;
105     /// Interval between each ping to the same host
106     PingInterval PingIntervalInSec;
107     /// delay for very first ping to avoid lots of simultaneous pings at startup
108     int FirstDelay;
109     /// Timer to trigger the next ping
110     boost::asio::deadline_timer NextPingTimer;
111     /// Keeps track of the time when the last ping was send
112     boost::posix_time::ptime TimeSentLastPing;
113     /// time threshold for ping
114     const int PingReplyTimeout;
115     /// Object responsible to evaluate the status of the host
116     HostStatus HostAnalyzer;
117     /// The Dns resolver
118     ResolverItem Resolver;
119     /// The actual pinger
120     PingerItem Ping;
121     /// a flag whether we should ping as soon as dns is ready
122     bool WantToPing;
123     /// Prefix to log messages for quicker analysis/debugging
124     std::string LogPrefix;
125     /// Flag whether DNS resolution has failed so we have to run on outdated IPs
126     bool ContinueOnOutdatedIps;
127 };
128
129 //-----------------------------------------------------------------------------
130 // PingSchedulerItem
131 //-----------------------------------------------------------------------------
132
133 typedef boost::shared_ptr<PingScheduler> PingSchedulerItem;
134
135 //-----------------------------------------------------------------------------
136 // PingSchedulerList
137 //-----------------------------------------------------------------------------
138
139 typedef std::vector< PingSchedulerItem > PingSchedulerList;
140
141 #endif // PING_SCHEDULER_H