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
29#include <boost/bimap.hpp>
30#include <boost/bimap/multiset_of.hpp>
31#include <boost/bimap/unordered_set_of.hpp>
32#include <boost/iterator/transform_iterator.hpp>
33
34namespace ripple {
35namespace PeerFinder {
36
53{
54private:
55 class Entry
56 {
57 public:
59 {
60 }
61
62 int&
64 {
65 return m_valence;
66 }
67
68 int
69 valence() const
70 {
71 return m_valence;
72 }
73
74 friend bool
75 operator<(Entry const& lhs, Entry const& rhs)
76 {
77 if (lhs.valence() > rhs.valence())
78 return true;
79 return false;
80 }
81
82 private:
84 };
85
86 using left_t = boost::bimaps::unordered_set_of<
88 boost::hash<beast::IP::Endpoint>,
90 using right_t = boost::bimaps::multiset_of<Entry, ripple::less<Entry>>;
91 using map_type = boost::bimap<left_t, right_t>;
92 using value_type = map_type::value_type;
93
94 struct Transform
95 {
97 map_type::right_map::const_iterator::value_type const&;
99
100 explicit Transform() = default;
101
104 map_type::right_map::const_iterator::value_type const& v) const
105 {
106 return v.get_left();
107 }
108 };
109
110private:
112
116
117 // Time after which we can update the database again
119
120 // Set to true when a database update is needed
122
123public:
124 static constexpr int staticValence = 32;
125
126 using iterator = boost::
127 transform_iterator<Transform, map_type::right_map::const_iterator>;
128
130
131 Bootcache(Store& store, clock_type& clock, beast::Journal journal);
132
133 ~Bootcache();
134
136 bool
137 empty() const;
138
140 map_type::size_type
141 size() const;
142
146 begin() const;
148 cbegin() const;
150 end() const;
152 cend() const;
153 void
154 clear();
158 void
159 load();
160
162 bool
163 insert(beast::IP::Endpoint const& endpoint);
164
166 bool
167 insertStatic(beast::IP::Endpoint const& endpoint);
168
170 void
171 on_success(beast::IP::Endpoint const& endpoint);
172
174 void
175 on_failure(beast::IP::Endpoint const& endpoint);
176
178 void
180
182 void
184
185private:
186 void
187 prune();
188 void
189 update();
190 void
191 checkUpdate();
192 void
194};
195
196} // namespace PeerFinder
197} // namespace ripple
198
199#endif
A version-independent IP address and port combination.
Definition: IPEndpoint.h:39
A generic endpoint for log messages.
Definition: Journal.h:60
typename Clock::time_point time_point
friend bool operator<(Entry const &lhs, Entry const &rhs)
Definition: Bootcache.h:75
Stores IP addresses useful for gaining initial connections.
Definition: Bootcache.h:53
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:118
const_iterator cbegin() const
Definition: Bootcache.cpp:61
const_iterator end() const
Definition: Bootcache.cpp:67
static constexpr int staticValence
Definition: Bootcache.h:124
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:127
map_type::value_type value_type
Definition: Bootcache.h:92
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:89
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:90
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:91
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:26
map_type::right_map::const_iterator::value_type const & first_argument_type
Definition: Bootcache.h:97
beast::IP::Endpoint const & result_type
Definition: Bootcache.h:98
beast::IP::Endpoint const & operator()(map_type::right_map::const_iterator::value_type const &v) const
Definition: Bootcache.h:103