xrpld
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::test::csf {
60template <class Peer>
62{
63 using peer_type = Peer;
64
66
68
70
82
85
86public:
87 BasicNetwork(BasicNetwork const&) = delete;
89 operator=(BasicNetwork const&) = delete;
90
92
115 bool
116 connect(Peer const& from, Peer const& to, duration const& delay = std::chrono::seconds{0});
117
130 bool
131 disconnect(Peer const& peer1, Peer const& peer2);
132
151 template <class Function>
152 void
153 send(Peer const& from, Peer const& to, Function&& f);
154
159 auto
160 links(Peer const& from)
161 {
162 return links_.outEdges(from);
163 }
164
167 [[nodiscard]] Digraph<Peer, LinkType> const&
168 graph() const
169 {
170 return links_;
171 }
172};
173//------------------------------------------------------------------------------
174template <class Peer>
178
179template <class Peer>
180inline bool
181BasicNetwork<Peer>::connect(Peer const& from, Peer const& to, duration const& delay)
182{
183 if (to == from)
184 return false;
185 time_point const now = scheduler_.now();
186 if (!links_.connect(from, to, LinkType{false, delay, now}))
187 return false;
188 auto const result = links_.connect(to, from, LinkType{true, delay, now});
189 (void)result;
190 assert(result);
191 return true;
192}
193
194template <class Peer>
195inline bool
196BasicNetwork<Peer>::disconnect(Peer const& peer1, Peer const& peer2)
197{
198 if (!links_.disconnect(peer1, peer2))
199 return false;
200 bool const r = links_.disconnect(peer2, peer1);
201 (void)r;
202 assert(r);
203 return true;
204}
205
206template <class Peer>
207template <class Function>
208inline void
209BasicNetwork<Peer>::send(Peer const& from, Peer const& to, Function&& f)
210{
211 auto link = links_.edge(from, to);
212 if (!link)
213 return;
214 time_point const sent = scheduler_.now();
215 scheduler_.in(link->delay, [from, to, sent, f = std::forward<Function>(f), this] {
216 // only process if still connected and connection was
217 // not broken since the message was sent
218 if (auto l = links_.edge(from, to); l && l->established <= sent)
219 {
220 f();
221 }
222 });
223}
224
225} // 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:35
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.