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 ripple {
14namespace PeerFinder {
15
16class ManagerImp : public Manager
17{
18public:
19 boost::asio::io_context& io_context_;
20 std::optional<boost::asio::executor_work_guard<
21 boost::asio::io_context::executor_type>>
29
30 //--------------------------------------------------------------------------
31
33 boost::asio::io_context& io_context,
34 clock_type& clock,
35 beast::Journal journal,
36 BasicConfig const& config,
37 beast::insight::Collector::ptr const& collector)
38 : Manager()
39 , io_context_(io_context)
40 , work_(std::in_place, boost::asio::make_work_guard(io_context_))
41 , m_clock(clock)
42 , m_journal(journal)
43 , m_store(journal)
45 , m_logic(clock, m_store, checker_, journal)
47 , m_stats(std::bind(&ManagerImp::collect_metrics, this), collector)
48 {
49 }
50
51 ~ManagerImp() override
52 {
53 stop();
54 }
55
56 void
57 stop() override
58 {
59 if (work_)
60 {
61 work_.reset();
62 checker_.stop();
63 m_logic.stop();
64 }
65 }
66
67 //--------------------------------------------------------------------------
68 //
69 // PeerFinder
70 //
71 //--------------------------------------------------------------------------
72
73 void
74 setConfig(Config const& config) override
75 {
77 }
78
79 Config
80 config() override
81 {
82 return m_logic.config();
83 }
84
85 void
87 std::string const& name,
88 std::vector<beast::IP::Endpoint> const& addresses) override
89 {
90 m_logic.addFixedPeer(name, addresses);
91 }
92
93 void
95 std::string const& name,
96 std::vector<std::string> const& strings) override
97 {
99 }
100
101 void
103 {
104 // VFALCO TODO This needs to be implemented
105 }
106
107 //--------------------------------------------------------------------------
108
111 beast::IP::Endpoint const& local_endpoint,
112 beast::IP::Endpoint const& remote_endpoint) override
113 {
114 return m_logic.new_inbound_slot(local_endpoint, remote_endpoint);
115 }
116
118 new_outbound_slot(beast::IP::Endpoint const& remote_endpoint) override
119 {
120 return m_logic.new_outbound_slot(remote_endpoint);
121 }
122
123 void
124 on_endpoints(std::shared_ptr<Slot> const& slot, Endpoints const& endpoints)
125 override
126 {
128 m_logic.on_endpoints(impl, endpoints);
129 }
130
131 void
132 on_closed(std::shared_ptr<Slot> const& slot) override
133 {
135 m_logic.on_closed(impl);
136 }
137
138 void
139 on_failure(std::shared_ptr<Slot> const& slot) override
140 {
142 m_logic.on_failure(impl);
143 }
144
145 void
147 boost::asio::ip::tcp::endpoint const& remote_address,
149 {
150 m_logic.onRedirects(eps.begin(), eps.end(), remote_address);
151 }
152
153 //--------------------------------------------------------------------------
154
155 bool
157 std::shared_ptr<Slot> const& slot,
158 beast::IP::Endpoint const& local_endpoint) override
159 {
161 return m_logic.onConnected(impl, local_endpoint);
162 }
163
164 Result
166 std::shared_ptr<Slot> const& slot,
167 PublicKey const& key,
168 bool reserved) override
169 {
171 return m_logic.activate(impl, key, reserved);
172 }
173
175 redirect(std::shared_ptr<Slot> const& slot) override
176 {
178 return m_logic.redirect(impl);
179 }
180
182 autoconnect() override
183 {
184 return m_logic.autoconnect();
185 }
186
187 void
189 {
191 }
192
195 {
197 }
198
199 void
200 start() override
201 {
203 m_logic.load();
204 }
205
206 //--------------------------------------------------------------------------
207 //
208 // PropertyStream
209 //
210 //--------------------------------------------------------------------------
211
212 void
214 {
215 m_logic.onWrite(map);
216 }
217
218private:
219 struct Stats
220 {
221 template <class Handler>
223 Handler const& handler,
224 beast::insight::Collector::ptr const& collector)
225 : hook(collector->make_hook(handler))
227 collector->make_gauge("Peer_Finder", "Active_Inbound_Peers"))
229 collector->make_gauge("Peer_Finder", "Active_Outbound_Peers"))
230 {
231 }
232
236 };
237
240
241 void
248};
249
250//------------------------------------------------------------------------------
251
252Manager::Manager() noexcept : beast::PropertyStream::Source("peerfinder")
253{
254}
255
258 boost::asio::io_context& io_context,
259 clock_type& clock,
260 beast::Journal journal,
261 BasicConfig const& config,
262 beast::insight::Collector::ptr const& collector)
263{
265 io_context, clock, journal, config, collector);
266}
267
268} // namespace PeerFinder
269} // namespace ripple
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 connectible.
Definition Checker.h:20
void stop()
Stop the service.
Definition Checker.h:164
int out_active() const
Returns the number of outbound peers assigned an open slot.
Definition Counts.h:94
int inboundActive() const
Returns the number of inbound peers assigned an open slot.
Definition Counts.h:154
The Logic for maintaining the list of Slot addresses.
void onWrite(beast::PropertyStream::Map &map)
bool onConnected(SlotImp::ptr const &slot, beast::IP::Endpoint const &local_endpoint)
std::vector< std::pair< std::shared_ptr< Slot >, std::vector< Endpoint > > > buildEndpointsForPeers()
void on_closed(SlotImp::ptr const &slot)
void on_failure(SlotImp::ptr const &slot)
Result activate(SlotImp::ptr const &slot, PublicKey const &key, bool reserved)
std::pair< SlotImp::ptr, Result > new_inbound_slot(beast::IP::Endpoint const &local_endpoint, beast::IP::Endpoint const &remote_endpoint)
std::vector< beast::IP::Endpoint > autoconnect()
Create new outbound connection attempts as needed.
void onRedirects(FwdIter first, FwdIter last, boost::asio::ip::tcp::endpoint const &remote_address)
void on_endpoints(SlotImp::ptr const &slot, Endpoints list)
void addStaticSource(std::shared_ptr< Source > const &source)
std::pair< SlotImp::ptr, Result > new_outbound_slot(beast::IP::Endpoint const &remote_endpoint)
void addFixedPeer(std::string const &name, beast::IP::Endpoint const &ep)
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() override
void addFixedPeer(std::string const &name, std::vector< beast::IP::Endpoint > const &addresses) override
Add a peer that should always be connected.
void on_closed(std::shared_ptr< Slot > const &slot) override
Called when the slot is closed.
void addFallbackStrings(std::string const &name, std::vector< std::string > const &strings) override
Add a set of strings as fallback IP::Endpoint sources.
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 stop() override
Transition to the stopped state, synchronously.
std::vector< beast::IP::Endpoint > autoconnect() override
Return a set of addresses we should connect to.
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_failure(std::shared_ptr< Slot > const &slot) override
Called when an outbound connection is deemed to have failed.
Checker< boost::asio::ip::tcp > checker_
bool onConnected(std::shared_ptr< Slot > const &slot, beast::IP::Endpoint const &local_endpoint) override
Called when an outbound connection attempt succeeds.
ManagerImp(boost::asio::io_context &io_context, clock_type &clock, beast::Journal journal, BasicConfig const &config, beast::insight::Collector::ptr const &collector)
boost::asio::io_context & io_context_
void onWrite(beast::PropertyStream::Map &map) override
Subclass override.
void once_per_second() override
Perform periodic activity.
void addFallbackURL(std::string const &name, std::string const &url)
Logic< decltype(checker_)> m_logic
void setConfig(Config const &config) override
Set the configuration for the manager.
std::optional< boost::asio::executor_work_guard< boost::asio::io_context::executor_type > > work_
Config config() override
Returns the configuration for the manager.
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.
std::vector< Endpoint > redirect(std::shared_ptr< Slot > const &slot) override
Returns a set of endpoints suitable for redirection.
void on_endpoints(std::shared_ptr< Slot > const &slot, Endpoints const &endpoints) override
Called when mtENDPOINTS is received.
void start() override
Transition to the started state, synchronously.
Result activate(std::shared_ptr< Slot > const &slot, PublicKey const &key, bool reserved) override
Request an active slot type.
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:35
A public key.
Definition PublicKey.h:43
T end(T... args)
T is_same_v
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.
Result
Possible results from activating a slot.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
STL namespace.
T reset(T... args)
PeerFinder configuration settings.
Stats(Handler const &handler, beast::insight::Collector::ptr const &collector)