rippled
Loading...
Searching...
No Matches
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 <xrpld/peerfinder/PeerfinderManager.h>
24#include <xrpld/peerfinder/detail/Store.h>
25
26#include <xrpl/basics/comparators.h>
27#include <xrpl/beast/utility/Journal.h>
28#include <xrpl/beast/utility/PropertyStream.h>
29
30#include <boost/bimap.hpp>
31#include <boost/bimap/multiset_of.hpp>
32#include <boost/bimap/unordered_set_of.hpp>
33#include <boost/iterator/transform_iterator.hpp>
34
35namespace ripple {
36namespace PeerFinder {
37
54{
55private:
56 class Entry
57 {
58 public:
60 {
61 }
62
63 int&
65 {
66 return m_valence;
67 }
68
69 int
70 valence() const
71 {
72 return m_valence;
73 }
74
75 friend bool
76 operator<(Entry const& lhs, Entry const& rhs)
77 {
78 if (lhs.valence() > rhs.valence())
79 return true;
80 return false;
81 }
82
83 private:
85 };
86
87 using left_t = boost::bimaps::unordered_set_of<
89 boost::hash<beast::IP::Endpoint>,
91 using right_t = boost::bimaps::multiset_of<Entry, ripple::less<Entry>>;
92 using map_type = boost::bimap<left_t, right_t>;
93 using value_type = map_type::value_type;
94
95 struct Transform
96 {
98 map_type::right_map::const_iterator::value_type const&;
100
101 explicit Transform() = default;
102
105 map_type::right_map::const_iterator::value_type const& v) const
106 {
107 return v.get_left();
108 }
109 };
110
111private:
113
117
118 // Time after which we can update the database again
120
121 // Set to true when a database update is needed
123
124public:
125 static constexpr int staticValence = 32;
126
127 using iterator = boost::
128 transform_iterator<Transform, map_type::right_map::const_iterator>;
129
131
132 Bootcache(Store& store, clock_type& clock, beast::Journal journal);
133
134 ~Bootcache();
135
137 bool
138 empty() const;
139
141 map_type::size_type
142 size() const;
143
147 begin() const;
149 cbegin() const;
151 end() const;
153 cend() const;
154 void
155 clear();
159 void
160 load();
161
163 bool
164 insert(beast::IP::Endpoint const& endpoint);
165
167 bool
168 insertStatic(beast::IP::Endpoint const& endpoint);
169
171 void
172 on_success(beast::IP::Endpoint const& endpoint);
173
175 void
176 on_failure(beast::IP::Endpoint const& endpoint);
177
179 void
181
183 void
185
186private:
187 void
188 prune();
189 void
190 update();
191 void
192 checkUpdate();
193 void
195};
196
197} // namespace PeerFinder
198} // namespace ripple
199
200#endif
A version-independent IP address and port combination.
Definition: IPEndpoint.h:38
A generic endpoint for log messages.
Definition: Journal.h:60
friend bool operator<(Entry const &lhs, Entry const &rhs)
Definition: Bootcache.h:76
Stores IP addresses useful for gaining initial connections.
Definition: Bootcache.h:54
map_type::size_type size() const
Returns the number of entries in the cache.
Definition: Bootcache.cpp:50
clock_type::time_point m_whenUpdate
Definition: Bootcache.h:119
const_iterator cbegin() const
Definition: Bootcache.cpp:62
const_iterator end() const
Definition: Bootcache.cpp:68
static constexpr int staticValence
Definition: Bootcache.h:125
const_iterator cend() const
Definition: Bootcache.cpp:74
void periodicActivity()
Stores the cache in the persistent database on a timer.
Definition: Bootcache.cpp:204
boost::transform_iterator< Transform, map_type::right_map::const_iterator > iterator
Definition: Bootcache.h:128
map_type::value_type value_type
Definition: Bootcache.h:93
bool insert(beast::IP::Endpoint const &endpoint)
Add a newly-learned address to the cache.
Definition: Bootcache.cpp:112
boost::bimaps::unordered_set_of< beast::IP::Endpoint, boost::hash< beast::IP::Endpoint >, ripple::equal_to< beast::IP::Endpoint > > left_t
Definition: Bootcache.h:90
void on_failure(beast::IP::Endpoint const &endpoint)
Called when an outbound connection attempt fails to handshake.
Definition: Bootcache.cpp:176
const_iterator begin() const
IP::Endpoint iterators that traverse in decreasing valence.
Definition: Bootcache.cpp:56
void onWrite(beast::PropertyStream::Map &map)
Write the cache state to the property stream.
Definition: Bootcache.cpp:212
void on_success(beast::IP::Endpoint const &endpoint)
Called when an outbound connection handshake completes.
Definition: Bootcache.cpp:148
bool empty() const
Returns true if the cache is empty.
Definition: Bootcache.cpp:44
bool insertStatic(beast::IP::Endpoint const &endpoint)
Add a staticallyconfigured address to the cache.
Definition: Bootcache.cpp:126
boost::bimaps::multiset_of< Entry, ripple::less< Entry > > right_t
Definition: Bootcache.h:91
void load()
Load the persisted data from the Store into the container.
Definition: Bootcache.cpp:89
boost::bimap< left_t, right_t > map_type
Definition: Bootcache.h:92
Abstract persistence for PeerFinder data.
Definition: Store.h:29
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:25
map_type::right_map::const_iterator::value_type const & first_argument_type
Definition: Bootcache.h:98
beast::IP::Endpoint const & result_type
Definition: Bootcache.h:99
beast::IP::Endpoint const & operator()(map_type::right_map::const_iterator::value_type const &v) const
Definition: Bootcache.h:104