moved time duration measurement of ping from scheduler to pingers
[pingcheck] / src / host / pinger.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 PINGER_H
22 #define PINGER_H
23
24 #include <stdint.h>
25
26 #include <string>
27
28 #include <boost/function.hpp>
29 #include <boost/shared_ptr.hpp>
30 #include <boost/utility.hpp>
31 #include <boost/asio/io_service.hpp>
32 #include <boost/asio/ip/address.hpp>
33 #include <boost/weak_ptr.hpp>
34
35 #include "host/pingstatus.h"
36
37 //-----------------------------------------------------------------------------
38 // IoServiceItem
39 //-----------------------------------------------------------------------------
40
41 typedef boost::shared_ptr<boost::asio::io_service> IoServiceItem;
42
43 //-----------------------------------------------------------------------------
44 // Pinger
45 //-----------------------------------------------------------------------------
46
47 /**
48  * @brief Abstract class for pingers.
49  * Scope: one object per host.
50  *
51  * Holds a weak_ptr to itself, so can create shared_ptrs from self
52  *   (adapted from amime project)
53  */
54 class Pinger : boost::noncopyable
55 {
56 public:
57     typedef boost::weak_ptr<Pinger> WeakPtr;
58
59     virtual void ping(
60             const boost::asio::ip::address &ip,
61             const uint16_t destination_port,
62             boost::function<void(PingStatus,long)> ping_done_callback
63     ) = 0;
64
65     virtual void stop_pinging() = 0;
66
67 protected:
68     Pinger();
69     virtual ~Pinger();
70
71     Pinger::WeakPtr get_myself() const;
72     virtual void set_myself(const Pinger::WeakPtr &myself);
73
74 private:
75     Pinger::WeakPtr Myself;
76 };
77
78
79 //-----------------------------------------------------------------------------
80 // PingerItem
81 //-----------------------------------------------------------------------------
82
83 typedef boost::shared_ptr<Pinger> PingerItem;
84
85 #endif // PINGER_H