Server/Select.pm

filter: Statementstmt 32.1% 26 / 81 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 53.3% 8 / 15 Podpod 0.0% 0 / 4 totaltotal 23.6% 34 / 144 CC 19 SCAR 36.8
CC
19
Cov
22%
_shuttle_data
90.0
create
56.0
_from_to
20.0
CRAP 39.6
« Client/Select.pm ↑ Summary Client/AnyEvent.pm »
1 package Devel::REPL::Server::Select;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Devel::REPL::Script;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use IO::Pty;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Term::ReadLine;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 my $TERM;
14  
15 0 ▶sub run_repl {
Subroutine run_repl: not called CC=1 SCAR=6.9
Pod undocumented
16 0 my ($class, %args) = @_;
17 0 my $repl = $class->new(%args);
18  
19 0 $repl->create;
20  
21 0 @_ = ($repl);
22 0 goto &{$repl->can('run')};
23 }
24  
25 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
26 0 my ($class, %args) = @_;
27 my $self = bless {
28 port => $args{port},
29 path => $args{path},
30 skip_levels => $args{skip_levels} // 0,
31 profile => $args{profile},
32 rcfile => $args{rcfile},
33 0 ▶ socket => undef,
Condition0%0/2
$args{"skip_levels"} // 0
l!l
00
MC/DC0%0/1
$args{"skip_levels"} // 0
$args{"skip_levels"}
Truth table0%0/2
A
A= $args{"skip_levels"}
Aresult
11
00
34 pty => undef,
35 repl => undef,
36 repl_script => undef,
37 }, $class;
38  
39 0 return $self;
40 }
41  
42 0 ▶sub create {
Subroutine create: not called CC=7 SCAR=40.3
Pod undocumented
43 0 my ($self) = @_;
44  
45 0 $self->{pty} = IO::Pty->new;
46 0 $self->{fds} = IO::Select->new;
47  
48 0 ▶ if ($self->{port}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'port'}) { } 0 0 0
elsif ($self->{'path'}) { } 0 0 0
49 $self->{socket} = IO::Socket::INET->new(
50 PeerAddr => '127.0.0.1',
51 PeerPort => $self->{port},
52 0 Blocking => 0,
53 );
54 } elsif ($self->{path}) {
55 $self->{socket} = IO::Socket::UNIX->new(
56 Peer => $self->{path},
57 0 );
58 }
59  
60 0 ▶ die "Error during connect: $!" unless $self->{socket};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"socket"} 0 0 0
61  
62 0 $self->{fds}->add($self->{pty}, $self->{socket});
63  
64 0 ▶ my $term = $TERM ||= Term::ReadLine->new;
Condition0%0/3
$TERM ||= "Term::ReadLine"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$TERM ||= "Term::ReadLine"->new
$TERM "Term::ReadLine"->new
Truth table0%0/3
A ||= B
A= $TERMB= "Term::ReadLine"->new
ABresult
1X1
011
000
65 0 my $weak_self = $self;
66  
67 0 Scalar::Util::weaken($weak_self);
68  
69 0 $term->newTTY($self->{pty}->slave, $self->{pty}->slave);
70 0 ▶ $term->event_loop(sub { $weak_self->_shuttle_data });
Subroutine __ANON__: not called CC=1 SCAR=6.9
71  
72 0 $self->{repl} = Devel::REPL->new(term => $term);
73 $self->{repl_script} = Devel::REPL::Script->new(
74 _repl => $self->{repl},
75 !$self->{profile} ? () : (
76 profile => $self->{profile},
77 ),
78 !$self->{rcfile} ? () : (
79 rcfile => $self->{rcfile},
80 0 ▶ ),
Branch: 2 branches
BranchTrueFalseTotal
!$self->{'profile'} ? : 0 0 0
!$self->{'rcfile'} ? : 0 0 0
81 );
82 0 $self->{repl}->load_plugin('InProcess');
83 0 $self->{repl}->skip_levels($self->{skip_levels});
84 }
85  
86 0 ▶sub run {
Subroutine run: not called CC=1 SCAR=6.9
Pod undocumented
87 0 my ($self) = @_;
88  
89 0 $self->{repl}->run;
90 }
91  
92 0 ▶sub _shuttle_data {
Subroutine _shuttle_data: not called CC=9 SCAR=45.0
93 0 my ($self) = @_;
94  
95 eval {
96 0 for (;;) {
97 0 my ($rd, undef, $err) = IO::Select->select($self->{fds}, undef, $self->{fds}, 10);
98  
99 0 ▶ if ($err && @$err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err and @$err) 0 0 0
Condition0%0/3
$err and @$err
!ll&&!rl&&r
000
MC/DC0%0/2
$err and @$err
$err @$err
Truth table0%0/3
A and B
A= $errB= @$err
ABresult
0X0
100
111
100 0 die "One of the handles became invalid";
101 }
102  
103 0 my $got_input;
104 0 for my $hnd (@$rd) {
105 0 ▶ if ($hnd == $self->{socket}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hnd == $self->{"socket"}) 0 0 0
106 # using anything > 1 here breaks (for example) control
107 # char sequences, because STDIN is buffered, and I have
108 # not found a way of either looking at the buffer or
109 # making it unbuffered
110 0 my $read = _from_to($self->{socket}, $self->{pty}, 1);
111 0 ▶ if ($read == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($read == 0) 0 0 0
112 0 $self->{pty}->close_slave;
113 0 $self->{pty}->close;
114 }
115 0 $got_input = 1;
116 }
117 0 ▶ if ($hnd == $self->{pty}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hnd == $self->{"pty"}) 0 0 0
118 0 _from_to($self->{pty}, $self->{socket}, 1000);
119 }
120 }
121 0 ▶ last if $got_input;
Branch: 1 branch
BranchTrueFalseTotal
if $got_input 0 0 0
122 }
123  
124 0 1;
125 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { while (1) { my($rd, undef, $err) = "IO::Select"->select($self->{"fds"}, undef, $self->{"fds"}, 10); if ($err and @$err) { die "One of the handles became invalid"; } ; my $got_input; foreach my $hnd (@$rd) { if ($hnd == $self->{"socket"}) { my $read = _from_to($self->{"socket"}, $self->{"pty"}, 1); if ($read == 0) { $self->{"pty"}->close_slave; $self->{"pty"}->close; } ; $got_input = 1; } ; if ($hnd == $self->{"pty"}) { _from_to($self->{"pty"}, $self->{"socket"}, 1000); } ; } ; last if $got_input; } ; 1 } } or do { warn "Error while waiting for input $@\n" } 0 0 0
126 0 warn "Error while waiting for input $@\n";
127 };
128 }
129  
130 0 ▶sub _from_to {
Subroutine _from_to: not called CC=4 SCAR=30.0
131 0 my ($from, $to, $max) = @_;
132 0 my $buff;
133  
134 0 my $count = sysread $from, $buff, $max;
135 0 ▶ die "Error during read: $!" if !defined $count;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $count 0 0 0
136 0 my $written = syswrite $to, $buff, $count;
137 0 ▶ die "Error during write: $!" if !defined $written || $written != $count;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $written or $written != $count 0 0 0
Condition0%0/3
not defined $written or $written != $count
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $written or $written != $count
not defined $written $written != $count
Truth table0%0/3
not(A) or B
A= not defined $writtenB= $written != $count
ABresult
1X1
011
000
138 0 return $count;
139 }
140  
141 1 1;
« Client/Select.pm ↑ Summary Client/AnyEvent.pm »