AnyEvent/Server/Handle.pm

filter: Statementstmt 89.0% 65 / 73 Branchbran 30.0% 6 / 20 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 95.4% 21 / 22 Podpod 66.6% 2 / 3 totaltotal 75.0% 96 / 128 CC 13 SCAR 19.6
CC
13
Cov
74%
__ANON__
30.0
new
10.5
__ANON__
2.1
CRAP 7.1
↑ Summary AnyEvent/Server.pm »
1 package JSON::RPC2::AnyEvent::Server::Handle;
2 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = "0.03";
7  
8 2 ▶use AnyEvent::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Errno ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Scalar::Util qw(blessed reftype openhandle);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use JSON::RPC2::AnyEvent::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use JSON::RPC2::AnyEvent::Constants qw(ERR_PARSE_ERROR);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 1 ▶sub new {
Subroutine new: called CC=6 SCAR=23.5
Pod documented
19 1 my ($class, $srv, $hdl) = @_;
20
21 1 ▶ croak "Not an JSON::RPC2::AnyEvent::Server object: $srv" unless blessed $srv && $srv->isa('JSON::RPC2::AnyEvent::Server');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($srv) and $srv->isa("JSON::RPC2::AnyEvent::Server") 0 1 1
Condition33%1/3
builtin::blessed($srv) and $srv->isa("JSON::RPC2::AnyEvent::Server")
!ll&&!rl&&r
001
MC/DC0%0/2
builtin::blessed($srv) and $srv->isa("JSON::RPC2::AnyEvent::Server")
builtin::blessed($srv) $srv->isa("JSON::RPC2::AnyEvent::Server")
Truth table33%1/3
A and B
A= builtin::blessed($srv)B= $srv->isa("JSON::RPC2::AnyEvent::Server")
ABresult
0X0
100
111
22
23 1 ▶ unless ( blessed $hdl && $hdl->isa('AnyEvent::Handle') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($hdl) and $hdl->isa("AnyEvent::Handle")) 1 0 1
Condition33%1/3
builtin::blessed($hdl) and $hdl->isa("AnyEvent::Handle")
!ll&&!rl&&r
100
MC/DC0%0/2
builtin::blessed($hdl) and $hdl->isa("AnyEvent::Handle")
builtin::blessed($hdl) $hdl->isa("AnyEvent::Handle")
Truth table33%1/3
A and B
A= builtin::blessed($hdl)B= $hdl->isa("AnyEvent::Handle")
ABresult
0X0
100
111
24 1 ▶ $hdl = openhandle $hdl or croak "Neither AnyEvent::Handle nor open filehandle: $hdl";
Branch: 1 branch
BranchTrueFalseTotal
$hdl = &openhandle($hdl) or croak("Neither AnyEvent::Handle nor open filehandle: $hdl") 1 0 1
25 1 $hdl = AnyEvent::Handle->new(fh => $hdl);
26 }
27
28 1 my $self = bless {
29 hdl => $hdl,
30 srv => $srv,
31 }, $class;
32
33 2 ▶ $hdl->on_read(sub{
Subroutine __ANON__: called CC=1 SCAR=0.0
34 2 ▶ shift->push_read(json => sub{
Subroutine __ANON__: called CC=1 SCAR=0.0
35 2 my ($h, $json) = @_;
36 2 ▶ $self->{srv}->dispatch($json)->cb(sub{
Subroutine __ANON__: called CC=2 SCAR=7.2
37 2 my $res = shift->recv;
38 2 ▶ $h->push_write(json => $res) if defined $res;
Branch: 1 branch
BranchTrueFalseTotal
if defined $res 2 0 2
39 2 });
40 2 });
41 1 });
42
43 1 ▶ $hdl->on_eof(sub{
Subroutine __ANON__: called CC=2 SCAR=7.1
44 1 my $on_end = $self->{on_end};
45 1 $self->destroy;
46 1 ▶ $on_end->($self) if $on_end;
Branch: 1 branch
BranchTrueFalseTotal
if $on_end 1 0 1
47 1 });
48
49 0 ▶ $hdl->on_error(sub{
Subroutine __ANON__: not called CC=5 SCAR=34.0
50 0 my ($h, $fatal, $msg) = @_;
51 0 ▶ if ( $! == Errno::EBADMSG ) { # JSON Parse error
Branch: 2 branches
BranchTrueFalseTotal
if ($! == 74) { } 0 0 0
elsif ($self->{'on_error'}) { } 0 0 0
52 0 my $res = JSON::RPC2::AnyEvent::Server::_make_error_response(undef, ERR_PARSE_ERROR, 'Parse error');
53 0 $h->push_write(json => $res);
54 } elsif ( $self->{on_error} ){
55 0 $self->{on_error}->($self, $fatal, $msg);
56 0 ▶ $self->destroy if $fatal;
Branch: 1 branch
BranchTrueFalseTotal
if $fatal 0 0 0
57 } else {
58 0 ▶ $self->destroy if $fatal;
Branch: 1 branch
BranchTrueFalseTotal
if $fatal 0 0 0
59 0 croak "JSON::RPC2::AnyEvent::Handle uncaught error: $msg";
60 }
61 1 });
62
63 1 $self;
64 }
65  
66  
67 1 ▶sub JSON::RPC2::AnyEvent::Server::dispatch_fh{
Subroutine dispatch_fh: called CC=1 SCAR=0.0
Pod undocumented
68 1 my ($self, $fh) = @_;
69 1 __PACKAGE__->new($self, $fh);
70 }
71  
72  
73 # Create on_xxx methods
74 2 for my $name ( qw/ on_end on_error / ) {
75 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
76 2 ▶ *$name = sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
77 2 my ($self, $code) = @_;
78 2 ▶ reftype $code eq 'CODE' or croak "coderef must be specified";
Branch: 1 branch
BranchTrueFalseTotal
builtin::reftype($code) eq "CODE" or croak("coderef must be specified") 2 0 2
79 2 $self->{$name} = $code;
80 4 };
81 }
82  
83  
84 # This DESTROY-pattern originates from AnyEvent::Handle code.
85 1 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
86 1 my ($self) = @_;
87 1 $self->{hdl}->destroy;
88 }
89  
90 1 ▶sub destroy {
Subroutine destroy: called CC=1 SCAR=0.0
Pod documented
91 1 my ($self) = @_;
92 1 $self->DESTROY;
93 1 %$self = ();
94 1 bless $self, "JSON::RPC2::AnyEvent::Server::Handle::destroyed";
95 }
96  
97 1 ▶sub JSON::RPC2::AnyEvent::Server::Handle::destroyed::AUTOLOAD {
Subroutine AUTOLOAD: called CC=1 SCAR=0.0
98 #nop
99 }
100  
101  
102 2 1;
103 __END__
↑ Summary AnyEvent/Server.pm »