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 : Manager()
37 , io_context_(io_context)
38 , work_(std::in_place, boost::asio::make_work_guard(io_context_))
39 , m_clock(clock)
40 , m_journal(journal)
41 , m_store(journal)
43 , m_logic(clock, m_store, checker_, journal)
45 , m_stats(std::bind(&ManagerImp::collect_metrics, this), collector)
46 {
47 }
48
49 ~ManagerImp() override
50 {
51 stop();
52 }
53
54 void
55 stop() override
56 {
57 if (work_)
58 {
59 work_.reset();
60 checker_.stop();
61 m_logic.stop();
62 }
63 }
64
65 //--------------------------------------------------------------------------
66 //
67 // PeerFinder
68 //
69 //--------------------------------------------------------------------------
70
71 void
72 setConfig(Config const& config) override
73 {
75 }
76
77 Config
78 config() override
79 {
80 return m_logic.config();
81 }
82
83 void
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
104 new_inbound_slot(beast::IP::Endpoint const& local_endpoint, beast::IP::Endpoint const& remote_endpoint) override
105 {
106 return m_logic.new_inbound_slot(local_endpoint, remote_endpoint);
107 }
108
110 new_outbound_slot(beast::IP::Endpoint const& remote_endpoint) override
111 {
112 return m_logic.new_outbound_slot(remote_endpoint);
113 }
114
115 void
116 on_endpoints(std::shared_ptr<Slot> const& slot, Endpoints const& endpoints) override
117 {
119 m_logic.on_endpoints(impl, endpoints);
120 }
121
122 void
123 on_closed(std::shared_ptr<Slot> const& slot) override
124 {
126 m_logic.on_closed(impl);
127 }
128
129 void
130 on_failure(std::shared_ptr<Slot> const& slot) override
131 {
133 m_logic.on_failure(impl);
134 }
135
136 void
138 boost::asio::ip::tcp::endpoint const& remote_address,
140 {
141 m_logic.onRedirects(eps.begin(), eps.end(), remote_address);
142 }
143
144 //--------------------------------------------------------------------------
145
146 bool
147 onConnected(std::shared_ptr<Slot> const& slot, beast::IP::Endpoint const& local_endpoint) override
148 {
150 return m_logic.onConnected(impl, local_endpoint);
151 }
152
153 Result
154 activate(std::shared_ptr<Slot> const& slot, PublicKey const& key, bool reserved) override
155 {
157 return m_logic.activate(impl, key, reserved);
158 }
159
161 redirect(std::shared_ptr<Slot> const& slot) override
162 {
164 return m_logic.redirect(impl);
165 }
166
168 autoconnect() override
169 {
170 return m_logic.autoconnect();
171 }
172
173 void
175 {
177 }
178
181 {
183 }
184
185 void
186 start() override
187 {
189 m_logic.load();
190 }
191
192 //--------------------------------------------------------------------------
193 //
194 // PropertyStream
195 //
196 //--------------------------------------------------------------------------
197
198 void
200 {
201 m_logic.onWrite(map);
202 }
203
204private:
205 struct Stats
206 {
207 template <class Handler>
208 Stats(Handler const& handler, beast::insight::Collector::ptr const& collector)
209 : hook(collector->make_hook(handler))
210 , activeInboundPeers(collector->make_gauge("Peer_Finder", "Active_Inbound_Peers"))
211 , activeOutboundPeers(collector->make_gauge("Peer_Finder", "Active_Outbound_Peers"))
212 {
213 }
214
218 };
219
222
223 void
230};
231
232//------------------------------------------------------------------------------
233
234Manager::Manager() noexcept : beast::PropertyStream::Source("peerfinder")
235{
236}
237
240 boost::asio::io_context& io_context,
241 clock_type& clock,
242 beast::Journal journal,
243 BasicConfig const& config,
244 beast::insight::Collector::ptr const& collector)
245{
246 return std::make_unique<ManagerImp>(io_context, clock, journal, config, collector);
247}
248
249} // namespace PeerFinder
250} // namespace xrpl
T begin(T... args)
A version-independent IP address and port combination.
Definition IPEndpoint.h:19
A generic endpoint for log messages.
Definition Journal.h:41
std::string const & name() const
Returns the name of this source.
A metric for measuring an integral value.
Definition Gauge.h:21
A reference to a handler for performing polled collection.
Definition Hook.h:13
Holds unparsed configuration information.
Tests remote listening sockets to make sure they are connectable.
Definition Checker.h:20
void stop()
Stop the service.
Definition Checker.h:152
int inboundActive() const
Returns the number of inbound peers assigned an open slot.
Definition Counts.h:154
int out_active() const
Returns the number of outbound peers assigned an open slot.
Definition Counts.h:94
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:20
Database persistence for PeerFinder using SQLite.
Definition StoreSqdb.h:13
void open(BasicConfig const &config)
Definition StoreSqdb.h:33
A public key.
Definition PublicKey.h:43
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:6
T reset(T... args)
PeerFinder configuration settings.
Stats(Handler const &handler, beast::insight::Collector::ptr const &collector)