rippled
Counts.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_COUNTS_H_INCLUDED
21 #define RIPPLE_PEERFINDER_COUNTS_H_INCLUDED
22 
23 #include <ripple/basics/random.h>
24 #include <ripple/peerfinder/PeerfinderManager.h>
25 #include <ripple/peerfinder/Slot.h>
26 #include <ripple/peerfinder/impl/Tuning.h>
27 
28 #include <cmath>
29 
30 namespace ripple {
31 namespace PeerFinder {
32 
34 class Counts
35 {
36 public:
38  : m_attempts(0)
39  , m_active(0)
40  , m_in_max(0)
41  , m_in_active(0)
42  , m_out_max(0)
43  , m_out_active(0)
44  , m_fixed(0)
45  , m_fixed_active(0)
46  , m_reserved(0)
47 
48  , m_acceptCount(0)
49  , m_closingCount(0)
50  {
51  }
52 
53  //--------------------------------------------------------------------------
54 
56  void
57  add(Slot const& s)
58  {
59  adjust(s, 1);
60  }
61 
63  void
64  remove(Slot const& s)
65  {
66  adjust(s, -1);
67  }
68 
70  bool
71  can_activate(Slot const& s) const
72  {
73  // Must be handshaked and in the right state
74  assert(s.state() == Slot::connected || s.state() == Slot::accept);
75 
76  if (s.fixed() || s.reserved())
77  return true;
78 
79  if (s.inbound())
80  return m_in_active < m_in_max;
81 
82  return m_out_active < m_out_max;
83  }
84 
88  {
90  return 0;
92  }
93 
96  attempts() const
97  {
98  return m_attempts;
99  }
100 
102  int
103  out_max() const
104  {
105  return m_out_max;
106  }
107 
111  int
112  out_active() const
113  {
114  return m_out_active;
115  }
116 
119  fixed() const
120  {
121  return m_fixed;
122  }
123 
126  fixed_active() const
127  {
128  return m_fixed_active;
129  }
130 
131  //--------------------------------------------------------------------------
132 
134  void
135  onConfig(Config const& config)
136  {
137  m_out_max = config.outPeers;
138  if (config.wantIncoming)
139  m_in_max = config.inPeers;
140  }
141 
143  int
144  acceptCount() const
145  {
146  return m_acceptCount;
147  }
148 
150  int
151  connectCount() const
152  {
153  return m_attempts;
154  }
155 
157  int
158  closingCount() const
159  {
160  return m_closingCount;
161  }
162 
164  int
165  inboundSlots() const
166  {
167  return m_in_max;
168  }
169 
171  int
173  {
174  return m_in_active;
175  }
176 
178  int
179  totalActive() const
180  {
181  return m_in_active + m_out_active;
182  }
183 
187  int
189  {
190  if (m_in_active < m_in_max)
191  return m_in_max - m_in_active;
192  return 0;
193  }
194 
198  int
200  {
201  if (m_out_active < m_out_max)
202  return m_out_max - m_out_active;
203  return 0;
204  }
205 
206  //--------------------------------------------------------------------------
207 
210  bool
212  {
213  // We will consider ourselves connected if we have reached
214  // the number of outgoing connections desired, or if connect
215  // automatically is false.
216  //
217  // Fixed peers do not count towards the active outgoing total.
218 
219  if (m_out_max > 0)
220  return false;
221 
222  return true;
223  }
224 
226  void
228  {
229  map["accept"] = acceptCount();
230  map["connect"] = connectCount();
231  map["close"] = closingCount();
232  map["in"] << m_in_active << "/" << m_in_max;
233  map["out"] << m_out_active << "/" << m_out_max;
234  map["fixed"] = m_fixed_active;
235  map["reserved"] = m_reserved;
236  map["total"] = m_active;
237  }
238 
241  state_string() const
242  {
244  ss << m_out_active << "/" << m_out_max << " out, " << m_in_active << "/"
245  << m_in_max << " in, " << connectCount() << " connecting, "
246  << closingCount() << " closing";
247  return ss.str();
248  }
249 
250  //--------------------------------------------------------------------------
251 private:
252  // Adjusts counts based on the specified slot, in the direction indicated.
253  void
254  adjust(Slot const& s, int const n)
255  {
256  if (s.fixed())
257  m_fixed += n;
258 
259  if (s.reserved())
260  m_reserved += n;
261 
262  switch (s.state())
263  {
264  case Slot::accept:
265  assert(s.inbound());
266  m_acceptCount += n;
267  break;
268 
269  case Slot::connect:
270  case Slot::connected:
271  assert(!s.inbound());
272  m_attempts += n;
273  break;
274 
275  case Slot::active:
276  if (s.fixed())
277  m_fixed_active += n;
278  if (!s.fixed() && !s.reserved())
279  {
280  if (s.inbound())
281  m_in_active += n;
282  else
283  m_out_active += n;
284  }
285  m_active += n;
286  break;
287 
288  case Slot::closing:
289  m_closingCount += n;
290  break;
291 
292  default:
293  assert(false);
294  break;
295  };
296  }
297 
298 private:
301 
304 
307 
310 
313 
316 
319 
322 
325 
326  // Number of inbound connections that are
327  // not active or gracefully closing.
329 
330  // Number of connections that are gracefully closing.
332 };
333 
334 } // namespace PeerFinder
335 } // namespace ripple
336 
337 #endif
ripple::PeerFinder::Counts::inboundActive
int inboundActive() const
Returns the number of inbound peers assigned an open slot.
Definition: Counts.h:172
ripple::PeerFinder::Counts::state_string
std::string state_string() const
Records the state for diagnostics.
Definition: Counts.h:241
ripple::PeerFinder::Slot::state
virtual State state() const =0
Returns the state of the connection.
ripple::PeerFinder::Counts::m_acceptCount
int m_acceptCount
Definition: Counts.h:328
std::string
STL class.
ripple::PeerFinder::Slot::accept
@ accept
Definition: peerfinder/Slot.h:37
beast::PropertyStream::Map
Definition: PropertyStream.h:224
ripple::PeerFinder::Counts::fixed
std::size_t fixed() const
Returns the number of fixed connections.
Definition: Counts.h:119
ripple::PeerFinder::Slot::fixed
virtual bool fixed() const =0
Returns true if this is a fixed connection.
ripple::PeerFinder::Counts::m_attempts
int m_attempts
Outbound connection attempts.
Definition: Counts.h:300
ripple::PeerFinder::Slot::active
@ active
Definition: peerfinder/Slot.h:37
ripple::PeerFinder::Slot::reserved
virtual bool reserved() const =0
Returns true if this is a reserved connection.
ripple::PeerFinder::Counts::isConnectedToNetwork
bool isConnectedToNetwork() const
Returns true if the slot logic considers us "connected" to the network.
Definition: Counts.h:211
std::stringstream
STL class.
ripple::PeerFinder::Counts::connectCount
int connectCount() const
Returns the number of connection attempts currently active.
Definition: Counts.h:151
ripple::PeerFinder::Tuning::maxConnectAttempts
@ maxConnectAttempts
Definition: peerfinder/impl/Tuning.h:44
cmath
ripple::PeerFinder::Counts::can_activate
bool can_activate(Slot const &s) const
Returns true if the slot can become active.
Definition: Counts.h:71
ripple::PeerFinder::Counts::m_active
std::size_t m_active
Active connections, including fixed and reserved.
Definition: Counts.h:303
ripple::PeerFinder::Counts::m_in_max
std::size_t m_in_max
Total number of inbound slots.
Definition: Counts.h:306
ripple::PeerFinder::Counts::inboundSlots
int inboundSlots() const
Returns the total number of inbound slots.
Definition: Counts.h:165
ripple::PeerFinder::Counts::acceptCount
int acceptCount() const
Returns the number of accepted connections that haven't handshaked.
Definition: Counts.h:144
ripple::PeerFinder::Counts::out_max
int out_max() const
Returns the total number of outbound slots.
Definition: Counts.h:103
ripple::PeerFinder::Counts
Manages the count of available connections for the various slots.
Definition: Counts.h:34
ripple::PeerFinder::Config::wantIncoming
bool wantIncoming
true if we want to accept incoming connections.
Definition: PeerfinderManager.h:64
ripple::PeerFinder::Counts::closingCount
int closingCount() const
Returns the number of connections that are gracefully closing.
Definition: Counts.h:158
ripple::PeerFinder::Counts::outboundSlotsFree
int outboundSlotsFree() const
Returns the number of unused outbound slots.
Definition: Counts.h:199
ripple::PeerFinder::Counts::out_active
int out_active() const
Returns the number of outbound peers assigned an open slot.
Definition: Counts.h:112
ripple::PeerFinder::Slot::connected
@ connected
Definition: peerfinder/Slot.h:37
ripple::PeerFinder::Config::outPeers
std::size_t outPeers
The number of automatic outbound connections to maintain.
Definition: PeerfinderManager.h:52
ripple::PeerFinder::Counts::onConfig
void onConfig(Config const &config)
Called when the config is set or changed.
Definition: Counts.h:135
ripple::PeerFinder::Counts::m_in_active
std::size_t m_in_active
Number of inbound slots assigned to active peers.
Definition: Counts.h:309
ripple::PeerFinder::Slot::closing
@ closing
Definition: peerfinder/Slot.h:37
ripple::PeerFinder::Counts::m_closingCount
int m_closingCount
Definition: Counts.h:331
ripple::PeerFinder::Counts::inboundSlotsFree
int inboundSlotsFree() const
Returns the number of unused inbound slots.
Definition: Counts.h:188
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::PeerFinder::Counts::m_out_max
std::size_t m_out_max
Maximum desired outbound slots.
Definition: Counts.h:312
ripple::PeerFinder::Counts::fixed_active
std::size_t fixed_active() const
Returns the number of active fixed connections.
Definition: Counts.h:126
ripple::PeerFinder::Counts::Counts
Counts()
Definition: Counts.h:37
ripple::PeerFinder::Counts::m_out_active
std::size_t m_out_active
Active outbound slots.
Definition: Counts.h:315
ripple::PeerFinder::Counts::attempts
std::size_t attempts() const
Returns the number of outbound connection attempts.
Definition: Counts.h:96
ripple::PeerFinder::Counts::adjust
void adjust(Slot const &s, int const n)
Definition: Counts.h:254
ripple::PeerFinder::Counts::add
void add(Slot const &s)
Adds the slot state and properties to the slot counts.
Definition: Counts.h:57
ripple::PeerFinder::Counts::onWrite
void onWrite(beast::PropertyStream::Map &map)
Output statistics.
Definition: Counts.h:227
std::stringstream::str
T str(T... args)
std::size_t
ripple::PeerFinder::Slot::inbound
virtual bool inbound() const =0
Returns true if this is an inbound connection.
ripple::PeerFinder::Config
PeerFinder configuration settings.
Definition: PeerfinderManager.h:40
ripple::PeerFinder::Counts::attempts_needed
std::size_t attempts_needed() const
Returns the number of attempts needed to bring us to the max.
Definition: Counts.h:87
ripple::PeerFinder::Slot::connect
@ connect
Definition: peerfinder/Slot.h:37
ripple::PeerFinder::Counts::remove
void remove(Slot const &s)
Removes the slot state and properties from the slot counts.
Definition: Counts.h:64
ripple::PeerFinder::Counts::m_fixed
std::size_t m_fixed
Fixed connections.
Definition: Counts.h:318
ripple::PeerFinder::Counts::m_reserved
std::size_t m_reserved
Reserved connections.
Definition: Counts.h:324
ripple::PeerFinder::Counts::totalActive
int totalActive() const
Returns the total number of active peers excluding fixed peers.
Definition: Counts.h:179
ripple::PeerFinder::Slot
Properties and state associated with a peer to peer overlay connection.
Definition: peerfinder/Slot.h:32
ripple::PeerFinder::Config::inPeers
std::size_t inPeers
The number of automatic inbound connections to maintain.
Definition: PeerfinderManager.h:58
ripple::PeerFinder::Counts::m_fixed_active
std::size_t m_fixed_active
Active fixed connections.
Definition: Counts.h:321