merged PingRotate into PingScheduler; fixed save/load of cache to/from file; started...
[pingcheck] / src / host / pingrotate.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
21 #ifndef PING_ROTATE_H
22 #define PING_ROTATE_H
23
24 #include <stdint.h>
25
26 #include <string>
27
28 #include <boost/asio.hpp>
29 #include <boost/function.hpp>
30 #include <boost/shared_ptr.hpp>
31 #include <boost/circular_buffer.hpp>
32
33 #include "dns/resolverbase.h"
34 #include "host/pinger.h"
35 #include "host/pingprotocol.h"
36
37 //-----------------------------------------------------------------------------
38 // PingRotate
39 //-----------------------------------------------------------------------------
40
41 typedef boost::circular_buffer<PingProtocol> CircularProtocolList;
42
43 /**
44  * @brief This class is a wrapper to the Pingers, and serves to alternate
45  * between protocols.
46  * Scope: one object per host.
47  */
48 class PingRotate
49 {
50 public:
51     PingRotate(
52             const IoServiceItem io_serv,
53             const std::string &network_interface,
54             const std::string &destination_address,
55             const uint16_t destination_port,
56             const int resolved_ip_ttl_threshold,
57             const int ping_reply_timeout,
58             const PingProtocolList &protocol_list
59     );
60     virtual ~PingRotate();
61
62     void ping( boost::function<void(bool)> ping_done_callback );
63
64     void stop_pinging();
65
66     void start_resolving_ping_address();
67     int get_resolved_ip_count() const;
68     bool have_up_to_date_ip() const;
69
70 private:
71     //
72     // Methods
73     //
74
75     void set_ping_done_callback( boost::function<void(bool)> ping_done_callback );
76     void ping_done_handler( bool ping_success ) const;
77
78     void init_ping_protocol();
79     void update_ping_protocol();
80     void get_next_ping_protocol();
81     bool can_change_ping_protocol() const;
82
83     void update_dns_resolver( PingProtocol current_protocol );
84
85     void try_to_ping();
86     void dns_resolve_callback(const bool was_success, const int cname_count);
87
88     //
89     // Attributes
90     //
91
92     /// The IO service object, which has the loop event
93     IoServiceItem IoService;
94     /// The network interface name
95     std::string NetworkInterfaceName;
96     /// The Dns resolver
97     ResolverItem Resolver;
98     /// The address to ping
99     std::string DestinationAddress;
100     /// The port to ping at destination host (same port to all protocols in the list)
101     uint16_t DestinationPort;
102     /// time threshold for address resolution
103     int ResolvedIpTtlThreshold;
104     /// timeout for ping reply
105     const int PingReplyTimeout;
106     /// The list of protocols to ping
107     CircularProtocolList ProtocolRotate;
108     /// Internal boost pinger object
109     PingerItem Ping;
110     /// The callback function
111     boost::function<void(bool)> PingDoneCallback;
112     /// a flag whether DNS resolution is finished or currently underway
113     bool DnsResolutionFinished;
114     /// a flag whether we should ping as soon as dns is ready
115     bool WantToPing;
116 };
117
118 //-----------------------------------------------------------------------------
119 // PingRotateItem
120 //-----------------------------------------------------------------------------
121
122 typedef boost::shared_ptr<PingRotate> PingRotateItem;
123
124 #endif // PING_ROTATE_H