rippled
Loading...
Searching...
No Matches
PeerfinderManager.h
1#pragma once
2
3#include <xrpld/core/Config.h>
4#include <xrpld/peerfinder/Slot.h>
5
6#include <xrpl/beast/clock/abstract_clock.h>
7#include <xrpl/beast/utility/PropertyStream.h>
8
9#include <boost/asio/ip/tcp.hpp>
10
11#include "xrpld/peerfinder/detail/Tuning.h"
12
13#include <string_view>
14
15namespace xrpl {
16namespace PeerFinder {
17
19
22
23//------------------------------------------------------------------------------
24
26struct Config
27{
33
39
45
47 bool peerPrivate = true;
48
50 bool wantIncoming{true};
51
53 bool autoConnect{true};
54
57
60
62 int ipLimit{0};
63
64 //--------------------------------------------------------------------------
65
67 Config();
68
71 calcOutPeers() const;
72
74 void
76
78 void
80
88 static Config
90 xrpl::Config const& config,
91 std::uint16_t port,
92 bool validationPublicKey,
93 int ipLimit);
94
95 friend bool
96 operator==(Config const& lhs, Config const& rhs);
97};
98
99//------------------------------------------------------------------------------
100
103{
104 Endpoint() = default;
105
107
110};
111
112inline bool
113operator<(Endpoint const& lhs, Endpoint const& rhs)
114{
115 return lhs.address < rhs.address;
116}
117
120
121//------------------------------------------------------------------------------
122
125
140inline std::string_view
141to_string(Result result) noexcept
142{
143 switch (result)
144 {
145 case Result::inboundDisabled:
146 return "inbound disabled";
147 case Result::duplicatePeer:
148 return "peer already connected";
149 case Result::ipLimitExceeded:
150 return "ip limit exceeded";
151 case Result::full:
152 return "slots full";
153 case Result::success:
154 return "success";
155 }
156
157 return "unknown";
158}
159
162{
163protected:
164 Manager() noexcept;
165
166public:
172 virtual ~Manager() = default;
173
179 virtual void
181
183 virtual void
184 start() = 0;
185
187 virtual void
188 stop() = 0;
189
191 virtual Config
192 config() = 0;
193
199 virtual void
201
205 virtual void
207
211 /* VFALCO NOTE Unimplemented
212 virtual void addFallbackURL (std::string const& name,
213 std::string const& url) = 0;
214 */
215
216 //--------------------------------------------------------------------------
217
224 beast::IP::Endpoint const& local_endpoint,
225 beast::IP::Endpoint const& remote_endpoint) = 0;
226
232 new_outbound_slot(beast::IP::Endpoint const& remote_endpoint) = 0;
233
235 virtual void
236 on_endpoints(std::shared_ptr<Slot> const& slot, Endpoints const& endpoints) = 0;
237
242 virtual void
244
246 virtual void
248
250 virtual void
252 boost::asio::ip::tcp::endpoint const& remote_address,
254
255 //--------------------------------------------------------------------------
256
264 virtual bool
265 onConnected(std::shared_ptr<Slot> const& slot, beast::IP::Endpoint const& local_endpoint) = 0;
266
268 virtual Result
269 activate(std::shared_ptr<Slot> const& slot, PublicKey const& key, bool reserved) = 0;
270
274
278
281
285 virtual void
287};
288
289} // namespace PeerFinder
290} // namespace xrpl
A version-independent IP address and port combination.
Definition IPEndpoint.h:18
Subclasses can be called to write to a stream and have children.
std::string const & name() const
Returns the name of this source.
Maintains a set of IP addresses used for getting into the network.
virtual void start()=0
Transition to the started state, synchronously.
virtual std::vector< beast::IP::Endpoint > autoconnect()=0
Return a set of addresses we should connect to.
virtual void addFallbackStrings(std::string const &name, std::vector< std::string > const &strings)=0
Add a set of strings as fallback IP::Endpoint sources.
virtual std::vector< std::pair< std::shared_ptr< Slot >, std::vector< Endpoint > > > buildEndpointsForPeers()=0
virtual std::pair< std::shared_ptr< Slot >, Result > new_outbound_slot(beast::IP::Endpoint const &remote_endpoint)=0
Create a new outbound slot with the specified remote endpoint.
virtual void addFixedPeer(std::string const &name, std::vector< beast::IP::Endpoint > const &addresses)=0
Add a peer that should always be connected.
virtual void onRedirects(boost::asio::ip::tcp::endpoint const &remote_address, std::vector< boost::asio::ip::tcp::endpoint > const &eps)=0
Called when we received redirect IPs from a busy peer.
virtual Result activate(std::shared_ptr< Slot > const &slot, PublicKey const &key, bool reserved)=0
Request an active slot type.
virtual void on_closed(std::shared_ptr< Slot > const &slot)=0
Called when the slot is closed.
virtual ~Manager()=default
Destroy the object.
virtual void stop()=0
Transition to the stopped state, synchronously.
virtual std::pair< std::shared_ptr< Slot >, Result > new_inbound_slot(beast::IP::Endpoint const &local_endpoint, beast::IP::Endpoint const &remote_endpoint)=0
Add a URL as a fallback location to obtain IP::Endpoint sources.
virtual Config config()=0
Returns the configuration for the manager.
virtual void once_per_second()=0
Perform periodic activity.
virtual void setConfig(Config const &config)=0
Set the configuration for the manager.
virtual void on_endpoints(std::shared_ptr< Slot > const &slot, Endpoints const &endpoints)=0
Called when mtENDPOINTS is received.
virtual bool onConnected(std::shared_ptr< Slot > const &slot, beast::IP::Endpoint const &local_endpoint)=0
Called when an outbound connection attempt succeeds.
virtual std::vector< Endpoint > redirect(std::shared_ptr< Slot > const &slot)=0
Returns a set of endpoints suitable for redirection.
virtual void on_failure(std::shared_ptr< Slot > const &slot)=0
Called when an outbound connection is deemed to have failed.
A public key.
Definition PublicKey.h:42
std::string_view to_string(Result result) noexcept
Converts a Result enum value to its string representation.
Result
Possible results from activating a slot.
bool operator<(Endpoint const &lhs, Endpoint const &rhs)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
PeerFinder configuration settings.
std::size_t maxPeers
The largest number of public peer slots to allow.
int ipLimit
Limit how many incoming connections we allow per IP.
std::size_t outPeers
The number of automatic outbound connections to maintain.
Config()
Create a configuration with default values.
void applyTuning()
Adjusts the values so they follow the business rules.
void onWrite(beast::PropertyStream::Map &map) const
Write the configuration into a property stream.
bool wantIncoming
true if we want to accept incoming connections.
bool autoConnect
true if we want to establish connections automatically
std::string features
The set of features we advertise.
std::size_t inPeers
The number of automatic inbound connections to maintain.
static Config makeConfig(xrpl::Config const &config, std::uint16_t port, bool validationPublicKey, int ipLimit)
Make PeerFinder::Config from configuration parameters.
std::uint16_t listeningPort
The listening port number.
std::size_t calcOutPeers() const
Returns a suitable value for outPeers according to the rules.
friend bool operator==(Config const &lhs, Config const &rhs)
bool peerPrivate
true if we want our IP address kept private.
Describes a connectable peer address along with some metadata.