blib/lib/POE/Component/Client/NTP.pm

filter: Statementstmt 88.3% 137 / 155 Branchbran 50.0% 14 / 28 Conditioncond 50.0% 4 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 94.4% 17 / 18 Podpod 100% 1 / 1 totaltotal 80.9% 174 / 215 CC 20 SCAR 14.0
CC
20
Cov
81%
get_ntp_response
8.1
_start
6.8
__ANON__
6.0
CRAP 4.0
↑ Summary
1 package POE::Component::Client::NTP;
2 3 $POE::Component::Client::NTP::VERSION = '0.14';
3 #ABSTRACT: A POE Component to query NTP servers
4  
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Socket qw[:all];
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use IO::Socket::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Time::HiRes qw[time];
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 our %MODE = (
14 '0' => 'reserved',
15 '1' => 'symmetric active',
16 '2' => 'symmetric passive',
17 '3' => 'client',
18 '4' => 'server',
19 '5' => 'broadcast',
20 '6' => 'reserved for NTP control message',
21 '7' => 'reserved for private use'
22 );
23  
24 3 our %STRATUM = (
25 '0' => 'unspecified or unavailable',
26 '1' => 'primary reference (e.g., radio clock)',
27 );
28  
29 3 for(2 .. 15){
30 42 $STRATUM{$_} = 'secondary reference (via NTP or SNTP)';
31 }
32  
33 3 for(16 .. 255){
34 720 $STRATUM{$_} = 'reserved';
35 }
36  
37 3 our %STRATUM_ONE_TEXT = (
38 'LOCL' => 'uncalibrated local clock used as a primary reference for a subnet without external means of synchronization',
39 'PPS' => 'atomic clock or other pulse-per-second source individually calibrated to national standards',
40 'ACTS' => 'NIST dialup modem service',
41 'USNO' => 'USNO modem service',
42 'PTB' => 'PTB (Germany) modem service',
43 'TDF' => 'Allouis (France) Radio 164 kHz',
44 'DCF' => 'Mainflingen (Germany) Radio 77.5 kHz',
45 'MSF' => 'Rugby (UK) Radio 60 kHz',
46 'WWV' => 'Ft. Collins (US) Radio 2.5, 5, 10, 15, 20 MHz',
47 'WWVB' => 'Boulder (US) Radio 60 kHz',
48 'WWVH' => 'Kaui Hawaii (US) Radio 2.5, 5, 10, 15 MHz',
49 'CHU' => 'Ottawa (Canada) Radio 3330, 7335, 14670 kHz',
50 'LORC' => 'LORAN-C radionavigation system',
51 'OMEG' => 'OMEGA radionavigation system',
52 'GPS' => 'Global Positioning Service',
53 'GOES' => 'Geostationary Orbit Environment Satellite',
54 );
55  
56 3 our %LEAP_INDICATOR = (
57 '0' => 'no warning',
58 '1' => 'last minute has 61 seconds',
59 '2' => 'last minute has 59 seconds)',
60 '3' => 'alarm condition (clock not synchronized)'
61 );
62  
63 {
64  
65 3 ▶ use constant NTP_ADJ => 2208988800;
Subroutine BEGIN: called CC=1 SCAR=0.0
66  
67 3 my @ntp_packet_fields =
68 (
69 'Leap Indicator',
70 'Version Number',
71 'Mode',
72 'Stratum',
73 'Poll Interval',
74 'Precision',
75 'Root Delay',
76 'Root Dispersion',
77 'Reference Clock Identifier',
78 'Reference Timestamp',
79 'Originate Timestamp',
80 'Receive Timestamp',
81 'Transmit Timestamp',
82 );
83  
84 2 ▶ my $frac2bin = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
85 2 my $bin = '';
86 2 my $frac = shift;
87 2 ▶ while ( length($bin) < 32 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $bin < 32) 46 2 48
88 46 $bin = $bin . int( $frac * 2 );
89 46 $frac = ( $frac * 2 ) - ( int( $frac * 2 ) );
90 }
91 2 return $bin;
92 3 };
93  
94 8 ▶ my $bin2frac = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
95 8 my @bin = split '', shift;
96 8 my $frac = 0;
97 8 ▶ while (@bin) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bin) 240 8 248
98 240 $frac = ( $frac + pop @bin ) / 2;
99 }
100 8 return $frac;
101 3 };
102  
103 0 ▶ my $percision = sub{
Subroutine __ANON__: not called CC=2 SCAR=17.9
104 0 my $number = shift;
105 0 ▶ if($number > 127){
Branch: 1 branch
BranchTrueFalseTotal
if ($number > 127) 0 0 0
106 0 $number -= 255;
107 }
108 0 return sprintf("%1.4e", 2**$number);
109 3 };
110  
111 1 ▶ my $unpack_ip = sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
112 1 my $ip;
113 1 my $stratum = shift;
114 1 my $tmp_ip = shift;
115 1 ▶ if($stratum < 2){
Branch: 1 branch
BranchTrueFalseTotal
if ($stratum < 2) { } 0 1 1
116 0 $ip = unpack("A4",
117 pack("H8", $tmp_ip)
118 );
119 }else{
120 1 $ip = sprintf("%d.%d.%d.%d",
121 unpack("C4",
122 pack("H8", $tmp_ip)
123 )
124 );
125 }
126 1 return $ip;
127 3 };
128  
129 2 ▶sub get_ntp_response {
Subroutine get_ntp_response: called CC=7 SCAR=21.0
Pod documented
130 2 my $package = shift;
131 2 my %opts = @_;
132 2 $opts{lc $_} = delete $opts{$_} for keys %opts;
133 croak "$package requires an 'event' argument\n"
134 2 ▶ unless $opts{event};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"event"} 0 2 2
135 2 my $options = delete $opts{options};
136 2 ▶ $opts{host} = 'localhost' unless $opts{host};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"host"} 0 2 2
137 2 ▶ $opts{port} = 123 unless $opts{port} and $opts{port} =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"port"} and $opts{"port"} =~ /^\d+$/ 2 0 2
Condition33%1/3
$opts{"port"} and $opts{"port"} =~ /^\d+$/
!ll&&!rl&&r
200
MC/DC0%0/2
$opts{"port"} and $opts{"port"} =~ /^\d+$/
$opts{"port"} $opts{"port"} =~ /^\d+$/
Truth table33%1/3
A and B
A= $opts{"port"}B= $opts{"port"} =~ /^\d+$/
ABresult
0X0
100
111
138 2 my $self = bless \%opts, $package;
139 2 ▶ $self->{session_id} = POE::Session->create(
Branch: 1 branch
BranchTrueFalseTotal
ref $options eq 'HASH' ? : 0 2 2
140 object_states => [
141 $self => [qw(_start _socket _dispatch _get_datagram _timeout)],
142 ],
143 heap => $self,
144 ( ref($options) eq 'HASH' ? ( options => $options ) : () ),
145 )->ID();
146 2 return $self;
147 }
148  
149 2 ▶sub _start {
Subroutine _start: called CC=5 SCAR=19.2
150 2 my ($kernel,$sender,$self) = @_[KERNEL,SENDER,OBJECT];
151 2 $self->{session_id} = $_[SESSION]->ID();
152 2 ▶ if ( $kernel == $sender and !$self->{session} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kernel == $sender and not $self->{"session"}) 0 2 2
Condition33%1/3
$kernel == $sender and not $self->{"session"}
!ll&&!rl&&r
200
MC/DC0%0/2
$kernel == $sender and not $self->{"session"}
$kernel == $sender not $self->{"session"}
Truth table33%1/3
A and not(B)
A= $kernel == $senderB= not $self->{"session"}
ABresult
0X0
100
111
153 0 croak "Not called from another POE session and 'session' wasn't set\n";
154 }
155 2 my $sender_id;
156 2 ▶ if ( $self->{session} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'session'}) { } 0 2 2
157 0 ▶ if ( my $ref = $kernel->alias_resolve( $self->{session} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = $kernel->alias_resolve($self->{'session'})) { } 0 0 0
158 0 $sender_id = $ref->ID();
159 }
160 else {
161 0 croak "Could not resolve 'session' to a valid POE session\n";
162 }
163 }
164 else {
165 2 $sender_id = $sender->ID();
166 }
167 2 $kernel->refcount_increment( $sender_id, __PACKAGE__ );
168 2 $self->{sender_id} = $sender_id;
169 2 $kernel->detach_myself();
170 2 $kernel->yield('_socket');
171 2 return;
172 }
173  
174 2 ▶sub _socket {
Subroutine _socket: called CC=4 SCAR=14.7
175 2 my ($kernel,$self) = @_[KERNEL,OBJECT];
176 2 my $socket = IO::Socket::IP->new( Proto => 'udp' );
177 2 my $ai;
178 {
179 2 my %hints = (socktype => SOCK_DGRAM, protocol => IPPROTO_UDP);
180 2 my ($err, @res) = getaddrinfo($self->{host}, $self->{port}, \%hints);
181 2 ▶ if ( $err ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 2 2
182 0 $self->{error} = $err;
183 0 $kernel->yield('_dispatch');
184 0 return;
185 }
186 2 $ai = shift @res;
187 }
188 2 my $client_localtime = $self->{client_localtime} = time();
189 2 my $client_adj_localtime = $client_localtime + NTP_ADJ;
190 2 my $client_frac_localtime = $frac2bin->($client_adj_localtime);
191  
192 2 my $ntp_msg =
193 pack( "B8 C3 N10 B32", '00011011', (0) x 12, int($client_localtime),
194 $client_frac_localtime );
195  
196 2 $socket->socket( $ai->{family}, $ai->{socktype}, $ai->{protocol} );
197 2 ▶ unless ( send( $socket, $ntp_msg, 0, $ai->{addr} ) == length($ntp_msg) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (send($socket, $ntp_msg, 0, $ai->{"addr"}) == length $ntp_msg) 0 2 2
198 0 $self->{error} = $!;
199 0 $kernel->yield('_dispatch');
200 0 return;
201 }
202 2 $kernel->select_read( $socket, '_get_datagram' );
203 2 ▶ $kernel->delay( '_timeout', ( $self->{timeout} || 60 ), $socket );
Condition100%2/2
$self->{'timeout'} || 60
l!l
11
MC/DC100%1/1
$self->{'timeout'} || 60
$self->{'timeout'}
Truth table100%2/2
A
A= $self->{'timeout'}
Aresult
11
00
204 2 return;
205 }
206  
207 1 ▶sub _timeout {
Subroutine _timeout: called CC=1 SCAR=0.0
208 1 my ($kernel,$self,$socket) = @_[KERNEL,OBJECT,ARG0];
209 1 $kernel->select_read( $socket );
210 1 $self->{error} = 'Socket timeout';
211 1 $kernel->yield('_dispatch');
212 1 return;
213 }
214  
215 1 ▶sub _get_datagram {
Subroutine _get_datagram: called CC=2 SCAR=7.0
216 1 my ($kernel,$self,$socket) = @_[KERNEL,OBJECT,ARG0];
217 1 $kernel->delay( '_timeout' );
218 1 $kernel->select_read( $socket );
219 1 my $remote_address = recv( $socket, my $data = '', 960, 0 );
220 1 ▶ unless ( defined $remote_address ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $remote_address) 0 1 1
221 0 $self->{error} = $!;
222 0 $kernel->yield('_dispatch');
223 0 return;
224 }
225 1 my $client_localtime = $self->{client_localtime};
226 1 my $client_recvtime = time;
227 1 my %tmp_pkt;
228 my %packet;
229 1 my @ntp_fields = qw/byte1 stratum poll precision/;
230 1 push @ntp_fields, qw/delay delay_fb disp disp_fb ident/;
231 1 push @ntp_fields, qw/ref_time ref_time_fb/;
232 1 push @ntp_fields, qw/org_time org_time_fb/;
233 1 push @ntp_fields, qw/recv_time recv_time_fb/;
234 1 push @ntp_fields, qw/trans_time trans_time_fb/;
235  
236 1 @tmp_pkt{@ntp_fields} =
237 unpack( "a C3 n B16 n B16 H8 N B32 N B32 N B32 N B32", $data );
238  
239 1 $packet{hex_ref_time} = sprintf '%x.%x', $tmp_pkt{ref_time}, substr +( split m!\.!, $bin2frac->($tmp_pkt{ref_time_fb}) )[1], 0, 9;
240 1 $packet{hex_org_time} = sprintf '%x.%x', ( $tmp_pkt{org_time} + NTP_ADJ ), substr +( split m!\.!, $bin2frac->($tmp_pkt{org_time_fb}) )[1], 0, 9;
241 1 $packet{hex_trans_time} = sprintf '%x.%x', $tmp_pkt{trans_time}, substr +( split m!\.!, $bin2frac->($tmp_pkt{trans_time_fb}) )[1], 0, 9;
242  
243 @packet{@ntp_packet_fields} = (
244 (unpack( "C", $tmp_pkt{byte1} & "\xC0" ) >> 6),
245 (unpack( "C", $tmp_pkt{byte1} & "\x38" ) >> 3),
246 (unpack( "C", $tmp_pkt{byte1} & "\x07" )),
247 $tmp_pkt{stratum},
248 (sprintf("%0.4f", $tmp_pkt{poll})),
249 $tmp_pkt{precision} - 255,
250 ($bin2frac->($tmp_pkt{delay_fb})),
251 (sprintf("%0.4f", $tmp_pkt{disp})),
252 $unpack_ip->($tmp_pkt{stratum}, $tmp_pkt{ident}),
253 (($tmp_pkt{ref_time} += $bin2frac->($tmp_pkt{ref_time_fb})) -= NTP_ADJ),
254 (($tmp_pkt{org_time} += $bin2frac->($tmp_pkt{org_time_fb})) ),
255 (($tmp_pkt{recv_time} += $bin2frac->($tmp_pkt{recv_time_fb})) -= NTP_ADJ),
256 1 (($tmp_pkt{trans_time} += $bin2frac->($tmp_pkt{trans_time_fb})) -= NTP_ADJ)
257 );
258  
259 1 my $dest_org = sprintf "%0.5f", (($client_recvtime - $client_localtime));
260 1 my $recv_trans = sprintf "%0.5f", ($packet{'Receive Timestamp'} - $packet{'Transmit Timestamp'});
261 1 my $delay = sprintf "%0.5f", ($dest_org + $recv_trans);
262  
263 1 my $recv_org = $packet{'Receive Timestamp'} - $client_recvtime;
264 1 my $trans_dest = $packet{'Transmit Timestamp'} - $client_localtime;
265 1 my $offset = ($recv_org + $trans_dest) / 2;
266  
267 # Calculated offset / delay
268 1 $packet{Offset} = $offset;
269 1 $packet{Delay} = $delay;
270  
271 1 $self->{response} = \%packet;
272 1 $kernel->yield('_dispatch');
273 1 return;
274 }
275  
276 2 ▶sub _dispatch {
Subroutine _dispatch: called CC=2 SCAR=6.9
277 2 my ($kernel,$self) = @_[KERNEL,OBJECT];
278 2 my $data = { };
279 2 $data->{$_} = $self->{$_} for grep { defined $self->{$_} } qw(response error context host);
280 2 $kernel->post( $self->{sender_id}, $self->{event}, $data );
281 2 $kernel->refcount_decrement( $self->{sender_id}, __PACKAGE__ );
282 2 return;
283 }
284  
285 }
286  
287 3 'What is the time, Mr Wolf?';
288  
289 __END__
↑ Summary