rippled
Bootcache.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_BOOTCACHE_H_INCLUDED
21 #define RIPPLE_PEERFINDER_BOOTCACHE_H_INCLUDED
22 
23 #include <ripple/beast/utility/Journal.h>
24 #include <ripple/beast/utility/PropertyStream.h>
25 #include <ripple/peerfinder/PeerfinderManager.h>
26 #include <ripple/peerfinder/impl/Store.h>
27 #include <boost/bimap.hpp>
28 #include <boost/bimap/multiset_of.hpp>
29 #include <boost/bimap/unordered_set_of.hpp>
30 #include <boost/iterator/transform_iterator.hpp>
31 
32 namespace ripple {
33 namespace PeerFinder {
34 
50 class Bootcache
51 {
52 private:
53  class Entry
54  {
55  public:
57  {
58  }
59 
60  int&
62  {
63  return m_valence;
64  }
65 
66  int
67  valence() const
68  {
69  return m_valence;
70  }
71 
72  friend bool
73  operator<(Entry const& lhs, Entry const& rhs)
74  {
75  if (lhs.valence() > rhs.valence())
76  return true;
77  return false;
78  }
79 
80  private:
81  int m_valence;
82  };
83 
84  using left_t = boost::bimaps::unordered_set_of<beast::IP::Endpoint>;
85  using right_t = boost::bimaps::multiset_of<Entry>;
86  using map_type = boost::bimap<left_t, right_t>;
87  using value_type = map_type::value_type;
88 
89  struct Transform
90 #ifdef _LIBCPP_VERSION
92  map_type::right_map::const_iterator::value_type const&,
93  beast::IP::Endpoint const&>
94 #endif
95  {
96 #ifndef _LIBCPP_VERSION
97  using first_argument_type =
98  map_type::right_map::const_iterator::value_type const&;
100 #endif
101 
102  explicit Transform() = default;
103 
104  beast::IP::Endpoint const&
106  map_type::right_map::const_iterator::value_type const& v) const
107  {
108  return v.get_left();
109  }
110  };
111 
112 private:
114 
118 
119  // Time after which we can update the database again
121 
122  // Set to true when a database update is needed
124 
125 public:
126  static constexpr int staticValence = 32;
127 
128  using iterator = boost::
129  transform_iterator<Transform, map_type::right_map::const_iterator>;
130 
132 
133  Bootcache(Store& store, clock_type& clock, beast::Journal journal);
134 
135  ~Bootcache();
136 
138  bool
139  empty() const;
140 
142  map_type::size_type
143  size() const;
144 
148  begin() const;
150  cbegin() const;
152  end() const;
154  cend() const;
155  void
156  clear();
160  void
161  load();
162 
164  bool
165  insert(beast::IP::Endpoint const& endpoint);
166 
168  bool
169  insertStatic(beast::IP::Endpoint const& endpoint);
170 
172  void
173  on_success(beast::IP::Endpoint const& endpoint);
174 
176  void
177  on_failure(beast::IP::Endpoint const& endpoint);
178 
180  void
182 
184  void
186 
187 private:
188  void
189  prune();
190  void
191  update();
192  void
193  checkUpdate();
194  void
195  flagForUpdate();
196 };
197 
198 } // namespace PeerFinder
199 } // namespace ripple
200 
201 #endif
ripple::PeerFinder::Bootcache::cend
const_iterator cend() const
Definition: Bootcache.cpp:73
ripple::PeerFinder::Bootcache::insertStatic
bool insertStatic(beast::IP::Endpoint const &endpoint)
Add a staticallyconfigured address to the cache.
Definition: Bootcache.cpp:125
ripple::PeerFinder::Bootcache::Entry::valence
int valence() const
Definition: Bootcache.h:67
beast::PropertyStream::Map
Definition: PropertyStream.h:236
ripple::PeerFinder::Bootcache::checkUpdate
void checkUpdate()
Definition: Bootcache.cpp:272
ripple::PeerFinder::Bootcache::Transform::first_argument_type
map_type::right_map::const_iterator::value_type const & first_argument_type
Definition: Bootcache.h:98
ripple::PeerFinder::Bootcache::m_store
Store & m_store
Definition: Bootcache.h:115
ripple::PeerFinder::Bootcache::map_type
boost::bimap< left_t, right_t > map_type
Definition: Bootcache.h:86
ripple::PeerFinder::Bootcache::on_failure
void on_failure(beast::IP::Endpoint const &endpoint)
Called when an outbound connection attempt fails to handshake.
Definition: Bootcache.cpp:173
ripple::PeerFinder::Bootcache::clear
void clear()
Definition: Bootcache.cpp:79
ripple::PeerFinder::Bootcache::m_journal
beast::Journal m_journal
Definition: Bootcache.h:117
ripple::PeerFinder::Bootcache::periodicActivity
void periodicActivity()
Stores the cache in the persistent database on a timer.
Definition: Bootcache.cpp:199
ripple::PeerFinder::Store
Abstract persistence for PeerFinder data.
Definition: Store.h:27
ripple::PeerFinder::Bootcache::flagForUpdate
void flagForUpdate()
Definition: Bootcache.cpp:280
ripple::PeerFinder::Bootcache::prune
void prune()
Definition: Bootcache.cpp:220
ripple::PeerFinder::Bootcache::load
void load()
Load the persisted data from the Store into the container.
Definition: Bootcache.cpp:88
ripple::PeerFinder::Bootcache::iterator
boost::transform_iterator< Transform, map_type::right_map::const_iterator > iterator
Definition: Bootcache.h:129
ripple::PeerFinder::Bootcache::size
map_type::size_type size() const
Returns the number of entries in the cache.
Definition: Bootcache.cpp:49
ripple::PeerFinder::Bootcache::const_iterator
iterator const_iterator
Definition: Bootcache.h:131
ripple::PeerFinder::Bootcache::~Bootcache
~Bootcache()
Definition: Bootcache.cpp:37
ripple::PeerFinder::Bootcache
Stores IP addresses useful for gaining initial connections.
Definition: Bootcache.h:50
ripple::PeerFinder::Bootcache::insert
bool insert(beast::IP::Endpoint const &endpoint)
Add a newly-learned address to the cache.
Definition: Bootcache.cpp:111
ripple::PeerFinder::Bootcache::end
const_iterator end() const
Definition: Bootcache.cpp:67
ripple::PeerFinder::Bootcache::Bootcache
Bootcache(Store &store, clock_type &clock, beast::Journal journal)
Definition: Bootcache.cpp:28
ripple::PeerFinder::Bootcache::m_clock
clock_type & m_clock
Definition: Bootcache.h:116
ripple::PeerFinder::Bootcache::m_map
map_type m_map
Definition: Bootcache.h:113
ripple::PeerFinder::Bootcache::staticValence
static constexpr int staticValence
Definition: Bootcache.h:126
ripple::PeerFinder::Bootcache::Entry::operator<
friend bool operator<(Entry const &lhs, Entry const &rhs)
Definition: Bootcache.h:73
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
ripple::PeerFinder::Bootcache::Transform::Transform
Transform()=default
ripple::PeerFinder::Bootcache::Transform::operator()
beast::IP::Endpoint const & operator()(map_type::right_map::const_iterator::value_type const &v) const
Definition: Bootcache.h:105
beast::abstract_clock< std::chrono::steady_clock >
ripple::PeerFinder::Bootcache::onWrite
void onWrite(beast::PropertyStream::Map &map)
Write the cache state to the property stream.
Definition: Bootcache.cpp:207
std::unary_function
ripple::PeerFinder::Bootcache::on_success
void on_success(beast::IP::Endpoint const &endpoint)
Called when an outbound connection handshake completes.
Definition: Bootcache.cpp:147
ripple::PeerFinder::Bootcache::update
void update()
Definition: Bootcache.cpp:251
ripple::PeerFinder::Bootcache::Transform
Definition: Bootcache.h:89
ripple::PeerFinder::Bootcache::right_t
boost::bimaps::multiset_of< Entry > right_t
Definition: Bootcache.h:85
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::PeerFinder::Bootcache::value_type
map_type::value_type value_type
Definition: Bootcache.h:87
ripple::PeerFinder::Bootcache::m_needsUpdate
bool m_needsUpdate
Definition: Bootcache.h:123
ripple::PeerFinder::Bootcache::cbegin
const_iterator cbegin() const
Definition: Bootcache.cpp:61
ripple::PeerFinder::Bootcache::empty
bool empty() const
Returns true if the cache is empty.
Definition: Bootcache.cpp:43
ripple::PeerFinder::Bootcache::begin
const_iterator begin() const
IP::Endpoint iterators that traverse in decreasing valence.
Definition: Bootcache.cpp:55
beast::IP::Endpoint
A version-independent IP address and port combination.
Definition: IPEndpoint.h:39
ripple::PeerFinder::Bootcache::m_whenUpdate
clock_type::time_point m_whenUpdate
Definition: Bootcache.h:120
ripple::PeerFinder::Bootcache::Transform::result_type
beast::IP::Endpoint const & result_type
Definition: Bootcache.h:99
ripple::PeerFinder::Bootcache::Entry::m_valence
int m_valence
Definition: Bootcache.h:81
beast::abstract_clock::time_point
typename Clock::time_point time_point
Definition: abstract_clock.h:63
ripple::PeerFinder::Bootcache::Entry::valence
int & valence()
Definition: Bootcache.h:61
ripple::PeerFinder::Bootcache::Entry::Entry
Entry(int valence)
Definition: Bootcache.h:56
ripple::PeerFinder::Bootcache::left_t
boost::bimaps::unordered_set_of< beast::IP::Endpoint > left_t
Definition: Bootcache.h:84
ripple::PeerFinder::Bootcache::Entry
Definition: Bootcache.h:53