Re: NTP Error
by BrowserUk (Patriarch) on May 28, 2012 at 09:05 UTC
|
1. If the NTP server is not reachable then it exits the script and i never get any error message as such. Can i add error handling here ?
A quick look inside the module shows that if any error occurs; the module dies with an error.
So, you can either allow that error to be displayed directly; or you could use block eval to trap it within your script: #! perl -slw
use strict;
use Data::Dump qw[ pp ];
use Net::NTP qw[ get_ntp_response ];
my %resp;
eval{
%resp = get_ntp_response( '0.poll.ntp.org' ); 1;
} or die $@;
pp \%resp;
Not that there would be much point if all you are going to do is die yourself; but you get the point.
If the way i am trying to get time the right way ?
This is a more complex question.
The reason for having the four timestamps in the response, is intended to allow the user to access the correctness of time returned.
In a typical repsonse: C:\test>ntp
{
"Leap Indicator" => 0,
Mode => 4,
"Originate Timestamp" => "1338194114.66406",
"Poll Interval" => "0.0000",
Precision => -22,
"Receive Timestamp" => "1338194119.80861",
"Reference Clock Identifier" => "195.66.241.3",
"Reference Timestamp" => "1338192423.2336",
"Root Delay" => "0.00360107421875",
"Root Dispersion" => "0.0000",
Stratum => 2,
"Transmit Timestamp" => "1338194119.80867",
"Version Number" => 3,
}
You can see by subtracting the time when the server recieved the request ("Receive Timestamp") from the time the client included in the request ("Originate Timestamp") that the packet took a little over 5 seconds to reach the server.
So, the server has factored that in, so that when it sends the response at ("Transmit Timestamp") 1338194119.80867, it adds that delay to the ("Reference Timestamp"), 1338192423.2336, so by the time you receive it, it should be pretty close to spot on.
Of course, if you delay utilising it at your end -- say, by letting it get caught up in interminable layers of some redundant behemoth of an IO library like POE -- then your just throwing away all work the NTP protocol uses to try and achieve accuracy.
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
| [reply] [d/l] [select] |
|
|
Sorry, but "Reference Timestamp" is something completely different than what you suggest. It may be worth correcting it in your answer, if that's possible.
| [reply] |
|
|
| [reply] |
Re: NTP Error
by Khen1950fx (Canon) on May 28, 2012 at 07:34 UTC
|
Why not take it up a notch by using
POE:::Component::Client::NTP. The author of
Net::NTP
joined forces with Chris Williams, so it's built on top of
Net::NTP.
I added some error handling, but the way you're doing time just doesn't work for me. I'm tryimg
Time::Stamp at the moment, but I haven't got it working right yet. Here's what I have so far:
#!/usr/bin/perl -l
use strict;
use warnings;
use Data::Dumper::Concise;
use POE qw(Component::Client::NTP);
#use Time::Stamp -stamps => {format => 'easy'};
my $host = shift @ARGV;
POE::Session->create(
package_states => [
main => [qw(_start _stop _response)],
],
);
$poe_kernel->run();
exit 0;
sub _start {
POE::Component::Client::NTP->get_ntp_response(
host => $host,
event => '_response',
) or die "Couldn't connect to server: $!";
return;
}
sub _stop {
print "Refcount was decremented";
return;
}
sub _response {
my $packet = $_[ARG0];
print Dumper( $packet );
}
| [reply] [d/l] |
|
|
Why not take it up a notch ...
I have to ask the counter question: Why take it up a notch?
For the OPs described purpose, what does adding the all that extra infrastructure into the equation buy?
Apart from pretty much guaranteeing that by the time he finally lays his hands on the timestamp he is after, it is a week out of date.
Its like using a MAC-truck as a shopping trolley.
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
| [reply] |
|
|
Glad to see that you're feeling chipper! I like your MAC analogy; however, I'd choose a MAC over a shopping trolley
any time:-).
I recommended
POE::Component::Client::NTP because it's a better fit with my
system, and because it doesn't look for
Socket6---it fails tests for me. To get around that, I started using POE::Component::Client::NTP.
As for which one is the closest to spot-on, I ran some simple timing tests. Hmmm...the jury is still out on that one.
Net::NTP
#!/usr/bin/perl
use strict;
use warnings;
use Time::AutoRes qw/time/;
use Data::Dumper::Concise;
use Net::NTP qw/get_ntp_response/;
my $host = 'pool.ntp.org';
print Dumper(my $start = time);
my %response = get_ntp_response($host);
print Dumper( %response ), "\n";
print Dumper(my $end = time);
print Dumper(my $elapsed_time = ($end - $start));
POE::Component::Client::NTP
#!/usr/bin/perl
use strict;
use warnings;
use Time::AutoRes qw/time/;
use Data::Dumper::Concise;
use Net::NTP qw/get_ntp_response/;
use POE qw/Component::Client::NTP/;
my $host = 'pool.ntp.org';
print Dumper(my $start = time);
POE::Session->create(
package_states => [
main => [qw(_start _stop _response)],
],
);
$poe_kernel->run();
print Dumper(my $end = time);
print Dumper(my $elapsed_time = ($end - $start));
exit 0;
sub _start {
POE::Component::Client::NTP->get_ntp_response(
host => $host,
event => '_response',
context => 'word',
) or die "Couldn't connect to server: $!";
return;
}
sub _stop {
print "Refcount was decremented";
}
sub _response {
my $packet = $_[ARG0];
print Dumper( $packet );
}
I got mixed results, but if I use my system time,
Net::NTP is consistently faster.
| [reply] [d/l] [select] |
|
|
Re: NTP Error
by rduke15 (Beadle) on Jan 11, 2016 at 00:09 UTC
|
"Reference Timestamp" is NOT what you want.
From the RFC:
Reference Timestamp: Time when the system clock was last set or corrected
If you only want to use one timestamp (ignoring delays, etc.), use "Transmit Timestamp". That is the time at the server when it sent you it's reply.
If you want to calculate your offset it would be
0.5 * (
($result{"Receive Timestamp"} - $result{"Originate Timestamp"})
+ ($result{"Transmit Timestamp"} - $time_when_reply_arrived})
);
| [reply] |