rippled
Loading...
Searching...
No Matches
PeerfinderManager.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpld/peerfinder/PeerfinderManager.h>
21#include <xrpld/peerfinder/detail/Checker.h>
22#include <xrpld/peerfinder/detail/Logic.h>
23#include <xrpld/peerfinder/detail/SourceStrings.h>
24#include <xrpld/peerfinder/detail/StoreSqdb.h>
25
26#include <boost/asio/executor_work_guard.hpp>
27#include <boost/asio/io_context.hpp>
28
29#include <memory>
30#include <optional>
31
32namespace ripple {
33namespace PeerFinder {
34
35class ManagerImp : public Manager
36{
37public:
38 boost::asio::io_context& io_context_;
39 std::optional<boost::asio::executor_work_guard<
40 boost::asio::io_context::executor_type>>
48
49 //--------------------------------------------------------------------------
50
52 boost::asio::io_context& io_context,
53 clock_type& clock,
54 beast::Journal journal,
55 BasicConfig const& config,
56 beast::insight::Collector::ptr const& collector)
57 : Manager()
58 , io_context_(io_context)
59 , work_(std::in_place, boost::asio::make_work_guard(io_context_))
60 , m_clock(clock)
61 , m_journal(journal)
62 , m_store(journal)
64 , m_logic(clock, m_store, checker_, journal)
66 , m_stats(std::bind(&ManagerImp::collect_metrics, this), collector)
67 {
68 }
69
70 ~ManagerImp() override
71 {
72 stop();
73 }
74
75 void
76 stop() override
77 {
78 if (work_)
79 {
80 work_.reset();
81 checker_.stop();
82 m_logic.stop();
83 }
84 }
85
86 //--------------------------------------------------------------------------
87 //
88 // PeerFinder
89 //
90 //--------------------------------------------------------------------------
91
92 void
93 setConfig(Config const& config) override
94 {
96 }
97
98 Config
99 config() override
100 {
101 return m_logic.config();
102 }
103
104 void
106 std::string const& name,
107 std::vector<beast::IP::Endpoint> const& addresses) override
108 {
109 m_logic.addFixedPeer(name, addresses);
110 }
111
112 void
114 std::string const& name,
115 std::vector<std::string> const& strings) override
116 {
118 }
119
120 void
122 {
123 // VFALCO TODO This needs to be implemented
124 }
125
126 //--------------------------------------------------------------------------
127
130 beast::IP::Endpoint const& local_endpoint,
131 beast::IP::Endpoint const& remote_endpoint) override
132 {
133 return m_logic.new_inbound_slot(local_endpoint, remote_endpoint);
134 }
135
137 new_outbound_slot(beast::IP::Endpoint const& remote_endpoint) override
138 {
139 return m_logic.new_outbound_slot(remote_endpoint);
140 }
141
142 void
143 on_endpoints(std::shared_ptr<Slot> const& slot, Endpoints const& endpoints)
144 override
145 {
147 m_logic.on_endpoints(impl, endpoints);
148 }
149
150 void
151 on_closed(std::shared_ptr<Slot> const& slot) override
152 {
154 m_logic.on_closed(impl);
155 }
156
157 void
158 on_failure(std::shared_ptr<Slot> const& slot) override
159 {
161 m_logic.on_failure(impl);
162 }
163
164 void
166 boost::asio::ip::tcp::endpoint const& remote_address,
168 {
169 m_logic.onRedirects(eps.begin(), eps.end(), remote_address);
170 }
171
172 //--------------------------------------------------------------------------
173
174 bool
176 std::shared_ptr<Slot> const& slot,
177 beast::IP::Endpoint const& local_endpoint) override
178 {
180 return m_logic.onConnected(impl, local_endpoint);
181 }
182
183 Result
185 std::shared_ptr<Slot> const& slot,
186 PublicKey const& key,
187 bool reserved) override
188 {
190 return m_logic.activate(impl, key, reserved);
191 }
192
194 redirect(std::shared_ptr<Slot> const& slot) override
195 {
197 return m_logic.redirect(impl);
198 }
199
201 autoconnect() override
202 {
203 return m_logic.autoconnect();
204 }
205
206 void
208 {
210 }
211
214 {
216 }
217
218 void
219 start() override
220 {
222 m_logic.load();
223 }
224
225 //--------------------------------------------------------------------------
226 //
227 // PropertyStream
228 //
229 //--------------------------------------------------------------------------
230
231 void
233 {
234 m_logic.onWrite(map);
235 }
236
237private:
238 struct Stats
239 {
240 template <class Handler>
242 Handler const& handler,
243 beast::insight::Collector::ptr const& collector)
244 : hook(collector->make_hook(handler))
246 collector->make_gauge("Peer_Finder", "Active_Inbound_Peers"))
248 collector->make_gauge("Peer_Finder", "Active_Outbound_Peers"))
249 {
250 }
251
255 };
256
259
260 void
267};
268
269//------------------------------------------------------------------------------
270
271Manager::Manager() noexcept : beast::PropertyStream::Source("peerfinder")
272{
273}
274
277 boost::asio::io_context& io_context,
278 clock_type& clock,
279 beast::Journal journal,
280 BasicConfig const& config,
281 beast::insight::Collector::ptr const& collector)
282{
284 io_context, clock, journal, config, collector);
285}
286
287} // namespace PeerFinder
288} // namespace ripple
T begin(T... args)
A version-independent IP address and port combination.
Definition IPEndpoint.h:38
A generic endpoint for log messages.
Definition Journal.h:60
std::string const & name() const
Returns the name of this source.
A metric for measuring an integral value.
Definition Gauge.h:40
A reference to a handler for performing polled collection.
Definition Hook.h:32
Holds unparsed configuration information.
Tests remote listening sockets to make sure they are connectible.
Definition Checker.h:39
void stop()
Stop the service.
Definition Checker.h:183
int out_active() const
Returns the number of outbound peers assigned an open slot.
Definition Counts.h:113
int inboundActive() const
Returns the number of inbound peers assigned an open slot.
Definition Counts.h:173
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:39
Database persistence for PeerFinder using SQLite.
Definition StoreSqdb.h:32
void open(BasicConfig const &config)
Definition StoreSqdb.h:54
A public key.
Definition PublicKey.h:61
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:25
STL namespace.
T reset(T... args)
PeerFinder configuration settings.
Stats(Handler const &handler, beast::insight::Collector::ptr const &collector)