lib/Net/SinFP3/Output/OsVersionFamily.pm untested

filter: Statementstmt 0.0% 0 / 50 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 75 CC 6 SCAR 32.6
CC
6
Cov
0%
run
42.0
take
2.0
BEGIN
2.0
CRAP 26.0
1 #
2 # $Id: OsVersionFamily.pm,v 008243d3e89a 2018/07/21 14:54:07 gomor $
3 #
4 package Net::SinFP3::Output::OsVersionFamily;
5 use strict;
6 use warnings;
7  
8 use base qw(Net::SinFP3::Output);
9 __PACKAGE__->cgBuildIndices;
10  
11 sub take {
12 return [
13 'Net::SinFP3::Result::Active',
14 'Net::SinFP3::Result::Unknown',
15 'Net::SinFP3::Result::PortError',
16 ];
17 }
18  
19 sub run {
20 my $self = shift->SUPER::run(@_) or return;
21  
22 my $global = $self->global;
23 my $log = $global->log;
24 my @results = $global->result;
25  
26 my $buf = '';
27 my $first = 1;
28 my %osVersionFamily = ();
29 for my $r (@results) {
30 my $ref = ref($r);
31 if ($ref =~ /^Net::SinFP3::Result::Unknown$/) {
32 $buf .= $r->printSignature."\n";
33 $buf .= $r->print."\n";
34 print $buf;
35 return 1;
36 }
37 elsif ($ref =~ /^Net::SinFP3::Result::PortError$/) {
38 $buf .= $r->printSignature."\n";
39 $buf .= $r->print."\n";
40 print $buf;
41 return 1;
42 }
43 elsif ($ref =~ /^Net::SinFP3::Result::Active$/) {
44 if ($first) {
45 $buf .= $r->printSignature."\n";
46 $first = 0;
47 }
48 (exists($osVersionFamily{$r->osVersionFamily}) &&
49 $r->matchScore >= $osVersionFamily{$r->osVersionFamily}->matchScore)
50 ? ($osVersionFamily{$r->osVersionFamily} = $r)
51 : ($osVersionFamily{$r->osVersionFamily} = $r);
52 }
53 else {
54 $log->warning("Don't know what to do with this result object ".
55 "with type: [$ref]");
56 return '';
57 }
58 }
59  
60 # Sort by score
61 my %byScore = ();
62 for my $k (keys %osVersionFamily) {
63 my $r = $osVersionFamily{$k};
64 push @{$byScore{$r->matchScore}}, $r;
65 }
66  
67 for my $k (sort { $b <=> $a } keys %byScore) {
68 my $list = $byScore{$k};
69 for my $r (@$list) {
70 my $str = $r->ipVersion.': [score:'.$r->matchScore.']: '.$r->matchMask.
71 '/'.$r->matchType.': '.$r->os.': '.$r->osVersionFamily."\n";
72 $buf .= $str;
73 }
74 }
75  
76 print $buf;
77  
78 return 1;
79 }
80  
81 1;
82  
83 __END__