« lib/Devel/Trepan/Interface/Client.pm
↑ Summary
blib/lib/Devel/Trepan/CmdProcessor/Command/Set_Subcmd/Variable.pm »
| 1 | # -*- coding: utf-8 -*- | ||||||||||||||||||||||||||||
| 2 | # Copyright (C) 2011-2012 Rocky Bernstein <rocky@cpan.org> | ||||||||||||||||||||||||||||
| 3 | # Debugger Socket Input/Output 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 | # Debugger Client Input/Output Socket. | ||||||||||||||||||||||||||||
| 10 | package Devel::Trepan::IO::TCPClient; | ||||||||||||||||||||||||||||
| 11 | 39 | ▶ | use English qw ( -no_match_vars ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 39 | ▶ | use IO::Socket qw(SOCK_STREAM); | ||||||||||||||||||||||||||
|
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 CLIENT_SOCKET_OPTS => { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | host => 'localhost', # Symbolic name | ||||||||||||||||||||||||||||
| 20 | port => 1027, # Arbitrary non-privileged port | ||||||||||||||||||||||||||||
| 21 | open => 1, | ||||||||||||||||||||||||||||
| 22 | logger => undef, # Complaints should be sent here. | ||||||||||||||||||||||||||||
| 23 | 39 | }; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # attr_reader :state | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | sub open($;$); | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | sub new($;$) | ||||||||||||||||||||||||||||
| 30 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 31 | 0 | my ($class, $opts) = @_; | |||||||||||||||||||||||||||
| 32 | 0 | $opts = hash_merge($opts, CLIENT_SOCKET_OPTS); | |||||||||||||||||||||||||||
| 33 | my $self = { | ||||||||||||||||||||||||||||
| 34 | addr => undef, | ||||||||||||||||||||||||||||
| 35 | buf => '', | ||||||||||||||||||||||||||||
| 36 | input => $opts->{input}, | ||||||||||||||||||||||||||||
| 37 | line_edit => 0, # Our name for GNU readline capability | ||||||||||||||||||||||||||||
| 38 | logger => $opts->{logger}, | ||||||||||||||||||||||||||||
| 39 | output => $opts->{output}, | ||||||||||||||||||||||||||||
| 40 | 0 | state => 'disconnected', | |||||||||||||||||||||||||||
| 41 | }; | ||||||||||||||||||||||||||||
| 42 | 0 | bless $self, $class; | |||||||||||||||||||||||||||
| 43 | 0 | ▶ | $self->open($opts) if $opts->{open}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 0 | return $self; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # Closes both input and output | ||||||||||||||||||||||||||||
| 48 | sub close($) | ||||||||||||||||||||||||||||
| 49 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
close: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 50 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 51 | 0 | $self->{state} = 'closing'; | |||||||||||||||||||||||||||
| 52 | 0 | ▶ | if ($self->{inout}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 0 | $self->{inout}->shutdown(2); | |||||||||||||||||||||||||||
| 54 | close($self->{inout}) | ||||||||||||||||||||||||||||
| 55 | 0 | } | |||||||||||||||||||||||||||
| 56 | 0 | $self->{state} = 'disconnected'; | |||||||||||||||||||||||||||
| 57 | 0 | $self->{input} = $self->{output} = undef; | |||||||||||||||||||||||||||
| 58 | 0 | ▶ | print {$self->{logger}} "Disconnected\n" if $self->{logger}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | sub is_disconnected($) | ||||||||||||||||||||||||||||
| 62 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
is_disconnected: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 63 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 64 | 0 | return 'disconnected' eq $self->{state}; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | sub open($;$) | ||||||||||||||||||||||||||||
| 68 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
open: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 69 | 0 | my ($self, $opts) = @_; | |||||||||||||||||||||||||||
| 70 | 0 | $opts = hash_merge($opts, CLIENT_SOCKET_OPTS); | |||||||||||||||||||||||||||
| 71 | 0 | $self->{host} = $opts->{host}; | |||||||||||||||||||||||||||
| 72 | 0 | $self->{port} = $opts->{port}; | |||||||||||||||||||||||||||
| 73 | $self->{input} = $opts->{input} || | ||||||||||||||||||||||||||||
| 74 | IO::Socket::INET->new(PeerAddr=> $self->{host}, | ||||||||||||||||||||||||||||
| 75 | PeerPort => $self->{port}, | ||||||||||||||||||||||||||||
| 76 | 0 | ▶ | Proto => 'tcp', | ||||||||||||||||||||||||||
|
Condition0%0/3
$opts->{'input'} || 'IO::Socket::INET'->new('PeerAddr', $self->{'host'}, 'PeerPort', $self->{'port'}, 'Proto', 'tcp', 'Type', 1)
MC/DC0%0/2
$opts->{'input'} || 'IO::Socket::INET'->new('PeerAddr', $self->{'host'}, 'PeerPort', $self->{'port'}, 'Proto', 'tcp', 'Type', 1)
$opts->{'input'}
'IO::Socket::INET'->new('PeerAddr', $self->{'host'}, 'PeerPort', $self->{'port'}, 'Proto', 'tcp', 'Type', 1)
Truth table0%0/3
A || B
A= $opts->{'input'}B= 'IO::Socket::INET'->new('PeerAddr', $self->{'host'}, 'PeerPort', $self->{'port'}, 'Proto', 'tcp', 'Type', 1)
| |||||||||||||||||||||||||||||
| 77 | Type => SOCK_STREAM | ||||||||||||||||||||||||||||
| 78 | ); | ||||||||||||||||||||||||||||
| 79 | 0 | $self->{output} = $self->{input}; | |||||||||||||||||||||||||||
| 80 | 0 | ▶ | if ($self->{input}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 0 | $self->{state} = 'connected'; | |||||||||||||||||||||||||||
| 82 | } else { | ||||||||||||||||||||||||||||
| 83 | my $msg = sprintf("Open client for host %s on port %s gives error: %s", | ||||||||||||||||||||||||||||
| 84 | 0 | $self->{host}, $self->{port}, $EVAL_ERROR); | |||||||||||||||||||||||||||
| 85 | 0 | die $msg; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | sub is_empty($) | ||||||||||||||||||||||||||||
| 90 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
is_empty: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 91 | 0 | my($self) = @_; | |||||||||||||||||||||||||||
| 92 | 0 | 0 == length($self->{buf}); | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # Read one message unit. It's possible however that | ||||||||||||||||||||||||||||
| 96 | # more than one message will be set in a receive, so we will | ||||||||||||||||||||||||||||
| 97 | # have to buffer that for the next read. | ||||||||||||||||||||||||||||
| 98 | # EOFError will be raised on EOF. | ||||||||||||||||||||||||||||
| 99 | sub read_msg($) | ||||||||||||||||||||||||||||
| 100 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
read_msg: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 101 | 0 | my($self) = @_; | |||||||||||||||||||||||||||
| 102 | 0 | ▶ | if ($self->{state} eq 'connected') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 0 | ▶ | if (!$self->{buf} || is_empty($self)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $self->{"buf"} or &is_empty($self)
MC/DC0%0/2
not $self->{"buf"} or &is_empty($self)
not $self->{"buf"}
&is_empty($self)
Truth table0%0/3
not(A) or B
A= not $self->{"buf"}B= &is_empty($self)
| |||||||||||||||||||||||||||||
| 104 | 0 | $self->{input}->recv($self->{buf}, TCP_MAX_PACKET); | |||||||||||||||||||||||||||
| 105 | 0 | ▶ | if (is_empty($self)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 0 | $self->close; | |||||||||||||||||||||||||||
| 107 | 0 | $self->{state} = 'disconnected'; | |||||||||||||||||||||||||||
| 108 | 0 | die "EOF while reading on socket"; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | 0 | my $data; | |||||||||||||||||||||||||||
| 112 | 0 | ($self->{buf}, $data) = unpack_msg($self->{buf}); | |||||||||||||||||||||||||||
| 113 | 0 | return $data; | |||||||||||||||||||||||||||
| 114 | } else { | ||||||||||||||||||||||||||||
| 115 | 0 | die sprintf("read_msg called in state: %s.", $self->{state}); | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | sub have_term_readline($) | ||||||||||||||||||||||||||||
| 120 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
have_term_readline: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 121 | 0 | return 0; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | # This method the debugger uses to write a message unit. | ||||||||||||||||||||||||||||
| 125 | sub write($$) | ||||||||||||||||||||||||||||
| 126 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
write: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 127 | 0 | my ($self, $msg) = @_; | |||||||||||||||||||||||||||
| 128 | # FIXME: do we have to check the size of msg and split output? | ||||||||||||||||||||||||||||
| 129 | 0 | $self->{output}->send(pack_msg($msg)); | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | sub writeline($$) | ||||||||||||||||||||||||||||
| 133 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
writeline: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 134 | 0 | my ($self, $msg) = @_; | |||||||||||||||||||||||||||
| 135 | 0 | $self->write($msg . "\n"); | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | # Demo | ||||||||||||||||||||||||||||
| 139 | 39 | ▶ | unless (caller) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 0 | ▶ | if (scalar @ARGV) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | # my $pid = fork(); | ||||||||||||||||||||||||||||
| 142 | #if ($pid) { | ||||||||||||||||||||||||||||
| 143 | 0 | print "Connecting...\n"; | |||||||||||||||||||||||||||
| 144 | 0 | my $client = Devel::Trepan::IO::TCPClient-> new({'open' => 1}); | |||||||||||||||||||||||||||
| 145 | 0 | $client->writeline("Hi there\n"); | |||||||||||||||||||||||||||
| 146 | # for (;;) { | ||||||||||||||||||||||||||||
| 147 | # undef $!; | ||||||||||||||||||||||||||||
| 148 | # my $line; | ||||||||||||||||||||||||||||
| 149 | # unless (defined( $line = <> )) { | ||||||||||||||||||||||||||||
| 150 | # if (eof) { | ||||||||||||||||||||||||||||
| 151 | # print "Got EOF\n"; | ||||||||||||||||||||||||||||
| 152 | # last; | ||||||||||||||||||||||||||||
| 153 | # } | ||||||||||||||||||||||||||||
| 154 | # if ($!) { | ||||||||||||||||||||||||||||
| 155 | # print STDERR $!; | ||||||||||||||||||||||||||||
| 156 | # last; | ||||||||||||||||||||||||||||
| 157 | # } | ||||||||||||||||||||||||||||
| 158 | # chomp $line; | ||||||||||||||||||||||||||||
| 159 | # last if $line eq 'quit'; | ||||||||||||||||||||||||||||
| 160 | # $line = $client->writeline($line); | ||||||||||||||||||||||||||||
| 161 | # # print "Got: #{client.read_msg.chomp}\n"; | ||||||||||||||||||||||||||||
| 162 | # } | ||||||||||||||||||||||||||||
| 163 | # } | ||||||||||||||||||||||||||||
| 164 | 0 | $client->close; | |||||||||||||||||||||||||||
| 165 | #} else { | ||||||||||||||||||||||||||||
| 166 | # server = TCPServer.new('localhost', 1027); | ||||||||||||||||||||||||||||
| 167 | # session = server.accept; | ||||||||||||||||||||||||||||
| 168 | # while 'quit' != (line = session.gets); | ||||||||||||||||||||||||||||
| 169 | # session.puts line ; | ||||||||||||||||||||||||||||
| 170 | # exec "nc -l 1027"; | ||||||||||||||||||||||||||||
| 171 | # } | ||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 39 | 1; | |||||||||||||||||||||||||||