Skip to content

Instantly share code, notes, and snippets.

@wilsonianb
Created July 20, 2016 18:27
Show Gist options
  • Save wilsonianb/5da816fb1c0453c473670d7aad5ce972 to your computer and use it in GitHub Desktop.
Save wilsonianb/5da816fb1c0453c473670d7aad5ce972 to your computer and use it in GitHub Desktop.
start to ValidatorSite class
#include <BeastConfig.h>
#include <ripple/core/app/misc/ValidatorSite.h>
#include <ripple/basics/Log.h>
#include <ripple/basics/ThreadName.h>
#include <ripple/basics/random.h>
#include <ripple/beast/core/Thread.h>
#include <ripple/core/ThreadEntry.h>
#include <beast/core/placeholders.hpp>
#include <boost/asio.hpp>
#include <boost/optional.hpp>
#include <mutex>
namespace ripple {
// validator site default query frequency - 5 minutes
auto constexpr DEFAULT_QUERY_FREQUENCY = 5min;
class ValidatorSiteImp
: public ValidatorSite
{
private:
template <class Duration>
using sys_time = std::chrono::time_point<clock_type, Duration>;
using sys_seconds = sys_time<std::chrono::seconds>;
beast::Journal j_;
std::mutex mutable mutex_;
boost::asio::io_service io_service_;
boost::optional<
boost::asio::io_service::work> work_;
std::string server_;
boost::asio::basic_waitable_timer<std::chrono::system_clock> timer_;
// sys_seconds lastUpdate_;
public:
using error_code = boost::system::error_code;
explicit
ValidatorSiteImp (std::string&boost::asio::io_service& io_service, beast::Journal j)
: j_ (j)
, server_(server)
, work_(io_service_)
, timer_ (io_service_)
{
}
~ValidatorSiteImp ()
{
if (thread_.joinable())
{
error_code ec;
timer_.cancel(ec);
work_ = boost::none;
}
}
//--------------------------------------------------------------------------
void
run (const std::string& server) override
{
std::vector<std::string>::const_iterator it = servers.begin ();
if (it == servers.end ())
{
JLOG(j_.info()) <<
"ValidatorSite: no server specified";
return;
}
{
std::lock_guard<std::mutex> lock (mutex_);
for (auto const& item : servers)
{
servers_.emplace_back(
item,
boost::asio::basic_waitable_timer<std::chrono::system_clock>(io_service_));
servers_.back ().second.expires_from_now (
DEFAULT_QUERY_FREQUENCY);
servers_.back ().second.async_wait (std::bind (
&ValidatorSiteImp::onTimer, this,
beast::asio::placeholders::error));
}
}
queryAll();
}
void
onTimer (error_code const& ec)
{
using namespace boost::asio;
if (ec == error::operation_aborted)
return;
if (ec)
{
JLOG(j_.error()) <<
"ValidatorSite::onTimer: " << ec.message();
return;
}
doQuery ();
//TODO: move this to when we handle response.
// timer_.expires_from_now(NTP_QUERY_FREQUENCY);
// timer_.async_wait(std::bind(
// &SNTPClientImp::onTimer, this,
// beast::asio::placeholders::error));
}
bool doQuery ()
{
std::lock_guard<std::mutex> lock (mutex_);
// TODO: http request
JLOG(j_.trace()) <<
"ValidatorSite: Resolve pending for " << server_;
return true;
}
std::unique_ptr <ValidatorSite>
make_ValidatorSite (boost::asio::io_service&, beast::Journal);
std::unique_ptr <ValidatorSite>
make_ValidatorSite (std::string& server, boost::asio::io_service& io_service, beast::Journal journal)
{
return std::make_unique<ValidatorSiteImp>(io_service, journal);
}
// validatorSites_ (make_ValidatorSite (get_io_service ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment