merged PingRotate into PingScheduler; fixed save/load of cache to/from file; started...
[pingcheck] / src / host / pingscheduler.h
CommitLineData
91fcc471
TJ
1/*
2The software in this package is distributed under the GNU General
3Public License version 2 (with a special exception described below).
4
5A copy of GNU General Public License (GPL) is included in this distribution,
6in the file COPYING.GPL.
7
8As a special exception, if other files instantiate templates or use macros
9or inline functions from this file, or you compile this file and link it
10with other works to produce a work based on this file, this file
11does not by itself cause the resulting work to be covered
12by the GNU General Public License.
13
14However the source code for this file must still be made available
15in accordance with section (3) of the GNU General Public License.
16
17This exception does not invalidate any other reasons why a work based
18on this file might be covered by the GNU General Public License.
19*/
a9c88e1c
GMF
20#ifndef PING_SCHEDULER_H
21#define PING_SCHEDULER_H
ced28dc7 22
238da857
GMF
23#include <stdint.h>
24
9c55ecd3 25#include <string>
5624490d 26#include <vector>
9c55ecd3 27
e39cc3da
GMF
28#include <boost/asio.hpp>
29#include <boost/shared_ptr.hpp>
ced28dc7 30
72e54d1c 31#include "link/linkstatus.h"
6c14bbee 32#include "host/hoststatus.h"
51cbc790 33#include "host/pinger.h"
8f66f529 34#include "host/pinginterval.h"
3fd74a53 35#include "host/pingprotocol.h"
26b0f687 36#include "dns/resolverbase.h"
ced28dc7 37
4c2a5ab5 38//-----------------------------------------------------------------------------
4bb97b45 39// PingScheduler
4c2a5ab5
GMF
40//-----------------------------------------------------------------------------
41
c5e4bfa1
GMF
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 */
4bb97b45 47class PingScheduler
ced28dc7
GMF
48{
49public:
4bb97b45 50 PingScheduler(
365036be 51 const IoServiceItem io_service,
2bf8720f
GMF
52 const std::string &network_interface,
53 const std::string &destination_address,
238da857 54 const uint16_t destination_port,
fe6a2f80 55 const PingProtocolList &ping_protocol_list,
c15a722d 56 const long ping_interval_in_sec,
a341119a 57 const int ping_fail_percentage_limit,
079d19ab 58 const int ping_reply_timeout,
59733431
CH
59 LinkStatusItem link_analyzer,
60 const int first_delay
e39cc3da 61 );
a86e8ddc 62 ~PingScheduler();
ced28dc7 63
365036be
CH
64 void start_pinging();
65 void stop_pinging();
88714861
GMF
66
67private:
010b8519
GMF
68 //
69 // Methods
70 //
71
26b0f687 72 void ping(const boost::system::error_code &error);
c1d776ba 73 void ping_done_handler(const bool ping_success);
d8a91bd6 74 void update_ping_interval();
09de3c4b 75 void update_ping_elapsed_time();
ced28dc7 76
23f51766
CH
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);
26b0f687
CH
86 void start_resolving_ping_address();
87
88 void update_log_prefix();
23f51766 89
010b8519
GMF
90 //
91 // Attributes
92 //
93
23f51766
CH
94 /// The IO service object, which has the loop event
95 IoServiceItem IoService;
96 /// The network interface name
97 std::string NetworkInterfaceName;
23f51766
CH
98 /// The address to ping
99 std::string DestinationAddress;
26b0f687 100 /// The port to ping at destination host (same for all protocols)
23f51766 101 uint16_t DestinationPort;
26b0f687
CH
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
23f51766 114 const int PingReplyTimeout;
26b0f687
CH
115 /// Object responsible to evaluate the status of the host
116 HostStatus HostAnalyzer;
117 /// The Dns resolver
118 ResolverItem Resolver;
119 /// The actual pinger
23f51766
CH
120 PingerItem Ping;
121 /// a flag whether we should ping as soon as dns is ready
122 bool WantToPing;
26b0f687
CH
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;
ced28dc7
GMF
127};
128
e39cc3da 129//-----------------------------------------------------------------------------
4bb97b45 130// PingSchedulerItem
e39cc3da
GMF
131//-----------------------------------------------------------------------------
132
4bb97b45 133typedef boost::shared_ptr<PingScheduler> PingSchedulerItem;
e39cc3da 134
5624490d
GMF
135//-----------------------------------------------------------------------------
136// PingSchedulerList
137//-----------------------------------------------------------------------------
138
139typedef std::vector< PingSchedulerItem > PingSchedulerList;
140
a9c88e1c 141#endif // PING_SCHEDULER_H