| 1 | package MooX::Async::Console::TCPClient; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | MooX::Async::Console::TCPClient - TCP client interaction for MooX::Async::Console | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | See L<MooX::Async::Console> | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | A L<IO::Async::Stream> subclass which waits for a complete line of | ||||||||||||||||||||||||||||
| 14 | text and invokes L</on_line>. | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 BUGS | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | Certainly. | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =cut | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 1 | ▶ | use Modern::Perl '2017'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 1 | ▶ | use strictures 2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 1 | ▶ | use MooX::Async; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 1 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 1 | extends MooXAsync('Stream'); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 1 | with 'MooX::Role::Logger'; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =over | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =item id | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | A string composed from L</address> and L</port> which identifies this | ||||||||||||||||||||||||||||
| 40 | client. | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =cut | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | has id => is => lazy => init_arg => undef, builder => | ||||||||||||||||||||||||||||
| 45 | 0 | ▶ | sub { sprintf 'tcp:%s:%s', $_[0]->address, $_[0]->port }; | ||||||||||||||||||||||||||
|
Subroutine
_build_id: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =item address | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | The IP address this client connected from. | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =cut | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | has address => is => lazy => init_arg => undef, builder => | ||||||||||||||||||||||||||||
| 54 | 0 | ▶ | sub { $_[0]->read_handle->peerhost }; # TODO: Force normalised ipv6 | ||||||||||||||||||||||||||
|
Subroutine
_build_address: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =item port | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | The TCP port this client connected from. | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =cut | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | has port => is => lazy => init_arg => undef, builder => | ||||||||||||||||||||||||||||
| 63 | 0 | ▶ | sub { $_[0]->read_handle->peerport }; | ||||||||||||||||||||||||||
|
Subroutine
_build_port: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =back | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =head1 METHODS | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =over | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =item flush | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | Flush the client's write buffer. | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =cut | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | sub flush { $_[0]->write('')->get } | ||||||||||||||||||||||||||
|
Subroutine
flush: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =item say | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | Write the arguments to the stream with C<\n> appended. | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =cut | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | ▶ | sub say { $_[0]->write($_[1] . "\n") } | ||||||||||||||||||||||||||
|
Subroutine
say: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =back | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =head1 EVENTS | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =over | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =item on_close | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | Invoked when the client has closed the connection. | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =item on_line | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | Invoked when the client has a complete line of text which is not just | ||||||||||||||||||||||||||||
| 100 | whitespace. | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =cut | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 1 | event $_ for qw(on_close on_line); | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =item on_error | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | Implemented by this module to close the connection and invoke | ||||||||||||||||||||||||||||
| 109 | C<on_close> when there's an error. | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =cut | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | ▶ | sub on_error { | ||||||||||||||||||||||||||
|
Subroutine
on_error: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 114 | 0 | local $_[0]->_logger->context->{client} = $_[0]->id; | |||||||||||||||||||||||||||
| 115 | 0 | $_[0]->_logger->errorf('%s write error: %s', ref $_[0], [ @_[1..$#_] ]); | |||||||||||||||||||||||||||
| 116 | 0 | $_[0]->close; | |||||||||||||||||||||||||||
| 117 | 0 | $_[0]->invoke_event(on_close => 'error', @_[1..$#_]); | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =item on_read | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | Implemented by this module to detect a complete line in the read | ||||||||||||||||||||||||||||
| 123 | buffer and invoke C<on_line>. | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | =cut | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | ▶ | sub on_read { | ||||||||||||||||||||||||||
|
Subroutine
on_read: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 128 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 129 | 0 | local $self->_logger->context->{client} = $self->id; | |||||||||||||||||||||||||||
| 130 | 0 | my ($rbuf, $eof) = @_; | |||||||||||||||||||||||||||
| 131 | 0 | ▶ | while ($$rbuf =~ s/^(.*?)\r?\n//) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 0 | ▶ | next unless length $1 or $1 =~ /^\s+$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
length $1 or $1 =~ /^\s+$/u
MC/DC0%0/2
length $1 or $1 =~ /^\s+$/u
length $1
$1 =~ /^\s+$/u
Truth table0%0/3
A or B
A= length $1B= $1 =~ /^\s+$/u
| |||||||||||||||||||||||||||||
| 133 | 0 | $self->invoke_event(on_line => $1); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | 0 | ▶ | return $eof ? 0 : 0+! length $$rbuf; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =item on_read_eof | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | Implemented by this module to invoke C<on_close> when the client | ||||||||||||||||||||||||||||
| 141 | disconnects. | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =cut | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 0 | ▶ | sub on_read_eof { | ||||||||||||||||||||||||||
|
Subroutine
on_read_eof: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 146 | 0 | local $_[0]->_logger->context->{client} = $_[0]->id; | |||||||||||||||||||||||||||
| 147 | 0 | $_[0]->invoke_event(on_close => 'EOF'); | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 1 | 1; | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =back | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | L<MooX::Async::Console> | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | L<MooX::Async::Console::TCP> | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | Matthew King <chohag@jtan.com> | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =cut | ||||||||||||||||||||||||||||