Lambda/Message.pm

filter: Statementstmt 8.8% 20 / 226 Branchbran 0.0% 0 / 122 Conditioncond 4.2% 2 / 47 MC/DCmcdc 0.0% 0 / 28 Subroutinesub 11.6% 5 / 43 Podpod 41.1% 7 / 17 totaltotal 7.0% 34 / 483 CC 78 SCAR 44.9
CC
78
Cov
6%
run
420.0
new
72.0
__ANON__
56.0
CRAP 89.1
« Lambda/Loop/Prima.pm ↑ Summary Lambda/HTTP/HTTPS.pm »
1 # $Id: Message.pm,v 1.14 2009/11/30 14:28:19 dk Exp $
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 package IO::Lambda::Message;
7
8 1 our $CRLF = "\x0a";
9 1 our @EXPORT_OK = qw(message);
10 1 ▶our $DEBUG = $IO::Lambda::DEBUG{message} || 0;
Condition50%1/2
$IO::Lambda::DEBUG{'message'} || 0
l!l
01
MC/DC0%0/1
$IO::Lambda::DEBUG{'message'} || 0
$IO::Lambda::DEBUG{'message'}
Truth table50%1/2
A
A= $IO::Lambda::DEBUG{'message'}
Aresult
11
00
11
12 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use IO::Lambda qw(:all :dev);
Subroutine BEGIN: called CC=1 SCAR=0.0
15
16 0 ▶sub _d { "message(" . _o($_[0]) . ")" }
Subroutine _d: not called CC=1 SCAR=6.9
17
18 sub new
19 {
20 0 ▶ my ( $class, $r, $w, %opt ) = @_;
Subroutine new: not called CC=8 SCAR=42.8
Pod documented
21
22 0 ▶ $opt{reader} ||= sysreader;
Condition0%0/3
$opt{"reader"} ||= &sysreader()
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt{"reader"} ||= &sysreader()
$opt{"reader"} &sysreader()
Truth table0%0/3
A ||= B
A= $opt{"reader"}B= &sysreader()
ABresult
1X1
011
000
23 0 ▶ $opt{writer} ||= syswriter;
Condition0%0/3
$opt{"writer"} ||= &syswriter()
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt{"writer"} ||= &syswriter()
$opt{"writer"} &syswriter()
Truth table0%0/3
A ||= B
A= $opt{"writer"}B= &syswriter()
ABresult
1X1
011
000
24 0 ▶ $opt{buf} ||= '';
Condition0%0/2
$opt{"buf"} ||= ""
l!l
00
MC/DC0%0/1
$opt{"buf"} ||= ""
$opt{"buf"}
Truth table0%0/2
A
A= $opt{"buf"}
Aresult
11
00
25 0 ▶ $opt{async} ||= 0;
Condition0%0/2
$opt{"async"} ||= 0
l!l
00
MC/DC0%0/1
$opt{"async"} ||= 0
$opt{"async"}
Truth table0%0/2
A
A= $opt{"async"}
Aresult
11
00
26
27 0 ▶ croak "Invalid read handle" unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 0 0 0
28 0 ▶ $w = $r unless $w;
Branch: 1 branch
BranchTrueFalseTotal
unless $w 0 0 0
29
30 0 my $self = bless {
31 %opt,
32 r => $r,
33 w => $w,
34 queue => [],
35 }, $class;
36
37 0 ▶ warn "new ", _d($self) . "\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Message::DEBUG 0 0 0
38
39 0 return $self;
40 }
41
42 # () :: (self, msg, deadline) -> error
43 sub sender
44 {
45 $_[0]->{sender} ||= lambda {
46 0 ▶ my ( $self, undef, $deadline) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
47 0 my $msg = sprintf("%08x%s%s%s", length($_[1]), $CRLF, $_[1], $CRLF);
48 0 ▶ warn _d($self), "msg > [$msg]\n" if $DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Message::DEBUG > 1 0 0 0
49 context
50 writebuf($self-> {writer}), $self-> {w},
51 0 \ $msg, undef, 0, $deadline;
52 tail {
53 0 ▶ $_[1]
Subroutine __ANON__: not called CC=1 SCAR=6.9
54 0 }}
55 0 ▶}
Subroutine sender: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/3
$_[0]{"sender"} ||= &lambda(sub { my($self, undef, $deadline) = @_; my $msg = sprintf("%08x%s%s%s", length $_[1], $CRLF, $_[1], $CRLF); warn _d($self), "msg > [$msg]\n" if $DEBUG > 1; context(writebuf($self->{"writer"}), $self->{"w"}, \$msg, undef, 0, $deadline); &tail(sub { $_[1]; } ); } )
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0]{"sender"} ||= &lambda(sub { my($self, undef, $deadline) = @_; my $msg = sprintf("%08x%s%s%s", length $_[1], $CRLF, $_[1], $CRLF); warn _d($self), "msg > [$msg]\n" if $DEBUG > 1; context(writebuf($self->{"writer"}), $self->{"w"}, \$msg, undef, 0, $deadline); &tail(sub { $_[1]; } ); } )
$_[0]{"sender"} &lambda(sub { my($self, undef, $deadline) = @_; my $msg = sprintf("%08x%s%s%s", length $_[1], $CRLF, $_[1], $CRLF); warn _d($self), "msg > [$msg]\n" if $DEBUG > 1; context(writebuf($self->{"writer"}), $self->{"w"}, \$msg, undef, 0, $deadline); &tail(sub { $_[1]; } ); } )
Truth table0%0/3
A ||= B
A= $_[0]{"sender"}B= &lambda(sub { my($self, undef, $deadline) = @_; my $msg = sprintf("%08x%s%s%s", length $_[1], $CRLF, $_[1], $CRLF); warn _d($self), "msg > [$msg]\n" if $DEBUG > 1; context(writebuf($self->{"writer"}), $self->{"w"}, \$msg, undef, 0, $deadline); &tail(sub { $_[1]; } ); } )
ABresult
1X1
011
000
56
57 # () :: (self, deadline) -> (msg, error)
58 sub receiver
59 {
60 $_[0]->{receiver} ||= lambda {
61 0 ▶ my ( $self, $deadline) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
62 context
63 0 readbuf($self-> {reader}), $self-> {r}, \$self-> {buf}, 9,
64 $deadline;
65 tail {
66 0 ▶ my ( $size, $error) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
67 0 ▶ return ( undef, $error) if defined $error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 0 0 0
68 0 $size = substr( $self-> {buf}, 0, 9, '');
69 0 ▶ return ( undef, "protocol error: chunk size not set")
Branch: 1 branch
BranchTrueFalseTotal
unless $size =~ /^[a-f0-9]+$/i 0 0 0
70 unless $size =~ /^[a-f0-9]+$/i;
71
72 0 chop $size;
73 0 $size = length($CRLF) + hex $size;
74
75 context
76 readbuf($self-> {reader}), $self-> {r}, \$self-> {buf},
77 0 $size, $deadline;
78 tail {
79 0 ▶ my $error = $_[1];
Subroutine __ANON__: not called CC=3 SCAR=24.8
80 0 ▶ return ( undef, $error) if defined $error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 0 0 0
81 0 my $msg = substr( $self-> {buf}, 0, $size, '');
82 0 chop $msg;
83 0 ▶ warn _d($self), "msg < [$msg]\n" if $DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Message::DEBUG > 1 0 0 0
84 0 return $msg;
85 0 }}}
86 0 ▶}
Subroutine receiver: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/3
$_[0]{"receiver"} ||= &lambda(sub { my($self, $deadline) = @_; context(readbuf($self->{"reader"}), $self->{"r"}, \$self->{"buf"}, 9, $deadline); &tail(sub { my($size, $error) = @_; return undef, $error if defined $error; $size = substr($self->{"buf"}, 0, 9, ""); return undef, "protocol error: chunk size not set" unless $size =~ /^[a-f0-9]+$/i; chop $size; $size = length($CRLF) + hex($size); context(readbuf($self->{"reader"}), $self->{"r"}, \$self->{"buf"}, $size, $deadline); &tail(sub { my $error = $_[1]; return undef, $error if defined $error; my $msg = substr($self->{"buf"}, 0, $size, ""); chop $msg; warn _d($self), "msg < [$msg]\n" if $DEBUG > 1; return $msg; } ); } ); } )
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0]{"receiver"} ||= &lambda(sub { my($self, $deadline) = @_; context(readbuf($self->{"reader"}), $self->{"r"}, \$self->{"buf"}, 9, $deadline); &tail(sub { my($size, $error) = @_; return undef, $error if defined $error; $size = substr($self->{"buf"}, 0, 9, ""); return undef, "protocol error: chunk size not set" unless $size =~ /^[a-f0-9]+$/i; chop $size; $size = length($CRLF) + hex($size); context(readbuf($self->{"reader"}), $self->{"r"}, \$self->{"buf"}, $size, $deadline); &tail(sub { my $error = $_[1]; return undef, $error if defined $error; my $msg = substr($self->{"buf"}, 0, $size, ""); chop $msg; warn _d($self), "msg < [$msg]\n" if $DEBUG > 1; return $msg; } ); } ); } )
$_[0]{"receiver"} &lambda(sub { my($self, $deadline) = @_; context(readbuf($self->{"reader"}), $self->{"r"}, \$self->{"buf"}, 9, $deadline); &tail(sub { my($size, $error) = @_; return undef, $error if defined $error; $size = substr($self->{"buf"}, 0, 9, ""); return undef, "protocol error: chunk size not set" unless $size =~ /^[a-f0-9]+$/i; chop $size; $size = length($CRLF) + hex($size); context(readbuf($self->{"reader"}), $self->{"r"}, \$self->{"buf"}, $size, $deadline); &tail(sub { my $error = $_[1]; return undef, $error if defined $error; my $msg = substr($self->{"buf"}, 0, $size, ""); chop $msg; warn _d($self), "msg < [$msg]\n" if $DEBUG > 1; return $msg; } ); } ); } )
Truth table0%0/3
A ||= B
A= $_[0]{"receiver"}B= &lambda(sub { my($self, $deadline) = @_; context(readbuf($self->{"reader"}), $self->{"r"}, \$self->{"buf"}, 9, $deadline); &tail(sub { my($size, $error) = @_; return undef, $error if defined $error; $size = substr($self->{"buf"}, 0, 9, ""); return undef, "protocol error: chunk size not set" unless $size =~ /^[a-f0-9]+$/i; chop $size; $size = length($CRLF) + hex($size); context(readbuf($self->{"reader"}), $self->{"r"}, \$self->{"buf"}, $size, $deadline); &tail(sub { my $error = $_[1]; return undef, $error if defined $error; my $msg = substr($self->{"buf"}, 0, $size, ""); chop $msg; warn _d($self), "msg < [$msg]\n" if $DEBUG > 1; return $msg; } ); } ); } )
ABresult
1X1
011
000
87
88 # () :: (self, msg, deadline) -> (response, error)
89 sub pusher
90 {
91 $_[0]->{pusher} ||= lambda {
92 0 ▶ my ( $self, undef, $deadline) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
93 0 context $self-> sender, $self, $_[1], $deadline;
94 tail {
95 0 ▶ my ( $result, $error) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
96 0 ▶ return ( undef, $error) if defined $error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 0 0 0
97 0 context $self-> receiver, $self, $deadline;
98 0 &tail();
99 0 }}
100 0 ▶}
Subroutine pusher: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/3
$_[0]{"pusher"} ||= &lambda(sub { my($self, undef, $deadline) = @_; context($self->sender, $self, $_[1], $deadline); &tail(sub { my($result, $error) = @_; return undef, $error if defined $error; context($self->receiver, $self, $deadline); &tail(); } ); } )
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0]{"pusher"} ||= &lambda(sub { my($self, undef, $deadline) = @_; context($self->sender, $self, $_[1], $deadline); &tail(sub { my($result, $error) = @_; return undef, $error if defined $error; context($self->receiver, $self, $deadline); &tail(); } ); } )
$_[0]{"pusher"} &lambda(sub { my($self, undef, $deadline) = @_; context($self->sender, $self, $_[1], $deadline); &tail(sub { my($result, $error) = @_; return undef, $error if defined $error; context($self->receiver, $self, $deadline); &tail(); } ); } )
Truth table0%0/3
A ||= B
A= $_[0]{"pusher"}B= &lambda(sub { my($self, undef, $deadline) = @_; context($self->sender, $self, $_[1], $deadline); &tail(sub { my($result, $error) = @_; return undef, $error if defined $error; context($self->receiver, $self, $deadline); &tail(); } ); } )
ABresult
1X1
011
000
101
102 # () :: (self, deadline) -> error
103 0 ▶sub incoming { die "abstract call" }
Subroutine incoming: not called CC=1 SCAR=6.9
Pod undocumented
104 sub puller
105 {
106 $_[0]->{puller} ||= lambda {
107 0 ▶ my ( $self, $deadline) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
108 0 context $self-> receiver, $self, $deadline;
109 tail {
110 0 ▶ my ( $msg, $error) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
111 0 ▶ return ( undef, $error) if defined $error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 0 0 0
112 0 $msg = $self-> incoming( $msg);
113
114 0 context $self-> sender, $self, $msg, $deadline;
115 0 &tail();
116 0 }}
117 0 ▶}
Subroutine puller: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/3
$_[0]{"puller"} ||= &lambda(sub { my($self, $deadline) = @_; context($self->receiver, $self, $deadline); &tail(sub { my($msg, $error) = @_; return undef, $error if defined $error; $msg = $self->incoming($msg); context($self->sender, $self, $msg, $deadline); &tail(); } ); } )
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0]{"puller"} ||= &lambda(sub { my($self, $deadline) = @_; context($self->receiver, $self, $deadline); &tail(sub { my($msg, $error) = @_; return undef, $error if defined $error; $msg = $self->incoming($msg); context($self->sender, $self, $msg, $deadline); &tail(); } ); } )
$_[0]{"puller"} &lambda(sub { my($self, $deadline) = @_; context($self->receiver, $self, $deadline); &tail(sub { my($msg, $error) = @_; return undef, $error if defined $error; $msg = $self->incoming($msg); context($self->sender, $self, $msg, $deadline); &tail(); } ); } )
Truth table0%0/3
A ||= B
A= $_[0]{"puller"}B= &lambda(sub { my($self, $deadline) = @_; context($self->receiver, $self, $deadline); &tail(sub { my($msg, $error) = @_; return undef, $error if defined $error; $msg = $self->incoming($msg); context($self->sender, $self, $msg, $deadline); &tail(); } ); } )
ABresult
1X1
011
000
118
119 sub error
120 {
121 0 ▶ return $_[0]-> {error} unless $#_;
Subroutine error: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless $#_ 0 0 0
122 0 $_[0]-> {error} = $_[1];
123 }
124
125 # lambda that sends all available messages in queue
126 # () :: self -> error
127 0 ▶sub outcoming { $_[1] }
Subroutine outcoming: not called CC=1 SCAR=6.9
Pod undocumented
128 sub queue_pusher
129 {
130 $_[0]->{queue_pusher} ||= lambda {
131 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
132
133 warn _d($self) . ": sending msg ",
134 length($self-> {queue}-> [0]-> [2]), " bytes ",
135 0 ▶ _t($self-> {queue}-> [0]-> [3]),
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Message::DEBUG 0 0 0
136 "\n" if $DEBUG;
137 context $self-> pusher,
138 $self,
139 $self-> {queue}-> [0]-> [2],
140 0 $self-> {queue}-> [0]-> [3];
141 tail {
142 0 ▶ my ( $result, $error) = @_;
Subroutine __ANON__: not called CC=7 SCAR=40.3
143 0 ▶ if ( defined $error) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $error) 0 0 0
144 0 $self-> error($error);
145 0 ▶ warn _d($self) . " > error $error\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Message::DEBUG 0 0 0
146 0 $self-> cancel_queue( undef, $error);
147 0 return $error;
148 }
149
150 # signal result to the outer lambda
151 0 my $q = shift @{$self-> {queue}};
152 0 ▶ unless ( $q) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($q) 0 0 0
153 # cancel_queue was called?
154 0 return;
155 }
156
157 0 my ( $outer, $bind) = @$q;
158 0 $outer-> resolve( $bind);
159 0 $outer-> terminate( $self-> outcoming( $result));
160
161 # stop if it's all
162 0 ▶ unless ( @{$self-> {queue}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->{"queue"};}) 0 0 0
163 0 ▶ warn _d($self) . ": push -> listen\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Message::DEBUG 0 0 0
164 0 $self-> listen;
165 0 return;
166 }
167 0 $q = $self-> {queue}-> [0];
168
169 # fire up the next request
170 0 ▶ warn _d($self) . ": sending msg ",
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Message::DEBUG 0 0 0
171 length($q->[2]), " bytes ",
172 _t($q->[3]),
173 "\n" if $DEBUG;
174 0 context $self-> pusher, $self, $q->[2], $q->[3];
175 0 again;
176 0 }}
177 0 ▶}
Subroutine queue_pusher: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/3
$_[0]{"queue_pusher"} ||= &lambda(sub { my $self = shift(); warn _d($self) . ': sending msg ', length $self->{"queue"}[0][2], " bytes ", _t($self->{"queue"}[0][3]), "\n" if $DEBUG; context($self->pusher, $self, $self->{"queue"}[0][2], $self->{"queue"}[0][3]); &tail(sub { my($result, $error) = @_; if (defined $error) { $self->error($error); warn _d($self) . " > error $error\n" if $DEBUG; $self->cancel_queue(undef, $error); return $error; } ; my $q = shift @{$$self{"queue"};}; unless ($q) { return; } ; my($outer, $bind) = @$q; $outer->resolve($bind); $outer->terminate($self->outcoming($result)); unless (@{$self->{"queue"};}) { warn _d($self) . ": push -> listen\n" if $DEBUG; $self->listen; return; } ; $q = $self->{"queue"}[0]; warn _d($self) . ': sending msg ', length $q->[2], " bytes ", _t($q->[3]), "\n" if $DEBUG; context($self->pusher, $self, $q->[2], $q->[3]); again(); } ); } )
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0]{"queue_pusher"} ||= &lambda(sub { my $self = shift(); warn _d($self) . ': sending msg ', length $self->{"queue"}[0][2], " bytes ", _t($self->{"queue"}[0][3]), "\n" if $DEBUG; context($self->pusher, $self, $self->{"queue"}[0][2], $self->{"queue"}[0][3]); &tail(sub { my($result, $error) = @_; if (defined $error) { $self->error($error); warn _d($self) . " > error $error\n" if $DEBUG; $self->cancel_queue(undef, $error); return $error; } ; my $q = shift @{$$self{"queue"};}; unless ($q) { return; } ; my($outer, $bind) = @$q; $outer->resolve($bind); $outer->terminate($self->outcoming($result)); unless (@{$self->{"queue"};}) { warn _d($self) . ": push -> listen\n" if $DEBUG; $self->listen; return; } ; $q = $self->{"queue"}[0]; warn _d($self) . ': sending msg ', length $q->[2], " bytes ", _t($q->[3]), "\n" if $DEBUG; context($self->pusher, $self, $q->[2], $q->[3]); again(); } ); } )
$_[0]{"queue_pusher"} &lambda(sub { my $self = shift(); warn _d($self) . ': sending msg ', length $self->{"queue"}[0][2], " bytes ", _t($self->{"queue"}[0][3]), "\n" if $DEBUG; context($self->pusher, $self, $self->{"queue"}[0][2], $self->{"queue"}[0][3]); &tail(sub { my($result, $error) = @_; if (defined $error) { $self->error($error); warn _d($self) . " > error $error\n" if $DEBUG; $self->cancel_queue(undef, $error); return $error; } ; my $q = shift @{$$self{"queue"};}; unless ($q) { return; } ; my($outer, $bind) = @$q; $outer->resolve($bind); $outer->terminate($self->outcoming($result)); unless (@{$self->{"queue"};}) { warn _d($self) . ": push -> listen\n" if $DEBUG; $self->listen; return; } ; $q = $self->{"queue"}[0]; warn _d($self) . ': sending msg ', length $q->[2], " bytes ", _t($q->[3]), "\n" if $DEBUG; context($self->pusher, $self, $q->[2], $q->[3]); again(); } ); } )
Truth table0%0/3
A ||= B
A= $_[0]{"queue_pusher"}B= &lambda(sub { my $self = shift(); warn _d($self) . ': sending msg ', length $self->{"queue"}[0][2], " bytes ", _t($self->{"queue"}[0][3]), "\n" if $DEBUG; context($self->pusher, $self, $self->{"queue"}[0][2], $self->{"queue"}[0][3]); &tail(sub { my($result, $error) = @_; if (defined $error) { $self->error($error); warn _d($self) . " > error $error\n" if $DEBUG; $self->cancel_queue(undef, $error); return $error; } ; my $q = shift @{$$self{"queue"};}; unless ($q) { return; } ; my($outer, $bind) = @$q; $outer->resolve($bind); $outer->terminate($self->outcoming($result)); unless (@{$self->{"queue"};}) { warn _d($self) . ": push -> listen\n" if $DEBUG; $self->listen; return; } ; $q = $self->{"queue"}[0]; warn _d($self) . ': sending msg ', length $q->[2], " bytes ", _t($q->[3]), "\n" if $DEBUG; context($self->pusher, $self, $q->[2], $q->[3]); again(); } ); } )
ABresult
1X1
011
000
178
179 # () :: self -> error
180 sub listener
181 {
182 $_[0]->{listener} ||= lambda {
183 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
184 0 context $self-> puller, $self;
185 tail {
186 0 ▶ my ( $result, $error) = @_;
Subroutine __ANON__: not called CC=5 SCAR=34.0
187 0 ▶ if ( defined $error) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $error) 0 0 0
188 0 $self-> error($error);
189 0 ▶ warn _d($self) . " > error $error\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Message::DEBUG 0 0 0
190 0 $self-> cancel_queue( undef, $error);
191 0 return $error;
192 }
193
194 # enough listening, now push
195 0 ▶ if ( @{$self-> {queue}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"queue"};}) 0 0 0
196 0 ▶ warn _d($self) . ": listen -> push\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Message::DEBUG 0 0 0
197 0 $self-> push;
198 0 return;
199 }
200
201 0 again;
202 0 }}
203 0 ▶}
Subroutine listener: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/3
$_[0]{"listener"} ||= &lambda(sub { my $self = shift(); context($self->puller, $self); &tail(sub { my($result, $error) = @_; if (defined $error) { $self->error($error); warn _d($self) . " > error $error\n" if $DEBUG; $self->cancel_queue(undef, $error); return $error; } ; if (@{$self->{"queue"};}) { warn _d($self) . ": listen -> push\n" if $DEBUG; $self->push; return; } ; again(); } ); } )
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0]{"listener"} ||= &lambda(sub { my $self = shift(); context($self->puller, $self); &tail(sub { my($result, $error) = @_; if (defined $error) { $self->error($error); warn _d($self) . " > error $error\n" if $DEBUG; $self->cancel_queue(undef, $error); return $error; } ; if (@{$self->{"queue"};}) { warn _d($self) . ": listen -> push\n" if $DEBUG; $self->push; return; } ; again(); } ); } )
$_[0]{"listener"} &lambda(sub { my $self = shift(); context($self->puller, $self); &tail(sub { my($result, $error) = @_; if (defined $error) { $self->error($error); warn _d($self) . " > error $error\n" if $DEBUG; $self->cancel_queue(undef, $error); return $error; } ; if (@{$self->{"queue"};}) { warn _d($self) . ": listen -> push\n" if $DEBUG; $self->push; return; } ; again(); } ); } )
Truth table0%0/3
A ||= B
A= $_[0]{"listener"}B= &lambda(sub { my $self = shift(); context($self->puller, $self); &tail(sub { my($result, $error) = @_; if (defined $error) { $self->error($error); warn _d($self) . " > error $error\n" if $DEBUG; $self->cancel_queue(undef, $error); return $error; } ; if (@{$self->{"queue"};}) { warn _d($self) . ": listen -> push\n" if $DEBUG; $self->push; return; } ; again(); } ); } )
ABresult
1X1
011
000
204
205 0 ▶sub is_pushing { $_[0]-> {queue_pusher} and $_[0]-> {queue_pusher}-> is_waiting }
Subroutine is_pushing: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"queue_pusher"} and $_[0]{"queue_pusher"}->is_waiting 0 0 0
206 0 ▶sub is_listening { $_[0]-> {listener} and $_[0]-> {listener}-> is_waiting }
Subroutine is_listening: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"listener"} and $_[0]{"listener"}->is_waiting 0 0 0
207
208 sub push
209 {
210 0 ▶ my ( $self) = @_;
Subroutine push: not called CC=5 SCAR=34.0
Pod undocumented
211
212 0 ▶ croak "won't start, have errors: $self->{error}" if $self-> {error};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"error"} 0 0 0
213 0 ▶ croak "won't start, already pushing" if $self-> is_pushing;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_pushing 0 0 0
214 0 ▶ croak "won't start, already listening" if $self-> is_listening;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_listening 0 0 0
215 0 ▶ warn _d($self) . ": start push\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Message::DEBUG 0 0 0
216
217 0 my $q = $self-> queue_pusher;
218 0 $q-> reset;
219 0 $q-> call($self);
220 0 $q-> start;
221 }
222
223 sub listen
224 {
225 0 ▶ my ( $self) = @_;
Subroutine listen: not called CC=6 SCAR=37.4
Pod undocumented
226
227 # need explicit consent
228 0 ▶ return unless $self-> {async};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"async"} 0 0 0
229
230 0 ▶ croak "won't listen, have errors: $self->{error}" if $self-> {error};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"error"} 0 0 0
231 0 ▶ croak "won't listen, already pushing" if $self-> is_pushing;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_pushing 0 0 0
232 0 ▶ croak "won't listen, already listening" if $self-> is_listening;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_listening 0 0 0
233 0 ▶ warn _d($self) . ": start listen\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Message::DEBUG 0 0 0
234
235 0 my $q = $self-> listener;
236 0 $q-> reset;
237 0 $q-> call($self);
238 0 $q-> start;
239 }
240
241 # cancel all messages, store error on all of them
242 sub cancel_queue
243 {
244 0 ▶ my ( $self, @reason) = @_;
Subroutine cancel_queue: not called CC=3 SCAR=24.8
Pod documented
245 0 ▶ return unless $self-> {queue};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"queue"} 0 0 0
246 0 for my $q ( @{ $self-> {queue}}) {
247 0 my ( $outer, $bind) = @$q;
248 0 $outer-> resolve( $bind);
249 0 $outer-> terminate( @reason);
250 }
251 0 @{ $self-> {queue} } = ();
252 }
253
254 # (msg,deadline) :: () -> (result,error)
255 sub new_message
256 {
257 0 ▶ my ( $self, $msg, $deadline) = @_;
Subroutine new_message: not called CC=5 SCAR=34.0
Pod documented
258
259 0 ▶ return lambda { $self-> error } if $self-> error;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $self->error 0 0 0
260
261 0 ▶ warn _d($self) . " > msg ", _t($deadline), " ", length($msg), " bytes\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Message::DEBUG 0 0 0
262
263 # won't end until we call resolve
264 0 my $outer = IO::Lambda-> new;
265 0 my $bind = $outer-> bind;
266 0 CORE::push @{ $self-> {queue} }, [ $outer, $bind, $msg, $deadline ];
267
268 0 ▶ $self-> push if 1 == @{$self-> {queue}} and not $self-> is_listening;
Branch: 1 branch
BranchTrueFalseTotal
if 1 == @{$self->{"queue"};} and not $self->is_listening 0 0 0
Condition0%0/3
1 == @{$self->{"queue"};} and not $self->is_listening
!ll&&!rl&&r
000
MC/DC0%0/2
1 == @{$self->{"queue"};} and not $self->is_listening
1 == @{$self->{"queue"};} not $self->is_listening
Truth table0%0/3
A and not(B)
A= 1 == @{$self->{"queue"};}B= not $self->is_listening
ABresult
0X0
100
111
269
270 0 return $outer;
271 }
272
273 0 ▶sub message(&) { new_message(context)-> condition( shift, \&message, 'message') }
Subroutine message: not called CC=1 SCAR=6.9
Pod documented
274
275 package IO::Lambda::Message::Simple;
276
277 1 ▶my $debug = $IO::Lambda::DEBUG{message} || 0;
Condition50%1/2
$IO::Lambda::DEBUG{'message'} || 0
l!l
01
MC/DC0%0/1
$IO::Lambda::DEBUG{'message'} || 0
$IO::Lambda::DEBUG{'message'}
Truth table50%1/2
A
A= $IO::Lambda::DEBUG{'message'}
Aresult
11
00
278
279 0 ▶sub _d { "simple_msg($_[0])" }
Subroutine _d: not called CC=1 SCAR=6.9
280
281 sub new
282 {
283 0 ▶ my ( $class, $r, $w) = @_;
Subroutine new: not called CC=3 SCAR=24.8
284 0 ▶ $w = $r unless $w;
Branch: 1 branch
BranchTrueFalseTotal
unless $w 0 0 0
285 0 my $self = bless {
286 r => $r,
287 w => $w,
288 }, $class;
289 0 ▶ warn "new ", _d($self) . "\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
290 0 return $self;
291 }
292
293 sub read
294 {
295 0 ▶ my $self = $_[0];
Subroutine read: not called CC=6 SCAR=37.4
296
297 0 my $size = readline($self-> {r});
298 0 ▶ die "bad size" unless defined($size) and $size =~ /^[0-9a-f]+\n$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $size and $size =~ /^[0-9a-f]+\n$/i 0 0 0
Condition0%0/3
defined $size and $size =~ /^[0-9a-f]+\n$/i
!ll&&!rl&&r
000
MC/DC0%0/2
defined $size and $size =~ /^[0-9a-f]+\n$/i
defined $size $size =~ /^[0-9a-f]+\n$/i
Truth table0%0/3
A and B
A= defined $sizeB= $size =~ /^[0-9a-f]+\n$/i
ABresult
0X0
100
111
299 0 chop $size;
300 0 $size = 1 + hex $size;
301
302 0 my $buf = '';
303 0 ▶ while ( $size > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size > 0) 0 0 0
304 0 my $b = readline($self-> {r});
305 0 ▶ die "can't read from socket: $!"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $b 0 0 0
306 unless defined $b;
307 0 $size -= length($b);
308 0 $buf .= $b;
309 }
310
311 0 chop $buf;
312
313 0 ▶ warn _d($self) . ": ", length($buf), " bytes read\n" if $debug > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $debug > 1 0 0 0
314
315 0 return $buf;
316 }
317
318 sub write
319 {
320 0 ▶ my ( $self, $msg) = @_;
Subroutine write: not called CC=3 SCAR=24.8
321 0 ▶ printf( { $self-> {w} } "%08x\x0a%s\x0a", length($msg), $msg)
Branch: 1 branch
BranchTrueFalseTotal
printf {$self->{"w"};} "%08x\n%s\n", length $msg, $msg or die "can't write to socket: $!" 0 0 0
322 or die "can't write to socket: $!";
323 0 ▶ warn _d($self) . ": ", length($msg), " bytes written\n" if $debug > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $debug > 1 0 0 0
324 }
325
326 0 ▶sub quit { $_[0]-> {run} = 0 }
Subroutine quit: not called CC=1 SCAR=6.9
327
328 sub run
329 {
330 0 ▶ my $self = $_[0];
Subroutine run: not called CC=20 SCAR=60.4
331
332 0 $self-> {run} = 1;
333 0 $self-> {w}-> autoflush(1);
334
335 0 ▶ while ( $self-> {run} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"run"}) 0 0 0
336 0 my ( $msg, $error) = $self-> read;
337 0 ▶ die "bad message: $error" if defined $error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 0 0 0
338 0 ( $msg, $error) = $self-> decode( $msg);
339
340 0 my $response;
341 0 ▶ if ( defined $error) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $error) 0 0 0
342 0 $response = [0, "bad message: $error"];
343 0 ▶ warn _d($self) . ": bad message: $error\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
344 0 goto SEND;
345 }
346 0 ▶ unless ( $msg and ref($msg) and ref($msg) eq 'ARRAY' and @$msg > 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($msg and ref $msg and ref $msg eq "ARRAY" and @$msg > 0) 0 0 0
Condition0%0/9
$msg and ref $msg and ref $msg eq "ARRAY" and @$msg > 0
!ll&&!rl&&r
000
$msg and ref $msg and ref $msg eq "ARRAY"
!ll&&!rl&&r
000
$msg and ref $msg
!ll&&!rl&&r
000
MC/DC0%0/4
$msg and ref $msg and ref $msg eq "ARRAY" and @$msg > 0
$msg ref $msg ref $msg eq "ARRAY" @$msg > 0
Truth table0%0/5
A and B and C and D
A= $msgB= ref $msgC= ref $msg eq "ARRAY"D= @$msg > 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
347 0 $response = [0, "bad message"];
348 0 ▶ warn _d($self) . ": bad message\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
349 0 goto SEND;
350 }
351
352 0 my $method = shift @$msg;
353
354 0 ▶ if ( $self-> can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($method)) { } 0 0 0
355 0 my $wantarray = shift @$msg;
356 0 my @r;
357 0 eval {
358 0 ▶ if ( $wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) { } 0 0 0
359 0 @r = $self-> $method(@$msg);
360 } else {
361 0 $r[0] = $self-> $method(@$msg);
362 }
363 };
364 0 ▶ if ( $@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
365 0 ▶ warn _d($self) . ": $method / died $@\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
366 0 $response = [0, $@];
367 0 $self-> quit;
368 } else {
369 0 ▶ warn _d($self) . ": $method / ok\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
370 0 $response = [1, @r];
371 }
372 } else {
373 0 ▶ warn _d($self) . ": no such method: $method\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
374 0 $response = [0, 'no such method'];
375 };
376 0 SEND:
377 ( $msg, $error) = $self-> encode($response);
378 0 ▶ if ( defined $error) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $error) 0 0 0
379 0 ▶ warn _d($self) . ": encode error $error\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
380 0 ( $msg, $error) = $self-> encode([0, $error]);
381 0 ▶ die $error if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 0 0
382 }
383 0 $self-> write($msg);
384 }
385
386 0 ▶ warn _d($self) . " quit\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
387 }
388
389 1 1;
390
391 __DATA__
« Lambda/Loop/Prima.pm ↑ Summary Lambda/HTTP/HTTPS.pm »