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/io_service.hpp>
27
28#include <memory>
29#include <optional>
30
31namespace ripple {
32namespace PeerFinder {
33
34class ManagerImp : public Manager
35{
36public:
37 boost::asio::io_service& io_service_;
45
46 //--------------------------------------------------------------------------
47
49 boost::asio::io_service& io_service,
50 clock_type& clock,
51 beast::Journal journal,
52 BasicConfig const& config,
53 beast::insight::Collector::ptr const& collector)
54 : Manager()
55 , io_service_(io_service)
56 , work_(std::in_place, std::ref(io_service_))
57 , m_clock(clock)
58 , m_journal(journal)
59 , m_store(journal)
61 , m_logic(clock, m_store, checker_, journal)
63 , m_stats(std::bind(&ManagerImp::collect_metrics, this), collector)
64 {
65 }
66
67 ~ManagerImp() override
68 {
69 stop();
70 }
71
72 void
73 stop() override
74 {
75 if (work_)
76 {
77 work_.reset();
78 checker_.stop();
79 m_logic.stop();
80 }
81 }
82
83 //--------------------------------------------------------------------------
84 //
85 // PeerFinder
86 //
87 //--------------------------------------------------------------------------
88
89 void
90 setConfig(Config const& config) override
91 {
93 }
94
95 Config
96 config() override
97 {
98 return m_logic.config();
99 }
100
101 void
103 std::string const& name,
104 std::vector<beast::IP::Endpoint> const& addresses) override
105 {
106 m_logic.addFixedPeer(name, addresses);
107 }
108
109 void
111 std::string const& name,
112 std::vector<std::string> const& strings) override
113 {
115 }
116
117 void
119 {
120 // VFALCO TODO This needs to be implemented
121 }
122
123 //--------------------------------------------------------------------------
124
127 beast::IP::Endpoint const& local_endpoint,
128 beast::IP::Endpoint const& remote_endpoint) override
129 {
130 return m_logic.new_inbound_slot(local_endpoint, remote_endpoint);
131 }
132
134 new_outbound_slot(beast::IP::Endpoint const& remote_endpoint) override
135 {
136 return m_logic.new_outbound_slot(remote_endpoint);
137 }
138
139 void
140 on_endpoints(std::shared_ptr<Slot> const& slot, Endpoints const& endpoints)
141 override
142 {
143 SlotImp::ptr impl(std::dynamic_pointer_cast<SlotImp>(slot));
144 m_logic.on_endpoints(impl, endpoints);
145 }
146
147 void
148 on_closed(std::shared_ptr<Slot> const& slot) override
149 {
150 SlotImp::ptr impl(std::dynamic_pointer_cast<SlotImp>(slot));
151 m_logic.on_closed(impl);
152 }
153
154 void
155 on_failure(std::shared_ptr<Slot> const& slot) override
156 {
157 SlotImp::ptr impl(std::dynamic_pointer_cast<SlotImp>(slot));
158 m_logic.on_failure(impl);
159 }
160
161 void
163 boost::asio::ip::tcp::endpoint const& remote_address,
165 {
166 m_logic.onRedirects(eps.begin(), eps.end(), remote_address);
167 }
168
169 //--------------------------------------------------------------------------
170
171 bool
173 std::shared_ptr<Slot> const& slot,
174 beast::IP::Endpoint const& local_endpoint) override
175 {
176 SlotImp::ptr impl(std::dynamic_pointer_cast<SlotImp>(slot));
177 return m_logic.onConnected(impl, local_endpoint);
178 }
179
180 Result
182 std::shared_ptr<Slot> const& slot,
183 PublicKey const& key,
184 bool reserved) override
185 {
186 SlotImp::ptr impl(std::dynamic_pointer_cast<SlotImp>(slot));
187 return m_logic.activate(impl, key, reserved);
188 }
189
191 redirect(std::shared_ptr<Slot> const& slot) override
192 {
193 SlotImp::ptr impl(std::dynamic_pointer_cast<SlotImp>(slot));
194 return m_logic.redirect(impl);
195 }
196
198 autoconnect() override
199 {
200 return m_logic.autoconnect();
201 }
202
203 void
205 {
207 }
208
211 {
213 }
214
215 void
216 start() override
217 {
219 m_logic.load();
220 }
221
222 //--------------------------------------------------------------------------
223 //
224 // PropertyStream
225 //
226 //--------------------------------------------------------------------------
227
228 void
230 {
231 m_logic.onWrite(map);
232 }
233
234private:
235 struct Stats
236 {
237 template <class Handler>
239 Handler const& handler,
240 beast::insight::Collector::ptr const& collector)
241 : hook(collector->make_hook(handler))
243 collector->make_gauge("Peer_Finder", "Active_Inbound_Peers"))
245 collector->make_gauge("Peer_Finder", "Active_Outbound_Peers"))
246 {
247 }
248
252 };
253
256
257 void
259 {
263 }
264};
265
266//------------------------------------------------------------------------------
267
268Manager::Manager() noexcept : beast::PropertyStream::Source("peerfinder")
269{
270}
271
274 boost::asio::io_service& io_service,
275 clock_type& clock,
276 beast::Journal journal,
277 BasicConfig const& config,
278 beast::insight::Collector::ptr const& collector)
279{
280 return std::make_unique<ManagerImp>(
281 io_service, clock, journal, config, collector);
282}
283
284} // namespace PeerFinder
285} // namespace ripple
T begin(T... args)
A version-independent IP address and port combination.
Definition: IPEndpoint.h:39
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.
Definition: BasicConfig.h:218
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)
SlotImp::ptr new_outbound_slot(beast::IP::Endpoint const &remote_endpoint)
Result activate(SlotImp::ptr const &slot, PublicKey const &key, bool reserved)
std::vector< beast::IP::Endpoint > autoconnect()
Create new outbound connection attempts as needed.
SlotImp::ptr new_inbound_slot(beast::IP::Endpoint const &local_endpoint, beast::IP::Endpoint const &remote_endpoint)
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)
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.
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.
void on_failure(std::shared_ptr< Slot > const &slot) override
Called when an outbound connection is deemed to have failed.
std::shared_ptr< Slot > 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.
Checker< boost::asio::ip::tcp > checker_
ManagerImp(boost::asio::io_service &io_service, clock_type &clock, beast::Journal journal, BasicConfig const &config, beast::insight::Collector::ptr const &collector)
bool onConnected(std::shared_ptr< Slot > const &slot, beast::IP::Endpoint const &local_endpoint) override
Called when an outbound connection attempt succeeds.
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
std::optional< boost::asio::io_service::work > work_
void setConfig(Config const &config) override
Set the configuration for the manager.
boost::asio::io_service & io_service_
Config config() override
Returns the configuration for the manager.
std::shared_ptr< Slot > new_outbound_slot(beast::IP::Endpoint const &remote_endpoint) override
Create a new outbound slot with the specified remote endpoint.
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:62
T end(T... args)
std::unique_ptr< Manager > make_Manager(boost::asio::io_service &io_service, 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:26
STL namespace.
T reset(T... args)
PeerFinder configuration settings.
Stats(Handler const &handler, beast::insight::Collector::ptr const &collector)