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/peerfinder/PeerfinderManager.h>
24 #include <ripple/peerfinder/impl/Store.h>
25 #include <ripple/beast/utility/Journal.h>
26 #include <ripple/beast/utility/PropertyStream.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:
56  Entry (int valence)
57  : m_valence (valence)
58  {
59  }
60 
61  int& valence ()
62  {
63  return m_valence;
64  }
65 
66  int valence () const
67  {
68  return m_valence;
69  }
70 
71  friend bool operator< (Entry const& lhs, Entry const& rhs)
72  {
73  if (lhs.valence() > rhs.valence())
74  return true;
75  return false;
76  }
77 
78  private:
79  int m_valence;
80  };
81 
82  using left_t = boost::bimaps::unordered_set_of <beast::IP::Endpoint>;
83  using right_t = boost::bimaps::multiset_of <Entry>;
84  using map_type = boost::bimap <left_t, right_t>;
85  using value_type = map_type::value_type;
86 
87  struct Transform
88 #ifdef _LIBCPP_VERSION
90  map_type::right_map::const_iterator::value_type const&,
91  beast::IP::Endpoint const&>
92 #endif
93  {
94 #ifndef _LIBCPP_VERSION
95  using first_argument_type = map_type::right_map::const_iterator::value_type const&;
97 #endif
98 
99  explicit Transform() = default;
100 
102  map_type::right_map::
103  const_iterator::value_type const& v) const
104  {
105  return v.get_left();
106  }
107  };
108 
109 private:
111 
115 
116  // Time after which we can update the database again
118 
119  // Set to true when a database update is needed
121 
122 public:
123  static constexpr int staticValence = 32;
124 
125  using iterator = boost::transform_iterator <Transform,
126  map_type::right_map::const_iterator>;
127 
129 
130  Bootcache (
131  Store& store,
132  clock_type& clock,
133  beast::Journal journal);
134 
135  ~Bootcache ();
136 
138  bool empty() const;
139 
141  map_type::size_type size() const;
142 
145  const_iterator begin() const;
146  const_iterator cbegin() const;
147  const_iterator end() const;
148  const_iterator cend() const;
149  void clear();
153  void load ();
154 
156  bool insert (beast::IP::Endpoint const& endpoint);
157 
159  bool insertStatic (beast::IP::Endpoint const& endpoint);
160 
162  void on_success (beast::IP::Endpoint const& endpoint);
163 
165  void on_failure (beast::IP::Endpoint const& endpoint);
166 
168  void periodicActivity ();
169 
172 
173 private:
174  void prune ();
175  void update ();
176  void checkUpdate ();
177  void flagForUpdate ();
178 };
179 
180 }
181 }
182 
183 #endif
ripple::PeerFinder::Bootcache::cend
const_iterator cend() const
Definition: Bootcache.cpp:76
ripple::PeerFinder::Bootcache::insertStatic
bool insertStatic(beast::IP::Endpoint const &endpoint)
Add a staticallyconfigured address to the cache.
Definition: Bootcache.cpp:132
ripple::PeerFinder::Bootcache::Entry::valence
int valence() const
Definition: Bootcache.h:66
ripple::PeerFinder::Bootcache::left_t
boost::bimaps::unordered_set_of< beast::IP::Endpoint > left_t
Definition: Bootcache.h:82
std::shared_ptr
STL class.
beast::PropertyStream::Map
Definition: PropertyStream.h:185
ripple::PeerFinder::Bootcache::checkUpdate
void checkUpdate()
Definition: Bootcache.cpp:286
ripple::PeerFinder::Bootcache::iterator
boost::transform_iterator< Transform, map_type::right_map::const_iterator > iterator
Definition: Bootcache.h:126
ripple::PeerFinder::Bootcache::Transform::first_argument_type
map_type::right_map::const_iterator::value_type const & first_argument_type
Definition: Bootcache.h:95
ripple::PeerFinder::Bootcache::m_store
Store & m_store
Definition: Bootcache.h:112
ripple::PeerFinder::Bootcache::map_type
boost::bimap< left_t, right_t > map_type
Definition: Bootcache.h:84
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:184
ripple::PeerFinder::Bootcache::clear
void clear()
Definition: Bootcache.cpp:82
ripple::PeerFinder::Bootcache::m_journal
beast::Journal m_journal
Definition: Bootcache.h:114
ripple::PeerFinder::Bootcache::periodicActivity
void periodicActivity()
Stores the cache in the persistent database on a timer.
Definition: Bootcache.cpp:213
ripple::PeerFinder::Store
Abstract persistence for PeerFinder data.
Definition: Store.h:27
ripple::PeerFinder::Bootcache::flagForUpdate
void flagForUpdate()
Definition: Bootcache.cpp:294
ripple::PeerFinder::Bootcache::prune
void prune()
Definition: Bootcache.cpp:234
ripple::PeerFinder::Bootcache::load
void load()
Load the persisted data from the Store into the container.
Definition: Bootcache.cpp:91
ripple::PeerFinder::Bootcache::size
map_type::size_type size() const
Returns the number of entries in the cache.
Definition: Bootcache.cpp:52
ripple::PeerFinder::Bootcache::const_iterator
iterator const_iterator
Definition: Bootcache.h:128
ripple::PeerFinder::Bootcache::~Bootcache
~Bootcache()
Definition: Bootcache.cpp:40
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:117
ripple::PeerFinder::Bootcache::end
const_iterator end() const
Definition: Bootcache.cpp:70
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:113
ripple::PeerFinder::Bootcache::m_map
map_type m_map
Definition: Bootcache.h:110
ripple::PeerFinder::Bootcache::staticValence
static constexpr int staticValence
Definition: Bootcache.h:123
ripple::PeerFinder::Bootcache::Entry::operator<
friend bool operator<(Entry const &lhs, Entry const &rhs)
Definition: Bootcache.h:71
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:60
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:101
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:221
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:156
ripple::PeerFinder::Bootcache::update
void update()
Definition: Bootcache.cpp:265
ripple::PeerFinder::Bootcache::Transform
Definition: Bootcache.h:87
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:85
ripple::PeerFinder::Bootcache::m_needsUpdate
bool m_needsUpdate
Definition: Bootcache.h:120
ripple::PeerFinder::Bootcache::cbegin
const_iterator cbegin() const
Definition: Bootcache.cpp:64
ripple::PeerFinder::Bootcache::empty
bool empty() const
Returns true if the cache is empty.
Definition: Bootcache.cpp:46
ripple::PeerFinder::Bootcache::begin
const_iterator begin() const
IP::Endpoint iterators that traverse in decreasing valence.
Definition: Bootcache.cpp:58
beast::IP::Endpoint
A version-independent IP address and port combination.
Definition: IPEndpoint.h:39
ripple::PeerFinder::Bootcache::right_t
boost::bimaps::multiset_of< Entry > right_t
Definition: Bootcache.h:83
ripple::PeerFinder::Bootcache::m_whenUpdate
clock_type::time_point m_whenUpdate
Definition: Bootcache.h:117
ripple::PeerFinder::Bootcache::Transform::result_type
beast::IP::Endpoint const & result_type
Definition: Bootcache.h:96
ripple::PeerFinder::Bootcache::Entry::m_valence
int m_valence
Definition: Bootcache.h:79
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::Entry
Definition: Bootcache.h:53