rippled
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 
26 namespace ripple {
27 namespace test {
28 namespace csf {
82 template <class Peer>
84 {
85  using peer_type = Peer;
86 
88 
89  using duration = typename clock_type::duration;
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 
108 public:
109  BasicNetwork(BasicNetwork const&) = delete;
110  BasicNetwork&
111  operator=(BasicNetwork const&) = delete;
112 
114 
137  bool
138  connect(
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 //------------------------------------------------------------------------------
199 template <class Peer>
201 {
202 }
203 
204 template <class Peer>
205 inline 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 
222 template <class Peer>
223 inline bool
224 BasicNetwork<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 
234 template <class Peer>
235 template <class Function>
236 inline void
237 BasicNetwork<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
ripple::test::csf::BasicNetwork::disconnect
bool disconnect(Peer const &peer1, Peer const &peer2)
Break a link.
Definition: BasicNetwork.h:224
ripple::test::csf::BasicNetwork::graph
Digraph< Peer, link_type > const & graph() const
Return the underlying digraph.
Definition: BasicNetwork.h:193
ripple::test::csf::Scheduler
Simulated discrete-event scheduler.
Definition: test/csf/Scheduler.h:47
ripple::test::csf::BasicNetwork::scheduler
Scheduler & scheduler
Definition: BasicNetwork.h:105
std::chrono::seconds
ripple::test::csf::Digraph
Directed graph.
Definition: Digraph.h:55
ripple::test::csf::BasicNetwork< ripple::test::csf::Peer * >::duration
typename clock_type::duration duration
Definition: BasicNetwork.h:89
ripple::test::csf::Scheduler::in
cancel_token in(duration const &delay, Function &&f)
Schedule an event after a specified duration passes.
ripple::test::csf::Digraph::disconnect
bool disconnect(Vertex source, Vertex target)
Disconnect two vertices.
Definition: Digraph.h:101
ripple::test::csf::BasicNetwork< ripple::test::csf::Peer * >::time_point
typename clock_type::time_point time_point
Definition: BasicNetwork.h:91
ripple::test::csf::BasicNetwork::connect
bool connect(Peer const &from, Peer const &to, duration const &delay=std::chrono::seconds{0})
Connect two peers.
Definition: BasicNetwork.h:206
ripple::test::csf::BasicNetwork::BasicNetwork
BasicNetwork(BasicNetwork const &)=delete
ripple::test::csf::BasicNetwork::links_
Digraph< Peer, link_type > links_
Definition: BasicNetwork.h:106
ripple::test::csf::Peer
A single peer in the simulation.
Definition: test/csf/Peer.h:54
ripple::test::csf::Digraph::edge
std::optional< EdgeData > edge(Vertex source, Vertex target) const
Return edge data between two vertices.
Definition: Digraph.h:119
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::test::csf::Scheduler::clock_type
beast::manual_clock< std::chrono::steady_clock > clock_type
Definition: test/csf/Scheduler.h:50
ripple::test::csf::BasicNetwork::send
void send(Peer const &from, Peer const &to, Function &&f)
Send a message to a peer.
Definition: BasicNetwork.h:237
ripple::test::csf::Digraph::connect
bool connect(Vertex source, Vertex target, EdgeData e)
Connect two vertices.
Definition: Digraph.h:74
beast::manual_clock< std::chrono::steady_clock >
ripple::test::csf::BasicNetwork::links
auto links(Peer const &from)
Return the range of active links.
Definition: BasicNetwork.h:185
ripple::test::csf::Scheduler::now
time_point now() const
Return the current network time.
Definition: test/csf/Scheduler.h:365
ripple::test::csf::BasicNetwork::operator=
BasicNetwork & operator=(BasicNetwork const &)=delete
beast::abstract_clock< std::chrono::steady_clock >::time_point
typename std::chrono::steady_clock ::time_point time_point
Definition: abstract_clock.h:63
ripple::test::csf::BasicNetwork
Peer to peer network simulator.
Definition: BasicNetwork.h:83
beast::abstract_clock< std::chrono::steady_clock >::duration
typename std::chrono::steady_clock ::duration duration
Definition: abstract_clock.h:62