rippled
timers.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012-2017 Ripple Labs Inc
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 #ifndef RIPPLE_TEST_CSF_TIMERS_H_INCLUDED
20 #define RIPPLE_TEST_CSF_TIMERS_H_INCLUDED
21 
22 #include <chrono>
23 #include <ostream>
24 #include <test/csf/Scheduler.h>
25 #include <test/csf/SimTime.h>
26 
27 namespace ripple {
28 namespace test {
29 namespace csf {
30 
31 // Timers are classes that schedule repeated events and are mostly independent
32 // of simulation-specific details.
33 
37 {
41 
44 
45 public:
47  Scheduler& sched,
48  SimDuration interval = std::chrono::seconds{60},
50  : scheduler_{sched}
51  , interval_{interval}
52  , out_{out}
54  , startSimTime_{sched.now()}
55  {
56  }
57 
58  void
60  {
61  scheduler_.in(interval_, [this]() { beat(scheduler_.now()); });
62  }
63 
64  void
65  beat(SimTime when)
66  {
67  using namespace std::chrono;
68  RealTime realTime = RealClock::now();
69  SimTime simTime = when;
70 
71  RealDuration realDuration = realTime - startRealTime_;
72  SimDuration simDuration = simTime - startSimTime_;
73  out_ << "Heartbeat. Time Elapsed: {sim: "
74  << duration_cast<seconds>(simDuration).count()
75  << "s | real: " << duration_cast<seconds>(realDuration).count()
76  << "s}\n"
77  << std::flush;
78 
79  scheduler_.in(interval_, [this]() { beat(scheduler_.now()); });
80  }
81 };
82 
83 } // namespace csf
84 } // namespace test
85 } // namespace ripple
86 
87 #endif
ripple::test::csf::SimTime
typename SimClock::time_point SimTime
Definition: SimTime.h:36
ripple::test::csf::RealDuration
RealClock::duration RealDuration
Definition: SimTime.h:31
ripple::test::csf::Scheduler
Simulated discrete-event scheduler.
Definition: test/csf/Scheduler.h:47
std::chrono::seconds
std::cerr
ripple::test::csf::HeartbeatTimer::start
void start()
Definition: timers.h:59
ripple::test::csf::HeartbeatTimer
Gives heartbeat of simulation to signal simulation progression.
Definition: timers.h:36
ripple::test::csf::HeartbeatTimer::out_
std::ostream & out_
Definition: timers.h:40
ripple::test::csf::HeartbeatTimer::HeartbeatTimer
HeartbeatTimer(Scheduler &sched, SimDuration interval=std::chrono::seconds{60}, std::ostream &out=std::cerr)
Definition: timers.h:46
ripple::test::csf::Scheduler::in
cancel_token in(duration const &delay, Function &&f)
Schedule an event after a specified duration passes.
ripple::QualityDirection::out
@ out
std::ostream
STL class.
chrono
ripple::test::csf::RealTime
RealClock::time_point RealTime
Definition: SimTime.h:32
std::flush
T flush(T... args)
ripple::test::csf::HeartbeatTimer::startSimTime_
SimTime startSimTime_
Definition: timers.h:43
ripple::test::csf::HeartbeatTimer::interval_
SimDuration interval_
Definition: timers.h:39
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::test::csf::HeartbeatTimer::startRealTime_
RealTime startRealTime_
Definition: timers.h:42
ripple::test::csf::SimDuration
typename SimClock::duration SimDuration
Definition: SimTime.h:35
ripple::test::csf::Scheduler::now
time_point now() const
Return the current network time.
Definition: test/csf/Scheduler.h:365
ostream
ripple::test::csf::HeartbeatTimer::scheduler_
Scheduler & scheduler_
Definition: timers.h:38
ripple::test::csf::HeartbeatTimer::beat
void beat(SimTime when)
Definition: timers.h:65
std::chrono
std::chrono::system_clock::now
T now(T... args)