xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
tests
libxrpl
csf
BasicNetwork.h
1
#pragma once
2
3
#include <csf/Digraph.h>
4
#include <csf/Scheduler.h>
5
6
#include <
cassert
>
7
8
namespace
xrpl::test::csf
{
62
template
<
class
Peer>
63
class
BasicNetwork
64
{
65
using
PeerType
=
Peer
;
66
67
using
ClockType
=
Scheduler::ClockType
;
68
69
using
duration
=
ClockType::duration
;
70
71
using
time_point
=
ClockType::time_point
;
72
73
struct
LinkType
74
{
75
bool
inbound
=
false
;
76
duration
delay
{};
77
time_point
established
;
78
LinkType
() =
default
;
79
LinkType
(
bool
inbound
,
duration
delay
,
time_point
established
)
80
:
inbound
(
inbound
),
delay
(
delay
),
established
(
established
)
81
{
82
}
83
};
84
85
Scheduler
&
scheduler_
;
86
Digraph<Peer, LinkType>
links_
;
87
88
public
:
89
BasicNetwork
(
BasicNetwork
const
&) =
delete
;
90
BasicNetwork
&
91
operator=
(
BasicNetwork
const
&) =
delete
;
92
93
BasicNetwork
(
Scheduler
& s);
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
//------------------------------------------------------------------------------
181
template
<
class
Peer>
182
BasicNetwork<Peer>::BasicNetwork
(
Scheduler
& s) :
scheduler_
(s)
183
{
184
}
185
186
template
<
class
Peer>
187
inline
bool
188
BasicNetwork<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
201
template
<
class
Peer>
202
inline
bool
203
BasicNetwork<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
213
template
<
class
Peer>
214
template
<
class
Function>
215
inline
void
216
BasicNetwork<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
cassert
beast::ManualClock< std::chrono::steady_clock >::duration
std::chrono::steady_clock::duration duration
Definition
abstract_clock.h:40
beast::ManualClock< std::chrono::steady_clock >::time_point
std::chrono::steady_clock::time_point time_point
Definition
abstract_clock.h:41
xrpl::test::csf::BasicNetwork::ClockType
Scheduler::ClockType ClockType
Definition
BasicNetwork.h:67
xrpl::test::csf::BasicNetwork::links_
Digraph< Peer, LinkType > links_
Definition
BasicNetwork.h:86
xrpl::test::csf::BasicNetwork::time_point
ClockType::time_point time_point
Definition
BasicNetwork.h:71
xrpl::test::csf::BasicNetwork::operator=
BasicNetwork & operator=(BasicNetwork const &)=delete
xrpl::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:188
xrpl::test::csf::BasicNetwork::links
auto links(Peer const &from)
Return the range of active links.
Definition
BasicNetwork.h:166
xrpl::test::csf::BasicNetwork::disconnect
bool disconnect(Peer const &peer1, Peer const &peer2)
Break a link.
Definition
BasicNetwork.h:203
xrpl::test::csf::BasicNetwork::duration
ClockType::duration duration
Definition
BasicNetwork.h:69
xrpl::test::csf::BasicNetwork::scheduler_
Scheduler & scheduler_
Definition
BasicNetwork.h:85
xrpl::test::csf::BasicNetwork::graph
Digraph< Peer, LinkType > const & graph() const
Return the underlying digraph.
Definition
BasicNetwork.h:175
xrpl::test::csf::BasicNetwork::PeerType
Peer PeerType
Definition
BasicNetwork.h:65
xrpl::test::csf::BasicNetwork::send
void send(Peer const &from, Peer const &to, Function &&f)
Send a message to a peer.
Definition
BasicNetwork.h:216
xrpl::test::csf::BasicNetwork::BasicNetwork
BasicNetwork(BasicNetwork const &)=delete
xrpl::test::csf::Digraph
Directed graph.
Definition
Digraph.h:36
xrpl::test::csf::Scheduler
Simulated discrete-event scheduler.
Definition
src/tests/libxrpl/csf/Scheduler.h:28
xrpl::test::csf::Scheduler::ClockType
beast::ManualClock< std::chrono::steady_clock > ClockType
Definition
src/tests/libxrpl/csf/Scheduler.h:30
std::chrono::seconds
std::forward
T forward(T... args)
xrpl::test::csf
Definition
Validations.cpp:23
xrpl::test::csf::BasicNetwork::LinkType
Definition
BasicNetwork.h:74
xrpl::test::csf::BasicNetwork::LinkType::established
time_point established
Definition
BasicNetwork.h:77
xrpl::test::csf::BasicNetwork::LinkType::inbound
bool inbound
Definition
BasicNetwork.h:75
xrpl::test::csf::BasicNetwork::LinkType::delay
duration delay
Definition
BasicNetwork.h:76
xrpl::test::csf::BasicNetwork::LinkType::LinkType
LinkType()=default
xrpl::test::csf::BasicNetwork::LinkType::LinkType
LinkType(bool inbound, duration delay, time_point established)
Definition
BasicNetwork.h:79
xrpl::test::csf::Peer
A single peer in the simulation.
Definition
tests/libxrpl/csf/Peer.h:57
Generated by
1.17.0