Lim/RPC/Protocols.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
protocol
9.2
_new
3.1
CRAP 40.7
« Lim/Component/CLI.pm ↑ Summary Lim/RPC/Transports.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);
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 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 protocol => {},
45 protocol_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->{protocol};
60 }
61  
62 END {
63 10 ▶ undef($INSTANCE);
Subroutine END: called CC=1 SCAR=0.0
64 }
65  
66 =item $instance = Lim::RPC::Protocols->instance
67  
68 Returns the singelton instance of this class.
69  
70 =cut
71  
72 sub instance {
73 4 ▶ $INSTANCE ||= Lim::RPC::Protocols->_new;
Subroutine instance: called CC=2 SCAR=9.2
Pod documented
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
74 }
75  
76 =item $instance->load
77  
78 Loads all classes that exists on the system under Lim::RPC::Protocol::. 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::Protocol) {
87 24 ▶ if (exists $self->{protocol}->{$module}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"protocol"}{$module}) 0 24 24
88 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
89 0 next;
90 }
91
92 24 ▶ if ($module =~ /^([\w:]+)$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module =~ /^([\w:]+)$/ou) { } 24 0 24
93 24 $module = $1;
94 }
95 else {
96 0 next;
97 }
98  
99 24 my $name;
100 24 eval {
101 24 eval "require $module;";
102 24 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 24 24
103 24 $name = $module->name;
104 };
105
106 24 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 24 24
107 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
108 0 $self->{protocol}->{$module} = {
109 module => $module,
110 loaded => 0,
111 error => $@
112 };
113 0 next;
114 }
115
116 24 ▶ unless ($name =~ /^[a-z0-9_\-\.]+$/o) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name =~ /^[a-z0-9_\-\.]+$/ou) 0 24 24
117 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
118 0 $self->{protocol}->{$module} = {
119 module => $module,
120 loaded => 0,
121 error => 'Illegal characters in protocol name'
122 };
123 0 next;
124 }
125  
126 24 ▶ if (exists $self->{protocol_name}->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"protocol_name"}{$name}) 0 24 24
127 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
128 0 next;
129 }
130
131 24 $self->{protocol}->{$module} = {
132 name => $name,
133 module => $module,
134 version => $module->VERSION,
135 loaded => 1
136 };
137 24 $self->{protocol_name}->{$name} = $module;
138 }
139  
140 4 $self;
141 }
142  
143 =item $protocol = $instance->protocol($name, ...)
144  
145 =cut
146  
147 sub protocol {
148 4 ▶ my $self = shift;
Subroutine protocol: called CC=8 SCAR=22.2
Pod documented
149 4 my $name = shift;
150  
151 4 ▶ if (defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) 4 0 4
152 4 my $module;
153
154 4 foreach (keys %{$self->{protocol}}) {
155 14 ▶ 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 10 14
Condition66%2/3
$self->{"protocol"}{$_}{"loaded"} and $self->{"protocol"}{$_}{"name"} eq $name
!ll&&!rl&&r
0104
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
156 4 $module = $self->{protocol}->{$_}->{module};
157 4 last;
158 }
159 }
160
161 4 ▶ if (defined $module) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $module) 4 0 4
162 4 my $protocol;
163 4 eval {
164 4 $protocol = $module->new(@_);
165 };
166 4 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 4 4
167 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
168 }
169 else {
170 4 return $protocol;
171 }
172 }
173 }
174 0 return;
175 }
176  
177 =back
178  
179 =head1 AUTHOR
180  
181 Jerry Lundström, C<< <lundstrom.jerry at gmail.com> >>
182  
183 =head1 BUGS
184  
185 Please report any bugs or feature requests to L<https://github.com/jelu/lim/issues>.
186  
187 =head1 SUPPORT
188  
189 You can find documentation for this module with the perldoc command.
190  
191 perldoc Lim::RPC::Protocols
192  
193 You can also look for information at:
194  
195 =over 4
196  
197 =item * Lim issue tracker (report bugs here)
198  
199 L<https://github.com/jelu/lim/issues>
200  
201 =back
202  
203 =head1 ACKNOWLEDGEMENTS
204  
205 =head1 LICENSE AND COPYRIGHT
206  
207 Copyright 2012-2013 Jerry Lundström.
208  
209 This program is free software; you can redistribute it and/or modify it
210 under the terms of either: the GNU General Public License as published
211 by the Free Software Foundation; or the Artistic License.
212  
213 See http://dev.perl.org/licenses/ for more information.
214  
215  
216 =cut
217  
218 10 1; # End of Lim::RPC::Protocols
« Lim/Component/CLI.pm ↑ Summary Lim/RPC/Transports.pm »