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#include <xrpl/basics/comparators.h>
26#include <xrpl/beast/utility/Journal.h>
27#include <xrpl/beast/utility/PropertyStream.h>
28#include <boost/bimap.hpp>
29#include <boost/bimap/multiset_of.hpp>
30#include <boost/bimap/unordered_set_of.hpp>
31#include <boost/iterator/transform_iterator.hpp>
32
33namespace ripple {
34namespace PeerFinder {
35
52{
53private:
54 class Entry
55 {
56 public:
58 {
59 }
60
61 int&
63 {
64 return m_valence;
65 }
66
67 int
68 valence() const
69 {
70 return m_valence;
71 }
72
73 friend bool
74 operator<(Entry const& lhs, Entry const& rhs)
75 {
76 if (lhs.valence() > rhs.valence())
77 return true;
78 return false;
79 }
80
81 private:
83 };
84
85 using left_t = boost::bimaps::unordered_set_of<
87 boost::hash<beast::IP::Endpoint>,
89 using right_t = boost::bimaps::multiset_of<Entry, ripple::less<Entry>>;
90 using map_type = boost::bimap<left_t, right_t>;
91 using value_type = map_type::value_type;
92
93 struct Transform
94 {
96 map_type::right_map::const_iterator::value_type const&;
98
99 explicit Transform() = default;
100
103 map_type::right_map::const_iterator::value_type const& v) const
104 {
105 return v.get_left();
106 }
107 };
108
109private:
111
115
116 // Time after which we can update the database again
118
119 // Set to true when a database update is needed
121
122public:
123 static constexpr int staticValence = 32;
124
125 using iterator = boost::
126 transform_iterator<Transform, map_type::right_map::const_iterator>;
127
129
130 Bootcache(Store& store, clock_type& clock, beast::Journal journal);
131
132 ~Bootcache();
133
135 bool
136 empty() const;
137
139 map_type::size_type
140 size() const;
141
145 begin() const;
147 cbegin() const;
149 end() const;
151 cend() const;
152 void
153 clear();
157 void
158 load();
159
161 bool
162 insert(beast::IP::Endpoint const& endpoint);
163
165 bool
166 insertStatic(beast::IP::Endpoint const& endpoint);
167
169 void
170 on_success(beast::IP::Endpoint const& endpoint);
171
173 void
174 on_failure(beast::IP::Endpoint const& endpoint);
175
177 void
179
181 void
183
184private:
185 void
186 prune();
187 void
188 update();
189 void
190 checkUpdate();
191 void
193};
194
195} // namespace PeerFinder
196} // namespace ripple
197
198#endif
A version-independent IP address and port combination.
Definition: IPEndpoint.h:39
A generic endpoint for log messages.
Definition: Journal.h:59
typename Clock::time_point time_point
friend bool operator<(Entry const &lhs, Entry const &rhs)
Definition: Bootcache.h:74
Stores IP addresses useful for gaining initial connections.
Definition: Bootcache.h:52
map_type::size_type size() const
Returns the number of entries in the cache.
Definition: Bootcache.cpp:49
clock_type::time_point m_whenUpdate
Definition: Bootcache.h:117
const_iterator cbegin() const
Definition: Bootcache.cpp:61
const_iterator end() const
Definition: Bootcache.cpp:67
static constexpr int staticValence
Definition: Bootcache.h:123
const_iterator cend() const
Definition: Bootcache.cpp:73
void periodicActivity()
Stores the cache in the persistent database on a timer.
Definition: Bootcache.cpp:203
boost::transform_iterator< Transform, map_type::right_map::const_iterator > iterator
Definition: Bootcache.h:126
map_type::value_type value_type
Definition: Bootcache.h:91
bool insert(beast::IP::Endpoint const &endpoint)
Add a newly-learned address to the cache.
Definition: Bootcache.cpp:111
boost::bimaps::unordered_set_of< beast::IP::Endpoint, boost::hash< beast::IP::Endpoint >, ripple::equal_to< beast::IP::Endpoint > > left_t
Definition: Bootcache.h:88
void on_failure(beast::IP::Endpoint const &endpoint)
Called when an outbound connection attempt fails to handshake.
Definition: Bootcache.cpp:175
const_iterator begin() const
IP::Endpoint iterators that traverse in decreasing valence.
Definition: Bootcache.cpp:55
void onWrite(beast::PropertyStream::Map &map)
Write the cache state to the property stream.
Definition: Bootcache.cpp:211
void on_success(beast::IP::Endpoint const &endpoint)
Called when an outbound connection handshake completes.
Definition: Bootcache.cpp:147
bool empty() const
Returns true if the cache is empty.
Definition: Bootcache.cpp:43
bool insertStatic(beast::IP::Endpoint const &endpoint)
Add a staticallyconfigured address to the cache.
Definition: Bootcache.cpp:125
boost::bimaps::multiset_of< Entry, ripple::less< Entry > > right_t
Definition: Bootcache.h:89
void load()
Load the persisted data from the Store into the container.
Definition: Bootcache.cpp:88
boost::bimap< left_t, right_t > map_type
Definition: Bootcache.h:90
Abstract persistence for PeerFinder data.
Definition: Store.h:28
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
map_type::right_map::const_iterator::value_type const & first_argument_type
Definition: Bootcache.h:96
beast::IP::Endpoint const & result_type
Definition: Bootcache.h:97
beast::IP::Endpoint const & operator()(map_type::right_map::const_iterator::value_type const &v) const
Definition: Bootcache.h:102