blib/lib/Festival/Client/Async.pm

filter: Statementstmt 15.4% 28 / 181 Branchbran 2.7% 2 / 74 Conditioncond 13.6% 3 / 22 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 30.7% 8 / 26 Podpod 10.5% 2 / 19 totaltotal 12.8% 43 / 336 CC 47 SCAR 40.8
CC
47
Cov
12%
read_more
132.0
server_eval_sync_old
132.0
server_eval_sync
90.0
CRAP 59.2
↑ Summary
1 # Festival::Client::Async: Non-blocking interface to a Festival server
2 #
3 # Copyright (c) 2000 Cepstral LLC. All rights Reserved.
4 #
5 # This module is free software; you can redistribute it and/or modify
6 # it under the same terms as Perl itself.
7 #
8 # Written by David Huggins-Daines <dhd@cepstral.com>
9  
10 package Festival::Client::Async;
11 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.2
16 1 ▶ unless (defined &DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &Festival::Client::Async::DEBUG) 1 0 1
17 *DEBUG = sub () { 0 }
18 1 }
19 };
20  
21 1 ▶use vars qw($VERSION @ISA @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 $VERSION = 0.03_03;
23 1 @ISA = qw(Exporter);
24 1 @EXPORT_OK = qw(parse_lisp);
25  
26 0 ▶sub parse_lisp {
Subroutine parse_lisp: not called CC=5 SCAR=34.0
Pod undocumented
27 0 my $lisp = shift;
28  
29 0 my (@stack, $top);
30 0 $top = [];
31 0 @stack = ($top);
32 0 ▶ while ($lisp =~ m{(
Branch: 1 branch
BranchTrueFalseTotal
if ($lisp =~ /( [()] | "(?:[^"\\]+|\\.)*" | \#<[^>]+> | [^()\s]+ )/gx) 0 0 0
33 [()]
34 |
35 "(?:[^"\\]+|\\.)*"
36 |
37 \#<[^>]+>
38 |
39 [^()\s]+
40 )}xg) {
41 0 my $tok = $1;
42 0 ▶ if ($tok eq '(') {
Branch: 2 branches
BranchTrueFalseTotal
if ($tok eq '(') { } 0 0 0
elsif ($tok eq ')') { } 0 0 0
43 0 my $newtop = [];
44 0 push @$top, $newtop;
45 0 push @stack, ($top = $newtop);
46 } elsif ($tok eq ')') {
47 0 pop @stack;
48 0 $top = $stack[-1];
49 0 ▶ die "stack underflow" unless defined $top;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $top 0 0 0
50 } else {
51 0 push @$top, $tok;
52 }
53 }
54  
55 0 return $top->[0];
56 }
57  
58 1 ▶sub new {
Subroutine new: called CC=5 SCAR=21.6
Pod documented
59 1 my $this = shift;
60 1 ▶ my $class = ref $this || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
61  
62 1 my ($host, $port) = @_;
63  
64 1 ▶ my $s = IO::Socket::INET->new(Proto => 'tcp',
Branch: 1 branch
BranchTrueFalseTotal
my $s = "IO::Socket::INET"->new("Proto", "tcp", "PeerAddr", $host || "localhost", "PeerPort", $port || 1314) or return undef 0 1 1
Condition50%2/4
$host || 'localhost'
l!l
01
$port || 1314
l!l
01
MC/DC0%0/1
$host || 'localhost'
$host
MC/DC0%0/1
$port || 1314
$port
Truth table50%1/2
A
A= $host
Aresult
11
00
Truth table50%1/2
A
A= $port
Aresult
11
00
65 PeerAddr => $host || 'localhost',
66 PeerPort => $port || 1314)
67 or return undef;
68 0 binmode $s;
69  
70 0 my $self = bless {
71 blocked => 0,
72 sock => $s,
73 outbuf => "",
74 outq => {
75 LP => [],
76 },
77 intag => "",
78 inbuf => "",
79 inq => {
80 LP => [],
81 WV => [],
82 OK => [],
83 ER => [],
84 },
85 }, $class;
86 0 $self->unblock;
87 0 return $self;
88 }
89  
90 0 ▶sub fh {
Subroutine fh: not called CC=1 SCAR=6.9
Pod documented
91 0 my $self = shift;
92 0 return $self->{sock};
93 }
94  
95 0 ▶sub block {
Subroutine block: not called CC=3 SCAR=24.8
Pod undocumented
96 0 my $self = shift;
97 0 my $flags = 0;
98 0 ▶ fcntl $self->{sock}, F_GETFL, $flags
Branch: 1 branch
BranchTrueFalseTotal
fcntl $self->{"sock"}, 3, $flags or die "fcntl(F_GETFL) failed: $!" 0 0 0
99 or die "fcntl(F_GETFL) failed: $!";
100 0 ▶ fcntl $self->{sock}, F_SETFL, $flags & ~O_NONBLOCK
Branch: 1 branch
BranchTrueFalseTotal
fcntl $self->{"sock"}, 4, $flags & '18446744073709549567' or die "fcntl(F_SETFL) failed: $!" 0 0 0
101 or die "fcntl(F_SETFL) failed: $!";
102 0 $self->{blocked} = 1;
103 }
104  
105 0 ▶sub unblock {
Subroutine unblock: not called CC=3 SCAR=24.8
Pod undocumented
106 0 my $self = shift;
107 0 my $flags = 0;
108 0 ▶ fcntl $self->{sock}, F_GETFL, $flags
Branch: 1 branch
BranchTrueFalseTotal
fcntl $self->{"sock"}, 3, $flags or die "fcntl(F_GETFL) failed: $!" 0 0 0
109 or die "fcntl(F_GETFL) failed: $!";
110 0 ▶ fcntl $self->{sock}, F_SETFL, $flags | O_NONBLOCK
Branch: 1 branch
BranchTrueFalseTotal
fcntl $self->{"sock"}, 4, $flags | 2048 or die "fcntl(F_SETFL) failed: $!" 0 0 0
111 or die "fcntl(F_SETFL) failed: $!";
112 0 $self->{blocked} = 0;
113 }
114  
115 # Protocol encoding
116 1 ▶use constant KEY => "ft_StUfF_key";
Subroutine BEGIN: called CC=1 SCAR=0.0
117 1 ▶use constant KEYLEN => length KEY;
Subroutine BEGIN: called CC=1 SCAR=0.0
118  
119 0 ▶sub write_more {
Subroutine write_more: not called CC=6 SCAR=37.4
Pod undocumented
120 0 my $self = shift;
121  
122 0 ▶ while (defined(my $expr = shift @{$self->{outq}{LP}})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $expr = shift @{$$self{"outq"}{"LP"};})) 0 0 0
123 0 $self->{outbuf} .= $expr;
124 }
125  
126 0 my $count;
127 0 ▶ while (defined(my $b = syswrite($self->{sock}, $self->{outbuf}, 4096))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $b = syswrite($self->{"sock"}, $self->{"outbuf"}, 4096))) 0 0 0
128 0 print "wrote $b bytes\n" if DEBUG;
129 0 ▶ last if $b == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $b == 0 0 0 0
130  
131 0 $count += $b;
132 0 substr($self->{outbuf}, 0, $b) = "";
133 0 ▶ last if $self->{blocked} and $b < 4096;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"blocked"} and $b < 4096 0 0 0
Condition0%0/3
$self->{"blocked"} and $b < 4096
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"blocked"} and $b < 4096
$self->{"blocked"} $b < 4096
Truth table0%0/3
A and B
A= $self->{"blocked"}B= $b < 4096
ABresult
0X0
100
111
134 }
135  
136 0 return $count;
137 }
138  
139 0 ▶sub read_more {
Subroutine read_more: not called CC=11 SCAR=48.8
Pod undocumented
140 0 my $self = shift;
141 0 my $fh = $self->{sock};
142  
143 0 my $count = 0;
144 0 my $burf = sysread $fh, my($rbuf), 4096;
145 0 print "read $burf bytes\n" if DEBUG;
146 0 $self->{inbuf} .= $rbuf;
147  
148 CHUNK:
149 0 ▶ while (length($self->{inbuf}) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"inbuf"} > 0) 0 0 0
150 # In the middle of a tag?
151 0 ▶ if ($self->{intag}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'intag'}) { } 0 0 0
152 # Look for the stuff key
153 0 ▶ if ((my $i = index($self->{inbuf}, KEY)) != $[-1) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $i = index($self->{'inbuf'}, 'ft_StUfF_key')) != $[ - 1) { } 0 0 0
154 0 ▶ if (substr($self->{inbuf}, $i+KEYLEN, 1) eq 'X') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($self->{'inbuf'}, $i + 12, 1) eq 'X') { } 0 0 0
155 # If there's an X at the end, it's literal
156 0 substr($self->{inbuf}, $i+KEYLEN, 1) = "";
157 } else {
158 # Otherwise, we've got a complete waveform/expr/whatever
159 0 push @{$self->{inq}{$self->{intag}}},
160 0 substr($self->{inbuf}, 0, $i);
161 0 print "queued $i bytes of $self->{intag}\n" if DEBUG;
162 0 substr($self->{inbuf}, 0, $i+KEYLEN) = "";
163 0 $self->{intag} = "";
164 0 $count += $i;
165 }
166 } else {
167 # Maybe we got *part* of the stuff key at the end of
168 # this block. Stranger things have happened.
169 0 my $leftover = "";
170 PARTIAL:
171 0 for my $sub (1..KEYLEN-1) {
172 0 my $foo = \substr($self->{inbuf}, -$sub);
173 0 my $bar = substr(KEY, 0, $sub);
174 0 ▶ if ($$foo eq $bar) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$foo eq $bar) 0 0 0
175 0 $$foo = "";
176 0 $leftover = $bar;
177 0 last PARTIAL;
178 }
179 }
180  
181 # In any case we don't have any more data
182 0 push @{$self->{inq}{$self->{intag}}}, $self->{inbuf};
183 0 print "queued ", length($self->{inbuf}), " bytes of $self->{intag}\n"
184 if DEBUG;
185 0 $count += length($self->{inbuf});
186 0 $self->{inbuf} = $leftover;
187  
188 # But don't keep looping if we left some stuff in there!
189 0 ▶ last CHUNK if $leftover;
Branch: 1 branch
BranchTrueFalseTotal
if $leftover 0 0 0
190 }
191 } else {
192 0 ▶ if ($self->{inbuf} =~ s/^(WV|LP|ER|OK)\n//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'inbuf'} =~ s/^(WV|LP|ER|OK)\n//) { } 0 0 0
193 0 print "got tag $1\n" if DEBUG;
194 0 $count += length($1);
195 # We got a tag, so a new type of data is coming
196 0 ▶ if ($1 eq 'OK') {
Branch: 2 branches
BranchTrueFalseTotal
if ($1 eq 'OK') { } 0 0 0
elsif ($1 eq 'ER') { } 0 0 0
197 0 push @{$self->{inq}{OK}}, time;
198 } elsif ($1 eq 'ER') {
199 0 push @{$self->{inq}{ER}}, time;
200 } else {
201 0 $self->{intag} = $1;
202 }
203 } else {
204 # Should not actually be fatal, it's always possible
205 # we just got the middle of a tag.
206 0 last CHUNK;
207 }
208 }
209 }
210  
211 0 return $count;
212 }
213  
214 0 ▶sub server_eval_sync {
Subroutine server_eval_sync: not called CC=9 SCAR=45.0
Pod undocumented
215 0 my ($self, $lisp, $actions) = @_;
216 0 $self->block;
217 0 $self->server_eval($lisp);
218  
219 0 ▶ unless ($self->write_more) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->write_more) 0 0 0
220 0 $self->unblock;
221 0 return undef;
222 }
223 0 ▶ while ($self->read_more) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->read_more) 0 0 0
224 0 ▶ while (defined(my $wav = $self->dequeue_wave)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $wav = $self->dequeue_wave)) 0 0 0
225 0 ▶ $actions->{WV}->($wav) if exists $actions->{WV};
Branch: 1 branch
BranchTrueFalseTotal
if exists $actions->{"WV"} 0 0 0
226 }
227 0 ▶ while (defined(my $lisp = $self->dequeue_lisp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $lisp = $self->dequeue_lisp)) 0 0 0
228 0 ▶ $actions->{LP}->($lisp) if exists $actions->{LP};
Branch: 1 branch
BranchTrueFalseTotal
if exists $actions->{"LP"} 0 0 0
229 }
230 0 ▶ if (defined($self->dequeue_error)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->dequeue_error) 0 0 0
231 0 $self->unblock;
232 0 return undef;
233 }
234 0 ▶ if (defined($self->dequeue_ok)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->dequeue_ok) 0 0 0
235 0 last;
236 }
237 }
238 0 $self->unblock;
239 0 return 1;
240 }
241  
242 # Don't mix this with async operations :(
243 0 ▶sub server_eval_sync_old {
Subroutine server_eval_sync_old: not called CC=11 SCAR=48.8
Pod undocumented
244 0 my ($self, $lisp, $actions) = @_;
245 0 my $fh = $self->{sock};
246 0 $self->block;
247  
248 0 local $|=1;
249  
250 0 my ($rbuf, $rest, $tag);
251 0 print $fh $lisp;
252 0 ▶ while (defined($rbuf = $rest) or defined($rbuf = <$fh>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($rbuf = $rest) or defined($rbuf = readline $fh)) 0 0 0
Condition0%0/3
defined($rbuf = $rest) or defined($rbuf = readline $fh)
l!l&&r!l&&!r
000
MC/DC0%0/2
defined($rbuf = $rest) or defined($rbuf = readline $fh)
defined($rbuf = $rest) defined($rbuf = readline $fh)
Truth table0%0/3
A or B
A= defined($rbuf = $rest)B= defined($rbuf = readline $fh)
ABresult
1X1
011
000
253 0 undef $rest;
254 0 ▶ if ($rbuf =~ s/^(WV|LP|ER|OK)\n$//s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rbuf =~ s/^(WV|LP|ER|OK)\n$//s) 0 0 0
255 0 $tag = $1;
256 0 ▶ last if $tag eq 'OK' or $tag eq 'ER';
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "OK" or $tag eq "ER" 0 0 0
Condition0%0/3
$tag eq "OK" or $tag eq "ER"
l!l&&r!l&&!r
000
MC/DC0%0/2
$tag eq "OK" or $tag eq "ER"
$tag eq "OK" $tag eq "ER"
Truth table0%0/3
A or B
A= $tag eq "OK"B= $tag eq "ER"
ABresult
1X1
011
000
257 }
258  
259 0 ▶ if ((my $i = index($rbuf, KEY)) != $[-1) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $i = index($rbuf, "ft_StUfF_key")) != $[ - 1) 0 0 0
260 0 ▶ if (substr($rbuf, $i+KEYLEN, 1) eq 'X') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($rbuf, $i + 12, 1) eq 'X') { } 0 0 0
261 0 substr($rbuf, $i+KEYLEN, 1) = "";
262 } else {
263 0 $rest = substr($rbuf, $i+KEYLEN);
264 0 substr($rbuf, $i) = "";
265 }
266 }
267  
268 0 ▶ if (defined $tag and exists $actions->{$tag}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tag and exists $actions->{$tag}) 0 0 0
Condition0%0/3
defined $tag and exists $actions->{$tag}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $tag and exists $actions->{$tag}
defined $tag exists $actions->{$tag}
Truth table0%0/3
A and B
A= defined $tagB= exists $actions->{$tag}
ABresult
0X0
100
111
269 0 $actions->{$tag}->($rbuf);
270 }
271 }
272  
273 0 $self->unblock;
274 0 ▶ return defined($tag) && ($tag eq 'OK');
Condition0%0/3
defined $tag && $tag eq 'OK'
!ll&&!rl&&r
000
MC/DC0%0/2
defined $tag && $tag eq 'OK'
defined $tag $tag eq 'OK'
Truth table0%0/3
A && B
A= defined $tagB= $tag eq 'OK'
ABresult
0X0
100
111
275 }
276  
277 0 ▶sub server_eval {
Subroutine server_eval: not called CC=1 SCAR=6.9
Pod undocumented
278 0 my $self = shift;
279 0 push @{$self->{outq}{LP}}, @_;
280 }
281  
282 0 ▶sub write_pending {
Subroutine write_pending: not called CC=1 SCAR=6.9
Pod undocumented
283 0 my $self = shift;
284 0 return @{$self->{outq}{LP}};
285 }
286  
287 0 ▶sub wave_pending {
Subroutine wave_pending: not called CC=1 SCAR=6.9
Pod undocumented
288 0 my $self = shift;
289 0 return @{$self->{inq}{WV}};
290 }
291  
292 0 ▶sub lisp_pending {
Subroutine lisp_pending: not called CC=1 SCAR=6.9
Pod undocumented
293 0 my $self = shift;
294 0 return @{$self->{inq}{LP}};
295 }
296  
297 0 ▶sub ok_pending {
Subroutine ok_pending: not called CC=1 SCAR=6.9
Pod undocumented
298 0 my $self = shift;
299 0 return @{$self->{inq}{OK}};
300 }
301  
302 0 ▶sub error_pending {
Subroutine error_pending: not called CC=1 SCAR=6.9
Pod undocumented
303 0 my $self = shift;
304 0 return @{$self->{inq}{ER}};
305 }
306  
307 0 ▶sub dequeue_wave {
Subroutine dequeue_wave: not called CC=1 SCAR=6.9
Pod undocumented
308 0 my $self = shift;
309 0 shift @{$self->{inq}{WV}};
310 }
311  
312 0 ▶sub dequeue_lisp {
Subroutine dequeue_lisp: not called CC=1 SCAR=6.9
Pod undocumented
313 0 my $self = shift;
314 0 shift @{$self->{inq}{LP}};
315 }
316  
317 0 ▶sub dequeue_ok {
Subroutine dequeue_ok: not called CC=1 SCAR=6.9
Pod undocumented
318 0 my $self = shift;
319 0 shift @{$self->{inq}{OK}};
320 }
321  
322 0 ▶sub dequeue_error {
Subroutine dequeue_error: not called CC=1 SCAR=6.9
Pod undocumented
323 0 my $self = shift;
324 0 shift @{$self->{inq}{ER}};
325 }
326  
327 1 1;
328 __END__
↑ Summary