xrpld
Loading...
Searching...
No Matches
timers.h
1#pragma once
2
3#include <csf/Scheduler.h>
4#include <csf/SimTime.h>
5
6#include <chrono>
7#include <iostream>
8#include <ostream>
9
10namespace xrpl::test::csf {
11
12// Timers are classes that schedule repeated events and are mostly independent
13// of simulation-specific details.
14
19{
23
26
27public:
29 Scheduler& sched,
30 SimDuration interval = std::chrono::seconds{60},
32 : scheduler_{sched}
33 , interval_{interval}
34 , out_{out}
36 , startSimTime_{sched.now()}
37 {
38 }
39
40 void
42 {
43 scheduler_.in(interval_, [this]() { beat(scheduler_.now()); });
44 }
45
46 void
48 {
49 using namespace std::chrono;
50 RealTime const realTime = RealClock::now();
51 SimTime const simTime = when;
52
53 RealDuration const realDuration = realTime - startRealTime_;
54 SimDuration const simDuration = simTime - startSimTime_;
55 out_ << "Heartbeat. Time Elapsed: {sim: " << duration_cast<seconds>(simDuration).count()
56 << "s | real: " << duration_cast<seconds>(realDuration).count() << "s}\n"
57 << std::flush;
58
59 scheduler_.in(interval_, [this]() { beat(scheduler_.now()); });
60 }
61};
62
63} // namespace xrpl::test::csf
HeartbeatTimer(Scheduler &sched, SimDuration interval=std::chrono::seconds{60}, std::ostream &out=std::cerr)
Definition timers.h:28
void beat(SimTime when)
Definition timers.h:47
Simulated discrete-event scheduler.
T duration_cast(T... args)
T flush(T... args)
std::chrono::system_clock RealClock
Definition SimTime.h:9
SimClock::duration SimDuration
Definition SimTime.h:14
RealClock::duration RealDuration
Definition SimTime.h:10
SimClock::time_point SimTime
Definition SimTime.h:15
RealClock::time_point RealTime
Definition SimTime.h:11