Lim/RPC/Transports.pm

filter: Statementstmt 83.3% 65 / 78 Branchbran 38.2% 13 / 34 Conditioncond 44.4% 4 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 100% 12 / 12 Podpod 100% 3 / 3 totaltotal 69.0% 98 / 142 SCAR 37.1
CC
23
Cov
68%
load
33.7
transport
9.2
_new
3.1
CRAP 40.7
« Lim/RPC/Protocols.pm ↑ Summary Lim/RPC/TLS.pm »
1 package Lim::RPC::Transports;
2  
3 10 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 ▶use Log::Log4perl ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Module::Find qw(findsubmod);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 10 ▶use Lim ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =encoding utf8
13  
14 =head1 NAME
15  
16 Lim::RPC::Transports - Lim's RPC transport loader and container
17  
18 =head1 VERSION
19  
20 See L<Lim> for version.
21  
22 =cut
23  
24 10 our $VERSION = $Lim::VERSION;
25 10 our $INSTANCE;
26  
27 =head1 SYNOPSIS
28  
29 use Lim::RPC::Transports;
30 $transport = Lim::RPC::Transports->instance->transport('name');
31  
32 =head1 METHODS
33  
34 =over 4
35  
36 =cut
37  
38 sub _new {
39 4 ▶ my $this = shift;
Subroutine _new: called CC=3 SCAR=11.3
40 4 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
040
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
41 4 my %args = ( @_ );
42 4 my $self = {
43 logger => Log::Log4perl->get_logger,
44 transport => {},
45 transport_name => {}
46 };
47 4 bless $self, $class;
48  
49 4 $self->load;
50  
51 4 ▶ Lim::OBJ_DEBUG and $self->{logger}->debug('new ', __PACKAGE__, ' ', $self);
Branch: 1 branch
BranchTrueFalseTotal
Lim::OBJ_DEBUG() and $self->{"logger"}->debug("new ", __PACKAGE__, " ", $self) 4 0 4
52 4 $self;
53 }
54  
55 sub DESTROY {
56 4 ▶ my ($self) = @_;
Subroutine DESTROY: called CC=2 SCAR=7.1
57 4 ▶ Lim::OBJ_DEBUG and $self->{logger}->debug('destroy ', __PACKAGE__, ' ', $self);
Branch: 1 branch
BranchTrueFalseTotal
Lim::OBJ_DEBUG() and $self->{"logger"}->debug("destroy ", __PACKAGE__, " ", $self) 4 0 4
58
59 4 delete $self->{transport};
60 }
61  
62 END {
63 10 ▶ undef($INSTANCE);
Subroutine END: called CC=1 SCAR=0.0
64 }
65  
66 =item $instance = Lim::RPC::Transports->instance
67  
68 Returns the singelton instance of this class.
69  
70 =cut
71  
72 sub instance {
73 4 ▶ $INSTANCE ||= Lim::RPC::Transports->_new;
Subroutine instance: called CC=2 SCAR=9.2
Pod documented
Condition33%1/3
$Lim::RPC::Transports::INSTANCE ||= "Lim::RPC::Transports"->_new
l!l&&r!l&&!r
040
MC/DC0%0/2
$Lim::RPC::Transports::INSTANCE ||= "Lim::RPC::Transports"->_new
$Lim::RPC::Transports::INSTANCE "Lim::RPC::Transports"->_new
Truth table33%1/3
A ||= B
A= $Lim::RPC::Transports::INSTANCEB= "Lim::RPC::Transports"->_new
ABresult
1X1
011
000
74 }
75  
76 =item $instance->load
77  
78 Loads all classes that exists on the system under Lim::RPC::Transport::. Returns
79 the reference to itself even on error.
80  
81 =cut
82  
83 sub load {
84 4 ▶ my ($self) = @_;
Subroutine load: called CC=12 SCAR=35.2
Pod documented
85
86 4 foreach my $module (findsubmod Lim::RPC::Transport) {
87 8 ▶ if (exists $self->{transport}->{$module}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"transport"}{$module}) 0 8 8
88 0 ▶ Lim::WARN and $self->{logger}->warn('Transport ', $module, ' already loaded');
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("Transport ", $module, " already loaded") 0 0 0
89 0 next;
90 }
91  
92 8 ▶ if ($module =~ /^([\w:]+)$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module =~ /^([\w:]+)$/ou) { } 8 0 8
93 8 $module = $1;
94 }
95 else {
96 0 next;
97 }
98  
99 8 my $name;
100 8 eval {
101 8 eval "require $module;";
102 8 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 8 8
103 8 $name = $module->name;
104 };
105
106 8 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 8 8
107 0 ▶ Lim::WARN and $self->{logger}->warn('Unable to load transport ', $module, ': ', $@);
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("Unable to load transport ", $module, ": ", $@) 0 0 0
108 0 $self->{transport}->{$module} = {
109 name => $name,
110 module => $module,
111 loaded => 0,
112 error => $@
113 };
114 0 next;
115 }
116
117 8 ▶ unless ($name =~ /^[a-z0-9_\-\.]+$/o) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name =~ /^[a-z0-9_\-\.]+$/ou) 0 8 8
118 0 ▶ Lim::WARN and $self->{logger}->warn('Unable to load transport ', $module, ': Illegal characters in transport name');
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("Unable to load transport ", $module, ": Illegal characters in transport name") 0 0 0
119 0 $self->{transport}->{$module} = {
120 module => $module,
121 loaded => 0,
122 error => 'Illegal characters in transport name'
123 };
124 0 next;
125 }
126  
127 8 ▶ if (exists $self->{transport_name}->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"transport_name"}{$name}) 0 8 8
128 0 ▶ Lim::WARN and $self->{logger}->warn('Transport name ', $name, ' already loaded by module ', $self->{transport_name}->{$name});
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("Transport name ", $name, " already loaded by module ", $self->{"transport_name"}{$name}) 0 0 0
129 0 next;
130 }
131  
132 8 $self->{transport}->{$module} = {
133 name => $name,
134 module => $module,
135 version => $module->VERSION,
136 loaded => 1
137 };
138 8 $self->{transport_name}->{$name} = $module;
139 }
140  
141 4 $self;
142 }
143  
144 =item $transport = $instance->transport($name, ...)
145  
146 =cut
147  
148 sub transport {
149 4 ▶ my $self = shift;
Subroutine transport: called CC=8 SCAR=22.2
Pod documented
150 4 my $name = shift;
151  
152 4 ▶ if (defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) 4 0 4
153 4 my $module;
154
155 4 foreach (keys %{$self->{transport}}) {
156 6 ▶ if ($self->{transport}->{$_}->{loaded} and $self->{transport}->{$_}->{name} eq $name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"transport"}{$_}{"loaded"} and $self->{"transport"}{$_}{"name"} eq $name) 4 2 6
Condition66%2/3
$self->{"transport"}{$_}{"loaded"} and $self->{"transport"}{$_}{"name"} eq $name
!ll&&!rl&&r
024
MC/DC50%1/2
$self->{"transport"}{$_}{"loaded"} and $self->{"transport"}{$_}{"name"} eq $name
$self->{"transport"}{$_}{"loaded"} $self->{"transport"}{$_}{"name"} eq $name
Truth table66%2/3
A and B
A= $self->{"transport"}{$_}{"loaded"}B= $self->{"transport"}{$_}{"name"} eq $name
ABresult
0X0
100
111
157 4 $module = $self->{transport}->{$_}->{module};
158 4 last;
159 }
160 }
161
162 4 ▶ if (defined $module) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $module) 4 0 4
163 4 my $transport;
164 4 eval {
165 4 $transport = $module->new(@_);
166 };
167 4 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 4 4
168 0 ▶ Lim::WARN and $self->{logger}->warn('Unable to create new instance of transport ', $name, '(', $module, '): ', $@);
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("Unable to create new instance of transport ", $name, "(", $module, "): ", $@) 0 0 0
169 }
170 else {
171 4 return $transport;
172 }
173 }
174 }
175 0 return;
176 }
177  
178 =back
179  
180 =head1 AUTHOR
181  
182 Jerry Lundström, C<< <lundstrom.jerry at gmail.com> >>
183  
184 =head1 BUGS
185  
186 Please report any bugs or feature requests to L<https://github.com/jelu/lim/issues>.
187  
188 =head1 SUPPORT
189  
190 You can find documentation for this module with the perldoc command.
191  
192 perldoc Lim::RPC::Transports
193  
194 You can also look for information at:
195  
196 =over 4
197  
198 =item * Lim issue tracker (report bugs here)
199  
200 L<https://github.com/jelu/lim/issues>
201  
202 =back
203  
204 =head1 ACKNOWLEDGEMENTS
205  
206 =head1 LICENSE AND COPYRIGHT
207  
208 Copyright 2012-2013 Jerry Lundström.
209  
210 This program is free software; you can redistribute it and/or modify it
211 under the terms of either: the GNU General Public License as published
212 by the Free Software Foundation; or the Artistic License.
213  
214 See http://dev.perl.org/licenses/ for more information.
215  
216  
217 =cut
218  
219 10 1; # End of Lim::RPC::Transports
« Lim/RPC/Protocols.pm ↑ Summary Lim/RPC/TLS.pm »