| 1 | package Ion::Conn; | ||||||||||||||||||||||
| 2 | # ABSTRACT: An Ion TCP socket connection | ||||||||||||||||||||||
| 3 | 3 | $Ion::Conn::VERSION = '0.06'; | |||||||||||||||||||||
| 4 | 3 | ▶ | use common::sense; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 5 | |||||||||||||||||||||||
| 6 | 3 | ▶ | use Carp; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 7 | 3 | ▶ | use Coro; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 8 | 3 | ▶ | use AnyEvent::Socket qw(tcp_connect); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 9 | 3 | ▶ | use Coro::Handle qw(unblock); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 10 | 3 | ▶ | use List::Util qw(reduce); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 11 | |||||||||||||||||||||||
| 12 | use overload ( | ||||||||||||||||||||||
| 13 | 3 | '<>' => 'readline', | |||||||||||||||||||||
| 14 | '&{}' => 'writer', | ||||||||||||||||||||||
| 15 | '>>' => 'encodes', | ||||||||||||||||||||||
| 16 | '>>=' => 'encodes', | ||||||||||||||||||||||
| 17 | '<<' => 'decodes', | ||||||||||||||||||||||
| 18 | '<<=' => 'decodes', | ||||||||||||||||||||||
| 19 | fallback => 1, | ||||||||||||||||||||||
| 20 | 3 | ▶ | ); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 21 | |||||||||||||||||||||||
| 22 | sub new { | ||||||||||||||||||||||
| 23 | 4 | ▶ | my ($class, %param) = @_; | ||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=19.9
Pod
undocumented
| |||||||||||||||||||||||
| 24 | |||||||||||||||||||||||
| 25 | 4 | ▶ | unless ($param{handle}) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 26 | 3 | ▶ | my $host = $param{host} || croak 'host is required when handle is not specified'; | ||||||||||||||||||||
|
Condition33%1/3
$param{'host'} || croak('host is required when handle is not specified')
MC/DC0%0/2
$param{'host'} || croak('host is required when handle is not specified')
$param{'host'}
croak('host is required when handle is not specified')
Truth table33%1/3
A || B
A= $param{'host'}B= croak('host is required when handle is not specified')
| |||||||||||||||||||||||
| 27 | 3 | ▶ | my $port = $param{port} || croak 'port is required when handle is not specified'; | ||||||||||||||||||||
|
Condition33%1/3
$param{'port'} || croak('port is required when handle is not specified')
MC/DC0%0/2
$param{'port'} || croak('port is required when handle is not specified')
$param{'port'}
croak('port is required when handle is not specified')
Truth table33%1/3
A || B
A= $param{'port'}B= croak('port is required when handle is not specified')
| |||||||||||||||||||||||
| 28 | 3 | my $fh; | |||||||||||||||||||||
| 29 | |||||||||||||||||||||||
| 30 | 3 | my $guard = tcp_connect($host, $port, rouse_cb); | |||||||||||||||||||||
| 31 | 3 | ($fh, $host, $port) = rouse_wait; | |||||||||||||||||||||
| 32 | |||||||||||||||||||||||
| 33 | 1 | ▶ | croak "connection failed: $!" unless $fh; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 34 | 1 | $param{handle} = unblock $fh; | |||||||||||||||||||||
| 35 | 1 | $param{guard} = $guard; | |||||||||||||||||||||
| 36 | 1 | $param{host} = $host; | |||||||||||||||||||||
| 37 | 1 | $param{port} = $port; | |||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||
| 39 | |||||||||||||||||||||||
| 40 | my $self = bless { | ||||||||||||||||||||||
| 41 | port => $param{port}, | ||||||||||||||||||||||
| 42 | host => $param{host}, | ||||||||||||||||||||||
| 43 | guard => $param{guard}, | ||||||||||||||||||||||
| 44 | handle => $param{handle}, | ||||||||||||||||||||||
| 45 | encoders => $param{encoders} || [], | ||||||||||||||||||||||
| 46 | 2 | ▶ | decoders => $param{decoders} || [], | ||||||||||||||||||||
|
Condition100%4/4
$param{'encoders'} || []
$param{'decoders'} || []
MC/DC100%1/1
$param{'encoders'} || []
$param{'encoders'}
MC/DC100%1/1
$param{'decoders'} || []
$param{'decoders'}
Truth table100%2/2
A
A= $param{'encoders'}
Truth table100%2/2
A
A= $param{'decoders'}
| |||||||||||||||||||||||
| 47 | }, $class; | ||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||
| 49 | |||||||||||||||||||||||
| 50 | sub DESTROY { | ||||||||||||||||||||||
| 51 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 52 | 1 | $self->close; | |||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||
| 54 | |||||||||||||||||||||||
| 55 | 0 | ▶ | sub host { $_[0]->{host} } | ||||||||||||||||||||
|
Subroutine
host: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 56 | 0 | ▶ | sub port { $_[0]->{port} } | ||||||||||||||||||||
|
Subroutine
port: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 57 | |||||||||||||||||||||||
| 58 | sub print { | ||||||||||||||||||||||
| 59 | 4 | ▶ | my ($self, $msg) = @_; | ||||||||||||||||||||
|
Subroutine
print: called CC=1 SCAR=0.1
Pod
documented
| |||||||||||||||||||||||
| 60 | 4 | ▶ | $msg = reduce{ $b->($a) } $msg, @{$self->{encoders}}; | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.4
| |||||||||||||||||||||||
| 61 | 4 | $self->{handle}->print($msg, $/); | |||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||
| 63 | |||||||||||||||||||||||
| 64 | sub readline { | ||||||||||||||||||||||
| 65 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||
|
Subroutine
readline: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||
| 66 | 5 | ▶ | my $line = $self->{handle}->readline($/) or return; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 67 | 4 | chomp $line; | |||||||||||||||||||||
| 68 | 4 | ▶ | reduce{ $b->($a) } $line, @{$self->{decoders}}; | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.4
| |||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||
| 70 | |||||||||||||||||||||||
| 71 | sub close { | ||||||||||||||||||||||
| 72 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||
|
Subroutine
close: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||
| 73 | 2 | ▶ | $self->{handle}->shutdown if $self->{handle}; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 74 | 2 | ▶ | $self->{handle}->close if $self->{handle}; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 75 | 2 | undef $self->{handle}; | |||||||||||||||||||||
| 76 | 2 | undef $self->{guard}; | |||||||||||||||||||||
| 77 | 2 | return 1; | |||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||
| 79 | |||||||||||||||||||||||
| 80 | sub writer { | ||||||||||||||||||||||
| 81 | 4 | ▶ | my $self = shift; | ||||||||||||||||||||
|
Subroutine
writer: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||
| 82 | 4 | ▶ | sub { $self->print(shift) }; | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||
| 84 | |||||||||||||||||||||||
| 85 | sub encodes { | ||||||||||||||||||||||
| 86 | 0 | ▶ | my ($self, $encoder) = @_; | ||||||||||||||||||||
|
Subroutine
encodes: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 87 | 0 | push @{$self->{encoders}}, $encoder; | |||||||||||||||||||||
| 88 | 0 | return $self; | |||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||
| 90 | |||||||||||||||||||||||
| 91 | sub decodes { | ||||||||||||||||||||||
| 92 | 0 | ▶ | my ($self, $decoder) = @_; | ||||||||||||||||||||
|
Subroutine
decodes: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 93 | 0 | push @{$self->{decoders}}, $decoder; | |||||||||||||||||||||
| 94 | 0 | return $self; | |||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||
| 96 | |||||||||||||||||||||||
| 97 | 3 | 1; | |||||||||||||||||||||
| 98 | |||||||||||||||||||||||
| 99 | __END__ | ||||||||||||||||||||||