| 1 | package Tak::JSONChannel; | ||||||||||||
| 2 | |||||||||||||
| 3 | 1 | ▶ | use JSON::PP qw(encode_json decode_json); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use IO::Handle; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 1 | ▶ | use Scalar::Util qw(weaken); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 1 | ▶ | use Log::Contextual qw(:log); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 1 | ▶ | use Moo; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | 1 | has read_fh => (is => 'ro', required => 1); | |||||||||||
| 10 | 1 | has write_fh => (is => 'ro', required => 1); | |||||||||||
| 11 | |||||||||||||
| 12 | 0 | ▶ | has _read_buf => (is => 'ro', default => sub { my $x = ''; \$x }); | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.6
| |||||||||||||
| 13 | |||||||||||||
| 14 | 0 | ▶ | sub BUILD { shift->write_fh->autoflush(1); } | ||||||||||
|
Subroutine
BUILD: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 15 | |||||||||||||
| 16 | sub read_messages { | ||||||||||||
| 17 | 0 | ▶ | my ($self, $cb) = @_; | ||||||||||
|
Subroutine
read_messages: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||
| 18 | 0 | my $rb = $self->_read_buf; | |||||||||||
| 19 | 0 | ▶ | if (sysread($self->read_fh, $$rb, 1024, length($$rb)) > 0) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 20 | 0 | ▶ | while ($$rb =~ s/^(.*)\n//) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 21 | 0 | my $line = $1; | |||||||||||
| 22 | 0 | ▶ | log_trace { "Received $line" }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 23 | 0 | ▶ | if (my $unpacked = $self->_unpack_line($line)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 24 | 0 | $cb->(@$unpacked); | |||||||||||
| 25 | } | ||||||||||||
| 26 | } | ||||||||||||
| 27 | } else { | ||||||||||||
| 28 | 0 | ▶ | log_trace { "Closing" }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 29 | 0 | $cb->('close', 'channel'); | |||||||||||
| 30 | } | ||||||||||||
| 31 | } | ||||||||||||
| 32 | |||||||||||||
| 33 | sub _unpack_line { | ||||||||||||
| 34 | 0 | ▶ | my ($self, $line) = @_; | ||||||||||
|
Subroutine
_unpack_line: not called CC=5 SCAR=34.0
| |||||||||||||
| 35 | 0 | my $data = eval { decode_json($line) }; | |||||||||||
| 36 | 0 | ▶ | unless ($data) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 37 | 0 | ▶ | $self->write_message(mistake => invalid_json => $@||'No data and no exception'); | ||||||||||
|
Condition0%0/2
$@ || 'No data and no exception'
MC/DC0%0/1
$@ || 'No data and no exception'
$@
Truth table0%0/2
A
A= $@
| |||||||||||||
| 38 | 0 | return; | |||||||||||
| 39 | } | ||||||||||||
| 40 | 0 | ▶ | unless (ref($data) eq 'ARRAY') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 41 | 0 | $self->write_message(mistake => message_format => "Not an ARRAY"); | |||||||||||
| 42 | 0 | return; | |||||||||||
| 43 | } | ||||||||||||
| 44 | 0 | ▶ | unless (@$data > 0) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 45 | 0 | $self->write_message(mistake => message_format => "Empty request array"); | |||||||||||
| 46 | 0 | return; | |||||||||||
| 47 | } | ||||||||||||
| 48 | 0 | $data; | |||||||||||
| 49 | } | ||||||||||||
| 50 | |||||||||||||
| 51 | sub write_message { | ||||||||||||
| 52 | 0 | ▶ | my ($self, @msg) = @_; | ||||||||||
|
Subroutine
write_message: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||
| 53 | 0 | my $json = eval { encode_json(\@msg) }; | |||||||||||
| 54 | 0 | ▶ | unless ($json) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 55 | 0 | ▶ | $self->_raw_write_message( | ||||||||||
|
Condition0%0/2
$@ || 'No data and no exception'
MC/DC0%0/1
$@ || 'No data and no exception'
$@
Truth table0%0/2
A
A= $@
| |||||||||||||
| 56 | encode_json( | ||||||||||||
| 57 | [ failure => invalid_message => $@||'No data and no exception' ] | ||||||||||||
| 58 | ) | ||||||||||||
| 59 | ); | ||||||||||||
| 60 | 0 | return; | |||||||||||
| 61 | } | ||||||||||||
| 62 | 0 | ▶ | log_trace { "Sending: $json" }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 63 | 0 | $self->_raw_write_message($json); | |||||||||||
| 64 | } | ||||||||||||
| 65 | |||||||||||||
| 66 | sub _raw_write_message { | ||||||||||||
| 67 | 0 | ▶ | my ($self, $raw) = @_; | ||||||||||
|
Subroutine
_raw_write_message: not called CC=2 SCAR=17.9
| |||||||||||||
| 68 | #warn "Sending: ${raw}\n"; | ||||||||||||
| 69 | 0 | print { $self->write_fh } $raw."\n" | |||||||||||
| 70 | 0 | ▶ | or log_error { "Error writing: $!" }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||
| 71 | } | ||||||||||||
| 72 | |||||||||||||
| 73 | 1 | 1; | |||||||||||