rippled
SlotImp.cpp
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 #include <ripple/peerfinder/PeerfinderManager.h>
21 #include <ripple/peerfinder/impl/SlotImp.h>
22 #include <ripple/peerfinder/impl/Tuning.h>
23 
24 namespace ripple {
25 namespace PeerFinder {
26 
28  beast::IP::Endpoint const& local_endpoint,
29  beast::IP::Endpoint const& remote_endpoint,
30  bool fixed,
31  clock_type& clock)
32  : recent(clock)
33  , m_inbound(true)
34  , m_fixed(fixed)
35  , m_reserved(false)
36  , m_state(accept)
37  , m_remote_endpoint(remote_endpoint)
38  , m_local_endpoint(local_endpoint)
39  , m_listening_port(unknownPort)
40  , checked(false)
41  , canAccept(false)
42  , connectivityCheckInProgress(false)
43 {
44 }
45 
47  beast::IP::Endpoint const& remote_endpoint,
48  bool fixed,
49  clock_type& clock)
50  : recent(clock)
51  , m_inbound(false)
52  , m_fixed(fixed)
53  , m_reserved(false)
54  , m_state(connect)
55  , m_remote_endpoint(remote_endpoint)
56  , m_listening_port(unknownPort)
57  , checked(true)
58  , canAccept(true)
59  , connectivityCheckInProgress(false)
60 {
61 }
62 
63 void
65 {
66  // Must go through activate() to set active state
67  assert(state_ != active);
68 
69  // The state must be different
70  assert(state_ != m_state);
71 
72  // You can't transition into the initial states
73  assert(state_ != accept && state_ != connect);
74 
75  // Can only become connected from outbound connect state
76  assert(state_ != connected || (!m_inbound && m_state == connect));
77 
78  // Can't gracefully close on an outbound connection attempt
79  assert(state_ != closing || m_state != connect);
80 
81  m_state = state_;
82 }
83 
84 void
86 {
87  // Can only become active from the accept or connected state
88  assert(m_state == accept || m_state == connected);
89 
90  m_state = active;
91  whenAcceptEndpoints = now;
92 }
93 
94 //------------------------------------------------------------------------------
95 
96 Slot::~Slot() = default;
97 
98 //------------------------------------------------------------------------------
99 
101 {
102 }
103 
104 void
106 {
107  auto const result(cache.emplace(ep, hops));
108  if (!result.second)
109  {
110  // NOTE Other logic depends on this <= inequality.
111  if (hops <= result.first->second)
112  {
113  result.first->second = hops;
114  cache.touch(result.first);
115  }
116  }
117 }
118 
119 bool
121 {
122  auto const iter(cache.find(ep));
123  if (iter == cache.end())
124  return false;
125  // We avoid sending an endpoint if we heard it
126  // from them recently at the same or lower hop count.
127  // NOTE Other logic depends on this <= inequality.
128  return iter->second <= hops;
129 }
130 
131 void
133 {
135 }
136 
137 } // namespace PeerFinder
138 } // namespace ripple
ripple::PeerFinder::Slot::accept
@ accept
Definition: peerfinder/Slot.h:37
ripple::PeerFinder::SlotImp::recent_t::expire
void expire()
Definition: SlotImp.cpp:132
ripple::PeerFinder::Slot::active
@ active
Definition: peerfinder/Slot.h:37
ripple::PeerFinder::SlotImp::whenAcceptEndpoints
clock_type::time_point whenAcceptEndpoints
Definition: SlotImp.h:206
ripple::PeerFinder::SlotImp::recent_t::filter
bool filter(beast::IP::Endpoint const &ep, std::uint32_t hops)
Returns true if we should not send endpoint to the slot.
Definition: SlotImp.cpp:120
ripple::PeerFinder::Tuning::liveCacheSecondsToLive
constexpr std::chrono::seconds liveCacheSecondsToLive(30)
ripple::PeerFinder::SlotImp::m_state
State m_state
Definition: SlotImp.h:179
ripple::PeerFinder::SlotImp::recent_t::recent_t
recent_t(clock_type &clock)
Definition: SlotImp.cpp:100
ripple::PeerFinder::Slot::connected
@ connected
Definition: peerfinder/Slot.h:37
ripple::PeerFinder::SlotImp::SlotImp
SlotImp(beast::IP::Endpoint const &local_endpoint, beast::IP::Endpoint const &remote_endpoint, bool fixed, clock_type &clock)
Definition: SlotImp.cpp:27
ripple::PeerFinder::SlotImp::recent_t::insert
void insert(beast::IP::Endpoint const &ep, std::uint32_t hops)
Called for each valid endpoint received for a slot.
Definition: SlotImp.cpp:105
beast::expire
std::enable_if< is_aged_container< AgedContainer >::value, std::size_t >::type expire(AgedContainer &c, std::chrono::duration< Rep, Period > const &age)
Expire aged container items past the specified age.
Definition: aged_container_utility.h:33
std::uint32_t
ripple::PeerFinder::Slot::closing
@ closing
Definition: peerfinder/Slot.h:37
ripple::PeerFinder::Slot::State
State
Definition: peerfinder/Slot.h:37
beast::abstract_clock< std::chrono::steady_clock >
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::PeerFinder::SlotImp::m_inbound
const bool m_inbound
Definition: SlotImp.h:176
beast::IP::Endpoint
A version-independent IP address and port combination.
Definition: IPEndpoint.h:38
ripple::PeerFinder::SlotImp::state
State state() const override
Returns the state of the connection.
Definition: SlotImp.h:70
ripple::PeerFinder::Slot::~Slot
virtual ~Slot()=0
ripple::PeerFinder::Slot::connect
@ connect
Definition: peerfinder/Slot.h:37
ripple::PeerFinder::SlotImp::activate
void activate(clock_type::time_point const &now)
Definition: SlotImp.cpp:85
beast::abstract_clock::time_point
typename Clock::time_point time_point
Definition: abstract_clock.h:63