rippled
Checker.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #ifndef RIPPLE_PEERFINDER_CHECKER_H_INCLUDED
21 #define RIPPLE_PEERFINDER_CHECKER_H_INCLUDED
22 
23 #include <ripple/beast/net/IPAddressConversion.h>
24 #include <boost/asio/detail/handler_invoke_helpers.hpp>
25 #include <boost/asio/io_service.hpp>
26 #include <boost/asio/ip/tcp.hpp>
27 #include <boost/intrusive/list.hpp>
28 #include <boost/system/error_code.hpp>
29 #include <condition_variable>
30 #include <memory>
31 #include <mutex>
32 #include <utility>
33 
34 namespace ripple {
35 namespace PeerFinder {
36 
38  template <class Protocol = boost::asio::ip::tcp>
39 class Checker
40 {
41 private:
42  using error_code = boost::system::error_code;
43 
44  struct basic_async_op : boost::intrusive::list_base_hook <
45  boost::intrusive::link_mode <boost::intrusive::normal_link>>
46  {
47  virtual
48  ~basic_async_op() = default;
49 
50  virtual
51  void
52  stop() = 0;
53 
54  virtual
55  void
56  operator() (error_code const& ec) = 0;
57  };
58 
59  template <class Handler>
61  {
62  using socket_type = typename Protocol::socket;
63  using endpoint_type = typename Protocol::endpoint;
64 
67  Handler handler_;
68 
69  async_op (Checker& owner, boost::asio::io_service& io_service,
70  Handler&& handler);
71 
72  ~async_op();
73 
74  void
75  stop() override;
76 
77  void
78  operator() (error_code const& ec) override;
79  };
80 
81  //--------------------------------------------------------------------------
82 
83  using list_type = typename boost::intrusive::make_list <basic_async_op,
84  boost::intrusive::constant_time_size <true>>::type;
85 
88  boost::asio::io_service& io_service_;
90  bool stop_ = false;
91 
92 public:
93  explicit
94  Checker (boost::asio::io_service& io_service);
95 
102  ~Checker();
103 
110  void
111  stop();
112 
114  void
115  wait();
116 
121  template <class Handler>
122  void
123  async_connect (beast::IP::Endpoint const& endpoint, Handler&& handler);
124 
125 private:
126  void
127  remove (basic_async_op& op);
128 };
129 
130 //------------------------------------------------------------------------------
131 
132 template <class Protocol>
133 template <class Handler>
135  boost::asio::io_service& io_service, Handler&& handler)
136  : checker_ (owner)
137  , socket_ (io_service)
138  , handler_ (std::forward<Handler>(handler))
139 {
140 }
141 
142 template <class Protocol>
143 template <class Handler>
145 {
146  checker_.remove (*this);
147 }
148 
149 template <class Protocol>
150 template <class Handler>
151 void
153 {
154  error_code ec;
155  socket_.cancel(ec);
156 }
157 
158 template <class Protocol>
159 template <class Handler>
160 void
162  error_code const& ec)
163 {
164  handler_(ec);
165 }
166 
167 //------------------------------------------------------------------------------
168 
169 template <class Protocol>
170 Checker<Protocol>::Checker (boost::asio::io_service& io_service)
171  : io_service_(io_service)
172 {
173 }
174 
175 template <class Protocol>
177 {
178  wait();
179 }
180 
181 template <class Protocol>
182 void
184 {
185  std::lock_guard lock (mutex_);
186  if (! stop_)
187  {
188  stop_ = true;
189  for (auto& c : list_)
190  c.stop();
191  }
192 }
193 
194 template <class Protocol>
195 void
197 {
198  std::unique_lock<std::mutex> lock (mutex_);
199  while (! list_.empty())
200  cond_.wait (lock);
201 }
202 
203 template <class Protocol>
204 template <class Handler>
205 void
207  beast::IP::Endpoint const& endpoint, Handler&& handler)
208 {
209  auto const op = std::make_shared<async_op<Handler>> (
210  *this, io_service_, std::forward<Handler>(handler));
211  {
212  std::lock_guard lock (mutex_);
213  list_.push_back (*op);
214  }
215  op->socket_.async_connect (beast::IPAddressConversion::to_asio_endpoint (
216  endpoint), std::bind (&basic_async_op::operator(), op,
217  std::placeholders::_1));
218 }
219 
220 template <class Protocol>
221 void
222 Checker<Protocol>::remove (basic_async_op& op)
223 {
224  std::lock_guard lock (mutex_);
225  list_.erase (list_.iterator_to (op));
226  if (list_.size() == 0)
227  cond_.notify_all();
228 }
229 
230 }
231 }
232 
233 #endif
ripple::PeerFinder::Checker
Tests remote listening sockets to make sure they are connectible.
Definition: Checker.h:39
ripple::PeerFinder::Checker::async_op::socket_
socket_type socket_
Definition: Checker.h:66
ripple::PeerFinder::Checker::stop_
bool stop_
Definition: Checker.h:90
std::bind
T bind(T... args)
ripple::PeerFinder::Checker::async_op::stop
void stop() override
Definition: Checker.h:152
utility
ripple::PeerFinder::Checker::remove
void remove(basic_async_op &op)
Definition: Checker.h:222
ripple::PeerFinder::Checker::async_op::endpoint_type
typename Protocol::endpoint endpoint_type
Definition: Checker.h:63
ripple::PeerFinder::Checker::async_op
Definition: Checker.h:60
ripple::PeerFinder::Checker::mutex_
std::mutex mutex_
Definition: Checker.h:86
ripple::PeerFinder::Checker::async_op::operator()
void operator()(error_code const &ec) override
Definition: Checker.h:161
std::lock_guard
STL class.
ripple::PeerFinder::Checker::async_op::~async_op
~async_op()
Definition: Checker.h:144
ripple::PeerFinder::Checker::async_op::checker_
Checker & checker_
Definition: Checker.h:65
ripple::PeerFinder::Checker::list_type
typename boost::intrusive::make_list< basic_async_op, boost::intrusive::constant_time_size< true > >::type list_type
Definition: Checker.h:84
ripple::PeerFinder::Checker::basic_async_op
Definition: Checker.h:44
ripple::PeerFinder::Checker::list_
list_type list_
Definition: Checker.h:89
std::unique_lock
STL class.
ripple::PeerFinder::Checker::async_op::handler_
Handler handler_
Definition: Checker.h:67
ripple::PeerFinder::Checker::async_op::async_op
async_op(Checker &owner, boost::asio::io_service &io_service, Handler &&handler)
Definition: Checker.h:134
ripple::PeerFinder::Checker::wait
void wait()
Block until all pending I/O completes.
Definition: Checker.h:196
ripple::PeerFinder::Checker::stop
void stop()
Stop the service.
Definition: Checker.h:183
memory
ripple::PeerFinder::Checker::basic_async_op::operator()
virtual void operator()(error_code const &ec)=0
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::PeerFinder::Checker::cond_
std::condition_variable cond_
Definition: Checker.h:87
ripple::PeerFinder::Checker::async_op::socket_type
typename Protocol::socket socket_type
Definition: Checker.h:62
ripple::PeerFinder::Checker::io_service_
boost::asio::io_service & io_service_
Definition: Checker.h:88
ripple::PeerFinder::Checker::async_connect
void async_connect(beast::IP::Endpoint const &endpoint, Handler &&handler)
Performs an async connection test on the specified endpoint.
Definition: Checker.h:206
std
STL namespace.
ripple::PeerFinder::Checker< boost::asio::ip::tcp >::error_code
boost::system::error_code error_code
Definition: Checker.h:42
condition_variable
mutex
beast::IP::Endpoint
A version-independent IP address and port combination.
Definition: IPEndpoint.h:39
beast::IPAddressConversion::to_asio_endpoint
static boost::asio::ip::tcp::endpoint to_asio_endpoint(IP::Endpoint const &address)
Definition: IPAddressConversion.h:64
ripple::PeerFinder::Checker::basic_async_op::~basic_async_op
virtual ~basic_async_op()=default
ripple::PeerFinder::Checker::Checker
Checker(boost::asio::io_service &io_service)
Definition: Checker.h:170
ripple::PeerFinder::Checker::basic_async_op::stop
virtual void stop()=0
ripple::PeerFinder::Checker::~Checker
~Checker()
Destroy the service.
Definition: Checker.h:176