AnyEvent/Listener.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« AnyEvent/Connection.pm ↑ Summary
1 package DBGp::Client::AnyEvent::Listener;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use AnyEvent::Socket qw(tcp_server);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use DBGp::Client::AnyEvent::Connection;
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use Scalar::Util qw(weaken);
9  
10 sub new {
11 my ($class, %args) = @_;
12 my $self = bless {
13 port => $args{port},
14 path => $args{path},
15 mode => $args{mode},
16 on_connection => $args{on_connection},
17 tcp_guard => undef,
18 }, $class;
19  
20 die "Specify either 'port' or 'path'" unless $self->{port} || $self->{path};
21  
22 return $self;
23 }
24  
25 sub listen {
26 my ($self) = @_;
27  
28 my $weak_self = $self;
29 weaken($weak_self);
30 my $cb = sub { $weak_self->_new_connection($_[0]) };
31 my $prepare = ($self->{path} && defined $self->{mode}) ? sub {
32 chmod $weak_self->{mode}, $weak_self->{path}
33 or die "Unable to change file mode for socket: $!";
34 } : undef;
35 if ($self->{port}) {
36 $self->{guard} = tcp_server('127.0.0.1', $self->{port}, $cb);
37 } elsif ($self->{path}) {
38 $self->{guard} = tcp_server('unix/', $self->{path}, $cb, $prepare);
39 }
40 }
41  
42 sub _new_connection {
43 my ($self, $fh) = @_;
44 my $connection = DBGp::Client::AnyEvent::Connection->new(socket => $fh);
45  
46 $self->{on_connection}->($connection);
47 }
48  
49 1;
« AnyEvent/Connection.pm ↑ Summary