Ion/Server.pm

filter: Statementstmt 92.9% 66 / 71 Branchbran 50.0% 6 / 12 Conditioncond 38.4% 5 / 13 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 89.4% 17 / 19 Podpod 88.8% 8 / 9 totaltotal 78.4% 102 / 130 CC 12 SCAR 12.9
CC
12
Cov
80%
start
8.6
stop
5.3
accept
2.0
CRAP 3.6
« Ion.pm ↑ Summary Ion/Conn.pm »
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
BranchTrueFalseTotal
unless $fh 0 1 1
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
BranchTrueFalseTotal
if $self->{"handle"} 0 3 3
64 3 ▶ $self->{queue} ||= Coro::Channel->new;
Condition33%1/3
$self->{"queue"} ||= "Coro::Channel"->new
l!l&&r!l&&!r
300
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
ABresult
1X1
011
000
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
l!l
03
$host or $self->{"host"}
l!l&&r!l&&!r
003
$port || $self->{'port'} || undef
l!l
03
$port or $self->{"port"}
l!l&&r!l&&!r
003
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"}
ABresult
1X1
011
000
Truth table33%1/3
A or B
A= $portB= $self->{"port"}
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if $self->{"queue"} 1 0 1
88 1 ▶ $self->{handle}->shutdown if $self->{handle};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"handle"} 1 0 1
89 1 ▶ $self->{handle}->close if $self->{handle};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"handle"} 1 0 1
90 1 ▶ $self->{cond}->() if $self->{cond};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cond"} 1 0 1
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__
« Ion.pm ↑ Summary Ion/Conn.pm »