rippled
Loading...
Searching...
No Matches
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 <xrpld/peerfinder/PeerfinderManager.h>
21#include <xrpld/peerfinder/detail/SlotImp.h>
22#include <xrpld/peerfinder/detail/Tuning.h>
23
24namespace ripple {
25namespace 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
63void
65{
66 // Must go through activate() to set active state
67 XRPL_ASSERT(
68 state_ != active,
69 "ripple::PeerFinder::SlotImp::state : input state is not active");
70
71 // The state must be different
72 XRPL_ASSERT(
73 state_ != m_state,
74 "ripple::PeerFinder::SlotImp::state : input state is different from "
75 "current");
76
77 // You can't transition into the initial states
78 XRPL_ASSERT(
79 state_ != accept && state_ != connect,
80 "ripple::PeerFinder::SlotImp::state : input state is not an initial");
81
82 // Can only become connected from outbound connect state
83 XRPL_ASSERT(
84 state_ != connected || (!m_inbound && m_state == connect),
85 "ripple::PeerFinder::SlotImp::state : input state is not connected an "
86 "invalid state");
87
88 // Can't gracefully close on an outbound connection attempt
89 XRPL_ASSERT(
90 state_ != closing || m_state != connect,
91 "ripple::PeerFinder::SlotImp::state : input state is not closing an "
92 "invalid state");
93
94 m_state = state_;
95}
96
97void
99{
100 // Can only become active from the accept or connected state
101 XRPL_ASSERT(
103 "ripple::PeerFinder::SlotImp::activate : valid state");
104
105 m_state = active;
107}
108
109//------------------------------------------------------------------------------
110
111Slot::~Slot() = default;
112
113//------------------------------------------------------------------------------
114
116{
117}
118
119void
121{
122 auto const result(cache.emplace(ep, hops));
123 if (!result.second)
124 {
125 // NOTE Other logic depends on this <= inequality.
126 if (hops <= result.first->second)
127 {
128 result.first->second = hops;
129 cache.touch(result.first);
130 }
131 }
132}
133
134bool
136{
137 auto const iter(cache.find(ep));
138 if (iter == cache.end())
139 return false;
140 // We avoid sending an endpoint if we heard it
141 // from them recently at the same or lower hop count.
142 // NOTE Other logic depends on this <= inequality.
143 return iter->second <= hops;
144}
145
146void
151
152} // namespace PeerFinder
153} // namespace ripple
A version-independent IP address and port combination.
Definition IPEndpoint.h:38
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:135
void insert(beast::IP::Endpoint const &ep, std::uint32_t hops)
Called for each valid endpoint received for a slot.
Definition SlotImp.cpp:120
void activate(clock_type::time_point const &now)
Definition SlotImp.cpp:98
SlotImp(beast::IP::Endpoint const &local_endpoint, beast::IP::Endpoint const &remote_endpoint, bool fixed, clock_type &clock)
Definition SlotImp.cpp:27
State state() const override
Returns the state of the connection.
Definition SlotImp.h:71
clock_type::time_point whenAcceptEndpoints
Definition SlotImp.h:207
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.
std::chrono::seconds constexpr liveCacheSecondsToLive(30)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25