File Coverage

blib/lib/DBGp/Client/AnyEvent/Listener.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package DBGp::Client::AnyEvent::Listener;
2              
3 2     2   67762 use strict;
  2         3  
  2         59  
4 2     2   7 use warnings;
  2         3  
  2         54  
5              
6 2     2   994 use AnyEvent::Socket qw(tcp_server);
  2         30377  
  2         166  
7 2     2   558 use DBGp::Client::AnyEvent::Connection;
  0            
  0            
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             on_connection => $args{on_connection},
16             tcp_guard => undef,
17             }, $class;
18              
19             die "Specify either 'port' or 'path'" unless $self->{port} || $self->{path};
20              
21             return $self;
22             }
23              
24             sub listen {
25             my ($self) = @_;
26              
27             my $weak_self = $self;
28             weaken($weak_self);
29             my $cb = sub { $weak_self->_new_connection($_[0]) };
30             if ($self->{port}) {
31             $self->{guard} = tcp_server('127.0.0.1', $self->{port}, $cb);
32             } elsif ($self->{path}) {
33             $self->{guard} = tcp_server('unix/', $self->{path}, $cb);
34             }
35             }
36              
37             sub _new_connection {
38             my ($self, $fh) = @_;
39             my $connection = DBGp::Client::AnyEvent::Connection->new(socket => $fh);
40              
41             $self->{on_connection}->($connection);
42             }
43              
44             1;