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