rippled
Loading...
Searching...
No Matches
PeerfinderManager.h
1#ifndef XRPL_PEERFINDER_MANAGER_H_INCLUDED
2#define XRPL_PEERFINDER_MANAGER_H_INCLUDED
3
4#include <xrpld/core/Config.h>
5#include <xrpld/peerfinder/Slot.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 ripple {
15namespace PeerFinder {
16
18
21
22//------------------------------------------------------------------------------
23
25struct Config
26{
32
38
44
46 bool peerPrivate = true;
47
50
53
56
59
62
63 //--------------------------------------------------------------------------
64
66 Config();
67
70 calcOutPeers() const;
71
73 void
75
77 void
79
87 static Config
89 ripple::Config const& config,
90 std::uint16_t port,
91 bool validationPublicKey,
92 int ipLimit);
93
94 friend bool
95 operator==(Config const& lhs, Config const& rhs);
96};
97
98//------------------------------------------------------------------------------
99
102{
103 Endpoint() = default;
104
106
109};
110
111inline bool
112operator<(Endpoint const& lhs, Endpoint const& rhs)
113{
114 return lhs.address < rhs.address;
115}
116
119
120//------------------------------------------------------------------------------
121
123enum class Result {
127 full,
128 success
129};
130
145inline std::string_view
146to_string(Result result) noexcept
147{
148 switch (result)
149 {
150 case Result::inboundDisabled:
151 return "inbound disabled";
152 case Result::duplicatePeer:
153 return "peer already connected";
154 case Result::ipLimitExceeded:
155 return "ip limit exceeded";
156 case Result::full:
157 return "slots full";
158 case Result::success:
159 return "success";
160 }
161
162 return "unknown";
163}
164
167{
168protected:
169 Manager() noexcept;
170
171public:
177 virtual ~Manager() = default;
178
184 virtual void
186
188 virtual void
189 start() = 0;
190
192 virtual void
193 stop() = 0;
194
196 virtual Config
197 config() = 0;
198
204 virtual void
206 std::string const& name,
207 std::vector<beast::IP::Endpoint> const& addresses) = 0;
208
212 virtual void
214 std::string const& name,
215 std::vector<std::string> const& strings) = 0;
216
220 /* VFALCO NOTE Unimplemented
221 virtual void addFallbackURL (std::string const& name,
222 std::string const& url) = 0;
223 */
224
225 //--------------------------------------------------------------------------
226
233 beast::IP::Endpoint const& local_endpoint,
234 beast::IP::Endpoint const& remote_endpoint) = 0;
235
241 new_outbound_slot(beast::IP::Endpoint const& remote_endpoint) = 0;
242
244 virtual void
246 std::shared_ptr<Slot> const& slot,
247 Endpoints const& endpoints) = 0;
248
253 virtual void
255
257 virtual void
259
261 virtual void
263 boost::asio::ip::tcp::endpoint const& remote_address,
265
266 //--------------------------------------------------------------------------
267
275 virtual bool
277 std::shared_ptr<Slot> const& slot,
278 beast::IP::Endpoint const& local_endpoint) = 0;
279
281 virtual Result
283 std::shared_ptr<Slot> const& slot,
284 PublicKey const& key,
285 bool reserved) = 0;
286
290
294
297
301 virtual void
303};
304
305} // namespace PeerFinder
306} // namespace ripple
307
308#endif
A version-independent IP address and port combination.
Definition IPEndpoint.h:19
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 once_per_second()=0
Perform periodic activity.
virtual void on_endpoints(std::shared_ptr< Slot > const &slot, Endpoints const &endpoints)=0
Called when mtENDPOINTS is received.
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 start()=0
Transition to the started state, synchronously.
virtual void setConfig(Config const &config)=0
Set the configuration for the manager.
virtual void addFixedPeer(std::string const &name, std::vector< beast::IP::Endpoint > const &addresses)=0
Add a peer that should always be connected.
virtual std::vector< beast::IP::Endpoint > autoconnect()=0
Return a set of addresses we should connect to.
virtual ~Manager()=default
Destroy the object.
virtual bool onConnected(std::shared_ptr< Slot > const &slot, beast::IP::Endpoint const &local_endpoint)=0
Called when an outbound connection attempt succeeds.
virtual Config config()=0
Returns the configuration for the manager.
virtual void on_closed(std::shared_ptr< Slot > const &slot)=0
Called when the slot is closed.
virtual void stop()=0
Transition to the stopped state, synchronously.
virtual std::vector< std::pair< std::shared_ptr< Slot >, std::vector< Endpoint > > > buildEndpointsForPeers()=0
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.
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 void addFallbackStrings(std::string const &name, std::vector< std::string > const &strings)=0
Add a set of strings as fallback IP::Endpoint sources.
virtual Result activate(std::shared_ptr< Slot > const &slot, PublicKey const &key, bool reserved)=0
Request an active slot type.
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.
A public key.
Definition PublicKey.h:43
bool operator<(Endpoint const &lhs, Endpoint const &rhs)
std::string_view to_string(Result result) noexcept
Converts a Result enum value to its string representation.
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
PeerFinder configuration settings.
void onWrite(beast::PropertyStream::Map &map)
Write the configuration into a property stream.
Config()
Create a configuration with default values.
bool autoConnect
true if we want to establish connections automatically
int ipLimit
Limit how many incoming connections we allow per IP.
std::size_t inPeers
The number of automatic inbound connections to maintain.
void applyTuning()
Adjusts the values so they follow the business rules.
static Config makeConfig(ripple::Config const &config, std::uint16_t port, bool validationPublicKey, int ipLimit)
Make PeerFinder::Config from configuration parameters.
std::size_t maxPeers
The largest number of public peer slots to allow.
std::string features
The set of features we advertise.
bool wantIncoming
true if we want to accept incoming connections.
std::size_t outPeers
The number of automatic outbound connections to maintain.
std::size_t calcOutPeers() const
Returns a suitable value for outPeers according to the rules.
std::uint16_t listeningPort
The listening port number.
bool peerPrivate
true if we want our IP address kept private.
friend bool operator==(Config const &lhs, Config const &rhs)
Describes a connectible peer address along with some metadata.