| 1 | package Net::Async::EmptyPort; | ||||||||||||
| 2 | 1 | $Net::Async::EmptyPort::VERSION = '0.001000'; | |||||||||||
| 3 | # ABSTRACT: Asynchronously wait for a port to open | ||||||||||||
| 4 | |||||||||||||
| 5 | 1 | ▶ | use Moo; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 1 | ▶ | use Future::Utils 'try_repeat_until_success', 'try_repeat'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 1 | has _loop => ( | |||||||||||
| 9 | is => 'ro', | ||||||||||||
| 10 | init_arg => 'loop', | ||||||||||||
| 11 | required => 1, | ||||||||||||
| 12 | handles => { | ||||||||||||
| 13 | _connect => 'connect', | ||||||||||||
| 14 | _listen => 'listen', | ||||||||||||
| 15 | _delay => 'delay_future', | ||||||||||||
| 16 | _timeout => 'timeout_future', | ||||||||||||
| 17 | }, | ||||||||||||
| 18 | ); | ||||||||||||
| 19 | |||||||||||||
| 20 | 1 | my %family_map = ( | |||||||||||
| 21 | tcp => 'stream', | ||||||||||||
| 22 | udp => 'dgram', | ||||||||||||
| 23 | ); | ||||||||||||
| 24 | 3 | ▶ | sub empty_port { | ||||||||||
|
Subroutine
empty_port: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||
| 25 | 3 | my ($self, $args) = @_; | |||||||||||
| 26 | |||||||||||||
| 27 | 3 | ▶ | $args //= {}; | ||||||||||
|
Condition100%2/2
$args //= {}
MC/DC100%1/1
$args //= {}
$args
Truth table100%2/2
A
A= $args
| |||||||||||||
| 28 | 3 | ▶ | $args->{host} //= '127.0.0.1'; | ||||||||||
|
Condition50%1/2
$args->{"host"} //= "127.0.0.1"
MC/DC0%0/1
$args->{"host"} //= "127.0.0.1"
$args->{"host"}
Truth table50%1/2
A
A= $args->{"host"}
| |||||||||||||
| 29 | 3 | ▶ | $args->{port} //= 0; | ||||||||||
|
Condition100%2/2
$args->{"port"} //= 0
MC/DC100%1/1
$args->{"port"} //= 0
$args->{"port"}
Truth table100%2/2
A
A= $args->{"port"}
| |||||||||||||
| 30 | 3 | ▶ | $args->{proto} //= 'tcp'; | ||||||||||
|
Condition50%1/2
$args->{"proto"} //= "tcp"
MC/DC0%0/1
$args->{"proto"} //= "tcp"
$args->{"proto"}
Truth table50%1/2
A
A= $args->{"proto"}
| |||||||||||||
| 31 | |||||||||||||
| 32 | 3 | ▶ | if ($args->{port} == 0) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 33 | $self->_listen( | ||||||||||||
| 34 | 1 | ▶ | on_socket => sub {}, | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 35 | host => $args->{host}, | ||||||||||||
| 36 | socktype => $family_map{$args->{proto}}, | ||||||||||||
| 37 | service => $args->{port}, | ||||||||||||
| 38 | ) | ||||||||||||
| 39 | 2 | } else { | |||||||||||
| 40 | 1 | my $port = $args->{port}; | |||||||||||
| 41 | |||||||||||||
| 42 | 1 | ▶ | try_repeat { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 43 | $self->_listen( | ||||||||||||
| 44 | 0 | ▶ | on_socket => sub {}, | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||
| 45 | host => $args->{host}, | ||||||||||||
| 46 | socktype => $family_map{$args->{proto}}, | ||||||||||||
| 47 | 1 | service => $port++, | |||||||||||
| 48 | ) | ||||||||||||
| 49 | 1 | ▶ | } while => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||||
| 50 | 1 | ▶ | !shift->is_done && $port < 65000 | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 51 | }, | ||||||||||||
| 52 | 1 | } | |||||||||||
| 53 | } | ||||||||||||
| 54 | |||||||||||||
| 55 | 1 | ▶ | sub wait_port { | ||||||||||
|
Subroutine
wait_port: called CC=6 SCAR=18.6
Pod
documented
| |||||||||||||
| 56 | 1 | my ($self, $args) = @_; | |||||||||||
| 57 | |||||||||||||
| 58 | die 'port is a required argument' | ||||||||||||
| 59 | 1 | ▶ | unless $args->{port}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 60 | |||||||||||||
| 61 | 1 | ▶ | $args->{host} //= '127.0.0.1'; | ||||||||||
|
Condition50%1/2
$args->{"host"} //= "127.0.0.1"
MC/DC0%0/1
$args->{"host"} //= "127.0.0.1"
$args->{"host"}
Truth table50%1/2
A
A= $args->{"host"}
| |||||||||||||
| 62 | 1 | ▶ | $args->{proto} //= 'tcp'; | ||||||||||
|
Condition50%1/2
$args->{"proto"} //= "tcp"
MC/DC0%0/1
$args->{"proto"} //= "tcp"
$args->{"proto"}
Truth table50%1/2
A
A= $args->{"proto"}
| |||||||||||||
| 63 | 1 | ▶ | $args->{max_wait} //= 10; | ||||||||||
|
Condition50%1/2
$args->{"max_wait"} //= 10
MC/DC0%0/1
$args->{"max_wait"} //= 10
$args->{"max_wait"}
Truth table50%1/2
A
A= $args->{"max_wait"}
| |||||||||||||
| 64 | |||||||||||||
| 65 | 1 | my $amount = 2; | |||||||||||
| 66 | 1 | my $attempt = 0; | |||||||||||
| 67 | |||||||||||||
| 68 | 1 | ▶ | my $f = try_repeat_until_success { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 69 | $self->_delay( | ||||||||||||
| 70 | after => $amount * (2 ** $attempt++) - $amount, | ||||||||||||
| 71 | 1 | ▶ | )->then(sub { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 72 | $self->_connect( | ||||||||||||
| 73 | host => $args->{host}, | ||||||||||||
| 74 | socktype => $family_map{$args->{proto}}, | ||||||||||||
| 75 | service => $args->{port}, | ||||||||||||
| 76 | ) | ||||||||||||
| 77 | 1 | }) | |||||||||||
| 78 | 1 | }; | |||||||||||
| 79 | |||||||||||||
| 80 | $f = Future->wait_any( | ||||||||||||
| 81 | $f, | ||||||||||||
| 82 | $self->_timeout( after => $args->{max_wait} ) | ||||||||||||
| 83 | 1 | ▶ | ) if $args->{max_wait} > 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 84 | |||||||||||||
| 85 | 1 | $f | |||||||||||
| 86 | } | ||||||||||||
| 87 | |||||||||||||
| 88 | 1 | 1; | |||||||||||
| 89 | |||||||||||||
| 90 | __END__ | ||||||||||||