Lim/RPC/Protocols.pm

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