1#ifndef XRPL_PEERFINDER_LIVECACHE_H_INCLUDED
2#define XRPL_PEERFINDER_LIVECACHE_H_INCLUDED
4#include <xrpld/peerfinder/PeerfinderManager.h>
5#include <xrpld/peerfinder/detail/Tuning.h>
6#include <xrpld/peerfinder/detail/iosformat.h>
8#include <xrpl/basics/Log.h>
9#include <xrpl/basics/random.h>
10#include <xrpl/beast/container/aged_map.h>
11#include <xrpl/beast/utility/maybe_const.h>
13#include <boost/intrusive/list.hpp>
14#include <boost/iterator/transform_iterator.hpp>
32 struct Element : boost::intrusive::list_base_hook<>
42 make_list<Element, boost::intrusive::constant_time_size<false>>::type;
49 template <
bool IsConst>
70 transform_iterator<Transform, typename list_type::const_iterator>;
76 typename list_type::const_reverse_iterator>;
132 auto& e(
const_cast<Element&
>(*pos.base()));
153 template <
bool IsConst>
177template <
class Allocator = std::allocator<
char>>
198 Allocator alloc = Allocator());
218 template <
bool IsConst>
229 typename lists_type::value_type>::type& list)
const
231 return make_hop<IsConst>(list);
237 transform_iterator<Transform<false>,
typename lists_type::iterator>;
241 typename lists_type::const_iterator>;
245 typename lists_type::reverse_iterator>;
249 typename lists_type::const_reverse_iterator>;
331 explicit hops_t(Allocator
const& alloc);
377template <
class Allocator>
382 : m_journal(journal), m_cache(clock, alloc), hops(alloc)
386template <
class Allocator>
398 iter = m_cache.
erase(iter);
404 << ((n > 1) ?
" entries" :
" entry");
408template <
class Allocator>
420 "ripple::PeerFinder::Livecache::insert : maximum input hops");
422 Element& e(result.first->second);
435 << ep.
address <<
" at hops +" << excess;
439 m_cache.
touch(result.first);
444 hops.reinsert(e, ep.
hops);
455template <
class Allocator>
461 map[
"size"] = size();
462 map[
"hist"] = hops.histogram();
464 for (
auto iter(m_cache.
cbegin()); iter != m_cache.
cend(); ++iter)
466 auto const& e(iter->second);
468 item[
"hops"] = e.endpoint.hops;
469 item[
"address"] = e.endpoint.address.to_string();
471 ss << (iter.when() -
expired).count();
472 item[
"expires"] = ss.
str();
478template <
class Allocator>
494template <
class Allocator>
499 for (
auto const& h : m_hist)
508template <
class Allocator>
511 std::fill(m_hist.begin(), m_hist.end(), 0);
514template <
class Allocator>
520 "ripple::PeerFinder::Livecache::hops_t::insert : maximum input hops");
526template <
class Allocator>
532 "ripple::PeerFinder::Livecache::hops_t::reinsert : maximum hops input");
535 list.erase(list.iterator_to(e));
543template <
class Allocator>
550 list.erase(list.iterator_to(e));
T back_inserter(T... args)
A version-independent IP address and port combination.
A generic endpoint for log messages.
Stream trace() const
Severity stream access functions.
virtual time_point now() const =0
Returns the current time.
time_point const & when() const
Associative container where each element is also indexed by time.
const_iterator cend() const
beast::detail::aged_container_iterator< false, Iterator > erase(beast::detail::aged_container_iterator< is_const, Iterator > pos)
const_iterator cbegin() const
void touch(beast::detail::aged_container_iterator< is_const, Iterator > pos)
class beast::detail::aged_ordered_container::chronological_t chronological
size_type size() const noexcept
auto emplace(Args &&... args) -> typename std::enable_if<!maybe_multi, std::pair< iterator, bool > >::type
bool empty() const noexcept
typename clock_type::time_point time_point
boost::transform_iterator< Transform< true >, typename lists_type::const_iterator > const_iterator
const_iterator begin() const
void shuffle()
Shuffle each hop list.
const_iterator end() const
std::string histogram() const
hops_t(Allocator const &alloc)
const_reverse_iterator crend() const
const_reverse_iterator rend() const
void reinsert(Element &e, std::uint32_t hops)
const_reverse_iterator rbegin() const
boost::transform_iterator< Transform< false >, typename lists_type::reverse_iterator > reverse_iterator
reverse_iterator rbegin()
const_iterator cbegin() const
boost::transform_iterator< Transform< false >, typename lists_type::iterator > iterator
const_iterator cend() const
const_reverse_iterator crbegin() const
boost::transform_iterator< Transform< true >, typename lists_type::const_reverse_iterator > const_reverse_iterator
The Livecache holds the short-lived relayed Endpoint messages.
cache_type::size_type size() const
Returns the number of entries in the cache.
void expire()
Erase entries whose time has expired.
Livecache(clock_type &clock, beast::Journal journal, Allocator alloc=Allocator())
Create the cache.
void insert(Endpoint const &ep)
Creates or updates an existing Element based on a new message.
void onWrite(beast::PropertyStream::Map &map)
Output statistics.
class ripple::PeerFinder::Livecache::hops_t hops
bool empty() const
Returns true if the cache is empty.
A list of Endpoint at the same hops This is a lightweight wrapper around a reference to the underlyin...
reverse_iterator crbegin() const
void move_back(const_iterator pos)
Hop(typename beast::maybe_const< IsConst, list_type >::type &list)
reverse_iterator rend() const
std::reference_wrapper< typename beast::maybe_const< IsConst, list_type >::type > m_list
boost::transform_iterator< Transform, typename list_type::const_iterator > iterator
reverse_iterator crend() const
reverse_iterator rbegin() const
boost::transform_iterator< Transform, typename list_type::const_reverse_iterator > reverse_iterator
reverse_iterator const_reverse_iterator
static Hop< IsConst > make_hop(typename beast::maybe_const< IsConst, list_type >::type &list)
boost::intrusive::make_list< Element, boost::intrusive::constant_time_size< false > >::type list_type
std::chrono::seconds constexpr liveCacheSecondsToLive(30)
std::uint32_t constexpr maxHops
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
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,...
@ expired
List is expired, but has the largest non-pending sequence seen so far.
beast::xor_shift_engine & default_prng()
Return the default random engine.
Left justifies a field at the specified width.
Makes T const or non const depending on a bool.
typename std::conditional< IsConst, typename std::remove_const< T >::type const, typename std::remove_const< T >::type >::type type
Describes a connectible peer address along with some metadata.
beast::IP::Endpoint address
Element(Endpoint const &endpoint_)