rippled
Classes | Public Member Functions | Private Types | Private Attributes | List of all members
ripple::test::csf::BasicNetwork< Peer > Class Template Reference

Peer to peer network simulator. More...

Collaboration diagram for ripple::test::csf::BasicNetwork< Peer >:
Collaboration graph
[legend]

Classes

struct  link_type
 

Public Member Functions

 BasicNetwork (BasicNetwork const &)=delete
 
BasicNetworkoperator= (BasicNetwork const &)=delete
 
 BasicNetwork (Scheduler &s)
 
bool connect (Peer const &from, Peer const &to, duration const &delay=std::chrono::seconds{0})
 Connect two peers. More...
 
bool disconnect (Peer const &peer1, Peer const &peer2)
 Break a link. More...
 
template<class Function >
void send (Peer const &from, Peer const &to, Function &&f)
 Send a message to a peer. More...
 
auto links (Peer const &from)
 Return the range of active links. More...
 
Digraph< Peer, link_type > const & graph () const
 Return the underlying digraph. More...
 

Private Types

using peer_type = Peer
 
using clock_type = Scheduler::clock_type
 
using duration = typename clock_type::duration
 
using time_point = typename clock_type::time_point
 

Private Attributes

Schedulerscheduler
 
Digraph< Peer, link_typelinks_
 

Detailed Description

template<class Peer>
class ripple::test::csf::BasicNetwork< Peer >

Peer to peer network simulator.

The network is formed from a set of Peer objects representing vertices and configurable connections representing edges. The caller is responsible for creating the Peer objects ahead of time.

Peer objects cannot be destroyed once the BasicNetwork is constructed. To handle peers going online and offline, callers can simply disconnect all links and reconnect them later. Connections are directed, one end is the inbound Peer and the other is the outbound Peer.

Peers may send messages along their connections. To simulate the effects of latency, these messages can be delayed by a configurable duration set when the link is established. Messages always arrive in the order they were sent on a particular connection.

A message is modeled using a lambda function. The caller provides the code to execute upon delivery of the message. If a Peer is disconnected, all messages pending delivery at either end of the connection will not be delivered.

When creating the Peer set, the caller needs to provide a Scheduler object for managing the the timing and delivery of messages. After constructing the network, and establishing connections, the caller uses the scheduler's step_* functions to drive messages through the network.

The graph of peers and connections is internally represented using Digraph<Peer,BasicNetwork::link_type>. Clients have const access to that graph to perform additional operations not directly provided by BasicNetwork.

Peer Requirements:

Peer should be a lightweight type, cheap to copy
and/or move. A good candidate is a simple pointer to
the underlying user defined type in the simulation.

Expression      Type        Requirements
----------      ----        ------------
P               Peer
u, v                        Values of type P
P u(v)                      CopyConstructible
u.~P()                      Destructible
u == v          bool        EqualityComparable
u < v           bool        LessThanComparable
std::hash<P>    class       std::hash is defined for P
! u             bool        true if u is not-a-peer

Definition at line 83 of file BasicNetwork.h.

Member Typedef Documentation

◆ peer_type

template<class Peer >
using ripple::test::csf::BasicNetwork< Peer >::peer_type = Peer
private

Definition at line 85 of file BasicNetwork.h.

◆ clock_type

template<class Peer >
using ripple::test::csf::BasicNetwork< Peer >::clock_type = Scheduler::clock_type
private

Definition at line 87 of file BasicNetwork.h.

◆ duration

template<class Peer >
using ripple::test::csf::BasicNetwork< Peer >::duration = typename clock_type::duration
private

Definition at line 89 of file BasicNetwork.h.

◆ time_point

template<class Peer >
using ripple::test::csf::BasicNetwork< Peer >::time_point = typename clock_type::time_point
private

Definition at line 91 of file BasicNetwork.h.

Constructor & Destructor Documentation

◆ BasicNetwork() [1/2]

template<class Peer >
ripple::test::csf::BasicNetwork< Peer >::BasicNetwork ( BasicNetwork< Peer > const &  )
delete

◆ BasicNetwork() [2/2]

template<class Peer >
ripple::test::csf::BasicNetwork< Peer >::BasicNetwork ( Scheduler s)

Definition at line 200 of file BasicNetwork.h.

Member Function Documentation

◆ operator=()

template<class Peer >
BasicNetwork& ripple::test::csf::BasicNetwork< Peer >::operator= ( BasicNetwork< Peer > const &  )
delete

◆ connect()

template<class Peer >
bool ripple::test::csf::BasicNetwork< Peer >::connect ( Peer const &  from,
Peer const &  to,
duration const &  delay = std::chrono::seconds{0} 
)

Connect two peers.

The link is directed, with from establishing the outbound connection and to receiving the incoming connection.

Preconditions:

from != to (self connect disallowed).

A link between from and to does not
already exist (duplicates disallowed).

Effects:

Creates a link between from and to.
Parameters
<tt>from</tt>The source of the outgoing connection
<tt>to</tt>The recipient of the incoming connection
<tt>delay</tt>The time delay of all delivered messages
Returns
true if a new connection was established

Definition at line 206 of file BasicNetwork.h.

◆ disconnect()

template<class Peer >
bool ripple::test::csf::BasicNetwork< Peer >::disconnect ( Peer const &  peer1,
Peer const &  peer2 
)

Break a link.

Effects:

If a connection is present, both ends are
disconnected.

Any pending messages on the connection
are discarded.
Returns
true if a connection was broken.

Definition at line 224 of file BasicNetwork.h.

◆ send()

template<class Peer >
template<class Function >
void ripple::test::csf::BasicNetwork< Peer >::send ( Peer const &  from,
Peer const &  to,
Function &&  f 
)

Send a message to a peer.

Preconditions:

A link exists between from and to.

Effects:

If the link is not broken when the
link's `delay` time has elapsed,
the function will be invoked with
no arguments.
Note
Its the caller's responsibility to ensure that the body of the function performs activity consistent with from's receipt of a message from to.

Definition at line 237 of file BasicNetwork.h.

◆ links()

template<class Peer >
auto ripple::test::csf::BasicNetwork< Peer >::links ( Peer const &  from)

Return the range of active links.

Returns
A random access range over Digraph::Edge instances

Definition at line 185 of file BasicNetwork.h.

◆ graph()

template<class Peer >
Digraph<Peer, link_type> const& ripple::test::csf::BasicNetwork< Peer >::graph ( ) const

Return the underlying digraph.

Definition at line 193 of file BasicNetwork.h.

Member Data Documentation

◆ scheduler

template<class Peer >
Scheduler& ripple::test::csf::BasicNetwork< Peer >::scheduler
private

Definition at line 105 of file BasicNetwork.h.

◆ links_

template<class Peer >
Digraph<Peer, link_type> ripple::test::csf::BasicNetwork< Peer >::links_
private

Definition at line 106 of file BasicNetwork.h.