| 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
Condition33%1/3
builtin::blessed($srv) and $srv->isa("JSON::RPC2::AnyEvent::Server")
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")
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 1 | ▶ | unless ( blessed $hdl && $hdl->isa('AnyEvent::Handle') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($hdl) and $hdl->isa("AnyEvent::Handle")
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")
| |||||||||||||||||||||||||||||
| 24 | 1 | ▶ | $hdl = openhandle $hdl or croak "Neither AnyEvent::Handle nor open filehandle: $hdl"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 57 | } else { | ||||||||||||||||||||||||||||
| 58 | 0 | ▶ | $self->destroy if $fatal; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||