Ident/Server.pm

filter: Statementstmt 91.0% 61 / 67 Branchbran 62.5% 5 / 8 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 81.8% 18 / 22 Podpod 75.0% 3 / 4 totaltotal 80.1% 89 / 111 CC 7 SCAR 8.9
CC
7
Cov
84%
new
7.6
start
2.0
__ANON__
2.0
CRAP 2.4
« Ident/Response.pm ↑ Summary Ident/Transaction.pm »
1 package AnyEvent::Ident::Server;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use AnyEvent::Socket qw( tcp_server );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use AnyEvent::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use AnyEvent::Ident::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use AnyEvent::Ident::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use AnyEvent::Ident::Transaction;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Carp qw( croak carp );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # ABSTRACT: Simple asynchronous ident server
14 4 our $VERSION = '0.08'; # VERSION
15  
16  
17 sub new
18 3 ▶{
Subroutine new: called CC=5 SCAR=20.3
Pod undocumented
19 3 my $class = shift;
20 0 ▶ my $args = ref $_[0] eq 'HASH' ? (\%{$_[0]}) : ({@_});
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 3 3
21 3 my $port = $args->{port};
22 3 ▶ $port = 113 unless defined $port;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $port 0 3 3
23 bless {
24 hostname => $args->{hostname},
25 port => $port,
26 0 ▶ on_error => $args->{on_error} || sub { carp $_[0] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
27 0 ▶ on_bind => $args->{on_bind} || sub { },
Subroutine __ANON__: not called CC=1 SCAR=0.0
28 3 ▶ }, $class;
Condition33%2/6
$args->{'on_error'} || sub { carp($_[0]); }
l!l&&r!l&&!r
030
$args->{'on_bind'} || sub { }
l!l&&r!l&&!r
300
MC/DC0%0/2
$args->{'on_error'} || sub { carp($_[0]); }
$args->{'on_error'} sub { carp($_[0]); }
MC/DC0%0/2
$args->{'on_bind'} || sub { }
$args->{'on_bind'} sub { }
Truth table33%1/3
A || B
A= $args->{'on_error'}B= sub { carp($_[0]); }
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $args->{'on_bind'}B= sub { }
ABresult
1X1
011
000
29 }
30  
31  
32 sub start
33 4 ▶{
Subroutine start: called CC=2 SCAR=7.0
Pod documented
34 4 my($self, $callback) = @_;
35
36 4 ▶ croak "already started" if $self->{guard};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"guard"} 0 4 4
37
38 6 ▶ my $cb = sub {
Subroutine __ANON__: called CC=1 SCAR=0.1
39 6 my ($fh, $host, $port) = @_;
40  
41 6 my $handle;
42 $handle = AnyEvent::Handle->new(
43 fh => $fh,
44 0 ▶ on_error => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
45 0 my ($hdl, $fatal, $msg) = @_;
46 0 $self->{on_error}->($msg);
47 0 $_[0]->destroy;
48 },
49 0 ▶ on_eof => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
50 0 $handle->destroy;
51 },
52 6 );
53
54 11 ▶ $handle->on_read(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
55 11 ▶ $handle->push_read( line => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
56 11 my($handle, $line) = @_;
57 11 $line =~ s/\015?\012//g;
58 11 my $req = eval { AnyEvent::Ident::Request->new($line) };
59 11 ▶ return $handle->push_write("$line:ERROR:INVALID-PORT\015\012") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 4 7 11
60 my $tx = bless {
61 req => $req,
62 remote_port => $port,
63 local_port => $self->{bindport},
64 remote_address => $host,
65 7 ▶ cb => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
66 7 my($res) = @_;
67 7 $handle->push_write($res->as_string . "\015\012");
68 },
69 7 }, 'AnyEvent::Ident::Transaction';
70 7 $callback->($tx);
71 })
72 6 });
73 4 };
74  
75 4 ▶ $self->{guard} = tcp_server $self->{hostname}, $self->{port}, $cb, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
76 4 my($fh, $host, $port) = @_;
77 4 $self->{bindport} = $port;
78 4 $self->{on_bind}->($self);
79 4 };
80
81 4 $self;
82 }
83  
84  
85 8 ▶sub bindport { shift->{bindport} }
Subroutine bindport: called CC=1 SCAR=0.0
Pod documented
86  
87  
88 sub stop
89 1 ▶{
Subroutine stop: called CC=1 SCAR=0.0
Pod documented
90 1 my($self) = @_;
91 1 delete $self->{guard};
92 1 delete $self->{bindport};
93 1 $self;
94 }
95  
96 4 1;
97  
98 __END__
« Ident/Response.pm ↑ Summary Ident/Transaction.pm »