xrpld
Loading...
Searching...
No Matches
BasicNetwork.h
1#pragma once
2
3#include <csf/Digraph.h>
4#include <csf/Scheduler.h>
5
6#include <cassert>
7
8namespace xrpl::test::csf {
62template <class Peer>
64{
65 using peer_type = Peer;
66
68
70
72
84
87
88public:
89 BasicNetwork(BasicNetwork const&) = delete;
91 operator=(BasicNetwork const&) = delete;
92
94
118 bool
119 connect(Peer const& from, Peer const& to, duration const& delay = std::chrono::seconds{0});
120
134 bool
135 disconnect(Peer const& peer1, Peer const& peer2);
136
156 template <class Function>
157 void
158 send(Peer const& from, Peer const& to, Function&& f);
159
165 auto
166 links(Peer const& from)
167 {
168 return links_.outEdges(from);
169 }
170
174 [[nodiscard]] Digraph<Peer, LinkType> const&
175 graph() const
176 {
177 return links_;
178 }
179};
180//------------------------------------------------------------------------------
181template <class Peer>
185
186template <class Peer>
187inline bool
188BasicNetwork<Peer>::connect(Peer const& from, Peer const& to, duration const& delay)
189{
190 if (to == from)
191 return false;
192 time_point const now = scheduler_.now();
193 if (!links_.connect(from, to, LinkType{false, delay, now}))
194 return false;
195 auto const result = links_.connect(to, from, LinkType{true, delay, now});
196 (void)result;
197 assert(result);
198 return true;
199}
200
201template <class Peer>
202inline bool
203BasicNetwork<Peer>::disconnect(Peer const& peer1, Peer const& peer2)
204{
205 if (!links_.disconnect(peer1, peer2))
206 return false;
207 bool const r = links_.disconnect(peer2, peer1);
208 (void)r;
209 assert(r);
210 return true;
211}
212
213template <class Peer>
214template <class Function>
215inline void
216BasicNetwork<Peer>::send(Peer const& from, Peer const& to, Function&& f)
217{
218 auto link = links_.edge(from, to);
219 if (!link)
220 return;
221 time_point const sent = scheduler_.now();
222 scheduler_.in(link->delay, [from, to, sent, f = std::forward<Function>(f), this] {
223 // only process if still connected and connection was
224 // not broken since the message was sent
225 if (auto l = links_.edge(from, to); l && l->established <= sent)
226 {
227 f();
228 }
229 });
230}
231
232} // namespace xrpl::test::csf
std::chrono::steady_clock::duration duration
std::chrono::steady_clock::time_point time_point
Digraph< Peer, LinkType > links_
clock_type::time_point time_point
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.
Scheduler::clock_type clock_type
bool disconnect(Peer const &peer1, Peer const &peer2)
Break a link.
clock_type::duration duration
Digraph< Peer, LinkType > const & graph() const
Return the underlying digraph.
void send(Peer const &from, Peer const &to, Function &&f)
Send a message to a peer.
BasicNetwork(BasicNetwork const &)=delete
Directed graph.
Definition Digraph.h:36
Simulated discrete-event scheduler.
beast::ManualClock< std::chrono::steady_clock > clock_type
T forward(T... args)
LinkType(bool inbound, duration delay, time_point established)
A single peer in the simulation.