Added more logging to ntp and fixed code indentation.

This commit is contained in:
dnotestein 2014-09-30 10:53:23 -04:00
parent 53196706f0
commit c4e814d7de

View file

@ -56,7 +56,7 @@ namespace fc
auto eps = resolve( item.first, item.second ); auto eps = resolve( item.first, item.second );
for( auto ep : eps ) for( auto ep : eps )
{ {
ilog( "sending request to ${ep}", ("ep",ep) ); wlog( "sending request to ${ep}", ("ep",ep) );
std::shared_ptr<char> send_buffer(new char[48], [](char* p){ delete[] p; }); std::shared_ptr<char> send_buffer(new char[48], [](char* p){ delete[] p; });
std::array<unsigned char, 48> packet_to_send { {010,0,0,0,0,0,0,0,0} }; std::array<unsigned char, 48> packet_to_send { {010,0,0,0,0,0,0,0,0} };
memcpy(send_buffer.get(), packet_to_send.data(), packet_to_send.size()); memcpy(send_buffer.get(), packet_to_send.data(), packet_to_send.size());
@ -109,7 +109,7 @@ namespace fc
try try
{ {
_sock.open(); _sock.open();
request_time_task(); request_time_task(); //this will re-send a time request
while( !_read_loop_done.canceled() ) while( !_read_loop_done.canceled() )
{ {
@ -117,6 +117,7 @@ namespace fc
try try
{ {
_sock.receive_from( receive_buffer, receive_buffer_size, from ); _sock.receive_from( receive_buffer, receive_buffer_size, from );
wlog("received ntp reply from ${from}",("from",from) );
} FC_RETHROW_EXCEPTIONS(error, "Error reading from NTP socket"); } FC_RETHROW_EXCEPTIONS(error, "Error reading from NTP socket");
uint64_t receive_timestamp_net_order = recv_buf[4]; uint64_t receive_timestamp_net_order = recv_buf[4];
@ -126,9 +127,13 @@ 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;
//if the reply we just received has occurred more than a second after our last time request (it was more than a second ago since our last request)
if( fc::time_point::now() - _last_request_time > fc::seconds(1) ) if( fc::time_point::now() - _last_request_time > fc::seconds(1) )
request_now(); {
else wlog("received stale ntp reply requested at ${request_time}, send a new time request",("request_time",_last_request_time));
request_now(); //request another reply and ignore this one
}
else //we think we have a timely reply, process it
{ {
auto ntp_time = (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));
if( ntp_time - fc::time_point::now() < fc::seconds(60*60*24) && if( ntp_time - fc::time_point::now() < fc::seconds(60*60*24) &&
@ -140,7 +145,7 @@ namespace fc
wlog("ntp_delta_time updated to ${delta_time}", ("delta_time",ntp_delta_time) ); wlog("ntp_delta_time updated to ${delta_time}", ("delta_time",ntp_delta_time) );
} }
else else
elog( "NTP time is way off ${time}", ("time",ntp_time)("local",fc::time_point::now()) ); elog( "NTP time and local time vary by more than a day! ntp:${ntp_time} local:${local}", ("ntp_time",ntp_time)("local",fc::time_point::now()) );
} }
} }
} // try } // try
@ -148,10 +153,15 @@ namespace fc
{ {
throw; throw;
} }
catch (const fc::exception& e)
{
//swallow any other exception and restart loop
elog("exception in read_loop, going to restart it. ${e}",("e",e));
}
catch (...) catch (...)
{ {
//swallow any other exception and restart loop //swallow any other exception and restart loop
elog("unexpected exception in read_loop, going to restart it."); elog("unknown exception in read_loop, going to restart it.");
} }
_sock.close(); _sock.close();
fc::usleep(fc::seconds(_request_interval_sec)); fc::usleep(fc::seconds(_request_interval_sec));
@ -225,4 +235,4 @@ namespace fc
return optional<time_point>(); return optional<time_point>();
} }
} } //namespace fc