rippled
Loading...
Searching...
No Matches
PeerfinderManager.cpp
1#include <xrpld/peerfinder/PeerfinderManager.h>
2#include <xrpld/peerfinder/detail/Checker.h>
3#include <xrpld/peerfinder/detail/Logic.h>
4#include <xrpld/peerfinder/detail/SourceStrings.h>
5#include <xrpld/peerfinder/detail/StoreSqdb.h>
6
7#include <boost/asio/executor_work_guard.hpp>
8#include <boost/asio/io_context.hpp>
9
10#include <memory>
11#include <optional>
12
13namespace xrpl {
14namespace PeerFinder {
15
16class ManagerImp : public Manager
17{
18public:
19 boost::asio::io_context& io_context_;
27
28 //--------------------------------------------------------------------------
29
31 boost::asio::io_context& io_context,
32 clock_type& clock,
33 beast::Journal journal,
34 BasicConfig const& config,
35 beast::insight::Collector::ptr const& collector)
36 : io_context_(io_context)
37 , work_(std::in_place, boost::asio::make_work_guard(io_context_))
38 , m_clock(clock)
39 , m_journal(journal)
40 , m_store(journal)
42 , m_logic(clock, m_store, checker_, journal)
44 , m_stats(std::bind(&ManagerImp::collect_metrics, this), collector)
45 {
46 }
47
48 ~ManagerImp() override
49 {
50 stop();
51 }
52
53 void
54 stop() override
55 {
56 if (work_)
57 {
58 work_.reset();
59 checker_.stop();
60 m_logic.stop();
61 }
62 }
63
64 //--------------------------------------------------------------------------
65 //
66 // PeerFinder
67 //
68 //--------------------------------------------------------------------------
69
70 void
71 setConfig(Config const& config) override
72 {
74 }
75
76 Config
77 config() override
78 {
79 return m_logic.config();
80 }
81
82 void
84 override
85 {
86 m_logic.addFixedPeer(name, addresses);
87 }
88
89 void
94
95 void
97 {
98 // VFALCO TODO This needs to be implemented
99 }
100
101 //--------------------------------------------------------------------------
102
105 beast::IP::Endpoint const& local_endpoint,
106 beast::IP::Endpoint const& remote_endpoint) override
107 {
108 return m_logic.new_inbound_slot(local_endpoint, remote_endpoint);
109 }
110
112 new_outbound_slot(beast::IP::Endpoint const& remote_endpoint) override
113 {
114 return m_logic.new_outbound_slot(remote_endpoint);
115 }
116
117 void
118 on_endpoints(std::shared_ptr<Slot> const& slot, Endpoints const& endpoints) override
119 {
121 m_logic.on_endpoints(impl, endpoints);
122 }
123
124 void
125 on_closed(std::shared_ptr<Slot> const& slot) override
126 {
128 m_logic.on_closed(impl);
129 }
130
131 void
132 on_failure(std::shared_ptr<Slot> const& slot) override
133 {
135 m_logic.on_failure(impl);
136 }
137
138 void
140 boost::asio::ip::tcp::endpoint const& remote_address,
142 {
143 m_logic.onRedirects(eps.begin(), eps.end(), remote_address);
144 }
145
146 //--------------------------------------------------------------------------
147
148 bool
149 onConnected(std::shared_ptr<Slot> const& slot, beast::IP::Endpoint const& local_endpoint)
150 override
151 {
153 return m_logic.onConnected(impl, local_endpoint);
154 }
155
156 Result
157 activate(std::shared_ptr<Slot> const& slot, PublicKey const& key, bool reserved) override
158 {
160 return m_logic.activate(impl, key, reserved);
161 }
162
164 redirect(std::shared_ptr<Slot> const& slot) override
165 {
167 return m_logic.redirect(impl);
168 }
169
171 autoconnect() override
172 {
173 return m_logic.autoconnect();
174 }
175
176 void
178 {
180 }
181
184 {
186 }
187
188 void
189 start() override
190 {
192 m_logic.load();
193 }
194
195 //--------------------------------------------------------------------------
196 //
197 // PropertyStream
198 //
199 //--------------------------------------------------------------------------
200
201 void
203 {
204 m_logic.onWrite(map);
205 }
206
207private:
208 struct Stats
209 {
210 template <class Handler>
211 Stats(Handler const& handler, beast::insight::Collector::ptr const& collector)
212 : hook(collector->make_hook(handler))
213 , activeInboundPeers(collector->make_gauge("Peer_Finder", "Active_Inbound_Peers"))
214 , activeOutboundPeers(collector->make_gauge("Peer_Finder", "Active_Outbound_Peers"))
215 {
216 }
217
221 };
222
225
226 void
233};
234
235//------------------------------------------------------------------------------
236
237Manager::Manager() noexcept : beast::PropertyStream::Source("peerfinder")
238{
239}
240
243 boost::asio::io_context& io_context,
244 clock_type& clock,
245 beast::Journal journal,
246 BasicConfig const& config,
247 beast::insight::Collector::ptr const& collector)
248{
249 return std::make_unique<ManagerImp>(io_context, clock, journal, config, collector);
250}
251
252} // namespace PeerFinder
253} // namespace xrpl
T begin(T... args)
A version-independent IP address and port combination.
Definition IPEndpoint.h:18
A generic endpoint for log messages.
Definition Journal.h:40
std::string const & name() const
Returns the name of this source.
A metric for measuring an integral value.
Definition Gauge.h:20
A reference to a handler for performing polled collection.
Definition Hook.h:12
Holds unparsed configuration information.
Tests remote listening sockets to make sure they are connectable.
Definition Checker.h:19
void stop()
Stop the service.
Definition Checker.h:155
int inboundActive() const
Returns the number of inbound peers assigned an open slot.
Definition Counts.h:135
int out_active() const
Returns the number of outbound peers assigned an open slot.
Definition Counts.h:75
The Logic for maintaining the list of Slot addresses.
void on_closed(SlotImp::ptr const &slot)
std::vector< Endpoint > redirect(SlotImp::ptr const &slot)
Return a list of addresses suitable for redirection.
std::vector< std::pair< std::shared_ptr< Slot >, std::vector< Endpoint > > > buildEndpointsForPeers()
void onRedirects(FwdIter first, FwdIter last, boost::asio::ip::tcp::endpoint const &remote_address)
void addFixedPeer(std::string const &name, beast::IP::Endpoint const &ep)
std::pair< SlotImp::ptr, Result > new_inbound_slot(beast::IP::Endpoint const &local_endpoint, beast::IP::Endpoint const &remote_endpoint)
Result activate(SlotImp::ptr const &slot, PublicKey const &key, bool reserved)
void on_endpoints(SlotImp::ptr const &slot, Endpoints list)
bool onConnected(SlotImp::ptr const &slot, beast::IP::Endpoint const &local_endpoint)
std::vector< beast::IP::Endpoint > autoconnect()
Create new outbound connection attempts as needed.
void addStaticSource(std::shared_ptr< Source > const &source)
std::pair< SlotImp::ptr, Result > new_outbound_slot(beast::IP::Endpoint const &remote_endpoint)
void on_failure(SlotImp::ptr const &slot)
void onWrite(beast::PropertyStream::Map &map)
ManagerImp(boost::asio::io_context &io_context, clock_type &clock, beast::Journal journal, BasicConfig const &config, beast::insight::Collector::ptr const &collector)
void setConfig(Config const &config) override
Set the configuration for the manager.
void on_closed(std::shared_ptr< Slot > const &slot) override
Called when the slot is closed.
void start() override
Transition to the started state, synchronously.
Config config() override
Returns the configuration for the manager.
void once_per_second() override
Perform periodic activity.
void on_failure(std::shared_ptr< Slot > const &slot) override
Called when an outbound connection is deemed to have failed.
std::vector< Endpoint > redirect(std::shared_ptr< Slot > const &slot) override
Returns a set of endpoints suitable for redirection.
std::pair< std::shared_ptr< Slot >, Result > new_outbound_slot(beast::IP::Endpoint const &remote_endpoint) override
Create a new outbound slot with the specified remote endpoint.
void on_endpoints(std::shared_ptr< Slot > const &slot, Endpoints const &endpoints) override
Called when mtENDPOINTS is received.
Logic< decltype(checker_)> m_logic
void addFallbackURL(std::string const &name, std::string const &url)
void onWrite(beast::PropertyStream::Map &map) override
Subclass override.
Checker< boost::asio::ip::tcp > checker_
void stop() override
Transition to the stopped state, synchronously.
std::pair< std::shared_ptr< Slot >, Result > new_inbound_slot(beast::IP::Endpoint const &local_endpoint, beast::IP::Endpoint const &remote_endpoint) override
Add a URL as a fallback location to obtain IP::Endpoint sources.
void addFallbackStrings(std::string const &name, std::vector< std::string > const &strings) override
Add a set of strings as fallback IP::Endpoint sources.
std::vector< beast::IP::Endpoint > autoconnect() override
Return a set of addresses we should connect to.
Result activate(std::shared_ptr< Slot > const &slot, PublicKey const &key, bool reserved) override
Request an active slot type.
bool onConnected(std::shared_ptr< Slot > const &slot, beast::IP::Endpoint const &local_endpoint) override
Called when an outbound connection attempt succeeds.
std::vector< std::pair< std::shared_ptr< Slot >, std::vector< Endpoint > > > buildEndpointsForPeers() override
void addFixedPeer(std::string const &name, std::vector< beast::IP::Endpoint > const &addresses) override
Add a peer that should always be connected.
void onRedirects(boost::asio::ip::tcp::endpoint const &remote_address, std::vector< boost::asio::ip::tcp::endpoint > const &eps) override
Called when we received redirect IPs from a busy peer.
boost::asio::io_context & io_context_
std::optional< boost::asio::executor_work_guard< boost::asio::io_context::executor_type > > work_
Maintains a set of IP addresses used for getting into the network.
static std::shared_ptr< Source > New(std::string const &name, Strings const &strings)
A static or dynamic source of peer addresses.
Definition Source.h:19
Database persistence for PeerFinder using SQLite.
Definition StoreSqdb.h:13
void open(BasicConfig const &config)
Definition StoreSqdb.h:34
A public key.
Definition PublicKey.h:42
T end(T... args)
T is_same_v
STL namespace.
Result
Possible results from activating a slot.
std::unique_ptr< Manager > make_Manager(boost::asio::io_context &io_context, clock_type &clock, beast::Journal journal, BasicConfig const &config, beast::insight::Collector::ptr const &collector)
Create a new Manager.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
T reset(T... args)
PeerFinder configuration settings.
Stats(Handler const &handler, beast::insight::Collector::ptr const &collector)