| 1 | package Ion::Server; | ||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: An Ion TCP service | ||||||||||||||||||||||||||||||||||||||
| 3 | 3 | $Ion::Server::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_server); | ||||||||||||||||||||||||||||||||||||
|
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 Scalar::Util qw(weaken); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Ion::Conn; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||
| 13 | use overload ( | ||||||||||||||||||||||||||||||||||||||
| 14 | 3 | '<>' => 'accept', | |||||||||||||||||||||||||||||||||||||
| 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 | 3 | ▶ | my ($class, %param) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 24 | my $self = bless { | ||||||||||||||||||||||||||||||||||||||
| 25 | host => $param{host}, | ||||||||||||||||||||||||||||||||||||||
| 26 | port => $param{port}, | ||||||||||||||||||||||||||||||||||||||
| 27 | 3 | guard => undef, | |||||||||||||||||||||||||||||||||||||
| 28 | handle => undef, | ||||||||||||||||||||||||||||||||||||||
| 29 | cond => undef, | ||||||||||||||||||||||||||||||||||||||
| 30 | queue => Coro::Channel->new, | ||||||||||||||||||||||||||||||||||||||
| 31 | conn => {}, | ||||||||||||||||||||||||||||||||||||||
| 32 | encoders => [], | ||||||||||||||||||||||||||||||||||||||
| 33 | decoders => [], | ||||||||||||||||||||||||||||||||||||||
| 34 | }, $class; | ||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||
| 37 | sub DESTROY { | ||||||||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 39 | 0 | $self->stop; | |||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||
| 42 | 3 | ▶ | sub host { $_[0]->{host} } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
host: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 43 | 3 | ▶ | sub port { $_[0]->{port} } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
port: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||
| 45 | sub accept { | ||||||||||||||||||||||||||||||||||||||
| 46 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
accept: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 47 | 1 | my $args = $self->{queue}->get; | |||||||||||||||||||||||||||||||||||||
| 48 | 1 | my ($fh, $host, $port) = @$args; | |||||||||||||||||||||||||||||||||||||
| 49 | 1 | ▶ | return unless $fh; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||
| 51 | Ion::Conn->new( | ||||||||||||||||||||||||||||||||||||||
| 52 | host => $host, | ||||||||||||||||||||||||||||||||||||||
| 53 | port => $port, | ||||||||||||||||||||||||||||||||||||||
| 54 | handle => unblock($fh), | ||||||||||||||||||||||||||||||||||||||
| 55 | encoders => $self->{encoders}, | ||||||||||||||||||||||||||||||||||||||
| 56 | decoders => $self->{decoders}, | ||||||||||||||||||||||||||||||||||||||
| 57 | 1 | ); | |||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||
| 60 | sub start { | ||||||||||||||||||||||||||||||||||||||
| 61 | 3 | ▶ | my ($self, $port, $host) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
start: called CC=5 SCAR=17.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 62 | 3 | ▶ | $self->stop if $self->{handle}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 63 | 3 | ▶ | $self->{queue} ||= Coro::Channel->new; | ||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"queue"} ||= "Coro::Channel"->new
MC/DC0%0/2
$self->{"queue"} ||= "Coro::Channel"->new
$self->{"queue"}
"Coro::Channel"->new
Truth table33%1/3
A ||= B
A= $self->{"queue"}B= "Coro::Channel"->new
| |||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||
| 65 | my $guard = tcp_server( | ||||||||||||||||||||||||||||||||||||||
| 66 | $host || $self->{host}, | ||||||||||||||||||||||||||||||||||||||
| 67 | $port || $self->{port}, | ||||||||||||||||||||||||||||||||||||||
| 68 | 1 | ▶ | sub{ $self->{queue}->put([@_]) }, | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 69 | 3 | ▶ | rouse_cb | ||||||||||||||||||||||||||||||||||||
|
Condition33%2/6
$host || $self->{'host'}
$port || $self->{'port'}
MC/DC0%0/2
$host || $self->{'host'}
$host
$self->{'host'}
MC/DC0%0/2
$port || $self->{'port'}
$port
$self->{'port'}
Truth table33%1/3
A || B
A= $hostB= $self->{'host'}
Truth table33%1/3
A || B
A= $portB= $self->{'port'}
| |||||||||||||||||||||||||||||||||||||||
| 70 | ); | ||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||
| 72 | 3 | weaken $self; | |||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||
| 74 | 3 | my @sock = rouse_wait; | |||||||||||||||||||||||||||||||||||||
| 75 | 3 | $self->{handle} = unblock(shift @sock); | |||||||||||||||||||||||||||||||||||||
| 76 | 3 | $self->{host} = shift @sock; | |||||||||||||||||||||||||||||||||||||
| 77 | 3 | $self->{port} = shift @sock; | |||||||||||||||||||||||||||||||||||||
| 78 | 3 | $self->{guard} = $guard; | |||||||||||||||||||||||||||||||||||||
| 79 | 3 | $self->{cond} = rouse_cb; | |||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||
| 81 | 3 | return 1; | |||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||
| 84 | sub stop { | ||||||||||||||||||||||||||||||||||||||
| 85 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
stop: called CC=5 SCAR=16.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 86 | 1 | ▶ | $self->{queue}->shutdown if $self->{queue}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 87 | 1 | ▶ | $self->{handle}->shutdown if $self->{handle}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 88 | 1 | ▶ | $self->{handle}->close if $self->{handle}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 89 | 1 | ▶ | $self->{cond}->() if $self->{cond}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 90 | 1 | undef $self->{queue}; | |||||||||||||||||||||||||||||||||||||
| 91 | 1 | undef $self->{handle}; | |||||||||||||||||||||||||||||||||||||
| 92 | 1 | undef $self->{guard}; | |||||||||||||||||||||||||||||||||||||
| 93 | 1 | return 1; | |||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||
| 96 | sub join { | ||||||||||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
join: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 98 | 0 | rouse_wait($self->{cond}); | |||||||||||||||||||||||||||||||||||||
| 99 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||
| 102 | sub encodes { | ||||||||||||||||||||||||||||||||||||||
| 103 | 2 | ▶ | my ($self, $encoder) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
encodes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 104 | 2 | push @{$self->{encoders}}, $encoder; | |||||||||||||||||||||||||||||||||||||
| 105 | 2 | return $self; | |||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||
| 108 | sub decodes { | ||||||||||||||||||||||||||||||||||||||
| 109 | 2 | ▶ | my ($self, $decoder) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
decodes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 110 | 2 | push @{$self->{decoders}}, $decoder; | |||||||||||||||||||||||||||||||||||||
| 111 | 2 | return $self; | |||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||
| 114 | 3 | 1; | |||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||
| 116 | __END__ | ||||||||||||||||||||||||||||||||||||||