| 1 | package Kelp::Module::WebSocket::AnyEvent; | ||||||||||||||||||||
| 2 | 2 | $Kelp::Module::WebSocket::AnyEvent::VERSION = '1.05'; | |||||||||||||||||||
| 3 | 2 | ▶ | use Kelp::Base qw(Kelp::Module::Symbiosis::Base); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 2 | ▶ | use Plack::App::WebSocket; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 2 | ▶ | use Kelp::Module::WebSocket::AnyEvent::Connection; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 2 | ▶ | use Carp qw(croak carp cluck); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 2 | ▶ | use Try::Tiny; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 2 | attr "-serializer"; | |||||||||||||||||||
| 10 | 0 | ▶ | attr "-connections" => sub { {} }; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 0 | ▶ | attr "on_open" => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 13 | 0 | ▶ | sub { } | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 0 | }; | |||||||||||||||||||
| 15 | 0 | ▶ | attr "on_close" => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 16 | 0 | ▶ | sub { } | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 0 | }; | |||||||||||||||||||
| 18 | 0 | ▶ | attr "on_message" => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 19 | 0 | ▶ | sub { } | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | 0 | }; | |||||||||||||||||||
| 21 | 0 | ▶ | attr "on_malformed_message" => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.6
| |||||||||||||||||||||
| 22 | 0 | ▶ | sub { die pop } | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 23 | 0 | }; | |||||||||||||||||||
| 24 | 2 | attr "on_error"; | |||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | # This function is here to work around Twiggy bug that is silencing errors | ||||||||||||||||||||
| 27 | # Warn them instead, so they can be logged and spotted | ||||||||||||||||||||
| 28 | sub _trap(&) | ||||||||||||||||||||
| 29 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
_trap: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 30 | 0 | my ($block) = @_; | |||||||||||||||||||
| 31 | 0 | ▶ | try { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 32 | 0 | $block->(); | |||||||||||||||||||
| 33 | } | ||||||||||||||||||||
| 34 | 0 | ▶ | catch { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 35 | 0 | cluck $_; | |||||||||||||||||||
| 36 | 0 | die $_; | |||||||||||||||||||
| 37 | 0 | }; | |||||||||||||||||||
| 38 | } | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 2 | ▶ | sub name { 'websocket' } | ||||||||||||||||||
|
Subroutine
name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | sub psgi | ||||||||||||||||||||
| 43 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
psgi: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 44 | 0 | my ($self) = @_; | |||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 0 | my $conn_max_id = 0; | |||||||||||||||||||
| 47 | my $websocket = Plack::App::WebSocket->new( | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | # on_error - optional | ||||||||||||||||||||
| 50 | 0 | ▶ | (defined $self->on_error ? (on_error => sub { $self->on_error->(@_) }) : ()), | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | # on_establish - mandatory | ||||||||||||||||||||
| 53 | 0 | ▶ | on_establish => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 54 | 0 | my ($orig_conn, $env) = @_; | |||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 0 | my $conn = Kelp::Module::WebSocket::AnyEvent::Connection->new( | |||||||||||||||||||
| 57 | connection => $orig_conn, | ||||||||||||||||||||
| 58 | id => ++$conn_max_id, | ||||||||||||||||||||
| 59 | manager => $self | ||||||||||||||||||||
| 60 | ); | ||||||||||||||||||||
| 61 | 0 | ▶ | _trap { $self->on_open->($conn, $env) }; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | $conn->connection->on( | ||||||||||||||||||||
| 64 | 0 | ▶ | message => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 65 | 0 | my ($orig_conn, $message) = @_; | |||||||||||||||||||
| 66 | 0 | my $err; | |||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | 0 | ▶ | if (my $s = $self->get_serializer) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 69 | 0 | ▶ | try { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 70 | 0 | $message = $s->decode($message); | |||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | 0 | ▶ | catch { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 73 | 0 | ▶ | $err = $_ || 'unknown error'; | ||||||||||||||||||
|
Condition0%0/2
$_ || 'unknown error'
MC/DC0%0/1
$_ || 'unknown error'
$_
Truth table0%0/2
A
A= $_
| |||||||||||||||||||||
| 74 | 0 | }; | |||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 0 | ▶ | _trap { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 78 | 0 | ▶ | if ($err) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 79 | 0 | $self->on_malformed_message->($conn, $message, $err); | |||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | else { | ||||||||||||||||||||
| 82 | 0 | $self->on_message->($conn, $message); | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | 0 | }; | |||||||||||||||||||
| 85 | }, | ||||||||||||||||||||
| 86 | 0 | ▶ | finish => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 87 | 0 | ▶ | _trap { $self->on_close->($conn) }; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 88 | 0 | $conn->close; | |||||||||||||||||||
| 89 | 0 | undef $orig_conn; | |||||||||||||||||||
| 90 | }, | ||||||||||||||||||||
| 91 | 0 | ); | |||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | 0 | ▶ | ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 0 | return $websocket->to_app; | |||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | sub add | ||||||||||||||||||||
| 99 | 7 | ▶ | { | ||||||||||||||||||
|
Subroutine
add: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||
| 100 | 7 | my ($self, $type, $sub) = @_; | |||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 7 | ▶ | croak "websocket handler for $type is not a code reference" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 103 | unless ref $sub eq 'CODE'; | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 7 | $type = "on_$type"; | |||||||||||||||||||
| 106 | 7 | my $setter = $self->can($type); | |||||||||||||||||||
| 107 | 7 | ▶ | croak "unknown websocket event `$type`" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 108 | unless defined $setter; | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 7 | return $setter->($self, $sub); | |||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | sub get_serializer | ||||||||||||||||||||
| 114 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
get_serializer: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 115 | 0 | my ($self) = @_; | |||||||||||||||||||
| 116 | 0 | ▶ | return undef unless defined $self->serializer; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | 0 | my $real_serializer_method = $self->app->can($self->serializer); | |||||||||||||||||||
| 119 | 0 | ▶ | croak "Kelp doesn't have $self->serializer serializer" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 120 | unless defined $real_serializer_method; | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | 0 | return $real_serializer_method->($self->app); | |||||||||||||||||||
| 123 | } | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | sub build | ||||||||||||||||||||
| 126 | 2 | ▶ | { | ||||||||||||||||||
|
Subroutine
build: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 127 | 2 | my ($self, %args) = @_; | |||||||||||||||||||
| 128 | 2 | $self->SUPER::build(%args); | |||||||||||||||||||
| 129 | 2 | ▶ | $self->{serializer} = $args{serializer} // $self->serializer; | ||||||||||||||||||
|
Condition66%2/3
$args{"serializer"} // $self->serializer
MC/DC50%1/2
$args{"serializer"} // $self->serializer
$args{"serializer"}
$self->serializer
Truth table66%2/3
A // B
A= $args{"serializer"}B= $self->serializer
| |||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | 2 | $self->register(websocket => $self); | |||||||||||||||||||
| 132 | } | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | 2 | 1; | |||||||||||||||||||
| 135 | __END__ | ||||||||||||||||||||