mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-06 17:27:52 +00:00
Replace Journal public data members with member function accessors in order to make Journal lighter weight. The change makes a Journal cheaper to pass by value. Also add missing stream checks (e.g., calls to JLOG) to avoid text processing that ultimately will not be stored in the log.
136 lines
3.9 KiB
C++
136 lines
3.9 KiB
C++
//------------------------------------------------------------------------------
|
|
/*
|
|
This file is part of rippled: https://github.com/ripple/rippled
|
|
Copyright (c) 2012, 2013 Ripple Labs Inc.
|
|
|
|
Permission to use, copy, modify, and/or distribute this software for any
|
|
purpose with or without fee is hereby granted, provided that the above
|
|
copyright notice and this permission notice appear in all copies.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
*/
|
|
//==============================================================================
|
|
|
|
#include <BeastConfig.h>
|
|
#include <ripple/basics/Log.h>
|
|
#include <ripple/core/TimeKeeper.h>
|
|
#include <ripple/core/impl/SNTPClock.h>
|
|
#include <memory>
|
|
#include <mutex>
|
|
|
|
namespace ripple {
|
|
|
|
class TimeKeeperImpl : public TimeKeeper
|
|
{
|
|
private:
|
|
beast::Journal j_;
|
|
std::mutex mutable mutex_;
|
|
std::chrono::duration<std::int32_t> closeOffset_;
|
|
std::unique_ptr<SNTPClock> clock_;
|
|
|
|
// Adjust system_clock::time_point for NetClock epoch
|
|
static
|
|
time_point
|
|
adjust (std::chrono::system_clock::time_point when)
|
|
{
|
|
return time_point(
|
|
std::chrono::duration_cast<duration>(
|
|
when.time_since_epoch() -
|
|
days(10957)));
|
|
}
|
|
|
|
public:
|
|
explicit
|
|
TimeKeeperImpl (beast::Journal j)
|
|
: j_ (j)
|
|
, closeOffset_ {}
|
|
, clock_ (make_SNTPClock(j))
|
|
{
|
|
}
|
|
|
|
void
|
|
run (std::vector<
|
|
std::string> const& servers) override
|
|
{
|
|
clock_->run(servers);
|
|
}
|
|
|
|
time_point
|
|
now() const override
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
return adjust(clock_->now());
|
|
}
|
|
|
|
time_point
|
|
closeTime() const override
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
return adjust(clock_->now()) + closeOffset_;
|
|
}
|
|
|
|
void
|
|
adjustCloseTime(
|
|
std::chrono::duration<std::int32_t> amount) override
|
|
{
|
|
using namespace std::chrono;
|
|
auto const s = amount.count();
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
// Take large offsets, ignore small offsets,
|
|
// push the close time towards our wall time.
|
|
if (s > 1)
|
|
closeOffset_ += seconds((s + 3) / 4);
|
|
else if (s < -1)
|
|
closeOffset_ += seconds((s - 3) / 4);
|
|
else
|
|
closeOffset_ = (closeOffset_ * 3) / 4;
|
|
if (closeOffset_.count() != 0)
|
|
{
|
|
if (std::abs (closeOffset_.count()) < 60)
|
|
{
|
|
JLOG(j_.info()) <<
|
|
"TimeKeeper: Close time offset now " <<
|
|
closeOffset_.count();
|
|
}
|
|
else
|
|
{
|
|
JLOG(j_.warn()) <<
|
|
"TimeKeeper: Large close time offset = " <<
|
|
closeOffset_.count();
|
|
}
|
|
}
|
|
}
|
|
|
|
std::chrono::duration<std::int32_t>
|
|
nowOffset() const override
|
|
{
|
|
using namespace std::chrono;
|
|
using namespace std;
|
|
lock_guard<mutex> lock(mutex_);
|
|
return duration_cast<chrono::duration<int32_t>>(clock_->offset());
|
|
}
|
|
|
|
std::chrono::duration<std::int32_t>
|
|
closeOffset() const override
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
return closeOffset_;
|
|
}
|
|
};
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
std::unique_ptr<TimeKeeper>
|
|
make_TimeKeeper (beast::Journal j)
|
|
{
|
|
return std::make_unique<TimeKeeperImpl>(j);
|
|
}
|
|
|
|
} // ripple
|