20 #ifndef RIPPLE_PEERFINDER_LIVECACHE_H_INCLUDED
21 #define RIPPLE_PEERFINDER_LIVECACHE_H_INCLUDED
23 #include <ripple/basics/Log.h>
24 #include <ripple/basics/random.h>
25 #include <ripple/beast/container/aged_map.h>
26 #include <ripple/beast/utility/maybe_const.h>
27 #include <ripple/peerfinder/PeerfinderManager.h>
28 #include <ripple/peerfinder/impl/Tuning.h>
29 #include <ripple/peerfinder/impl/iosformat.h>
30 #include <boost/intrusive/list.hpp>
31 #include <boost/iterator/transform_iterator.hpp>
36 namespace PeerFinder {
49 struct Element : boost::intrusive::list_base_hook<>
59 make_list<Element, boost::intrusive::constant_time_size<false>>::type;
66 template <
bool IsConst>
87 transform_iterator<Transform, typename list_type::const_iterator>;
93 typename list_type::const_reverse_iterator>;
149 auto& e(
const_cast<Element&
>(*pos.base()));
170 template <
bool IsConst>
194 template <
class Allocator = std::allocator<
char>>
195 class Livecache :
protected detail::LivecacheBase
215 Allocator alloc = Allocator());
235 template <
bool IsConst>
246 typename lists_type::value_type>::type& list)
const
248 return make_hop<IsConst>(list);
254 transform_iterator<Transform<false>,
typename lists_type::iterator>;
258 typename lists_type::const_iterator>;
262 typename lists_type::reverse_iterator>;
266 typename lists_type::const_reverse_iterator>;
348 explicit hops_t(Allocator
const& alloc);
394 template <
class Allocator>
399 : m_journal(journal), m_cache(clock, alloc), hops(alloc)
403 template <
class Allocator>
413 Element& e(iter->second);
415 iter = m_cache.
erase(iter);
421 << ((n > 1) ?
" entries" :
" entry");
425 template <
class Allocator>
437 Element& e(result.first->second);
445 else if (!result.second && (ep.
hops > e.endpoint.hops))
450 << ep.
address <<
" at hops +" << excess;
454 m_cache.
touch(result.first);
457 if (ep.
hops < e.endpoint.hops)
459 hops.reinsert(e, ep.
hops);
470 template <
class Allocator>
476 map[
"size"] = size();
477 map[
"hist"] = hops.histogram();
479 for (
auto iter(m_cache.
cbegin()); iter != m_cache.
cend(); ++iter)
481 auto const& e(iter->second);
483 item[
"hops"] = e.endpoint.hops;
484 item[
"address"] = e.endpoint.address.to_string();
486 ss << (iter.when() -
expired).count();
487 item[
"expires"] = ss.
str();
493 template <
class Allocator>
509 template <
class Allocator>
514 for (
auto const& h : m_hist)
523 template <
class Allocator>
526 std::fill(m_hist.begin(), m_hist.end(), 0);
529 template <
class Allocator>
535 m_lists[e.endpoint.hops].push_front(e);
536 ++m_hist[e.endpoint.hops];
539 template <
class Allocator>
545 auto& list = m_lists[e.endpoint.hops];
546 list.erase(list.iterator_to(e));
548 --m_hist[e.endpoint.hops];
550 e.endpoint.hops = numHops;
554 template <
class Allocator>
558 --m_hist[e.endpoint.hops];
560 auto& list = m_lists[e.endpoint.hops];
561 list.erase(list.iterator_to(e));
boost::transform_iterator< Transform, typename list_type::const_reverse_iterator > reverse_iterator
bool empty() const noexcept
std::string histogram() const
const_reverse_iterator rbegin() const
Stream trace() const
Severity stream access functions.
time_point const & when() const
boost::transform_iterator< Transform< false >, typename lists_type::reverse_iterator > reverse_iterator
T back_inserter(T... args)
void onWrite(beast::PropertyStream::Map &map)
Output statistics.
boost::transform_iterator< Transform< false >, typename lists_type::iterator > iterator
A list of Endpoint at the same hops This is a lightweight wrapper around a reference to the underlyin...
boost::intrusive::make_list< Element, boost::intrusive::constant_time_size< false > >::type list_type
constexpr std::chrono::seconds liveCacheSecondsToLive(30)
@ expired
List is expired, but has the largest non-pending sequence seen so far.
typename clock_type::time_point time_point
cache_type::size_type size() const
Returns the number of entries in the cache.
virtual time_point now() const =0
Returns the current time.
Element(Endpoint const &endpoint_)
void insert(Endpoint const &ep)
Creates or updates an existing Element based on a new message.
boost::transform_iterator< Transform, typename list_type::const_iterator > iterator
const_iterator cend() const
reverse_iterator rend() const
const_reverse_iterator crend() const
reverse_iterator rbegin() const
const_reverse_iterator rend() const
const_reverse_iterator crbegin() const
auto emplace(Args &&... args) -> typename std::enable_if<!maybe_multi, std::pair< iterator, bool >>::type
Livecache(clock_type &clock, beast::Journal journal, Allocator alloc=Allocator())
Create the cache.
void shuffle()
Shuffle each hop list.
reverse_iterator rbegin()
void move_back(const_iterator pos)
bool set(T &target, std::string const &name, Section const §ion)
Set a value from a configuration Section If the named value is not found or doesn't parse as a T,...
boost::transform_iterator< Transform< true >, typename lists_type::const_iterator > const_iterator
std::reference_wrapper< typename beast::maybe_const< IsConst, list_type >::type > m_list
beast::xor_shift_engine & default_prng()
Return the default random engine.
The Livecache holds the short-lived relayed Endpoint messages.
beast::detail::aged_container_iterator< false, Iterator > erase(beast::detail::aged_container_iterator< is_const, Iterator > pos)
A generic endpoint for log messages.
const_iterator end() const
reverse_iterator crend() const
class ripple::PeerFinder::Livecache::hops_t hops
Makes T const or non const depending on a bool.
Left justifies a field at the specified width.
const_iterator begin() const
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
size_type size() const noexcept
hops_t(Allocator const &alloc)
const_iterator cbegin() const
Associative container where each element is also indexed by time.
typename std::conditional< IsConst, typename std::remove_const< T >::type const, typename std::remove_const< T >::type >::type type
class beast::detail::aged_ordered_container::chronological_t chronological
Hop(typename beast::maybe_const< IsConst, list_type >::type &list)
beast::aged_map< beast::IP::Endpoint, Element, std::chrono::steady_clock, std::less< beast::IP::Endpoint >, Allocator > cache_type
void expire()
Erase entries whose time has expired.
const_iterator cend() const
void touch(beast::detail::aged_container_iterator< is_const, Iterator > pos)
bool empty() const
Returns true if the cache is empty.
A version-independent IP address and port combination.
void reinsert(Element &e, std::uint32_t hops)
constexpr std::uint32_t maxHops
boost::transform_iterator< Transform< true >, typename lists_type::const_reverse_iterator > const_reverse_iterator
const_iterator cbegin() const
Describes a connectible peer address along with some metadata.
reverse_iterator const_reverse_iterator
beast::IP::Endpoint address
static Hop< IsConst > make_hop(typename beast::maybe_const< IsConst, list_type >::type &list)
reverse_iterator crbegin() const