PurePerl/NodeID.pm

filter: Statementstmt 94.1% 64 / 68 Branchbran 55.5% 10 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 6 totaltotal 83.1% 89 / 107 SCAR 23.3
CC
10
Cov
86%
pseudo_node_id
6.4
_pseudo_node_source
4.0
new
2.1
CRAP 10.3
« PurePerl/RNG/MRMTPP.pm ↑ Summary PurePerl/RNG.pm »
1 package UUID::Generator::PurePerl::NodeID;
2  
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use POSIX ;#qw( uname );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Digest;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use UUID::Generator::PurePerl::RNG;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use UUID::Generator::PurePerl::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 10 our $USE_RANDOM_FACTOR_FOR_PSEUDO_NODE = 1;
14  
15 10 my $singleton;
16 sub singleton {
17 3 ▶ my $class = shift;
Subroutine singleton: called CC=2 SCAR=7.0
Pod undocumented
18  
19 3 ▶ if (! defined $singleton) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $singleton) 3 0 3
20 3 $singleton = $class->new();
21 }
22  
23 3 return $singleton;
24 }
25  
26 sub new {
27 4 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=7.4
Pod undocumented
28 4 my $self = bless {}, $class;
29  
30 4 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 4 4
31 0 $self->{use_rand} = shift;
32 }
33  
34 4 return $self;
35 }
36  
37 sub node_id {
38 14 ▶ my $self = shift;
Subroutine node_id: called CC=1 SCAR=0.0
Pod undocumented
39  
40 14 return $self->pseudo_node_id(0);
41 }
42  
43 sub physical_node_id {
44 1 ▶ my $self = shift;
Subroutine physical_node_id: called CC=1 SCAR=0.0
Pod undocumented
45  
46 1 return;
47 }
48  
49 sub pseudo_node_id {
50 26 ▶ my $self = shift;
Subroutine pseudo_node_id: called CC=4 SCAR=18.6
Pod undocumented
51  
52 26 my $use_rand = shift;
53 26 ▶ if (! defined $use_rand) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $use_rand) 0 26 26
54 0 ▶ $use_rand = $self->{use_rand} if ref $self;
Branch: 1 branch
BranchTrueFalseTotal
if ref $self 0 0 0
55 0 ▶ if (! defined $use_rand) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $use_rand) 0 0 0
56 0 $use_rand = $USE_RANDOM_FACTOR_FOR_PSEUDO_NODE;
57 }
58 }
59  
60 26 my $id = digest_as_octets(6, $self->_pseudo_node_source($use_rand));
61  
62 # set MSB
63 26 substr $id, 0, 1, chr(ord(substr($id, 0, 1)) | 0x80);
64  
65 26 return $id;
66 }
67  
68 sub random_node_id {
69 6 ▶ my $self = shift;
Subroutine random_node_id: called CC=2 SCAR=6.9
Pod undocumented
70  
71 6 ▶ if (! defined $self->{rng}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"rng"}) 1 5 6
72 1 my $seed = digest_as_32bit($self->_pseudo_node_source(1));
73  
74 1 my $rng = UUID::Generator::PurePerl::RNG->new($seed);
75  
76 1 $self->{rng} = $rng;
77 }
78  
79 6 my $r1 = $self->{rng}->rand_32bit;
80 6 my $r2 = $self->{rng}->rand_32bit;
81  
82 6 my $hi = ($r1 >> 8) ^ ($r2 & 0xff);
83 6 my $lo = ($r2 >> 8) ^ ($r1 & 0xff);
84  
85 # set MSB
86 6 $hi |= 0x80;
87  
88 6 my $id = substr pack('V', $hi), 0, 3;
89 6 $id .= substr pack('V', $lo), 0, 3;
90  
91 ## set MSB
92 #substr $id, 0, 1, chr(ord(substr($r, 0, 1)) | 0x80);
93  
94 6 return $id;
95 }
96  
97 sub _pseudo_node_source {
98 27 ▶ my ($class, $use_rand) = @_;
Subroutine _pseudo_node_source: called CC=4 SCAR=13.9
99  
100 27 my @r;
101  
102 27 ▶ push @r, q{} . Time::HiRes::time() if $use_rand;
Branch: 1 branch
BranchTrueFalseTotal
if $use_rand 7 20 27
103 27 ▶ push @r, q{:} . $$ if $use_rand;
Branch: 1 branch
BranchTrueFalseTotal
if $use_rand 7 20 27
104 27 push @r, join(q{:}, POSIX::uname());
105  
106 27 ▶ return wantarray ? @r : join q{}, @r;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 27 0 27
107 }
108  
109 10 1;
110 __END__
« PurePerl/RNG/MRMTPP.pm ↑ Summary PurePerl/RNG.pm »