updating ntp service

This commit is contained in:
Daniel Larimer 2014-06-16 11:17:29 -04:00
parent 888a01881a
commit b7ad720647
3 changed files with 150 additions and 37 deletions

View file

@ -1,15 +1,26 @@
#pragma once #pragma once
#include <string> #include <string>
#include <fc/time.hpp> #include <fc/time.hpp>
#include <fc/optional.hpp>
namespace fc { namespace fc {
namespace detail { class ntp_impl; }
class ntp class ntp
{ {
public: public:
static void set_server( const std::string& hostname, uint16_t port = 123 ); ntp();
static fc::time_point get_time(); ~ntp();
void add_server( const std::string& hostname, uint16_t port = 123 );
void set_request_interval( uint32_t interval_sec );
void request_now();
optional<time_point> get_time()const;
private:
std::unique_ptr<detail::ntp_impl> my;
}; };
} // namespace fc } // namespace fc

View file

@ -1,15 +1,27 @@
#include <fc/network/ntp.hpp> #include <fc/network/ntp.hpp>
#include <fc/log/logger.hpp> #include <fc/log/logger.hpp>
#include <fc/thread/thread.hpp>
int main( int argc, char** argv ) int main( int argc, char** argv )
{ {
fc::time_point ntp_time = fc::ntp::get_time(); fc::ntp ntp_service;
ntp_service.set_request_interval(5);
fc::usleep(fc::seconds(4) );
auto time = ntp_service.get_time();
if( time )
{
auto ntp_time = *time;
auto delta = ntp_time - fc::time_point::now(); auto delta = ntp_time - fc::time_point::now();
auto minutes = delta.count() / 1000000 / 60; auto minutes = delta.count() / 1000000 / 60;
auto hours = delta.count() / 1000000 / 60 / 60; auto hours = delta.count() / 1000000 / 60 / 60;
auto seconds = delta.count() / 1000000; auto seconds = delta.count() / 1000000;
auto msec= delta.count() / 1000; auto msec= delta.count() / 1000;
idump( (ntp_time)(delta)(msec)(seconds)(minutes)(hours) ); idump( (ntp_time)(delta)(msec)(seconds)(minutes)(hours) );
}
else
{
elog( "no response" );
}
return 0; return 0;
} }

View file

@ -2,6 +2,7 @@
#include <fc/network/udp_socket.hpp> #include <fc/network/udp_socket.hpp>
#include <fc/network/resolve.hpp> #include <fc/network/resolve.hpp>
#include <fc/network/ip.hpp> #include <fc/network/ip.hpp>
#include <fc/thread/thread.hpp>
#include <stdint.h> #include <stdint.h>
#include "../byteswap.hpp" #include "../byteswap.hpp"
@ -10,35 +11,68 @@
namespace fc namespace fc
{ {
static fc::ip::endpoint ntp_server; namespace detail {
class ntp_impl
void ntp::set_server( const std::string& hostname, uint16_t port )
{ {
auto eps = resolve( hostname, port ); public:
if( eps.size() ) ntp_impl() :_request_interval_sec( 60*60 /* 1 hr */)
ntp_server = eps.front(); {
_ntp_hosts.push_back( std::make_pair( "pool.ntp.org",123 ) );
} }
fc::time_point ntp::get_time() /** vector < host, port > */
{ std::vector< std::pair< std::string, uint16_t> > _ntp_hosts;
static bool init_ntp_server = false; fc::future<void> _request_loop;
if( !init_ntp_server ) fc::future<void> _read_loop;
{ udp_socket _sock;
set_server( "pool.ntp.org", 123 ); uint32_t _request_interval_sec;
init_ntp_server = true; fc::time_point _next_request_time;
} optional<fc::microseconds> _last_ntp_delta;
udp_socket sock;
sock.open();
void request_now()
{
for( auto item : _ntp_hosts )
{
try
{
ilog( "resolving... ${r}", ("r", item) );
auto eps = resolve( item.first, item.second );
for( auto ep : eps )
{
ilog( "sending request to ${ep}", ("ep",ep) );
std::array<unsigned char, 48> send_buf { {010,0,0,0,0,0,0,0,0} }; std::array<unsigned char, 48> send_buf { {010,0,0,0,0,0,0,0,0} };
_sock.send_to( (const char*)send_buf.data(), send_buf.size(), ep );
}
}
// this could fail to resolve but we want to go on to other hosts..
catch ( const fc::exception& e )
{
elog( "${e}", ("e",e.to_detail_string() ) );
}
}
} // request_now
sock.send_to( (const char*)send_buf.data(), send_buf.size(), ntp_server ); void request_loop()
{
while( !_request_loop.canceled() )
{
if( _next_request_time < fc::time_point::now() )
{
_next_request_time += fc::seconds( _request_interval_sec );
request_now();
}
fc::usleep( fc::seconds(1) ); // TODO: fix FC timers..
} // while
} // request_loop
void read_loop()
{
while( !_read_loop.canceled() )
{
fc::ip::endpoint from; fc::ip::endpoint from;
std::array<uint64_t, 1024> recv_buf; std::array<uint64_t, 1024> recv_buf;
sock.receive_from( (char*)recv_buf.data(), recv_buf.size(), from ); _sock.receive_from( (char*)recv_buf.data(), recv_buf.size(), from );
uint64_t receive_timestamp_net_order = recv_buf[4]; uint64_t receive_timestamp_net_order = recv_buf[4];
uint64_t receive_timestamp_host = bswap_64(receive_timestamp_net_order); uint64_t receive_timestamp_host = bswap_64(receive_timestamp_net_order);
@ -47,7 +81,63 @@ namespace fc
uint32_t seconds_since_1900 = receive_timestamp_host >> 32; uint32_t seconds_since_1900 = receive_timestamp_host >> 32;
uint32_t seconds_since_epoch = seconds_since_1900 - 2208988800; uint32_t seconds_since_epoch = seconds_since_1900 - 2208988800;
return fc::time_point() + fc::seconds(seconds_since_epoch) + fc::microseconds(microseconds); auto ntp_time = (fc::time_point() + fc::seconds(seconds_since_epoch) + fc::microseconds(microseconds));
_last_ntp_delta = ntp_time - fc::time_point::now();
}
} // read_loop
};
} // namespace detail
ntp::ntp()
:my( new detail::ntp_impl() )
{
my->_sock.open();
my->_request_loop = fc::async( [=](){ my->request_loop(); } );
my->_read_loop = fc::async( [=](){ my->read_loop(); } );
}
ntp::~ntp()
{
try {
my->_request_loop.cancel();
my->_read_loop.cancel();
my->_sock.close();
my->_request_loop.wait();
my->_read_loop.wait();
}
catch ( const fc::exception& e )
{
// we exepect canceled exceptions, but cannot throw
// from destructor
}
}
void ntp::add_server( const std::string& hostname, uint16_t port)
{
my->_ntp_hosts.push_back( std::make_pair(hostname,port) );
}
void ntp::set_request_interval( uint32_t interval_sec )
{
my->_request_interval_sec = interval_sec;
my->_next_request_time = fc::time_point::now();
}
void ntp::request_now()
{
my->request_now();
}
optional<time_point> ntp::get_time()const
{
if( my->_last_ntp_delta )
return fc::time_point::now() + *my->_last_ntp_delta;
return optional<time_point>();
} }
} }