rippled
Loading...
Searching...
No Matches
BasicNetwork.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_TEST_CSF_BASICNETWORK_H_INCLUDED
21#define RIPPLE_TEST_CSF_BASICNETWORK_H_INCLUDED
22
23#include <test/csf/Digraph.h>
24#include <test/csf/Scheduler.h>
25
26namespace ripple {
27namespace test {
28namespace csf {
82template <class Peer>
84{
85 using peer_type = Peer;
86
88
90
92
93 struct link_type
94 {
95 bool inbound = false;
98 link_type() = default;
99 link_type(bool inbound_, duration delay_, time_point established_)
100 : inbound(inbound_), delay(delay_), established(established_)
101 {
102 }
103 };
104
107
108public:
109 BasicNetwork(BasicNetwork const&) = delete;
111 operator=(BasicNetwork const&) = delete;
112
114
137 bool
139 Peer const& from,
140 Peer const& to,
141 duration const& delay = std::chrono::seconds{0});
142
155 bool
156 disconnect(Peer const& peer1, Peer const& peer2);
157
176 template <class Function>
177 void
178 send(Peer const& from, Peer const& to, Function&& f);
179
184 auto
185 links(Peer const& from)
186 {
187 return links_.outEdges(from);
188 }
189
193 graph() const
194 {
195 return links_;
196 }
197};
198//------------------------------------------------------------------------------
199template <class Peer>
201{
202}
203
204template <class Peer>
205inline bool
207 Peer const& from,
208 Peer const& to,
209 duration const& delay)
210{
211 if (to == from)
212 return false;
213 time_point const now = scheduler.now();
214 if (!links_.connect(from, to, link_type{false, delay, now}))
215 return false;
216 auto const result = links_.connect(to, from, link_type{true, delay, now});
217 (void)result;
218 assert(result);
219 return true;
220}
221
222template <class Peer>
223inline bool
224BasicNetwork<Peer>::disconnect(Peer const& peer1, Peer const& peer2)
225{
226 if (!links_.disconnect(peer1, peer2))
227 return false;
228 bool r = links_.disconnect(peer2, peer1);
229 (void)r;
230 assert(r);
231 return true;
232}
233
234template <class Peer>
235template <class Function>
236inline void
237BasicNetwork<Peer>::send(Peer const& from, Peer const& to, Function&& f)
238{
239 auto link = links_.edge(from, to);
240 if (!link)
241 return;
242 time_point const sent = scheduler.now();
243 scheduler.in(
244 link->delay, [from, to, sent, f = std::forward<Function>(f), this] {
245 // only process if still connected and connection was
246 // not broken since the message was sent
247 if (auto l = links_.edge(from, to); l && l->established <= sent)
248 {
249 f();
250 }
251 });
252}
253
254} // namespace csf
255} // namespace test
256} // namespace ripple
257
258#endif
typename Clock::time_point time_point
typename Clock::duration duration
Peer to peer network simulator.
Definition: BasicNetwork.h:84
typename clock_type::duration duration
Definition: BasicNetwork.h:89
void send(Peer const &from, Peer const &to, Function &&f)
Send a message to a peer.
Definition: BasicNetwork.h:237
BasicNetwork & operator=(BasicNetwork const &)=delete
bool disconnect(Peer const &peer1, Peer const &peer2)
Break a link.
Definition: BasicNetwork.h:224
auto links(Peer const &from)
Return the range of active links.
Definition: BasicNetwork.h:185
Digraph< Peer, link_type > links_
Definition: BasicNetwork.h:106
BasicNetwork(BasicNetwork const &)=delete
Digraph< Peer, link_type > const & graph() const
Return the underlying digraph.
Definition: BasicNetwork.h:193
typename clock_type::time_point time_point
Definition: BasicNetwork.h:91
bool connect(Peer const &from, Peer const &to, duration const &delay=std::chrono::seconds{0})
Connect two peers.
Definition: BasicNetwork.h:206
Directed graph.
Definition: Digraph.h:56
std::optional< EdgeData > edge(Vertex source, Vertex target) const
Return edge data between two vertices.
Definition: Digraph.h:119
bool disconnect(Vertex source, Vertex target)
Disconnect two vertices.
Definition: Digraph.h:101
bool connect(Vertex source, Vertex target, EdgeData e)
Connect two vertices.
Definition: Digraph.h:74
Simulated discrete-event scheduler.
beast::manual_clock< std::chrono::steady_clock > clock_type
time_point now() const
Return the current network time.
cancel_token in(duration const &delay, Function &&f)
Schedule an event after a specified duration passes.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:25
A single peer in the simulation.
Definition: test/csf/Peer.h:60