« blib/lib/Devel/Trepan/CmdProcessor/Command/Load_Subcmd/Subcmd.pm
↑ Summary
lib/Devel/Trepan/CmdProcessor/Command/Clear.pm »
| 1 | # -*- coding: utf-8 -*- | ||||||||||
| 2 | # Copyright (C) 2014 Rocky Bernstein <rocky@cpan.org> | ||||||||||
| 3 | # Debugger Input/Output FIFO client interface. | ||||||||||
| 4 | |||||||||||
| 5 | 39 | ▶ | use warnings; use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 39 | ▶ | use rlib '../../..'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | package Devel::Trepan::IO::FIFOClient; | ||||||||||
| 10 | 39 | ▶ | use English qw ( -no_match_vars ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 39 | ▶ | use POSIX qw(mkfifo); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 39 | ▶ | use Fcntl; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 39 | ▶ | use Devel::Trepan::IO::TCPPack; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 39 | ▶ | use Devel::Trepan::Util qw(hash_merge); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 39 | our(@ISA); | |||||||||
| 17 | |||||||||||
| 18 | 39 | ▶ | use constant DEFAULT_INIT_OPTS => { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | open => 1, | ||||||||||
| 20 | |||||||||||
| 21 | # input and output names are the reverse of the server | ||||||||||
| 22 | input_name => '/tmp/trepanpl.outputfifo', | ||||||||||
| 23 | input => undef, | ||||||||||
| 24 | input_mode => 0777, | ||||||||||
| 25 | output_name => '/tmp/trepanpl.inputfifo', | ||||||||||
| 26 | output => undef, | ||||||||||
| 27 | output_mode => 0777, | ||||||||||
| 28 | logger => undef, # Complaints should be sent here. | ||||||||||
| 29 | # name_pat pattern to go into tmpname | ||||||||||
| 30 | 39 | }; | |||||||||
| 31 | |||||||||||
| 32 | # attr_reader :state | ||||||||||
| 33 | |||||||||||
| 34 | sub open($;$); | ||||||||||
| 35 | |||||||||||
| 36 | sub new($;$) | ||||||||||
| 37 | 0 | ▶ | { | ||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 38 | 0 | my ($class, $opts) = @_; | |||||||||
| 39 | 0 | $opts = hash_merge($opts, DEFAULT_INIT_OPTS); | |||||||||
| 40 | my $self = { | ||||||||||
| 41 | addr => undef, | ||||||||||
| 42 | buf => '', | ||||||||||
| 43 | line_edit => 0, # Our name for GNU readline capability | ||||||||||
| 44 | input_name => $opts->{input_name}, | ||||||||||
| 45 | input_mode => $opts->{input_mode}, | ||||||||||
| 46 | output => undef, | ||||||||||
| 47 | output_name => $opts->{output_name}, | ||||||||||
| 48 | output_mode => $opts->{output_mode}, | ||||||||||
| 49 | state => 'uninit', | ||||||||||
| 50 | logger => $opts->{logger} | ||||||||||
| 51 | 0 | }; | |||||||||
| 52 | 0 | bless $self, $class; | |||||||||
| 53 | 0 | ▶ | $self->open($opts) if $opts->{open}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 0 | return $self; | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | 0 | ▶ | sub is_interactive($) { | ||||||||
|
Subroutine
is_interactive: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 58 | 0 | 0 | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | sub have_term_readline($) | ||||||||||
| 62 | 0 | ▶ | { | ||||||||
|
Subroutine
have_term_readline: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 63 | 0 | return 0; | |||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | # Closes server connection. | ||||||||||
| 67 | # FIXME dry with FIFOClient by making a common FIFO routine | ||||||||||
| 68 | sub close | ||||||||||
| 69 | 0 | ▶ | { | ||||||||
|
Subroutine
close: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 70 | 0 | my $self = shift; | |||||||||
| 71 | 0 | $self->{state} = 'closing'; | |||||||||
| 72 | 0 | foreach my $FIFO ( $self->{input}, $self->{output} ) { | |||||||||
| 73 | 0 | close($FIFO); | |||||||||
| 74 | } | ||||||||||
| 75 | 0 | $self->{state} = 'uninit'; | |||||||||
| 76 | 0 | $self->{input} = $self->{output} = undef; | |||||||||
| 77 | 0 | ▶ | print {$self->{logger}} "Disconnected FIFO client\n" if $self->{logger}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | |||||||||||
| 81 | sub is_disconnected($) | ||||||||||
| 82 | 0 | ▶ | { | ||||||||
|
Subroutine
is_disconnected: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 83 | 0 | my $self = shift; | |||||||||
| 84 | 0 | return 'disconnected' eq $self->{state}; | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | sub open($;$) | ||||||||||
| 88 | 0 | ▶ | { | ||||||||
|
Subroutine
open: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||
| 89 | 0 | my ($self, $opts) = @_; | |||||||||
| 90 | 0 | $opts = hash_merge($self, $opts); | |||||||||
| 91 | |||||||||||
| 92 | 0 | foreach my $tuple ( [$opts->{input_name}, $opts->{input_mode}], | |||||||||
| 93 | [$opts->{output_name}, $opts->{output_mode} ] ) { | ||||||||||
| 94 | 0 | my ($named_pipe, $create_mode) = @$tuple; | |||||||||
| 95 | 0 | ▶ | unless ( -p $named_pipe ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | 0 | ▶ | mkfifo($named_pipe, $create_mode) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | or die "mkfifo($named_pipe) failed: $!"; | ||||||||||
| 98 | |||||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | } | ||||||||||
| 102 | 0 | ▶ | sysopen($self->{output}, $self->{output_name}, O_NONBLOCK|O_RDWR) or | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | die "Can't open $self->{output_name} for writing: $!"; | ||||||||||
| 104 | |||||||||||
| 105 | # Flush output as soon as possbile (autoflush). | ||||||||||
| 106 | 0 | my $oldfh = select($self->{output}); | |||||||||
| 107 | 0 | $OUTPUT_AUTOFLUSH = 1; | |||||||||
| 108 | 0 | select($oldfh); | |||||||||
| 109 | |||||||||||
| 110 | 0 | $self->{state} = 'listening'; | |||||||||
| 111 | } | ||||||||||
| 112 | |||||||||||
| 113 | # Read one message unit. | ||||||||||
| 114 | # EOFError will be raised on EOF. | ||||||||||
| 115 | sub read_msg($) | ||||||||||
| 116 | 0 | ▶ | { | ||||||||
|
Subroutine
read_msg: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||
| 117 | 0 | my($self) = @_; | |||||||||
| 118 | 0 | ▶ | unless ($self->{input}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 119 | 0 | ▶ | sysopen($self->{input}, $self->{input_name}, O_RDONLY) or | ||||||||
|
Branch: 1 branch
| |||||||||||
| 120 | die "Can't open $self->{input_name} for reading"; | ||||||||||
| 121 | } | ||||||||||
| 122 | 0 | my $fh = $self->{input}; | |||||||||
| 123 | # print "+++ client self input ($self->{input_name}) ", $fh, "\n"; | ||||||||||
| 124 | 0 | my $msg; | |||||||||
| 125 | 0 | ▶ | unless (eof($fh)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 126 | 0 | $msg = <$fh>; | |||||||||
| 127 | 0 | ▶ | return unpack_msg($msg) if $msg; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 128 | } | ||||||||||
| 129 | 0 | ▶ | die "Remote has closed connection" if eof($fh); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 130 | } | ||||||||||
| 131 | |||||||||||
| 132 | # This method the debugger uses to write. In contrast to | ||||||||||
| 133 | # writeline, no newline is added to the end of to `str'. Also | ||||||||||
| 134 | # $msg doesn't have to be a string. | ||||||||||
| 135 | # FIXME dry with FIFOServer by making a common FIFO routine | ||||||||||
| 136 | sub write($$) | ||||||||||
| 137 | 0 | ▶ | { | ||||||||
|
Subroutine
write: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 138 | 0 | my($self, $msg) = @_; | |||||||||
| 139 | # print "+++ client self output ($self->{output_name})\n"; | ||||||||||
| 140 | 0 | syswrite($self->{output}, pack_msg($msg) . "\n"); | |||||||||
| 141 | } | ||||||||||
| 142 | |||||||||||
| 143 | |||||||||||
| 144 | # FIXME dry with FIFOServer by making a common FIFO routine | ||||||||||
| 145 | sub writeline($$) | ||||||||||
| 146 | 0 | ▶ | { | ||||||||
|
Subroutine
writeline: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 147 | 0 | my ($self, $msg) = @_; | |||||||||
| 148 | 0 | $self->write($msg . "\n"); | |||||||||
| 149 | } | ||||||||||
| 150 | |||||||||||
| 151 | # Demo | ||||||||||
| 152 | 39 | ▶ | unless (caller) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 153 | 0 | ▶ | if (scalar @ARGV) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 154 | 0 | require Devel::Trepan::IO::FIFOServer; | |||||||||
| 155 | 0 | my $pid = fork(); | |||||||||
| 156 | 0 | ▶ | if ($pid) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 157 | 0 | print "Client pid $$...\n"; | |||||||||
| 158 | 0 | my $client = __PACKAGE__-> new({'open' => 1}); | |||||||||
| 159 | 0 | print "client before read\n"; | |||||||||
| 160 | 0 | my $msg = $client->read_msg(); | |||||||||
| 161 | 0 | print "Client read from server message: $msg\n"; | |||||||||
| 162 | 0 | $client->writeline("client to server"); | |||||||||
| 163 | 0 | print "client before second read\n"; | |||||||||
| 164 | 0 | $msg = $client->read_msg(); | |||||||||
| 165 | 0 | print "Client read from server message: $msg\n"; | |||||||||
| 166 | 0 | print "Client $$ is done but waiting on server $pid\n"; | |||||||||
| 167 | 0 | waitpid($pid, 0); | |||||||||
| 168 | 0 | $client->close(); | |||||||||
| 169 | 0 | print "Client is leaving\n"; | |||||||||
| 170 | } else { | ||||||||||
| 171 | 0 | print "Server pid $$...\n"; | |||||||||
| 172 | 0 | require Devel::Trepan::IO::FIFOServer; | |||||||||
| 173 | 0 | my $server = Devel::Trepan::IO::FIFOServer->new(); | |||||||||
| 174 | 0 | print "server before write\n"; | |||||||||
| 175 | 0 | $server->writeline("server to client"); | |||||||||
| 176 | 0 | print "server before read\n"; | |||||||||
| 177 | 0 | my $msg = $server->read_msg(); | |||||||||
| 178 | 0 | print "Server read from client message: $msg\n"; | |||||||||
| 179 | 0 | print "server before second write\n"; | |||||||||
| 180 | 0 | $server->write("server to client nocr"); | |||||||||
| 181 | 0 | print "Server is leaving\n"; | |||||||||
| 182 | 0 | $server->close(); | |||||||||
| 183 | } | ||||||||||
| 184 | } else { | ||||||||||
| 185 | 0 | my $client = __PACKAGE__->new({'open' => 1}); | |||||||||
| 186 | 0 | $client->close(); | |||||||||
| 187 | } | ||||||||||
| 188 | } | ||||||||||
| 189 | |||||||||||
| 190 | 39 | 1; | |||||||||