xrpld
Loading...
Searching...
No Matches
PeerfinderManager.h
1#pragma once
2
3#include <xrpld/core/Config.h>
4#include <xrpld/peerfinder/Slot.h>
5#include <xrpld/peerfinder/detail/Tuning.h>
6
7#include <xrpl/beast/clock/abstract_clock.h>
8#include <xrpl/beast/utility/PropertyStream.h>
9
10#include <boost/asio/ip/tcp.hpp>
11
12#include <string_view>
13
14namespace xrpl::PeerFinder {
15
17
20
21//------------------------------------------------------------------------------
22
24struct Config
25{
31
37
43
45 bool peerPrivate = true;
46
48 bool wantIncoming{true};
49
51 bool autoConnect{true};
52
55
58
60 int ipLimit{0};
61
63 bool verifyEndpoints = true;
64
65 //--------------------------------------------------------------------------
66
68 Config();
69
71 [[nodiscard]] std::size_t
72 calcOutPeers() const;
73
75 void
77
79 void
81
91 static Config
93 xrpl::Config const& config,
94 std::uint16_t port,
95 bool validationPublicKey,
96 int ipLimit,
97 bool verifyEndpoints);
98
99 friend bool
100 operator==(Config const& lhs, Config const& rhs) = default;
101};
102
103//------------------------------------------------------------------------------
104
115
116inline bool
117operator<(Endpoint const& lhs, Endpoint const& rhs)
118{
119 return lhs.address < rhs.address;
120}
121
124
125//------------------------------------------------------------------------------
126
129
144inline std::string_view
145to_string(Result result) noexcept
146{
147 switch (result)
148 {
149 case Result::InboundDisabled:
150 return "inbound disabled";
151 case Result::DuplicatePeer:
152 return "peer already connected";
153 case Result::IpLimitExceeded:
154 return "ip limit exceeded";
155 case Result::Full:
156 return "slots full";
157 case Result::Success:
158 return "success";
159 }
160
161 return "unknown";
162}
163
166{
167protected:
168 Manager() noexcept;
169
170public:
176 ~Manager() override = default;
177
183 virtual void
185
187 virtual void
188 start() = 0;
189
191 virtual void
192 stop() = 0;
193
195 virtual Config
196 config() = 0;
197
203 virtual void
205
209 virtual void
211
215 /* VFALCO NOTE Unimplemented
216 virtual void addFallbackURL (std::string const& name,
217 std::string const& url) = 0;
218 */
219
220 //--------------------------------------------------------------------------
221
228 beast::IP::Endpoint const& localEndpoint,
229 beast::IP::Endpoint const& remoteEndpoint) = 0;
230
236 newOutboundSlot(beast::IP::Endpoint const& remoteEndpoint) = 0;
237
239 virtual void
240 onEndpoints(std::shared_ptr<Slot> const& slot, Endpoints const& endpoints) = 0;
241
246 virtual void
248
250 virtual void
252
254 virtual void
256 boost::asio::ip::tcp::endpoint const& remoteAddress,
258
259 //--------------------------------------------------------------------------
260
268 virtual bool
269 onConnected(std::shared_ptr<Slot> const& slot, beast::IP::Endpoint const& localEndpoint) = 0;
270
272 virtual Result
273 activate(std::shared_ptr<Slot> const& slot, PublicKey const& key, bool reserved) = 0;
274
278
282
285
289 virtual void
291};
292
293} // namespace xrpl::PeerFinder
Abstract interface to a clock.
A version-independent IP address and port combination.
Definition IPEndpoint.h:17
Subclasses can be called to write to a stream and have children.
std::string const & name() const
Returns the name of this source.
virtual void onEndpoints(std::shared_ptr< Slot > const &slot, Endpoints const &endpoints)=0
Called when mtENDPOINTS is received.
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.
~Manager() override=default
Destroy the object.
virtual void oncePerSecond()=0
Perform periodic activity.
virtual std::pair< std::shared_ptr< Slot >, Result > newInboundSlot(beast::IP::Endpoint const &localEndpoint, beast::IP::Endpoint const &remoteEndpoint)=0
Add a URL as a fallback location to obtain IP::Endpoint sources.
virtual std::vector< std::pair< std::shared_ptr< Slot >, std::vector< Endpoint > > > buildEndpointsForPeers()=0
virtual void onClosed(std::shared_ptr< Slot > const &slot)=0
Called when the slot is closed.
virtual void onFailure(std::shared_ptr< Slot > const &slot)=0
Called when an outbound connection is deemed to have failed.
virtual Result activate(std::shared_ptr< Slot > const &slot, PublicKey const &key, bool reserved)=0
Request an active slot type.
virtual bool onConnected(std::shared_ptr< Slot > const &slot, beast::IP::Endpoint const &localEndpoint)=0
Called when an outbound connection attempt succeeds.
virtual void stop()=0
Transition to the stopped state, synchronously.
virtual std::pair< std::shared_ptr< Slot >, Result > newOutboundSlot(beast::IP::Endpoint const &remoteEndpoint)=0
Create a new outbound slot with the specified remote endpoint.
virtual void onRedirects(boost::asio::ip::tcp::endpoint const &remoteAddress, std::vector< boost::asio::ip::tcp::endpoint > const &eps)=0
Called when we received redirect IPs from a busy peer.
virtual Config config()=0
Returns the configuration for the manager.
virtual void setConfig(Config const &config)=0
Set the configuration for the manager.
virtual void addFixedPeer(std::string_view name, std::vector< beast::IP::Endpoint > const &addresses)=0
Add a peer that should always be connected.
virtual std::vector< Endpoint > redirect(std::shared_ptr< Slot > const &slot)=0
Returns a set of endpoints suitable for redirection.
A public key.
Definition PublicKey.h:42
static constexpr auto kDefaultMaxPeers
The default value of Config::maxPeers.
std::vector< Endpoint > Endpoints
A set of Endpoint used for connecting.
std::string_view to_string(Result result) noexcept
Converts a Result enum value to its string representation.
std::vector< beast::IP::Endpoint > IPAddresses
Represents a set of addresses.
Result
Possible results from activating a slot.
bool operator<(Endpoint const &lhs, Endpoint const &rhs)
beast::AbstractClock< std::chrono::steady_clock > clock_type
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.
bool verifyEndpoints
true if we want to verify endpoints in TMEndpoints messages
Config()
Create a configuration with default values.
friend bool operator==(Config const &lhs, Config const &rhs)=default
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.
std::uint16_t listeningPort
The listening port number.
std::size_t calcOutPeers() const
Returns a suitable value for outPeers according to the rules.
static Config makeConfig(xrpl::Config const &config, std::uint16_t port, bool validationPublicKey, int ipLimit, bool verifyEndpoints)
Make PeerFinder::Config from configuration parameters.
bool peerPrivate
true if we want our IP address kept private.
Describes a connectable peer address along with some metadata.