IMP/Base.pm

filter: Statementstmt 47.6% 71 / 149 Branchbran 28.3% 17 / 60 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 65.0% 13 / 20 Podpod 92.8% 13 / 14 totaltotal 42.5% 114 / 268 CC 40 SCAR 37.7
CC
40
Cov
39%
get_interface
132.0
busy
56.0
run_callback
46.7
CRAP 43.2
« IMP/Example/LogServerCertificate.pm ↑ Summary IMP.pm »
1 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Net::IMP::Base;
5 7 ▶use Net::IMP qw(:DEFAULT IMP_PASS_IF_BUSY);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 use fields (
8 7 'factory_args', # arguments given to new_factory
9 'meta', # hash with meta data given to new_analyzer
10 'analyzer_cb', # callback, set from new_analyzer or with set_callback
11 'analyzer_rv', # collected results for polling or callback, set
12 # from add_results
13 'ignore_rv', # hash with return values like IMP_PAUSE or
14 # IMP_REPLACE_LATER which are unsupported by the data
15 # provider and can be ignored
16 'busy', # if data provider is busy
17 7 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 7 ▶use Net::IMP::Debug;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21  
22 ############################################################################
23 # API plugin methods
24 ############################################################################
25  
26 # creates new factory
27 sub new_factory {
28 63 ▶ my ($class,%args) = @_;
Subroutine new_factory: called CC=1 SCAR=0.0
Pod documented
29 63 my Net::IMP::Base $factory = fields::new($class);
30 63 $factory->{factory_args} = \%args;
31 63 return $factory;
32 }
33  
34 # make string from hash config, using URL encoding to escape special chars
35 sub cfg2str {
36 3 ▶ my (undef,%cfg) = @_;
Subroutine cfg2str: called CC=2 SCAR=6.9
Pod documented
37 return join('&', map {
38 3 my $v = $cfg{$_};
39 # only encode really necessary stuff
40 19 s{([=&%\x00-\x20\x7f-\xff])}{ sprintf("%%%02X",ord($1)) }eg; # key
41 19 ▶ if ( defined $v ) { # value
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v) { } 18 1 19
42 18 $v =~s{([&%\x00-\x20\x7f-\xff])}{ sprintf("%%%02X",ord($1)) }eg;
43 18 "$_=$v"
44 } else {
45 1 "$_"
46 }
47 } sort keys %cfg);
48 }
49  
50 # make has config from string created by cfg2str
51 sub str2cfg {
52 9 ▶ my (undef,$str) = @_;
Subroutine str2cfg: called CC=4 SCAR=14.0
Pod documented
53 9 my %cfg;
54 9 for my $kv (split('&',$str)) {
55 57 my ($k,$v) = $kv =~m{^([^=]+)(?:=(.*))?};
56 57 $k =~s{%([\dA-F][\dA-F])}{ chr(hex($1)) }ieg;
57 57 ▶ exists $cfg{$k} and croak "duplicate definition for key $k";
Branch: 1 branch
BranchTrueFalseTotal
exists $cfg{$k} and croak("duplicate definition for key $k") 0 57 57
58 57 ▶ $v =~s{%([\dA-F][\dA-F])}{ chr(hex($1)) }ieg if defined $v;
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 54 3 57
59 57 $cfg{$k} = $v;
60 }
61 9 return %cfg;
62 }
63  
64 # validate config, return list of errors
65 sub validate_cfg {
66 46 ▶ my (undef,%cfg) = @_;
Subroutine validate_cfg: called CC=2 SCAR=7.1
Pod documented
67 46 delete $cfg{eventlib}; # accepted everywhere
68 46 ▶ return %cfg ? "unexpected config keys ".join(', ',keys %cfg) : ();
Branch: 1 branch
BranchTrueFalseTotal
%cfg ? : 0 46 46
69 }
70  
71 ############################################################################
72 # API factory methods
73 ############################################################################
74  
75 # create new analyzer
76 sub new_analyzer {
77 74 ▶ my Net::IMP::Base $factory = shift;
Subroutine new_analyzer: called CC=2 SCAR=6.9
Pod documented
78 74 my %args = @_;
79 74 my $cb = delete $args{cb};
80  
81 74 my $analyzer = fields::new(ref($factory));
82 74 %$analyzer = (
83 %$factory, # common properties of all analyzers
84 %args, # properties of this analyzer
85 analyzer_rv => [], # reset queued return values
86 busy => undef, # busy per dir
87 );
88 74 ▶ $analyzer->set_callback(@$cb) if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 41 33 74
89 74 return $analyzer;
90 }
91  
92 # get available interfaces
93 # returns factory for the given interface
94 # might be a new one or same as called on
95 sub set_interface {
96 0 ▶ my Net::IMP::Base $factory = shift;
Subroutine set_interface: not called CC=4 SCAR=30.0
Pod documented
97 0 my $want = shift;
98 0 ▶ my ($if) = $factory->get_interface($want) or return;
Branch: 1 branch
BranchTrueFalseTotal
my($if) = $factory->get_interface($want) or return 0 0 0
99  
100 0 my %ignore = map { $_+0 => $_ }
101 ( IMP_PAUSE, IMP_CONTINUE, IMP_REPLACE_LATER );
102 0 delete @ignore{ map { $_+0 } @{$if->[1]}};
103 0 ▶ $factory->{ignore_rv} = %ignore ? \%ignore : undef;
Branch: 1 branch
BranchTrueFalseTotal
%ignore ? : 0 0 0
104  
105 0 ▶ if ( my $adaptor = $if->[2] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $adaptor = $if->[2]) { } 0 0 0
106 # use adaptor
107 0 return $adaptor->new_factory(factory => $factory)
108 } else {
109 0 return $factory
110 }
111 }
112  
113 # returns list of available [ if, adaptor_class ], restricted by given @if
114 0 ▶sub INTERFACE { die "needs to be implemented" }
Subroutine INTERFACE: not called CC=1 SCAR=6.9
Pod undocumented
115 sub get_interface {
116 0 ▶ my Net::IMP::Base $factory = shift;
Subroutine get_interface: not called CC=11 SCAR=48.8
Pod documented
117 0 my @local = $factory->INTERFACE;
118  
119 # return all supported interfaces if none are given
120 0 ▶ return @local if ! @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
121  
122 # find matching interfaces
123 0 my @match;
124 0 for my $if (@_) {
125 0 my ($in,$out) = @$if;
126 0 for my $lif (@local) {
127 0 my ($lin,$lout,$adaptor) = @$lif;
128 0 ▶ if ( $lin and $lin != $in ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lin and $lin != $in) 0 0 0
Condition0%0/3
$lin and $lin != $in
!ll&&!rl&&r
000
MC/DC0%0/2
$lin and $lin != $in
$lin $lin != $in
Truth table0%0/3
A and B
A= $linB= $lin != $in
ABresult
0X0
100
111
129 # no match data type/proto
130 0 debug("data type mismatch: want $in have $lin");
131 0 next;
132 }
133  
134 0 ▶ if ( ! $out || ! @$out ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $out && @$out) { } 0 0 0
Condition0%0/3
$out && @$out
!ll&&!rl&&r
000
MC/DC0%0/2
$out && @$out
$out @$out
Truth table0%0/3
A && B
A= $outB= @$out
ABresult
0X0
100
111
135 # caller will accept any return types
136 } else {
137 # any local return types from not in out?
138 0 my %lout = map { $_ => 1 } ( @$lout, IMP_FATAL );
139 delete @lout{
140 0 @$out,
141 # these don't need to be supported
142 (IMP_PAUSE, IMP_CONTINUE, IMP_REPLACE_LATER)
143 };
144 0 ▶ if ( %lout ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%lout) 0 0 0
145 # caller does not support all return types
146 0 debug("no support for return types ".join(' ',keys %lout));
147 0 next;
148 }
149 }
150  
151 0 ▶ if ( $adaptor ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($adaptor) 0 0 0
152 # make sure adaptor class exists
153 0 ▶ if ( ! eval "require $adaptor" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval "require $adaptor") 0 0 0
154 0 debug("failed to load $adaptor: $@");
155 0 next;
156 }
157 }
158  
159 # matches
160 0 push @match, [ $in,$out,$adaptor ];
161 0 last;
162 }
163 }
164  
165 0 return @match;
166 }
167  
168 ############################################################################
169 # API analyzer methods
170 ############################################################################
171  
172 # set callback
173 sub set_callback {
174 74 ▶ my Net::IMP::Base $analyzer = shift;
Subroutine set_callback: called CC=3 SCAR=11.4
Pod documented
175 74 my ($sub,@args) = @_;
176 74 ▶ $analyzer->{analyzer_cb} = $sub ? [ $sub,@args ]:undef;
Branch: 1 branch
BranchTrueFalseTotal
$sub ? : 74 0 74
177 74 ▶ $analyzer->run_callback if $analyzer->{analyzer_rv};
Branch: 1 branch
BranchTrueFalseTotal
if $analyzer->{"analyzer_rv"} 74 0 74
178 }
179  
180 # return queued results
181 sub poll_results {
182 0 ▶ my Net::IMP::Base $analyzer = shift;
Subroutine poll_results: not called CC=1 SCAR=6.9
Pod documented
183 0 my $rv = $analyzer->{analyzer_rv};
184 0 $analyzer->{analyzer_rv} = [];
185 0 return @$rv;
186 }
187  
188 0 ▶sub data { die "needs to be implemented" }
Subroutine data: not called CC=1 SCAR=6.9
Pod documented
189  
190 sub busy {
191 0 ▶ my Net::IMP::Base $analyzer = shift;
Subroutine busy: not called CC=7 SCAR=40.3
Pod documented
192 0 my ($dir,$busy) = @_;
193 0 ▶ if ( $busy ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($busy) { } 0 0 0
elsif (not $analyzer->{'busy'} && $analyzer->{'busy'}[$dir]) { } 0 0 0
194 return if $analyzer->{busy}
195 0 ▶ && $analyzer->{busy}[$dir]; # no change - stay busy
Branch: 1 branch
BranchTrueFalseTotal
if $analyzer->{"busy"} and $analyzer->{"busy"}[$dir] 0 0 0
Condition0%0/3
$analyzer->{"busy"} and $analyzer->{"busy"}[$dir]
!ll&&!rl&&r
000
MC/DC0%0/2
$analyzer->{"busy"} and $analyzer->{"busy"}[$dir]
$analyzer->{"busy"} $analyzer->{"busy"}[$dir]
Truth table0%0/3
A and B
A= $analyzer->{"busy"}B= $analyzer->{"busy"}[$dir]
ABresult
0X0
100
111
196 0 $analyzer->{busy}[$dir] = 1; # unbusy -> busy
197 ▶ } elsif ( ! $analyzer->{busy}
Condition0%0/3
$analyzer->{'busy'} && $analyzer->{'busy'}[$dir]
!ll&&!rl&&r
000
MC/DC0%0/2
$analyzer->{'busy'} && $analyzer->{'busy'}[$dir]
$analyzer->{'busy'} $analyzer->{'busy'}[$dir]
Truth table0%0/3
A && B
A= $analyzer->{'busy'}B= $analyzer->{'busy'}[$dir]
ABresult
0X0
100
111
198 || ! $analyzer->{busy}[$dir] ) {
199 0 return; # no change - stay not busy
200 } else {
201 # set to no busy on $dir, maybe no busy at all
202 0 $analyzer->{busy}[$dir] = 0; # busy -> unbusy
203 0 ▶ if ( ! grep { $_ } @{$analyzer->{busy}} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_;} @{$$analyzer{"busy"};}) 0 0 0
204 # all dir are not busy anymore
205 0 $analyzer->{busy} = undef;
206 }
207 }
208  
209 # run callback, either for important stuff on busy or for
210 # all stuff if not busy
211 0 $analyzer->run_callback;
212 }
213  
214  
215 ############################################################################
216 # internal analyzer methods
217 ############################################################################
218  
219 sub add_results {
220 0 ▶ my Net::IMP::Base $analyzer = shift;
Subroutine add_results: not called CC=2 SCAR=17.9
Pod documented
221 0 ▶ if ( my $ignore = $analyzer->{ignore_rv} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ignore = $analyzer->{'ignore_rv'}) { } 0 0 0
222 0 push @{$analyzer->{analyzer_rv}}, grep { ! $ignore->{$_->[0]+0} } @_;
223 } else {
224 0 push @{$analyzer->{analyzer_rv}},@_;
225 }
226 }
227  
228 {
229 7 my %important = do {
230 7 my $p = IMP_PASS_IF_BUSY;
231 7 map { $p->[$_]+0 => $_+1 } (0..$#$p)
232 };
233  
234 sub run_callback {
235 326 ▶ my Net::IMP::Base $analyzer = shift;
Subroutine run_callback: called CC=12 SCAR=38.4
Pod documented
236 326 my $rv = $analyzer->{analyzer_rv}; # get collected results
237 326 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 252 74 326
238 # add more results
239 252 ▶ if ( my $ignore = $analyzer->{ignore_rv} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ignore = $analyzer->{'ignore_rv'}) { } 0 252 252
240 0 push @$rv, grep { ! $ignore->{$_->[0]+0} } @_;
241 } else {
242 252 push @$rv,@_;
243 }
244 }
245 326 ▶ if ( my $cb = $analyzer->{analyzer_cb} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $analyzer->{"analyzer_cb"}) 326 0 326
246 326 my ($sub,@args) = @$cb;
247 326 ▶ if ( my $busy = $analyzer->{busy} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $busy = $analyzer->{'busy'}) { } 0 326 326
elsif (@$rv) { } 252 74 326
248 # at least one dir is busy
249 0 my (@important,@nobusy,@busy);
250 0 for( @$rv ) {
251 0 ▶ if ( my $lvl = $important{ $_->[0]+0 } ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $lvl = $important{$_->[0] + 0}) { } 0 0 0
elsif ($$busy[$_->[1]]) { } 0 0 0
252 0 push @important,[ $_, $lvl ]
253 } elsif ( $busy->[$_->[1]] ) {
254 0 push @busy,$_
255 } else {
256 0 push @nobusy,$_
257 }
258 }
259 # sort by importance
260 @important =
261 0 ▶ map { $_->[0] } sort { $a->[1] <=> $b->[1] } @important
Branch: 1 branch
BranchTrueFalseTotal
if @important 0 0 0
262 if @important;
263 0 ▶ if (@nobusy || @important) {
Branch: 1 branch
BranchTrueFalseTotal
if (@nobusy or @important) { } 0 0 0
Condition0%0/3
@nobusy or @important
l!l&&r!l&&!r
000
MC/DC0%0/2
@nobusy or @important
@nobusy @important
Truth table0%0/3
A or B
A= @nobusyB= @important
ABresult
1X1
011
000
264 0 $analyzer->{analyzer_rv} = \@busy;
265 0 $sub->(@args,@important,@nobusy);
266 } else {
267 # nothing important enough to call back
268 }
269 } elsif (@$rv) {
270 252 $analyzer->{analyzer_rv} = []; # reset
271 252 $sub->(@args,@$rv); # and call back
272 }
273 }
274 }
275 }
276  
277  
278 7 1;
279 __END__
« IMP/Example/LogServerCertificate.pm ↑ Summary IMP.pm »