rippled
PeerfinderManager.h
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 #ifndef RIPPLE_PEERFINDER_MANAGER_H_INCLUDED
21 #define RIPPLE_PEERFINDER_MANAGER_H_INCLUDED
22 
23 #include <ripple/beast/clock/abstract_clock.h>
24 #include <ripple/beast/utility/PropertyStream.h>
25 #include <ripple/core/Stoppable.h>
26 #include <ripple/peerfinder/Slot.h>
27 #include <boost/asio/ip/tcp.hpp>
28 
29 namespace ripple {
30 namespace PeerFinder {
31 
33 
36 
37 //------------------------------------------------------------------------------
38 
40 struct Config
41 {
46  int maxPeers;
47 
58  double outPeers;
59 
61  bool peerPrivate = true;
62 
65 
68 
71 
74 
76  int ipLimit;
77 
78  //--------------------------------------------------------------------------
79 
81  Config();
82 
84  double
85  calcOutPeers() const;
86 
88  void
89  applyTuning();
90 
92  void
94 };
95 
96 //------------------------------------------------------------------------------
97 
99 struct Endpoint
100 {
101  Endpoint();
102 
103  Endpoint(beast::IP::Endpoint const& ep, int hops_);
104 
105  int hops;
107 };
108 
109 bool
110 operator<(Endpoint const& lhs, Endpoint const& rhs);
111 
114 
115 //------------------------------------------------------------------------------
116 
118 enum class Result { duplicate, full, success };
119 
122 {
123 protected:
124  explicit Manager(Stoppable& parent);
125 
126 public:
132  virtual ~Manager() = default;
133 
139  virtual void
140  setConfig(Config const& config) = 0;
141 
143  virtual Config
144  config() = 0;
145 
151  virtual void
152  addFixedPeer(
153  std::string const& name,
154  std::vector<beast::IP::Endpoint> const& addresses) = 0;
155 
159  virtual void
161  std::string const& name,
162  std::vector<std::string> const& strings) = 0;
163 
167  /* VFALCO NOTE Unimplemented
168  virtual void addFallbackURL (std::string const& name,
169  std::string const& url) = 0;
170  */
171 
172  //--------------------------------------------------------------------------
173 
178  virtual std::shared_ptr<Slot>
180  beast::IP::Endpoint const& local_endpoint,
181  beast::IP::Endpoint const& remote_endpoint) = 0;
182 
187  virtual std::shared_ptr<Slot>
188  new_outbound_slot(beast::IP::Endpoint const& remote_endpoint) = 0;
189 
191  virtual void
192  on_endpoints(
193  std::shared_ptr<Slot> const& slot,
194  Endpoints const& endpoints) = 0;
195 
200  virtual void
201  on_closed(std::shared_ptr<Slot> const& slot) = 0;
202 
204  virtual void
205  on_failure(std::shared_ptr<Slot> const& slot) = 0;
206 
208  virtual void
209  onRedirects(
210  boost::asio::ip::tcp::endpoint const& remote_address,
212 
213  //--------------------------------------------------------------------------
214 
222  virtual bool
223  onConnected(
224  std::shared_ptr<Slot> const& slot,
225  beast::IP::Endpoint const& local_endpoint) = 0;
226 
228  virtual Result
229  activate(
230  std::shared_ptr<Slot> const& slot,
231  PublicKey const& key,
232  bool reserved) = 0;
233 
235  virtual std::vector<Endpoint>
236  redirect(std::shared_ptr<Slot> const& slot) = 0;
237 
240  autoconnect() = 0;
241 
244 
248  virtual void
249  once_per_second() = 0;
250 };
251 
252 } // namespace PeerFinder
253 } // namespace ripple
254 
255 #endif
beast::PropertyStream::Source::name
std::string const & name() const
Returns the name of this source.
Definition: beast_PropertyStream.cpp:190
ripple::PeerFinder::Result::duplicate
@ duplicate
std::string
STL class.
std::shared_ptr
STL class.
beast::PropertyStream::Map
Definition: PropertyStream.h:236
ripple::PeerFinder::Manager::once_per_second
virtual void once_per_second()=0
Perform periodic activity.
ripple::PeerFinder::Manager::addFallbackStrings
virtual void addFallbackStrings(std::string const &name, std::vector< std::string > const &strings)=0
Add a set of strings as fallback IP::Endpoint sources.
ripple::PeerFinder::Manager::buildEndpointsForPeers
virtual std::vector< std::pair< std::shared_ptr< Slot >, std::vector< Endpoint > > > buildEndpointsForPeers()=0
beast::PropertyStream::Source
Subclasses can be called to write to a stream and have children.
Definition: PropertyStream.h:342
std::vector< beast::IP::Endpoint >
ripple::PeerFinder::Config::features
std::string features
The set of features we advertise.
Definition: PeerfinderManager.h:73
ripple::PeerFinder::operator<
bool operator<(Endpoint const &lhs, Endpoint const &rhs)
Definition: Endpoint.cpp:35
ripple::PeerFinder::Manager::new_outbound_slot
virtual std::shared_ptr< Slot > new_outbound_slot(beast::IP::Endpoint const &remote_endpoint)=0
Create a new outbound slot with the specified remote endpoint.
ripple::PeerFinder::Manager::new_inbound_slot
virtual std::shared_ptr< Slot > 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.
ripple::PeerFinder::Config::peerPrivate
bool peerPrivate
true if we want our IP address kept private.
Definition: PeerfinderManager.h:61
ripple::PeerFinder::Manager::autoconnect
virtual std::vector< beast::IP::Endpoint > autoconnect()=0
Return a set of addresses we should connect to.
ripple::PeerFinder::Manager::on_endpoints
virtual void on_endpoints(std::shared_ptr< Slot > const &slot, Endpoints const &endpoints)=0
Called when mtENDPOINTS is received.
ripple::PeerFinder::Manager::activate
virtual Result activate(std::shared_ptr< Slot > const &slot, PublicKey const &key, bool reserved)=0
Request an active slot type.
ripple::PeerFinder::Manager::redirect
virtual std::vector< Endpoint > redirect(std::shared_ptr< Slot > const &slot)=0
Returns a set of endpoints suitable for redirection.
ripple::PeerFinder::Manager::on_closed
virtual void on_closed(std::shared_ptr< Slot > const &slot)=0
Called when the slot is closed.
ripple::PeerFinder::Result::success
@ success
ripple::Stoppable
Provides an interface for starting and stopping.
Definition: Stoppable.h:200
ripple::PeerFinder::Config::wantIncoming
bool wantIncoming
true if we want to accept incoming connections.
Definition: PeerfinderManager.h:64
ripple::PeerFinder::Manager::Manager
Manager(Stoppable &parent)
Definition: PeerfinderManager.cpp:288
ripple::PublicKey
A public key.
Definition: PublicKey.h:59
ripple::PeerFinder::Manager::setConfig
virtual void setConfig(Config const &config)=0
Set the configuration for the manager.
ripple::PeerFinder::Config::outPeers
double outPeers
The number of automatic outbound connections to maintain.
Definition: PeerfinderManager.h:58
ripple::PeerFinder::Config::maxPeers
int maxPeers
The largest number of public peer slots to allow.
Definition: PeerfinderManager.h:46
std::uint16_t
beast::abstract_clock
Abstract interface to a clock.
Definition: abstract_clock.h:57
ripple::PeerFinder::Endpoint::Endpoint
Endpoint()
Definition: Endpoint.cpp:25
ripple::PeerFinder::Config::applyTuning
void applyTuning()
Adjusts the values so they follow the business rules.
Definition: PeerfinderConfig.cpp:44
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::PeerFinder::Manager::onConnected
virtual bool onConnected(std::shared_ptr< Slot > const &slot, beast::IP::Endpoint const &local_endpoint)=0
Called when an outbound connection attempt succeeds.
ripple::PeerFinder::Manager::~Manager
virtual ~Manager()=default
Destroy the object.
ripple::PeerFinder::Manager::onRedirects
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.
ripple::PeerFinder::Manager::addFixedPeer
virtual void addFixedPeer(std::string const &name, std::vector< beast::IP::Endpoint > const &addresses)=0
Add a peer that should always be connected.
ripple::PeerFinder::Config::listeningPort
std::uint16_t listeningPort
The listening port number.
Definition: PeerfinderManager.h:70
ripple::PeerFinder::Manager::config
virtual Config config()=0
Returns the configuration for the manager.
ripple::PeerFinder::Config::calcOutPeers
double calcOutPeers() const
Returns a suitable value for outPeers according to the rules.
Definition: PeerfinderConfig.cpp:37
beast::IP::Endpoint
A version-independent IP address and port combination.
Definition: IPEndpoint.h:39
ripple::PeerFinder::Manager::on_failure
virtual void on_failure(std::shared_ptr< Slot > const &slot)=0
Called when an outbound connection is deemed to have failed.
ripple::PeerFinder::Config::autoConnect
bool autoConnect
true if we want to establish connections automatically
Definition: PeerfinderManager.h:67
ripple::PeerFinder::Config
PeerFinder configuration settings.
Definition: PeerfinderManager.h:40
ripple::PeerFinder::Endpoint::hops
int hops
Definition: PeerfinderManager.h:105
ripple::PeerFinder::Endpoint
Describes a connectible peer address along with some metadata.
Definition: PeerfinderManager.h:99
ripple::PeerFinder::Config::Config
Config()
Create a configuration with default values.
Definition: PeerfinderConfig.cpp:26
ripple::PeerFinder::Result::full
@ full
ripple::PeerFinder::Config::onWrite
void onWrite(beast::PropertyStream::Map &map)
Write the configuration into a property stream.
Definition: PeerfinderConfig.cpp:70
ripple::PeerFinder::Endpoint::address
beast::IP::Endpoint address
Definition: PeerfinderManager.h:106
ripple::PeerFinder::Config::ipLimit
int ipLimit
Limit how many incoming connections we allow per IP.
Definition: PeerfinderManager.h:76
ripple::PeerFinder::Manager
Maintains a set of IP addresses used for getting into the network.
Definition: PeerfinderManager.h:121
ripple::PeerFinder::Result
Result
Possible results from activating a slot.
Definition: PeerfinderManager.h:118