Nodelist.pm

filter: Statementstmt 88.0% 110 / 125 Branchbran 73.8% 62 / 84 Conditioncond 71.6% 48 / 67 MC/DCmcdc 48.3% 15 / 31 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 77.3% 246 / 318 SCAR 46.3
CC
66
Cov
80%
getNode
88.9
new
10.1
BEGIN
1.0
CRAP 102.4
↑ Summary Nodelist/Node.pm »
1 # FTN/Nodelist.pm
2 #
3 # Copyright (c) 2005-2007 Serguei Trouchelle. All rights reserved.
4 #
5 # This program is free software; you can redistribute it and/or modify it
6 # under the same terms as Perl itself.
7  
8 # History:
9 # 1.07 2007/02/28 License added
10 # 1.06 2007/02/04 Quality update (Test::Pod, Test::Pod::Coverage)
11 # 1.05 2005/09/29 Fixed problem with non-existing node
12 # 1.04 2005/09/29 Fixed problem with missing nodelist
13 # 1.03 2005/02/25 Cache problem fixed
14 # 1.02 2005/02/22 Perl license added
15 # Pointlist processing added
16 # Documentation improved
17 # 1.01 2005/02/16 Initial revision
18  
19 =head1 NAME
20  
21 FTN::Nodelist - Process FTN nodelist
22  
23 =head1 SYNOPSIS
24  
25 my $ndl = new FTN::Nodelist(-file => '/fido/var/ndl/nodelist.*');
26 if (my $node = $ndl->getNode('2:550/4077')) {
27 print $node->sysop();
28 } else {
29 warn 'Cannot find node';
30 }
31  
32 =head1 DESCRIPTION
33  
34 C<FTN::Nodelist> contains functions that can be used to process Fidonet
35 Technology Network nodelist and pointlist.
36  
37 =head1 METHODS
38  
39 =head2 new
40  
41 This method creates C<FTN::Nodelist> object.
42 Can get following arguments:
43  
44 Nodelist file path:
45  
46 -file => '/path/to/nodelist'
47  
48 Path can point to definite file (ex.: C<'/var/ndl/nodelist.357'>) or contain
49 wildcard (.*) instead of digital extension. Maximum extension value will be
50 used to find exact nodelist (ex.: C<'/var/ndl/nodelist.*'>)
51  
52 Cacheable status:
53  
54 -cache => 0/1
55  
56 Default is 1. When cacheable status is set to 1, all search results are
57 stored in object cache. It saves resources when searching the same address,
58 but eats memory to store results. Choose appropriate behaviour depending on
59 your tasks.
60  
61 =head2 getNode( $addr )
62  
63 Takes FTN address as argument. Address can be feed in 3D or 4D style
64 (Zone:Net/Node, Zone:Net/Node.Point).
65  
66 If 4D style is specified, point address is searching.
67  
68 Returns C<FTN::Nodelist::Node> object if node can be found in nodelist.
69  
70 See L<FTN::Nodelist::Node> for details how these results can be used.
71  
72 Examples:
73  
74 my $node = $ndl->getNode('2:550/0');
75 my $node = $ndl->getNode('2:2/0');
76 my $node = $ndl->getNode('2:550/4077');
77 my $node = $ndl->getNode('2:550/4077.101');
78  
79 =head1 KNOWN ISSUES
80  
81 When using wildcard in nodelist path, maximum extension is taken into
82 account. It may bring to wrong results when there are many nodelist files
83 and current nodelist has lesser number (for example, C<nodelist.365> and
84 C<nodelist.006>).
85  
86 This issue may be resolved in next versions of C<FTN::Nodelist>.
87  
88 =head1 AUTHORS
89  
90 Serguei Trouchelle E<lt>F<stro@railways.dp.ua>E<gt>
91  
92 =head1 LICENSE
93  
94 This program is free software; you can redistribute it and/or modify it
95 under the same terms as Perl itself.
96  
97 =head1 COPYRIGHT
98  
99 Copyright (c) 2005-2007 Serguei Trouchelle. All rights reserved.
100  
101 =cut
102  
103 package FTN::Nodelist;
104  
105 7 ▶use FTN::Nodelist::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
106 7 ▶use FTN::Address;
Subroutine BEGIN: called CC=1 SCAR=0.0
107  
108 7 require Exporter;
109 7 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
110  
111 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
112 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
113  
114 7 our @EXPORT_OK = qw//;
115 7 our %EXPORT_TAGS = ();
116 7 our @ISA = qw/Exporter/;
117  
118 7 $FTN::Nodelist::VERSION = "1.07";
119  
120 7 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
121 7 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
122  
123 sub new {
124 5 ▶ my $self = shift;
Subroutine new: called CC=8 SCAR=23.1
Pod documented
125 5 my %attr = @_;
126 5 $self = {};
127  
128 5 my $ndlfile = $attr{'-file'};
129  
130 5 ▶ unless (defined $ndlfile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ndlfile) 0 5 5
131 0 @$ = "No `-file' attribute specified, cannot find nodelist";
132 0 return undef;
133 }
134  
135 5 ▶ if ($ndlfile =~ /\.\*$/) { # wildmask used, find corresonding nodelist
Branch: 1 branch
BranchTrueFalseTotal
if ($ndlfile =~ /\.\*$/) 5 0 5
136 5 my $directory = dirname($ndlfile);
137 5 my $filename = basename($ndlfile);
138  
139 5 $filename =~ s/\.\*$/.\\d\\d\\d/;
140  
141 5 ▶ if (opendir(DIR, $directory)) {
Branch: 1 branch
BranchTrueFalseTotal
if (opendir DIR, $directory) { } 5 0 5
142 4 my ($ndl, @rest) = sort {$b cmp $a}
143 5 ▶ grep { /^$filename/ && -f "$directory/$_" }
Branch: 1 branch
BranchTrueFalseTotal
/^$filename/ and -f "$directory/$_" 9 71 80
144 readdir(DIR);
145 5 closedir DIR;
146 5 ▶ if (defined $ndl) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ndl) { } 5 0 5
147 5 $ndlfile = File::Spec->catfile($directory, $ndl);
148 } else {
149 0 $@ = 'Cannot find file ' . $ndlfile;
150 0 return undef;
151 }
152  
153 } else {
154 # failed to read directory
155 0 $@ = 'Cannot read directory ' . $directory;
156 0 return undef;
157 }
158 }
159  
160 5 ▶ unless (-e $ndlfile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $ndlfile) 0 5 5
161 0 $@ = 'Cannot find file ' . $ndlfile;
162 0 return undef;
163 }
164  
165 5 $self->{'__ndlfile'} = $ndlfile;
166  
167 5 $self->{'__cache'} = 1; # cache search results by default
168 # but may be overriden
169 5 ▶ $self->{'__cache'} = $attr{'-cache'} if exists $attr{'-cache'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $attr{"-cache"} 0 5 5
170  
171 5 bless $self ;
172 5 return $self;
173 }
174  
175 sub getNode {
176 27 ▶ my $self = shift;
Subroutine getNode: called CC=59 SCAR=44.9
Pod documented
177 27 my $node = shift;
178  
179 27 ▶ if ($self->{'__cache'} and
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"__cache"} and $self->{"__nodes"}{$node}) 0 27 27
Condition33%1/3
$self->{"__cache"} and $self->{"__nodes"}{$node}
!ll&&!rl&&r
0270
MC/DC0%0/2
$self->{"__cache"} and $self->{"__nodes"}{$node}
$self->{"__cache"} $self->{"__nodes"}{$node}
Truth table33%1/3
A and B
A= $self->{"__cache"}B= $self->{"__nodes"}{$node}
ABresult
0X0
100
111
180 $self->{'__nodes'}->{$node}) {
181 # Return cached copy
182 0 return $self->{'__nodes'}->{$node};
183 }
184  
185 27 ▶ if (my $addr = new FTN::Address($node)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $addr = 'FTN::Address'->new($node)) { } 27 0 27
186 27 ▶ if ($addr->{'p'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addr->{"p"}) 12 15 27
187 # Points are not in nodelist
188 # Process boss/boss-point format pointlists...
189 12 ▶ if (open (F, '<' . $self->{'__ndlfile'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (open F, '<' . $self->{'__ndlfile'}) { } 12 0 12
190 12 my $found;
191  
192 PNT:
193 12 ▶ while(<F>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline F)) 140 4 144
194 140 ▶ next if /^;/; # strip comments
Branch: 1 branch
BranchTrueFalseTotal
if /^;/ 24 116 140
195 116 ▶ if (m!^Boss,(\d+):(\d+)/(\d+)!
Branch: 1 branch
BranchTrueFalseTotal
if (m[^Boss,(\d+):(\d+)/(\d+)] and $1 eq $addr->{"z"} and $2 eq $addr->{"n"} and $3 eq $addr->{"f"}) 8 108 116
Condition77%7/9
m[^Boss,(\d+):(\d+)/(\d+)] and $1 eq $addr->{"z"} and $2 eq $addr->{"n"} and $3 eq $addr->{"f"}
!ll&&!rl&&r
10448
m[^Boss,(\d+):(\d+)/(\d+)] and $1 eq $addr->{"z"} and $2 eq $addr->{"n"}
!ll&&!rl&&r
104012
m[^Boss,(\d+):(\d+)/(\d+)] and $1 eq $addr->{"z"}
!ll&&!rl&&r
104012
MC/DC50%2/4
m[^Boss,(\d+):(\d+)/(\d+)] and $1 eq $addr->{"z"} and $2 eq $addr->{"n"} and $3 eq $addr->{"f"}
m[^Boss,(\d+):(\d+)/(\d+)] $1 eq $addr->{"z"} $2 eq $addr->{"n"} $3 eq $addr->{"f"}
Truth table60%3/5
A and B and C and D
A= m[^Boss,(\d+):(\d+)/(\d+)]B= $1 eq $addr->{"z"}C= $2 eq $addr->{"n"}D= $3 eq $addr->{"f"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
196 and $1 eq $addr->{'z'}
197 and $2 eq $addr->{'n'}
198 and $3 eq $addr->{'f'} ) {
199 8 ▶ while(<F>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline F)) 100 0 100
200 100 ▶ next if /^;/; # strip comments
Branch: 1 branch
BranchTrueFalseTotal
if /^;/ 0 100 100
201 100 ▶ if (((/^,(\d+),/) or
Branch: 1 branch
BranchTrueFalseTotal
if (/^,(\d+),/ || /^Point,(\d+),/ || 0 and $addr->{"p"} == $1) 8 92 100
Condition62%5/8
/^,(\d+),/ || /^Point,(\d+),/ || 0 and $addr->{"p"} == $1
!ll&&!rl&&r
0928
/^,(\d+),/ or /^Point,(\d+),/ or 0
l!l
1000
/^,(\d+),/ or /^Point,(\d+),/
l!l&&r!l&&!r
50500
MC/DC33%1/3
/^,(\d+),/ || /^Point,(\d+),/ || 0 and $addr->{"p"} == $1
/^,(\d+),/ /^Point,(\d+),/ $addr->{"p"} == $1
Truth table80%4/5
A or B and C
A= /^,(\d+),/B= /^Point,(\d+),/C= $addr->{"p"} == $1
ABCresult
00X0
1X00
0100
1X11
0111
202 (/^Point,(\d+),/) or
203 0
204 ) and ($addr->{'p'} == $1)) {
205 8 $found = $_;
206 8 last PNT;
207 }
208  
209 92 ▶ last PNT if /^Boss/; # Not found
Branch: 1 branch
BranchTrueFalseTotal
if /^Boss/ 0 92 92
210 }
211 }
212 }
213  
214 12 close(F);
215 12 ▶ if ($found) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found) { } 8 4 12
216 8 chomp $found;
217 8 my $node = new FTN::Nodelist::Node($addr, $found);
218 # cache result if needed
219 8 ▶ $self->{'__nodes'}->{$node->address()} = $node if $self->{'__cache'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"__cache"} 8 0 8
220 8 return $node;
221 } else {
222 # We will search point-format in nodelist
223 }
224 } else {
225 0 $@ = 'Cannot read nodelist ' . $@;
226 0 return undef;
227 }
228 }
229  
230 # Process nodelist
231  
232 19 ▶ if (open (F, '<' . $self->{'__ndlfile'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (open F, '<' . $self->{'__ndlfile'}) { } 19 0 19
233 19 my $found;
234  
235 NDL:
236  
237 19 ▶ while(<F>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline F)) 76 0 76
238 76 ▶ next if /^;/; # strip comments
Branch: 1 branch
BranchTrueFalseTotal
if /^;/ 57 19 76
239 19 ▶ if ((/^Zone,(\d+),/) and ($addr->{'z'} == $1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^Zone,(\d+),/ and $addr->{"z"} == $1) 19 0 19
Condition33%1/3
/^Zone,(\d+),/ and $addr->{"z"} == $1
!ll&&!rl&&r
0019
MC/DC0%0/2
/^Zone,(\d+),/ and $addr->{"z"} == $1
/^Zone,(\d+),/ $addr->{"z"} == $1
Truth table33%1/3
A and B
A= /^Zone,(\d+),/B= $addr->{"z"} == $1
ABresult
0X0
100
111
240 19 ▶ if ($addr->{'z'} eq $addr->{'n'} and $addr->{'f'} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addr->{"z"} eq $addr->{"n"} and $addr->{"f"} == 0) 1 18 19
Condition100%3/3
$addr->{"z"} eq $addr->{"n"} and $addr->{"f"} == 0
!ll&&!rl&&r
1351
MC/DC100%2/2
$addr->{"z"} eq $addr->{"n"} and $addr->{"f"} == 0
$addr->{"z"} eq $addr->{"n"} $addr->{"f"} == 0
Truth table100%3/3
A and B
A= $addr->{"z"} eq $addr->{"n"}B= $addr->{"f"} == 0
ABresult
0X0
100
111
241 1 $found = $_;
242 1 last NDL;
243 }
244 18 my $reg;
245 18 ▶ while(<F>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline F)) 122 0 122
246 122 ▶ next if /^;/; # strip comments
Branch: 1 branch
BranchTrueFalseTotal
if /^;/ 21 101 122
247 101 ▶ $reg = 1 if /^Region,/;
Branch: 1 branch
BranchTrueFalseTotal
if /^Region,/ 13 88 101
248 101 ▶ if ((/^Region,(\d+),/ or
Branch: 2 branches
BranchTrueFalseTotal
if (/^Region,(\d+),/ || /^Host,(\d+),/ and $addr->{'n'} == $1) { } 13 88 101
elsif (not $reg and $addr->{'z'} eq $addr->{'n'} and /,(\d+)/ and $addr->{'f'} eq $1) { } 5 156 161
Condition100%6/6
/^Region,(\d+),/ || /^Host,(\d+),/ and $addr->{"n"} == $1
!ll&&!rl&&r
80813
/^Region,(\d+),/ or /^Host,(\d+),/
l!l&&r!l&&!r
13880
MC/DC100%3/3
/^Region,(\d+),/ || /^Host,(\d+),/ and $addr->{"n"} == $1
/^Region,(\d+),/ /^Host,(\d+),/ $addr->{"n"} == $1
Truth table80%4/5
A or B and C
A= /^Region,(\d+),/B= /^Host,(\d+),/C= $addr->{"n"} == $1
ABCresult
00X0
1X00
0100
1X11
0111
249 /^Host,(\d+),/
250 ) and ($addr->{'n'} == $1)) {
251  
252 13 ▶ if ($addr->{'f'} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addr->{"f"} == 0) 2 11 13
253 2 $found = $_;
254 2 last NDL;
255 }
256  
257 11 ▶ while(<F>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline F)) 24 0 24
258 24 ▶ next if /^;/; # strip comments
Branch: 1 branch
BranchTrueFalseTotal
if /^;/ 4 20 24
259 20 ▶ last NDL if /^Zone,/ or
Branch: 1 branch
BranchTrueFalseTotal
if /^Zone,/ or /^Region,/ or /^Host,/ 4 16 20
Condition50%3/6
/^Zone,/ or /^Region,/ or /^Host,/
l!l&&r!l&&!r
0416
/^Zone,/ or /^Region,/
l!l&&r!l&&!r
0020
MC/DC33%1/3
/^Zone,/ or /^Region,/ or /^Host,/
/^Zone,/ /^Region,/ /^Host,/
Truth table50%2/4
A or B or C
A= /^Zone,/B= /^Region,/C= /^Host,/
ABCresult
1XX1
01X1
0011
0000
260 /^Region,/ or
261 /^Host,/;
262 16 ▶ if (((/^,(\d+),/) or
Branch: 1 branch
BranchTrueFalseTotal
if (/^,(\d+),/ || /^Hub,(\d+),/ || /^Pvt,(\d+),/ || /^Hold,(\d+),/ || /^Down,(\d+),/ || 0 and $addr->{"f"} == $1) 7 9 16
Condition70%12/17
/^,(\d+),/ || /^Hub,(\d+),/ || /^Pvt,(\d+),/ || /^Hold,(\d+),/ || /^Down,(\d+),/ || 0 and $addr->{"f"} == $1
!ll&&!rl&&r
457
/^,(\d+),/ or /^Hub,(\d+),/ or /^Pvt,(\d+),/ or /^Hold,(\d+),/ or /^Down,(\d+),/ or 0
l!l
124
/^,(\d+),/ or /^Hub,(\d+),/ or /^Pvt,(\d+),/ or /^Hold,(\d+),/ or /^Down,(\d+),/
l!l&&r!l&&!r
1204
/^,(\d+),/ or /^Hub,(\d+),/ or /^Pvt,(\d+),/ or /^Hold,(\d+),/
l!l&&r!l&&!r
1204
/^,(\d+),/ or /^Hub,(\d+),/ or /^Pvt,(\d+),/
l!l&&r!l&&!r
0124
/^,(\d+),/ or /^Hub,(\d+),/
l!l&&r!l&&!r
0016
MC/DC33%2/6
/^,(\d+),/ || /^Hub,(\d+),/ || /^Pvt,(\d+),/ || /^Hold,(\d+),/ || /^Down,(\d+),/ || 0 and $addr->{"f"} == $1
/^,(\d+),/ /^Hub,(\d+),/ /^Pvt,(\d+),/ /^Hold,(\d+),/ /^Down,(\d+),/ $addr->{"f"} == $1
Truth table27%3/11
A or B or C or D or E and F
A= /^,(\d+),/B= /^Hub,(\d+),/C= /^Pvt,(\d+),/D= /^Hold,(\d+),/E= /^Down,(\d+),/F= $addr->{"f"} == $1
ABCDEFresult
00000X0
1XXXX00
01XXX00
001XX00
0001X00
0000100
1XXXX11
01XXX11
001XX11
0001X11
0000111
263 (/^Hub,(\d+),/) or
264 (/^Pvt,(\d+),/) or
265 (/^Hold,(\d+),/) or
266 (/^Down,(\d+),/) or
267 0
268 ) and ($addr->{'f'} == $1)) {
269 7 $found = $_;
270 7 last NDL;
271 }
272 }
273 ▶ } elsif (not $reg and $addr->{'z'} eq $addr->{'n'}
Condition88%8/9
not $reg and $addr->{"z"} eq $addr->{"n"} and /,(\d+)/ and $addr->{"f"} eq $1
!ll&&!rl&&r
73105
not $reg and $addr->{"z"} eq $addr->{"n"} and /,(\d+)/
!ll&&!rl&&r
73015
not $reg and $addr->{"z"} eq $addr->{"n"}
!ll&&!rl&&r
86515
MC/DC75%3/4
not $reg and $addr->{"z"} eq $addr->{"n"} and /,(\d+)/ and $addr->{"f"} eq $1
not $reg $addr->{"z"} eq $addr->{"n"} /,(\d+)/ $addr->{"f"} eq $1
Truth table80%4/5
not(A) and B and C and D
A= not $regB= $addr->{"z"} eq $addr->{"n"}C= /,(\d+)/D= $addr->{"f"} eq $1
ABCDresult
0XXX0
10XX0
110X0
11100
11111
274 and /,(\d+)/ and $addr->{'f'} eq $1) {
275 5 $found = $_;
276 5 last NDL;
277 }
278 }
279 }
280 }
281  
282 19 ▶ if ($addr->{'p'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addr->{"p"}) 4 15 19
283 # Search for point (point-format)
284 4 undef $found; # Don't need boss-node
285 4 ▶ while(<F>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline F)) 8 0 8
286 8 ▶ next if /^;/; # strip comments
Branch: 1 branch
BranchTrueFalseTotal
if /^;/ 0 8 8
287 8 ▶ last if /^((Zone)|(Region)|(Host)|(Hub)|(Pvt)|(Hold)|(Down))?,/;
Branch: 1 branch
BranchTrueFalseTotal
if /^((Zone)|(Region)|(Host)|(Hub)|(Pvt)|(Hold)|(Down))?,/ 0 8 8
288 # Next node found
289 8 ▶ if (/^Point,(\d+),/
Branch: 1 branch
BranchTrueFalseTotal
if (/^Point,(\d+),/ and $1 == $addr->{"p"}) 4 4 8
Condition66%2/3
/^Point,(\d+),/ and $1 == $addr->{"p"}
!ll&&!rl&&r
044
MC/DC50%1/2
/^Point,(\d+),/ and $1 == $addr->{"p"}
/^Point,(\d+),/ $1 == $addr->{"p"}
Truth table66%2/3
A and B
A= /^Point,(\d+),/B= $1 == $addr->{"p"}
ABresult
0X0
100
111
290 and $1 == $addr->{'p'}) {
291 4 $found = $_;
292 4 last;
293 }
294 }
295 }
296  
297 19 close(F);
298 19 ▶ if ($found) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found) { } 15 4 19
299 15 chomp $found;
300 15 my $node = new FTN::Nodelist::Node($addr, $found);
301 # cache result if needed
302 15 ▶ $self->{'__nodes'}->{$node->address()} = $node if $self->{'__cache'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"__cache"} 15 0 15
303 15 return $node;
304 } else {
305 4 return undef; # Not found
306 }
307 } else {
308 0 $@ = 'Cannot read nodelist ' . $@;
309 0 return undef;
310 }
311 } else {
312 0 $@ = 'Invalid address : ' . $node;
313 0 return undef;
314 }
315 }
316  
317 7 1;
↑ Summary Nodelist/Node.pm »