rippled
Loading...
Searching...
No Matches
BasicNetwork.h
1#pragma once
2
3#include <test/csf/Digraph.h>
4#include <test/csf/Scheduler.h>
5
6namespace xrpl {
7namespace test {
8namespace csf {
62template <class Peer>
64{
65 using peer_type = Peer;
66
68
70
72
73 struct link_type
74 {
75 bool inbound = false;
78 link_type() = default;
79 link_type(bool inbound_, duration delay_, time_point established_)
80 : inbound(inbound_), delay(delay_), established(established_)
81 {
82 }
83 };
84
87
88public:
89 BasicNetwork(BasicNetwork const&) = delete;
91 operator=(BasicNetwork const&) = delete;
92
94
117 bool
118 connect(Peer const& from, Peer const& to, duration const& delay = std::chrono::seconds{0});
119
132 bool
133 disconnect(Peer const& peer1, Peer const& peer2);
134
153 template <class Function>
154 void
155 send(Peer const& from, Peer const& to, Function&& f);
156
161 auto
162 links(Peer const& from)
163 {
164 return links_.outEdges(from);
165 }
166
170 graph() const
171 {
172 return links_;
173 }
174};
175//------------------------------------------------------------------------------
176template <class Peer>
178{
179}
180
181template <class Peer>
182inline bool
183BasicNetwork<Peer>::connect(Peer const& from, Peer const& to, duration const& delay)
184{
185 if (to == from)
186 return false;
187 time_point const now = scheduler.now();
188 if (!links_.connect(from, to, link_type{false, delay, now}))
189 return false;
190 auto const result = links_.connect(to, from, link_type{true, delay, now});
191 (void)result;
192 assert(result);
193 return true;
194}
195
196template <class Peer>
197inline bool
198BasicNetwork<Peer>::disconnect(Peer const& peer1, Peer const& peer2)
199{
200 if (!links_.disconnect(peer1, peer2))
201 return false;
202 bool r = links_.disconnect(peer2, peer1);
203 (void)r;
204 assert(r);
205 return true;
206}
207
208template <class Peer>
209template <class Function>
210inline void
211BasicNetwork<Peer>::send(Peer const& from, Peer const& to, Function&& f)
212{
213 auto link = links_.edge(from, to);
214 if (!link)
215 return;
216 time_point const sent = scheduler.now();
217 scheduler.in(link->delay, [from, to, sent, f = std::forward<Function>(f), this] {
218 // only process if still connected and connection was
219 // not broken since the message was sent
220 if (auto l = links_.edge(from, to); l && l->established <= sent)
221 {
222 f();
223 }
224 });
225}
226
227} // namespace csf
228} // namespace test
229} // namespace xrpl
typename Clock::time_point time_point
typename Clock::duration duration
Peer to peer network simulator.
BasicNetwork & operator=(BasicNetwork const &)=delete
bool connect(Peer const &from, Peer const &to, duration const &delay=std::chrono::seconds{0})
Connect two peers.
auto links(Peer const &from)
Return the range of active links.
bool disconnect(Peer const &peer1, Peer const &peer2)
Break a link.
typename clock_type::duration duration
typename clock_type::time_point time_point
Digraph< Peer, link_type > links_
void send(Peer const &from, Peer const &to, Function &&f)
Send a message to a peer.
Digraph< Peer, link_type > const & graph() const
Return the underlying digraph.
BasicNetwork(BasicNetwork const &)=delete
Directed graph.
Definition Digraph.h:36
bool connect(Vertex source, Vertex target, EdgeData e)
Connect two vertices.
Definition Digraph.h:54
bool disconnect(Vertex source, Vertex target)
Disconnect two vertices.
Definition Digraph.h:81
std::optional< EdgeData > edge(Vertex source, Vertex target) const
Return edge data between two vertices.
Definition Digraph.h:99
Simulated discrete-event scheduler.
cancel_token in(duration const &delay, Function &&f)
Schedule an event after a specified duration passes.
time_point now() const
Return the current network time.
beast::manual_clock< std::chrono::steady_clock > clock_type
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
A single peer in the simulation.