rippled
TimeKeeper.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 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 
20 #include <ripple/basics/Log.h>
21 #include <ripple/core/TimeKeeper.h>
22 #include <ripple/core/impl/SNTPClock.h>
23 #include <memory>
24 #include <mutex>
25 
26 namespace ripple {
27 
28 class TimeKeeperImpl : public TimeKeeper
29 {
30 private:
32  std::mutex mutable mutex_;
35 
36  // Adjust system_clock::time_point for NetClock epoch
37  static
39  adjust (std::chrono::system_clock::time_point when)
40  {
41  return time_point(
42  std::chrono::duration_cast<duration>(
43  when.time_since_epoch() -
44  days(10957)));
45  }
46 
47 public:
48  explicit
50  : j_ (j)
51  , closeOffset_ {}
52  , clock_ (make_SNTPClock(j))
53  {
54  }
55 
56  void
58  std::string> const& servers) override
59  {
60  clock_->run(servers);
61  }
62 
64  now() const override
65  {
66  std::lock_guard lock(mutex_);
67  return adjust(clock_->now());
68  }
69 
71  closeTime() const override
72  {
73  std::lock_guard lock(mutex_);
74  return adjust(clock_->now()) + closeOffset_;
75  }
76 
77  void
80  {
81  using namespace std::chrono;
82  auto const s = amount.count();
83  std::lock_guard lock(mutex_);
84  // Take large offsets, ignore small offsets,
85  // push the close time towards our wall time.
86  if (s > 1)
87  closeOffset_ += seconds((s + 3) / 4);
88  else if (s < -1)
89  closeOffset_ += seconds((s - 3) / 4);
90  else
91  closeOffset_ = (closeOffset_ * 3) / 4;
92  if (closeOffset_.count() != 0)
93  {
94  if (std::abs (closeOffset_.count()) < 60)
95  {
96  JLOG(j_.info()) <<
97  "TimeKeeper: Close time offset now " <<
99  }
100  else
101  {
102  JLOG(j_.warn()) <<
103  "TimeKeeper: Large close time offset = " <<
105  }
106  }
107  }
108 
110  nowOffset() const override
111  {
112  using namespace std::chrono;
113  using namespace std;
115  return duration_cast<chrono::duration<int32_t>>(clock_->offset());
116  }
117 
119  closeOffset() const override
120  {
122  return closeOffset_;
123  }
124 };
125 
126 //------------------------------------------------------------------------------
127 
130 {
131  return std::make_unique<TimeKeeperImpl>(j);
132 }
133 
134 } // ripple
std::lock
T lock(T... args)
ripple::TimeKeeperImpl::adjust
static time_point adjust(std::chrono::system_clock::time_point when)
Definition: TimeKeeper.cpp:39
std::string
STL class.
std::vector
STL class.
ripple::TimeKeeperImpl::closeOffset_
std::chrono::duration< std::int32_t > closeOffset_
Definition: TimeKeeper.cpp:33
std::chrono::duration< std::int32_t >
ripple::TimeKeeperImpl::closeTime
time_point closeTime() const override
Returns the close time, in network time.
Definition: TimeKeeper.cpp:71
beast::Journal::warn
Stream warn() const
Definition: Journal.h:302
std::lock_guard
STL class.
ripple::TimeKeeperImpl::mutex_
std::mutex mutex_
Definition: TimeKeeper.cpp:32
ripple::TimeKeeperImpl::j_
const beast::Journal j_
Definition: TimeKeeper.cpp:31
ripple::TimeKeeperImpl::nowOffset
std::chrono::duration< std::int32_t > nowOffset() const override
Definition: TimeKeeper.cpp:110
ripple::make_SNTPClock
std::unique_ptr< SNTPClock > make_SNTPClock(beast::Journal j)
Definition: SNTPClock.cpp:474
beast::Journal::info
Stream info() const
Definition: Journal.h:297
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:60
ripple::TimeKeeper
Manages various times used by the server.
Definition: TimeKeeper.h:32
memory
ripple::TimeKeeperImpl::adjustCloseTime
void adjustCloseTime(std::chrono::duration< std::int32_t > amount) override
Adjust the close time.
Definition: TimeKeeper.cpp:78
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::TimeKeeperImpl::run
void run(std::vector< std::string > const &servers) override
Launch the internal thread.
Definition: TimeKeeper.cpp:57
std
STL namespace.
ripple::TimeKeeperImpl::closeOffset
std::chrono::duration< std::int32_t > closeOffset() const override
Definition: TimeKeeper.cpp:119
ripple::make_TimeKeeper
std::unique_ptr< TimeKeeper > make_TimeKeeper(beast::Journal j)
Definition: TimeKeeper.cpp:129
ripple::TimeKeeperImpl::clock_
std::unique_ptr< SNTPClock > clock_
Definition: TimeKeeper.cpp:34
std::chrono::duration::count
T count(T... args)
ripple::TimeKeeperImpl
Definition: TimeKeeper.cpp:28
mutex
ripple::TimeKeeperImpl::now
time_point now() const override
Returns the estimate of wall time, in network time.
Definition: TimeKeeper.cpp:64
std::unique_ptr
STL class.
beast::abstract_clock< NetClock >::time_point
typename NetClock ::time_point time_point
Definition: abstract_clock.h:63
ripple::TimeKeeperImpl::TimeKeeperImpl
TimeKeeperImpl(beast::Journal j)
Definition: TimeKeeper.cpp:49
std::chrono