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/Scheduler.h>
24 #include <test/csf/Digraph.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 
180 
185  auto
186  links(Peer const& from)
187  {
188  return links_.outEdges(from);
189  }
190 
194  graph() const
195  {
196  return links_;
197  }
198 };
199 //------------------------------------------------------------------------------
200 template <class Peer>
202 {
203 }
204 
205 template <class Peer>
206 inline bool
208  Peer const& from,
209  Peer const& to,
210  duration const& delay)
211 {
212  if (to == from)
213  return false;
214  time_point const now = scheduler.now();
215  if(!links_.connect(from, to, link_type{false, delay, now}))
216  return false;
217  auto const result = links_.connect(to, from, link_type{true, delay, now});
218  (void)result;
219  assert(result);
220  return true;
221 }
222 
223 template <class Peer>
224 inline bool
225 BasicNetwork<Peer>::disconnect(Peer const& peer1, Peer const& peer2)
226 {
227  if (! links_.disconnect(peer1, peer2))
228  return false;
229  bool r = links_.disconnect(peer2, peer1);
230  (void)r;
231  assert(r);
232  return true;
233 }
234 
235 template <class Peer>
236 template <class Function>
237 inline void
238 BasicNetwork<Peer>::send(Peer const& from, Peer const& to, Function&& f)
239 {
240  auto link = links_.edge(from,to);
241  if(!link)
242  return;
243  time_point const sent = scheduler.now();
244  scheduler.in(
245  link->delay,
246  [ from, to, sent, f = std::forward<Function>(f), this ] {
247  // only process if still connected and connection was
248  // not broken since the message was sent
249  if (auto l = links_.edge(from, to);
250  l && l->established <= sent)
251  {
252  f();
253  }
254  });
255 }
256 
257 } // namespace csf
258 } // namespace test
259 } // namespace ripple
260 
261 #endif
ripple::test::csf::BasicNetwork::disconnect
bool disconnect(Peer const &peer1, Peer const &peer2)
Break a link.
Definition: BasicNetwork.h:225
ripple::test::csf::Scheduler
Simulated discrete-event scheduler.
Definition: test/csf/Scheduler.h:44
ripple::test::csf::BasicNetwork::scheduler
Scheduler & scheduler
Definition: BasicNetwork.h:105
std::chrono::seconds
ripple::test::csf::Digraph
Directed graph.
Definition: Digraph.h:54
ripple::test::csf::BasicNetwork< ripple::test::csf::Peer * >::duration
typename clock_type::duration duration
Definition: BasicNetwork.h:89
ripple::test::csf::BasicNetwork::graph
const Digraph< Peer, link_type > & graph() const
Return the underlying digraph.
Definition: BasicNetwork.h:194
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:100
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:207
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::Digraph::edge
boost::optional< EdgeData > edge(Vertex source, Vertex target) const
Return edge data between two vertices.
Definition: Digraph.h:118
ripple::test::csf::Peer
A single peer in the simulation.
Definition: test/csf/Peer.h:54
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:47
ripple::test::csf::BasicNetwork::send
void send(Peer const &from, Peer const &to, Function &&f)
Send a message to a peer.
Definition: BasicNetwork.h:238
ripple::test::csf::Digraph::connect
bool connect(Vertex source, Vertex target, EdgeData e)
Connect two vertices.
Definition: Digraph.h:73
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:186
ripple::test::csf::Scheduler::now
time_point now() const
Return the current network time.
Definition: test/csf/Scheduler.h:372
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