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