ResponseParser.pm

filter: Statementstmt 80.8% 59 / 73 Branchbran 72.7% 16 / 22 Conditioncond 50.0% 3 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 84.6% 11 / 13 Podpod 87.5% 7 / 8 totaltotal 77.7% 98 / 126 CC 14 SCAR 20.2
CC
14
Cov
77%
parse_sock
20.0
parse_data
10.1
new
2.0
CRAP 7.5
« Client.pm ↑ Summary Objects.pm »
1 package Gearman::ResponseParser;
2 8 ▶use version ();
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 $Gearman::ResponseParser::VERSION = version->declare("2.004.015");
4  
5 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Gearman::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Gearman::ResponseParser - gearmand abstract response parser implementation
12  
13 =head1 DESCRIPTION
14  
15  
16 I<Gearman::ResponseParser> is an abstract base class.
17  
18 See: L<Gearman::ResponseParser::Taskset>
19  
20 Subclasses should call this first, then add their own data in underscore members
21  
22 =head1 METHODS
23  
24 =cut
25  
26 # Gearman::ResponseParser::Danga (for Gearman::Client::Danga, the async version)
27 4 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
28 4 my $class = shift;
29 4 my %opts = @_;
30 4 my $src = delete $opts{'source'};
31 4 ▶ die "unsupported arguments '@{[keys %opts]}'" if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 1 3 4
32  
33 3 my $self = bless {
34  
35 # the source object/socket that is primarily feeding this.
36 source => $src,
37 }, $class;
38  
39 3 $self->reset;
40 3 return $self;
41 } ## end sub new
42  
43 =head2 source()
44  
45 B<return> source. The source is object/socket
46  
47 =cut
48  
49 1 ▶sub source {
Subroutine source: called CC=1 SCAR=0.0
Pod documented
50 1 my $self = shift;
51 1 return $self->{source};
52 }
53  
54 =head2 on_packet($packet, $parser)
55  
56 subclasses should override this
57  
58 =cut
59  
60 1 ▶sub on_packet {
Subroutine on_packet: called CC=1 SCAR=0.0
Pod documented
61 1 my ($self, $packet, $parser) = @_;
62 1 die "SUBCLASSES SHOULD OVERRIDE THIS";
63 }
64  
65 =head2 on_error($msg, $parser)
66  
67 subclasses should override this
68  
69 =cut
70  
71 2 ▶sub on_error {
Subroutine on_error: called CC=1 SCAR=0.0
Pod documented
72 2 my ($self, $errmsg, $parser) = @_;
73  
74 # NOTE: this interface will evolve.
75 2 die "SUBCLASSES SHOULD OVERRIDE THIS";
76 } ## end sub on_error
77  
78 =head2 reset()
79  
80 =cut
81  
82 12 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
83 12 my $self = shift;
84 12 $self->{header} = '';
85 12 $self->{pkt} = undef;
86 }
87  
88 =head2 parse_data($data)
89  
90 don't override:
91 FUTURE OPTIMIZATION: let caller say "you can own this scalarref", and then we can keep it
92 on the initial setting of $self->{data} and avoid copying into our own. overkill for now.
93  
94 =cut
95  
96 9 ▶sub parse_data {
Subroutine parse_data: called CC=10 SCAR=23.1
Pod documented
97 9 my ($self, $data) = @_; # where $data is a scalar or scalarref to parse
98 9 ▶ my $dataref = ref $data ? $data : \$data;
Branch: 1 branch
BranchTrueFalseTotal
ref $data ? : 9 0 9
99  
100 0 ▶ my $err = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
101 0 my $code = shift;
102 0 $self->on_error($code);
103 0 return undef;
104 9 };
105  
106 9 ▶ while (my $lendata = length $$data) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $lendata = length $$data) 16 9 25
107  
108 # read the header
109 16 my $hdr_len = length $self->{header};
110 16 ▶ unless ($hdr_len == 12) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hdr_len == 12) 9 7 16
111 9 my $need = 12 - $hdr_len;
112 9 $self->{header} .= substr($$dataref, 0, $need, '');
113 9 ▶ next unless length $self->{header} == 12;
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"header"} == 12 1 8 9
114  
115 8 my ($magic, $type, $len) = unpack("a4NN", $self->{header});
116 8 ▶ return $err->("malformed_magic") unless $magic eq "\0RES";
Branch: 1 branch
BranchTrueFalseTotal
unless $magic eq "\0RES" 0 8 8
117  
118 8 my $blob = "";
119 $self->{pkt} = {
120 8 type => Gearman::Util::cmd_name($type),
121 len => $len,
122 blobref => \$blob,
123 };
124 8 next;
125 } ## end unless ($hdr_len == 12)
126  
127 # how much data haven't we read for the current packet?
128 7 my $need = $self->{pkt}{len} - length(${ $self->{pkt}{blobref} });
129  
130 # copy the MAX(need, have)
131 7 ▶ my $to_copy = $lendata > $need ? $need : $lendata;
Branch: 1 branch
BranchTrueFalseTotal
$lendata > $need ? : 2 5 7
132  
133 7 ${ $self->{pkt}{blobref} } .= substr($$dataref, 0, $to_copy, '');
134  
135 7 ▶ if ($to_copy == $need) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to_copy == $need) 5 2 7
136 5 $self->on_packet($self->{pkt}, $self);
137 5 $self->reset;
138 }
139 } ## end while (my $lendata = length...)
140  
141 9 ▶ if (defined($self->{pkt})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"pkt"} and length ${$self->{"pkt"}{"blobref"};} == $self->{"pkt"}{"len"}) 3 6 9
Condition100%3/3
defined $self->{"pkt"} and length ${$self->{"pkt"}{"blobref"};} == $self->{"pkt"}{"len"}
!ll&&!rl&&r
423
MC/DC100%2/2
defined $self->{"pkt"} and length ${$self->{"pkt"}{"blobref"};} == $self->{"pkt"}{"len"}
defined $self->{"pkt"} length ${$self->{"pkt"}{"blobref"};} == $self->{"pkt"}{"len"}
Truth table100%3/3
A and B
A= defined $self->{"pkt"}B= length ${$self->{"pkt"}{"blobref"};} == $self->{"pkt"}{"len"}
ABresult
0X0
100
111
142 5 && length(${ $self->{pkt}{blobref} }) == $self->{pkt}{len})
143 {
144 3 $self->on_packet($self->{pkt}, $self);
145 3 $self->reset;
146 } ## end if (defined($self->{pkt...}))
147 } ## end sub parse_data
148  
149 =head2 eof()
150  
151 don't override
152  
153 =cut
154  
155 1 ▶sub eof {
Subroutine eof: called CC=1 SCAR=0.0
Pod documented
156 1 my $self = shift;
157  
158 1 $self->on_error("EOF");
159  
160 # ERROR if in middle of packet
161 } ## end sub eof
162  
163 =head2 parse_sock($sock)
164  
165 don't override
166  
167 C<$sock> is readable, we should sysread it and feed it to L<parse_data($data)>
168  
169 =cut
170  
171 0 ▶sub parse_sock {
Subroutine parse_sock: not called CC=4 SCAR=30.0
Pod documented
172 0 my ($self, $sock) = @_;
173 0 my $res = Gearman::Util::read_res_packet($sock, \my $err);
174 0 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 0 0
175 0 $self->on_error("read_error: ${$err}");
176 0 return;
177 }
178  
179 0 $self->{pkt} = $res;
180 0 ▶ if (defined($self->{pkt})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"pkt"} and length ${$self->{"pkt"}{"blobref"};} == $self->{"pkt"}{"len"}) 0 0 0
Condition0%0/3
defined $self->{"pkt"} and length ${$self->{"pkt"}{"blobref"};} == $self->{"pkt"}{"len"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"pkt"} and length ${$self->{"pkt"}{"blobref"};} == $self->{"pkt"}{"len"}
defined $self->{"pkt"} length ${$self->{"pkt"}{"blobref"};} == $self->{"pkt"}{"len"}
Truth table0%0/3
A and B
A= defined $self->{"pkt"}B= length ${$self->{"pkt"}{"blobref"};} == $self->{"pkt"}{"len"}
ABresult
0X0
100
111
181 0 && length(${ $self->{pkt}{blobref} }) == $self->{pkt}{len})
182 {
183 0 $self->on_packet($self->{pkt}, $self);
184 0 $self->reset;
185 } ## end if (defined($self->{pkt...}))
186 } ## end sub parse_sock
187  
188 8 1;
« Client.pm ↑ Summary Objects.pm »