Lim/RPC/Server.pm

filter: Statementstmt 59.2% 215 / 363 Branchbran 33.7% 75 / 222 Conditioncond 24.4% 11 / 45 MC/DCmcdc 6.9% 2 / 29 Subroutinesub 86.2% 25 / 29 Podpod 100% 10 / 10 totaltotal 48.4% 338 / 698 CC 139 SCAR 66.4
CC
139
Cov
48%
serve
1419.1
__ANON__
237.5
new
44.1
CRAP 763.7
« Lim/RPC/Call.pm ↑ Summary Lim/Component.pm »
1 package Lim::RPC::Server;
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  
9 10 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use URI::Split ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 10 ▶use Lim ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use Lim::RPC ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 ▶use Lim::RPC::Value ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 10 ▶use Lim::RPC::Value::Collection ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 10 ▶use Lim::RPC::Protocols ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 10 ▶use Lim::RPC::Transports ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 10 ▶use Lim::RPC::URIMaps ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 =encoding utf8
21  
22 =head1 NAME
23  
24 ...
25  
26 =head1 VERSION
27  
28 See L<Lim> for version.
29  
30 =cut
31  
32 10 our $VERSION = $Lim::VERSION;
33  
34 =head1 SYNOPSIS
35  
36 ...
37  
38 =head1 SUBROUTINES/METHODS
39  
40 =head2 new
41  
42 =cut
43  
44 4 ▶sub new {
Subroutine new: called CC=17 SCAR=37.9
Pod documented
45 4 my $this = shift;
46 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
47 4 my %args = ( @_ );
48 4 my $self = {
49 logger => Log::Log4perl->get_logger($class),
50 protocol => {},
51 transports => [],
52 module => {},
53 transport_modules => {}
54 };
55 4 bless $self, $class;
56 4 weaken($self->{logger});
57  
58 4 ▶ unless (defined $args{uri}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"uri"}) 0 4 4
59 0 confess __PACKAGE__, ': No uri specified';
60 }
61  
62 0 ▶ foreach my $uri (ref($args{uri}) eq 'ARRAY' ? @{$args{uri}} : $args{uri}) {
Branch: 1 branch
BranchTrueFalseTotal
ref $args{'uri'} eq 'ARRAY' ? : 0 4 4
63 4 my $modules;
64 4 my $transport_config = {};
65  
66 4 ▶ if (ref($uri) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $uri eq "HASH") 0 4 4
67 0 ▶ if (ref($uri->{plugin}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $uri->{"plugin"} eq "ARRAY") 0 0 0
68 0 $modules = { map { $_ => 1 } @{$uri->{plugin}} };
69 }
70 0 ▶ unless (defined $uri->{uri}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $uri->{"uri"}) 0 0 0
71 0 next;
72 }
73 0 ▶ if (ref($uri->{transport}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $uri->{"transport"} eq "HASH") 0 0 0
74 0 $transport_config = $uri->{transport};
75 }
76 0 $uri = $uri->{uri};
77 }
78  
79 4 my ($scheme, $auth, $path, $query, $frag) = URI::Split::uri_split($uri);
80  
81 4 ▶ if ($scheme =~ /^([a-z0-9_\-\.]+)(?:\+([a-z0-9_\-\.\+]+))*/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scheme =~ /^([a-z0-9_\-\.]+)(?:\+([a-z0-9_\-\.\+]+))*/ou) { } 4 0 4
82 4 my ($transport_name, $protocols) = ($1, $2);
83 4 my (@protocols, $transport);
84 4 $uri = URI->new('', 'http');
85 4 $uri->query($query);
86 4 $uri->host_port($auth);
87  
88 4 foreach my $protocol_name (split(/\+/o, $protocols)) {
89 4 ▶ unless (exists $self->{protocol}->{$protocol_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'protocol'}{$protocol_name}) { } 0 4 4
90 4 my $protocol;
91  
92 4 ▶ unless (defined ($protocol = Lim::RPC::Protocols->instance->protocol($protocol_name, server => $self))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($protocol = "Lim::RPC::Protocols"->instance->protocol($protocol_name, "server", $self))) 0 4 4
93 0 confess __PACKAGE__, ': Protocol ', $protocol_name, ' does not exists';
94 }
95  
96 4 $self->{protocol}->{$protocol_name} = $protocol;
97 4 push(@protocols, $protocol);
98 }
99 else {
100 0 push(@protocols, $self->{protocol}->{$protocol_name});
101 }
102 }
103  
104 4 ▶ unless (defined ($transport = Lim::RPC::Transports->instance->transport($transport_name,
Branch: 2 branches
BranchTrueFalseTotal
unless (defined($transport = "Lim::RPC::Transports"->instance->transport($transport_name, ref $transport_config->{$transport_name} eq "HASH" ? %{$$transport_config{$transport_name};} : (), "server", $self, "uri", $uri))) 0 4 4
ref $transport_config->{$transport_name} eq 'HASH' ? : 0 4 4
105 0 (ref($transport_config->{$transport_name}) eq 'HASH' ? (%{$transport_config->{$transport_name}}) : ()),
106 server => $self, uri => $uri)))
107 {
108 0 confess __PACKAGE__, ': Transport ', $transport_name, ' does not exists';
109 }
110  
111 4 $transport->add_protocol(@protocols);
112 4 push(@{$self->{transports}}, $transport);
113 4 ▶ if ($modules) {
Branch: 1 branch
BranchTrueFalseTotal
if ($modules) 0 4 4
114 0 $self->{transport_modules}->{$transport} = $modules;
115 }
116 }
117 else {
118 0 confess __PACKAGE__, ': Unable to parse URI schema: ', $uri;
119 }
120 }
121  
122 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
123 4 $self;
124 }
125  
126 4 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=7.0
127 4 my ($self) = @_;
128 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
129  
130 4 delete $self->{transports};
131 4 delete $self->{module};
132 4 delete $self->{protocol};
133 }
134  
135 =head2 serve
136  
137 =cut
138  
139 4 ▶sub serve {
Subroutine serve: called CC=85 SCAR=72.6
Pod documented
140 4 my ($self) = shift;
141  
142 4 foreach my $module (@_) {
143 4 my $obj;
144  
145 4 eval {
146 4 $obj = $module->Server;
147 };
148 4 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 4 4
149 0 ▶ Lim::WARN and $self->{logger}->warn('Can not serve ', $module, ': ', $@);
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("Can not serve ", $module, ": ", $@) 0 0 0
150 0 next;
151 }
152 4 ▶ unless (defined $obj) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $obj) 0 4 4
153 0 ▶ Lim::DEBUG and $self->{logger}->debug('Can not serve ', $module, ': no object from Module->Server so may only have Client installed');
Branch: 1 branch
BranchTrueFalseTotal
Lim::DEBUG() and $self->{"logger"}->debug("Can not serve ", $module, ": no object from Module->Server so may only have Client installed") 0 0 0
154 0 next;
155 }
156  
157 4 ▶ if ($obj->isa('Lim::Component::Server')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->isa("Lim::Component::Server")) 4 0 4
158 4 my $name = lc($module->Name);
159  
160 4 ▶ if (exists $self->{module}->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"module"}{$name}) 0 4 4
161 0 ▶ Lim::WARN and $self->{logger}->warn('Can not serve ', $name, ': plugin already served');
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("Can not serve ", $name, ": plugin already served") 0 0 0
162 0 next;
163 }
164  
165 4 ▶ unless ($module->VERSION) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($module->VERSION) 0 4 4
166 0 ▶ Lim::WARN and $self->{logger}->warn('Can not serve ', $name, ': no VERSION specified in plugin');
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("Can not serve ", $name, ": no VERSION specified in plugin") 0 0 0
167 0 next;
168 }
169  
170 4 my $calls = $module->Calls;
171 4 ▶ unless ($calls) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($calls) 0 4 4
172 0 ▶ Lim::INFO and $self->{logger}->info('Not serving ', $name, ', nothing to serve');
Branch: 1 branch
BranchTrueFalseTotal
Lim::INFO() and $self->{"logger"}->info("Not serving ", $name, ", nothing to serve") 0 0 0
173 0 next;
174 }
175 4 ▶ unless (ref($calls) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $calls eq "HASH") 0 4 4
176 0 ▶ Lim::WARN and $self->{logger}->warn('Can not serve ', $name, ': Calls() return was invalid');
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("Can not serve ", $name, ": Calls() return was invalid") 0 0 0
177 0 next;
178 }
179 4 ▶ unless (%$calls) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%$calls) 0 4 4
180 0 ▶ Lim::INFO and $self->{logger}->info('Not serving ', $name, ', nothing to serve');
Branch: 1 branch
BranchTrueFalseTotal
Lim::INFO() and $self->{"logger"}->info("Not serving ", $name, ", nothing to serve") 0 0 0
181 0 next;
182 }
183  
184 4 my $uri_maps = {};
185  
186 4 foreach my $call (keys %$calls) {
187 20 ▶ unless ($obj->can($call)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj->can($call)) 0 20 20
188 0 ▶ Lim::WARN and $self->{logger}->warn('Can not serve ', $name, ': Missing specified call ', $call, ' function');
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("Can not serve ", $name, ": Missing specified call ", $call, " function") 0 0 0
189 0 undef($calls);
190 0 last;
191 }
192  
193 20 my $create_call = 0;
194  
195 20 foreach my $protocol_name (keys %{$self->{protocol}}) {
196 20 my $base = 'Lim::RPC::ProtocolCall::'.$protocol_name.'::'.ref($obj);
197  
198 20 ▶ if ($base->isa('UNIVERSAL') and $base->can($call)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base->isa("UNIVERSAL") and $base->can($call)) 0 20 20
Condition33%1/3
$base->isa("UNIVERSAL") and $base->can($call)
!ll&&!rl&&r
0200
MC/DC0%0/2
$base->isa("UNIVERSAL") and $base->can($call)
$base->isa("UNIVERSAL") $base->can($call)
Truth table33%1/3
A and B
A= $base->isa("UNIVERSAL")B= $base->can($call)
ABresult
0X0
100
111
199 0 next;
200 }
201 20 $create_call = 1;
202 20 last;
203 }
204  
205 20 ▶ if ($create_call) {
Branch: 1 branch
BranchTrueFalseTotal
if ($create_call) 20 0 20
206 20 my $call_def = $calls->{$call};
207  
208 20 ▶ unless (ref($call_def) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $call_def eq "HASH") 0 20 20
209 0 ▶ Lim::WARN and $self->{logger}->warn('Can not serve ', $name, ': call ', $call, ' has invalid definition');
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("Can not serve ", $name, ": call ", $call, " has invalid definition") 0 0 0
210 0 undef($calls);
211 0 last;
212 }
213  
214 20 ▶ if (exists $call_def->{uri_map}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $call_def->{"uri_map"}) 12 8 20
215 12 ▶ unless (ref($call_def->{uri_map}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $call_def->{"uri_map"} eq "ARRAY") 0 12 12
216 0 ▶ Lim::WARN and $self->{logger}->warn('Can not serve ', $name, ': call ', $call, ' has invalid uri_map parameter definition');
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("Can not serve ", $name, ": call ", $call, " has invalid uri_map parameter definition") 0 0 0
217 0 undef($calls);
218 0 last;
219 }
220  
221 12 my $uri_map = Lim::RPC::URIMaps->new;
222  
223 12 foreach my $map (@{$call_def->{uri_map}}) {
224 20 ▶ if (defined (my $redirect_call = $uri_map->add($map))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $redirect_call = $uri_map->add($map))) { } 20 0 20
225 20 ▶ if ($redirect_call) {
Branch: 1 branch
BranchTrueFalseTotal
if ($redirect_call) 8 12 20
226 8 ▶ unless (exists $calls->{$redirect_call}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $calls->{$redirect_call}) 0 8 8
227 0 ▶ Lim::WARN and $self->{logger}->warn('Can not serve ', $name, ': call ', $call, ' has invalid uri_map: redirected to non-existing call ', $redirect_call);
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("Can not serve ", $name, ": call ", $call, " has invalid uri_map: redirected to non-existing call ", $redirect_call) 0 0 0
228 0 undef($calls);
229 0 last;
230 }
231 }
232 }
233 else {
234 0 ▶ Lim::WARN and $self->{logger}->warn('Can not serve ', $name, ': call ', $call, ' has invalid uri_map: ', $@);
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("Can not serve ", $name, ": call ", $call, " has invalid uri_map: ", $@) 0 0 0
235 0 undef($calls);
236 0 last;
237 }
238 }
239 12 ▶ unless (defined $calls) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $calls) 0 12 12
240 0 last;
241 }
242  
243 12 $uri_maps->{$call} = $uri_map;
244 }
245  
246 20 ▶ if (exists $call_def->{in}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $call_def->{"in"}) 12 8 20
247 12 ▶ unless (ref($call_def->{in}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $call_def->{"in"} eq "HASH") 0 12 12
248 0 ▶ Lim::WARN and $self->{logger}->warn('Can not serve ', $name, ': call ', $call, ' has invalid in parameter definition');
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("Can not serve ", $name, ": call ", $call, " has invalid in parameter definition") 0 0 0
249 0 undef($calls);
250 0 last;
251 }
252  
253 12 my @keys = keys %{$call_def->{in}};
254 12 ▶ unless (scalar @keys) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @keys) 0 12 12
255 0 ▶ Lim::WARN and $self->{logger}->warn('Can not serve ', $name, ': call ', $call, ' has invalid in parameter definition');
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("Can not serve ", $name, ": call ", $call, " has invalid in parameter definition") 0 0 0
256 0 undef($calls);
257 0 last;
258 }
259  
260 12 my @values = ($call_def->{in});
261 12 ▶ while (defined $calls and (my $value = shift(@values))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $calls and my $value = shift @values) 24 12 36
Condition66%2/3
defined $calls and my $value = shift @values
!ll&&!rl&&r
01224
MC/DC50%1/2
defined $calls and my $value = shift @values
defined $calls my $value = shift @values
Truth table66%2/3
A and B
A= defined $callsB= my $value = shift @values
ABresult
0X0
100
111
262 24 foreach my $key (keys %$value) {
263 24 ▶ if (ref($value->{$key}) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value->{$key} eq 'HASH') { } 12 12 24
elsif (builtin::blessed($value->{$key})) { } 0 12 12
264 12 ▶ if (exists $value->{$key}->{''}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $value->{$key}{''}) { } 0 12 12
265 0 eval {
266 0 my $collection = Lim::RPC::Value::Collection->new($value->{$key}->{''});
267 0 delete $value->{$key}->{''};
268 0 $value->{$key} = $collection->set_children($value->{$key});
269 };
270 0 ▶ unless ($@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 0 0
271 0 push(@values, $value->{$key}->children);
272 0 next;
273 }
274 0 ▶ Lim::WARN and $self->{logger}->warn('Unable to create Lim::RPC::Value::Collection: ', $@);
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("Unable to create Lim::RPC::Value::Collection: ", $@) 0 0 0
275 }
276 else {
277 12 push(@values, $value->{$key});
278 12 next;
279 }
280 }
281 elsif (blessed $value->{$key}) {
282 0 ▶ if ($value->{$key}->isa('Lim::RPC::Value')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value->{$key}->isa("Lim::RPC::Value")) 0 0 0
283 0 next;
284 }
285 0 ▶ if ($value->{$key}->isa('Lim::RPC::Value::Collection')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value->{$key}->isa("Lim::RPC::Value::Collection")) 0 0 0
286 0 push(@values, $value->{$key}->children);
287 0 next;
288 }
289 }
290 else {
291 12 eval {
292 12 $value->{$key} = Lim::RPC::Value->new($value->{$key});
293 };
294 12 ▶ unless ($@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 12 0 12
295 12 next;
296 }
297 0 ▶ Lim::WARN and $self->{logger}->warn('Unable to create Lim::RPC::Value: ', $@);
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("Unable to create Lim::RPC::Value: ", $@) 0 0 0
298 }
299  
300 0 ▶ Lim::WARN and $self->{logger}->warn('Can not server ', $name, ': call ', $call, ' has invalid in parameter definition');
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("Can not server ", $name, ": call ", $call, " has invalid in parameter definition") 0 0 0
301 0 undef($calls);
302 }
303 }
304  
305 12 ▶ unless (defined $calls) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $calls) 0 12 12
306 0 last;
307 }
308 }
309  
310 20 ▶ if (exists $call_def->{out}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $call_def->{"out"}) 20 0 20
311 20 ▶ unless (ref($call_def->{out}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $call_def->{"out"} eq "HASH") 0 20 20
312 0 ▶ Lim::WARN and $self->{logger}->warn('Can not serve ', $name, ': call ', $call, ' has invalid out parameter definition');
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("Can not serve ", $name, ": call ", $call, " has invalid out parameter definition") 0 0 0
313 0 undef($calls);
314 0 last;
315 }
316  
317 20 my @keys = keys %{$call_def->{out}};
318 20 ▶ unless (scalar @keys) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @keys) 0 20 20
319 0 ▶ Lim::WARN and $self->{logger}->warn('Can not serve ', $name, ': call ', $call, ' has invalid out parameter definition');
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("Can not serve ", $name, ": call ", $call, " has invalid out parameter definition") 0 0 0
320 0 undef($calls);
321 0 last;
322 }
323  
324 20 my @values = ($call_def->{out});
325 20 ▶ while (defined $calls and (my $value = shift(@values))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $calls and my $value = shift @values) 36 20 56
Condition66%2/3
defined $calls and my $value = shift @values
!ll&&!rl&&r
02036
MC/DC50%1/2
defined $calls and my $value = shift @values
defined $calls my $value = shift @values
Truth table66%2/3
A and B
A= defined $callsB= my $value = shift @values
ABresult
0X0
100
111
326 36 foreach my $key (keys %$value) {
327 76 ▶ if (ref($value->{$key}) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value->{$key} eq 'HASH') { } 16 60 76
elsif (builtin::blessed($value->{$key})) { } 0 60 60
328 16 ▶ if (exists $value->{$key}->{''}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $value->{$key}{''}) { } 0 16 16
329 0 eval {
330 0 my $collection = Lim::RPC::Value::Collection->new($value->{$key}->{''});
331 0 delete $value->{$key}->{''};
332 0 $value->{$key} = $collection->set_children($value->{$key});
333 };
334 0 ▶ unless ($@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 0 0
335 0 push(@values, $value->{$key}->children);
336 0 next;
337 }
338 0 ▶ Lim::WARN and $self->{logger}->warn('Unable to create Lim::RPC::Value::Collection: ', $@);
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("Unable to create Lim::RPC::Value::Collection: ", $@) 0 0 0
339 }
340 else {
341 16 push(@values, $value->{$key});
342 16 next;
343 }
344 }
345 elsif (blessed $value->{$key}) {
346 0 ▶ if ($value->{$key}->isa('Lim::RPC::Value')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value->{$key}->isa("Lim::RPC::Value")) 0 0 0
347 0 next;
348 }
349 0 ▶ if ($value->{$key}->isa('Lim::RPC::Value::Collection')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value->{$key}->isa("Lim::RPC::Value::Collection")) 0 0 0
350 0 push(@values, $value->{$key}->children);
351 0 next;
352 }
353 }
354 else {
355 60 eval {
356 60 $value->{$key} = Lim::RPC::Value->new($value->{$key});
357 };
358 60 ▶ unless ($@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 60 0 60
359 60 next;
360 }
361 0 ▶ Lim::WARN and $self->{logger}->warn('Unable to create Lim::RPC::Value: ', $@);
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("Unable to create Lim::RPC::Value: ", $@) 0 0 0
362 }
363  
364 0 ▶ Lim::WARN and $self->{logger}->warn('Can not server ', $name, ': call ', $call, ' has invalid out parameter definition');
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("Can not server ", $name, ": call ", $call, " has invalid out parameter definition") 0 0 0
365 0 undef($calls);
366 }
367 }
368  
369 20 ▶ unless (defined $calls) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $calls) 0 20 20
370 0 last;
371 }
372 }
373  
374 20 my $logger = $self->{logger};
375 20 weaken($logger);
376  
377 20 foreach my $protocol_name (keys %{$self->{protocol}}) {
378 20 my $base = 'Lim::RPC::ProtocolCall::'.$protocol_name.'::'.ref($obj);
379 20 my $protocol_call = $base.'::'.$call;
380 20 my $protocol = $self->{protocol}->{$protocol_name};
381 20 weaken($protocol);
382 20 my $weak_obj = $obj;
383 20 weaken($weak_obj);
384 20 weaken($call_def);
385  
386 20 ▶ if ($base->isa('UNIVERSAL') and $base->can($call)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base->isa("UNIVERSAL") and $base->can($call)) 0 20 20
Condition33%1/3
$base->isa("UNIVERSAL") and $base->can($call)
!ll&&!rl&&r
0200
MC/DC0%0/2
$base->isa("UNIVERSAL") and $base->can($call)
$base->isa("UNIVERSAL") $base->can($call)
Truth table33%1/3
A and B
A= $base->isa("UNIVERSAL")B= $base->can($call)
ABresult
0X0
100
111
387 0 next;
388 }
389  
390 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
391 4 ▶ *$protocol_call = sub {
Subroutine __ANON__: called CC=25 SCAR=54.7
392 4 ▶ unless (defined $protocol and defined $weak_obj and defined $call_def) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $protocol and defined $weak_obj and defined $call_def) 0 4 4
Condition33%2/6
defined $protocol and defined $weak_obj and defined $call_def
!ll&&!rl&&r
004
defined $protocol and defined $weak_obj
!ll&&!rl&&r
004
MC/DC0%0/3
defined $protocol and defined $weak_obj and defined $call_def
defined $protocol defined $weak_obj defined $call_def
Truth table25%1/4
A and B and C
A= defined $protocolB= defined $weak_objC= defined $call_def
ABCresult
0XX0
10X0
1100
1111
393 0 return;
394 }
395 4 my ($self, $cb, $q, @args);
396 4 eval {
397 4 ($self, $cb, $q, @args) = $protocol->precall($call, @_);
398 };
399 4 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 4 4
400 0 ▶ Lim::WARN and defined $logger and $logger->warn($weak_obj, '->', $call, '() precall failed: ', $@);
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and defined $logger and $logger->warn($weak_obj, "->", $call, "() precall failed: ", $@) 0 0 0
Condition0%0/3
Lim::WARN() and defined $logger
!ll&&!rl&&r
000
MC/DC0%0/2
Lim::WARN() and defined $logger
Lim::WARN() defined $logger
Truth table0%0/3
A and B
A= Lim::WARN()B= defined $logger
ABresult
0X0
100
111
401 0 $weak_obj->Error($cb);
402 0 return;
403 }
404  
405 4 ▶ Lim::RPC_DEBUG and defined $logger and $logger->debug('Call to ', $weak_obj, ' ', $call);
Branch: 1 branch
BranchTrueFalseTotal
Lim::RPC_DEBUG() and defined $logger and $logger->debug("Call to ", $weak_obj, " ", $call) 4 0 4
Condition33%1/3
Lim::RPC_DEBUG() and defined $logger
!ll&&!rl&&r
004
MC/DC0%0/2
Lim::RPC_DEBUG() and defined $logger
Lim::RPC_DEBUG() defined $logger
Truth table33%1/3
A and B
A= Lim::RPC_DEBUG()B= defined $logger
ABresult
0X0
100
111
406  
407 4 ▶ if (!defined $q) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $q) 4 0 4
408 4 $q = {};
409 }
410 4 ▶ if (ref($q) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $q ne "HASH") 0 4 4
411 0 ▶ Lim::WARN and defined $logger and $logger->warn($weak_obj, '->', $call, '() called without data as hash');
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and defined $logger and $logger->warn($weak_obj, "->", $call, "() called without data as hash") 0 0 0
Condition0%0/3
Lim::WARN() and defined $logger
!ll&&!rl&&r
000
MC/DC0%0/2
Lim::WARN() and defined $logger
Lim::WARN() defined $logger
Truth table0%0/3
A and B
A= Lim::WARN()B= defined $logger
ABresult
0X0
100
111
412 0 $weak_obj->Error($cb);
413 0 return;
414 }
415  
416 4 ▶ if (exists $call_def->{in}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $call_def->{'in'}) { } 0 4 4
elsif (%$q) { } 0 4 4
417 0 eval {
418 0 Lim::RPC::V($q, $call_def->{in});
419 };
420 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
421 0 ▶ Lim::WARN and defined $logger and $logger->warn($weak_obj, '->', $call, '() data validation failed: ', $@);
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and defined $logger and $logger->warn($weak_obj, "->", $call, "() data validation failed: ", $@) 0 0 0
Condition0%0/3
Lim::WARN() and defined $logger
!ll&&!rl&&r
000
MC/DC0%0/2
Lim::WARN() and defined $logger
Lim::WARN() defined $logger
Truth table0%0/3
A and B
A= Lim::WARN()B= defined $logger
ABresult
0X0
100
111
422 0 ▶ Lim::DEBUG and defined $logger and eval {
Branch: 1 branch
BranchTrueFalseTotal
Lim::DEBUG() and defined $logger and eval { do { $logger->debug(Dumper($q)); $logger->debug(Dumper($call_def->{"in"})) } } 0 0 0
Condition0%0/3
Lim::DEBUG() and defined $logger
!ll&&!rl&&r
000
MC/DC0%0/2
Lim::DEBUG() and defined $logger
Lim::DEBUG() defined $logger
Truth table0%0/3
A and B
A= Lim::DEBUG()B= defined $logger
ABresult
0X0
100
111
423 10 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
424 0 $logger->debug(Dumper($q));
425 0 $logger->debug(Dumper($call_def->{in}));
426 };
427 0 $weak_obj->Error($cb);
428 0 return;
429 }
430 }
431 elsif (%$q) {
432 0 ▶ Lim::WARN and defined $logger and $logger->warn($weak_obj, '->', $call, '() have data but no definition');
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and defined $logger and $logger->warn($weak_obj, "->", $call, "() have data but no definition") 0 0 0
Condition0%0/3
Lim::WARN() and defined $logger
!ll&&!rl&&r
000
MC/DC0%0/2
Lim::WARN() and defined $logger
Lim::WARN() defined $logger
Truth table0%0/3
A and B
A= Lim::WARN()B= defined $logger
ABresult
0X0
100
111
433 0 $weak_obj->Error($cb);
434 0 return;
435 }
436 4 $cb->set_call_def($call_def);
437  
438 4 eval {
439 4 $weak_obj->$call($cb, $q, @args);
440 };
441 4 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 4 4
442 0 ▶ Lim::WARN and defined $logger and $logger->warn($weak_obj, '->', $call, '() failed: ', $@);
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and defined $logger and $logger->warn($weak_obj, "->", $call, "() failed: ", $@) 0 0 0
Condition0%0/3
Lim::WARN() and defined $logger
!ll&&!rl&&r
000
MC/DC0%0/2
Lim::WARN() and defined $logger
Lim::WARN() defined $logger
Truth table0%0/3
A and B
A= Lim::WARN()B= defined $logger
ABresult
0X0
100
111
443 0 $weak_obj->Error($cb);
444 }
445 4 return;
446 20 };
447 }
448 }
449 }
450 4 ▶ unless ($calls) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($calls) 0 4 4
451 0 next;
452 }
453  
454 4 ▶ Lim::DEBUG and $self->{logger}->debug('serving ', $name);
Branch: 1 branch
BranchTrueFalseTotal
Lim::DEBUG() and $self->{"logger"}->debug("serving ", $name) 4 0 4
455  
456 4 $self->{module}->{$name} = {
457 name => $name,
458 module => $module,
459 obj => $obj,
460 calls => $calls,
461 uri_maps => $uri_maps
462 };
463  
464 4 foreach my $protocol (values %{$self->{protocol}}) {
465 4 ▶ Lim::DEBUG and $self->{logger}->debug('serving ', $name, ' to protocol ', $protocol->name);
Branch: 1 branch
BranchTrueFalseTotal
Lim::DEBUG() and $self->{"logger"}->debug("serving ", $name, " to protocol ", $protocol->name) 4 0 4
466 4 $protocol->serve($module, $name);
467 }
468 4 foreach my $transport (@{$self->{transports}}) {
469 4 ▶ if (exists $self->{transport_modules}->{$transport}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"transport_modules"}{$transport} and not exists $self->{"transport_modules"}{$transport}{$module->Name}) 0 4 4
Condition33%1/3
exists $self->{"transport_modules"}{$transport} and not exists $self->{"transport_modules"}{$transport}{$module->Name}
!ll&&!rl&&r
400
MC/DC0%0/2
exists $self->{"transport_modules"}{$transport} and not exists $self->{"transport_modules"}{$transport}{$module->Name}
exists $self->{"transport_modules"}{$transport} not exists $self->{"transport_modules"}{$transport}{$module->Name}
Truth table33%1/3
A and not(B)
A= exists $self->{"transport_modules"}{$transport}B= not exists $self->{"transport_modules"}{$transport}{$module->Name}
ABresult
0X0
100
111
470 and !exists $self->{transport_modules}->{$transport}->{$module->Name})
471 {
472 0 next;
473 }
474 4 ▶ Lim::DEBUG and $self->{logger}->debug('serving ', $name, ' to transport ', $transport->name, ' at ', $transport->uri);
Branch: 1 branch
BranchTrueFalseTotal
Lim::DEBUG() and $self->{"logger"}->debug("serving ", $name, " to transport ", $transport->name, " at ", $transport->uri) 4 0 4
475 4 $transport->serve($module, $name);
476 }
477 }
478 }
479  
480 4 $self;
481 }
482  
483 =head2 have_module
484  
485 =cut
486  
487 4 ▶sub have_module {
Subroutine have_module: called CC=2 SCAR=7.6
Pod documented
488 4 my ($self, $module) = @_;
489  
490 4 ▶ unless (exists $self->{module}->{$module}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"module"}{$module}) 0 4 4
491 0 return;
492 }
493  
494 4 return 1;
495 }
496  
497 =head2 have_module_call
498  
499 =cut
500  
501 2 ▶sub have_module_call {
Subroutine have_module_call: called CC=3 SCAR=12.7
Pod documented
502 2 my ($self, $module, $call) = @_;
503  
504 2 ▶ unless (exists $self->{module}->{$module}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"module"}{$module}) 0 2 2
505 0 return;
506 }
507  
508 2 ▶ unless (exists $self->{module}->{$module}->{calls}->{$call}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"module"}{$module}{"calls"}{$call}) 0 2 2
509 0 return;
510 }
511  
512 2 return 1;
513 }
514  
515 =head2 module_obj
516  
517 =cut
518  
519 2 ▶sub module_obj {
Subroutine module_obj: called CC=2 SCAR=7.6
Pod documented
520 2 my ($self, $module) = @_;
521  
522 2 ▶ unless (exists $self->{module}->{$module}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"module"}{$module}) 0 2 2
523 0 return;
524 }
525  
526 2 return $self->{module}->{$module}->{obj};
527 }
528  
529 =head2 module_class
530  
531 =cut
532  
533 6 ▶sub module_class {
Subroutine module_class: called CC=2 SCAR=7.6
Pod documented
534 6 my ($self, $module) = @_;
535  
536 6 ▶ unless (exists $self->{module}->{$module}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"module"}{$module}) 0 6 6
537 0 return;
538 }
539  
540 6 return $self->{module}->{$module}->{module};
541 }
542  
543 =head2 module_obj_by_protocol
544  
545 =cut
546  
547 6 ▶sub module_obj_by_protocol {
Subroutine module_obj_by_protocol: called CC=3 SCAR=12.7
Pod documented
548 6 my ($self, $module, $protocol) = @_;
549  
550 6 ▶ unless (exists $self->{module}->{$module}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"module"}{$module}) 0 6 6
551 0 return;
552 }
553  
554 6 ▶ unless (exists $self->{protocol}->{$protocol}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"protocol"}{$protocol}) 0 6 6
555 0 return;
556 }
557  
558 6 bless {}, 'Lim::RPC::ProtocolCall::'.$protocol.'::'.$self->{module}->{$module}->{module}.'::Server';
559 }
560  
561 =head2 process_module_call_uri_map
562  
563 =cut
564  
565 0 ▶sub process_module_call_uri_map {
Subroutine process_module_call_uri_map: not called CC=4 SCAR=30.0
Pod documented
566 0 my ($self, $module, $call, $uri, $data) = @_;
567  
568 0 ▶ unless (exists $self->{module}->{$module}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"module"}{$module}) 0 0 0
569 0 return;
570 }
571  
572 0 ▶ unless (ref($data) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $data eq "HASH") 0 0 0
573 0 return;
574 }
575  
576 0 ▶ unless (exists $self->{module}->{$module}->{uri_maps}->{$call}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"module"}{$module}{"uri_maps"}{$call}) 0 0 0
577 0 return;
578 }
579  
580 0 return $self->{module}->{$module}->{uri_maps}->{$call}->process($uri, $data);
581 }
582  
583 =head2 transports
584  
585 =cut
586  
587 4 ▶sub transports {
Subroutine transports: called CC=1 SCAR=0.0
Pod documented
588 4 @{$_[0]->{transports}};
589 }
590  
591 =head2 close
592  
593 =cut
594  
595 0 ▶sub close {
Subroutine close: not called CC=4 SCAR=30.0
Pod documented
596 0 my ($self, $cb) = @_;
597  
598 0 ▶ unless (ref($cb) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $cb eq "CODE") 0 0 0
599 0 confess '$cb is not CODE';
600 }
601  
602 0 my $cv = AnyEvent->condvar;
603 0 ▶ $cv->begin(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
604 0 $cb->();
605 0 $cv = undef;
606 0 });
607  
608 0 foreach my $transport (@{$self->{transports}}) {
609 0 $cv->begin;
610 0 ▶ Lim::DEBUG and $self->{logger}->debug('Closing transport ', $transport->name);
Branch: 1 branch
BranchTrueFalseTotal
Lim::DEBUG() and $self->{"logger"}->debug("Closing transport ", $transport->name) 0 0 0
611 0 ▶ $transport->close(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
612 0 $cv->end;
613 0 });
614 }
615 0 $cv->end;
616  
617 0 $self;
618 }
619  
620 =head1 AUTHOR
621  
622 Jerry Lundström, C<< <lundstrom.jerry at gmail.com> >>
623  
624 =head1 BUGS
625  
626 Please report any bugs or feature requests to L<https://github.com/jelu/lim/issues>.
627  
628 =head1 SUPPORT
629  
630 You can find documentation for this module with the perldoc command.
631  
632 perldoc Lim
633  
634 You can also look for information at:
635  
636 =over 4
637  
638 =item * Lim issue tracker (report bugs here)
639  
640 L<https://github.com/jelu/lim/issues>
641  
642 =back
643  
644 =head1 ACKNOWLEDGEMENTS
645  
646 =head1 LICENSE AND COPYRIGHT
647  
648 Copyright 2012-2013 Jerry Lundström.
649  
650 This program is free software; you can redistribute it and/or modify it
651 under the terms of either: the GNU General Public License as published
652 by the Free Software Foundation; or the Artistic License.
653  
654 See http://dev.perl.org/licenses/ for more information.
655  
656  
657 =cut
658  
659 10 1; # End of Lim::RPC::Server
« Lim/RPC/Call.pm ↑ Summary Lim/Component.pm »