blib/lib/Thrift/Server.pm

filter: Statementstmt 27.2% 42 / 154 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 50.0% 13 / 26 Podpod 0.0% 0 / 2 totaltotal 24.0% 55 / 229 CC 20 SCAR 25.4
CC
20
Cov
22%
_handleException
30.0
tryClose
30.0
new
20.0
CRAP 12.6
« blib/lib/Thrift/Exception.pm ↑ Summary blib/lib/Thrift/FramedTransport.pm »
1 #
2 # Licensed to the Apache Software Foundation (ASF) under one
3 # or more contributor license agreements. See the NOTICE file
4 # distributed with this work for additional information
5 # regarding copyright ownership. The ASF licenses this file
6 # to you under the Apache License, Version 2.0 (the
7 # "License"); you may not use this file except in compliance
8 # with the License. You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing,
13 # software distributed under the License is distributed on an
14 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 # KIND, either express or implied. See the License for the
16 # specific language governing permissions and limitations
17 # under the License.
18 #
19  
20 1 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 ▶use Thrift;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use Thrift::BinaryProtocol;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use Thrift::BufferedTransport;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use Thrift::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 #
30 # Server base class module
31 #
32 package Thrift::Server;
33 1 ▶use version 0.77; our $VERSION = version->declare("$Thrift::VERSION");
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 #
36 # 3 possible constructors:
37 # 1. (processor, serverTransport)
38 # Uses a BufferedTransportFactory and a BinaryProtocolFactory.
39 # 2. (processor, serverTransport, transportFactory, protocolFactory)
40 # Uses the same factory for input and output of each type.
41 # 3. (processor, serverTransport,
42 # inputTransportFactory, outputTransportFactory,
43 # inputProtocolFactory, outputProtocolFactory)
44 #
45 sub new
46 0 ▶{
Subroutine new: not called CC=4 SCAR=30.0
Pod undocumented
47 0 my $classname = shift;
48 0 my @args = @_;
49  
50 0 my $self;
51  
52 0 ▶ if (scalar @args == 2)
Branch: 3 branches
BranchTrueFalseTotal
if (scalar @args == 2) { } 0 0 0
elsif (scalar @args == 4) { } 0 0 0
elsif (scalar @args == 6) { } 0 0 0
53 {
54 0 $self = _init($args[0], $args[1],
55 Thrift::BufferedTransportFactory->new(),
56 Thrift::BufferedTransportFactory->new(),
57 Thrift::BinaryProtocolFactory->new(),
58 Thrift::BinaryProtocolFactory->new());
59 }
60 elsif (scalar @args == 4)
61 {
62 0 $self = _init($args[0], $args[1], $args[2], $args[2], $args[3], $args[3]);
63 }
64 elsif (scalar @args == 6)
65 {
66 0 $self = _init($args[0], $args[1], $args[2], $args[3], $args[4], $args[5]);
67 }
68 else
69 {
70 0 die Thrift::TException->new('Thrift::Server expects exactly 2, 4, or 6 args');
71 }
72  
73 0 return bless($self,$classname);
74 }
75  
76 sub _init
77 0 ▶{
Subroutine _init: not called CC=1 SCAR=6.9
78 0 my $processor = shift;
79 0 my $serverTransport = shift;
80 0 my $inputTransportFactory = shift;
81 0 my $outputTransportFactory = shift;
82 0 my $inputProtocolFactory = shift;
83 0 my $outputProtocolFactory = shift;
84  
85 0 my $self = {
86 processor => $processor,
87 serverTransport => $serverTransport,
88 inputTransportFactory => $inputTransportFactory,
89 outputTransportFactory => $outputTransportFactory,
90 inputProtocolFactory => $inputProtocolFactory,
91 outputProtocolFactory => $outputProtocolFactory,
92 };
93 }
94  
95 sub serve
96 0 ▶{
Subroutine serve: not called CC=1 SCAR=6.9
Pod undocumented
97 0 die 'abstract';
98 }
99  
100 sub _clientBegin
101 0 ▶{
Subroutine _clientBegin: not called CC=3 SCAR=24.8
102 0 my $self = shift;
103 0 my $iprot = shift;
104 0 my $oprot = shift;
105  
106 0 ▶ if (exists $self->{serverEventHandler} and
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"serverEventHandler"} and defined $self->{"serverEventHandler"}) 0 0 0
Condition0%0/3
exists $self->{"serverEventHandler"} and defined $self->{"serverEventHandler"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{"serverEventHandler"} and defined $self->{"serverEventHandler"}
exists $self->{"serverEventHandler"} defined $self->{"serverEventHandler"}
Truth table0%0/3
A and B
A= exists $self->{"serverEventHandler"}B= defined $self->{"serverEventHandler"}
ABresult
0X0
100
111
107 defined $self->{serverEventHandler})
108 {
109 0 $self->{serverEventHandler}->clientBegin($iprot, $oprot);
110 }
111 }
112  
113 sub _handleException
114 0 ▶{
Subroutine _handleException: not called CC=5 SCAR=34.0
115 0 my $self = shift;
116 0 my $e = shift;
117  
118 0 ▶ if ($e->isa('Thrift::TException') and exists $e->{message}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->isa('Thrift::TException') and exists $e->{'message'}) { } 0 0 0
Condition0%0/3
$e->isa("Thrift::TException") and exists $e->{"message"}
!ll&&!rl&&r
000
MC/DC0%0/2
$e->isa("Thrift::TException") and exists $e->{"message"}
$e->isa("Thrift::TException") exists $e->{"message"}
Truth table0%0/3
A and B
A= $e->isa("Thrift::TException")B= exists $e->{"message"}
ABresult
0X0
100
111
119 0 my $message = $e->{message};
120 0 my $code = $e->{code};
121 0 my $out = $code . ':' . $message;
122  
123 0 ▶ $message =~ m/TTransportException/ and die $out;
Branch: 1 branch
BranchTrueFalseTotal
$message =~ /TTransportException/ and die $out 0 0 0
124 0 ▶ if ($message =~ m/Socket/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($message =~ /Socket/) { } 0 0 0
125 # suppress Socket messages
126 }
127 else {
128 0 warn $out;
129 }
130 }
131 else {
132 0 warn $e;
133 }
134 }
135  
136 #
137 # SimpleServer from the Server base class that handles one connection at a time
138 #
139 package Thrift::SimpleServer;
140 1 ▶use parent -norequire, 'Thrift::Server';
Subroutine BEGIN: called CC=1 SCAR=0.0
141 1 ▶use version 0.77; our $VERSION = version->declare("$Thrift::VERSION");
Subroutine BEGIN: called CC=1 SCAR=0.0
142  
143 sub new
144 0 ▶{
Subroutine new: not called CC=1 SCAR=6.9
145 0 my $classname = shift;
146  
147 0 my $self = $classname->SUPER::new(@_);
148  
149 0 return bless($self,$classname);
150 }
151  
152 sub serve
153 0 ▶{
Subroutine serve: not called CC=4 SCAR=30.0
154 0 my $self = shift;
155 0 my $stop = 0;
156  
157 0 $self->{serverTransport}->listen();
158 0 ▶ while (!$stop) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $stop) 0 0 0
159 0 my $client = $self->{serverTransport}->accept();
160 0 ▶ if (defined $client) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $client) { } 0 0 0
161 0 my $itrans = $self->{inputTransportFactory}->getTransport($client);
162 0 my $otrans = $self->{outputTransportFactory}->getTransport($client);
163 0 my $iprot = $self->{inputProtocolFactory}->getProtocol($itrans);
164 0 my $oprot = $self->{outputProtocolFactory}->getProtocol($otrans);
165 0 eval {
166 0 $self->_clientBegin($iprot, $oprot);
167 0 while (1)
168 {
169 0 $self->{processor}->process($iprot, $oprot);
170 }
171 };
172 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
173 0 $self->_handleException($@);
174 }
175 0 $itrans->close();
176 0 $otrans->close();
177 } else {
178 0 $stop = 1;
179 }
180 }
181 }
182  
183  
184 #
185 # ForkingServer that forks a new process for each request
186 #
187 package Thrift::ForkingServer;
188 1 ▶use parent -norequire, 'Thrift::Server';
Subroutine BEGIN: called CC=1 SCAR=0.0
189 1 ▶use POSIX ':sys_wait_h';
Subroutine BEGIN: called CC=1 SCAR=0.0
190 1 ▶use version 0.77; our $VERSION = version->declare("$Thrift::VERSION");
Subroutine BEGIN: called CC=1 SCAR=0.0
191  
192 sub new
193 0 ▶{
Subroutine new: not called CC=1 SCAR=6.9
194 0 my $classname = shift;
195 0 my @args = @_;
196  
197 0 my $self = $classname->SUPER::new(@args);
198 0 return bless($self,$classname);
199 }
200  
201  
202 sub serve
203 0 ▶{
Subroutine serve: not called CC=1 SCAR=6.9
204 0 my $self = shift;
205  
206 # THRIFT-3848: without ignoring SIGCHLD, perl ForkingServer goes into a tight loop
207 0 $SIG{CHLD} = 'IGNORE';
208  
209 0 $self->{serverTransport}->listen();
210 0 while (1)
211 {
212 0 my $client = $self->{serverTransport}->accept();
213 0 $self->_client($client);
214 }
215 }
216  
217 sub _client
218 0 ▶{
Subroutine _client: not called CC=3 SCAR=24.8
219 0 my $self = shift;
220 0 my $client = shift;
221  
222 0 eval {
223 0 my $itrans = $self->{inputTransportFactory}->getTransport($client);
224 0 my $otrans = $self->{outputTransportFactory}->getTransport($client);
225  
226 0 my $iprot = $self->{inputProtocolFactory}->getProtocol($itrans);
227 0 my $oprot = $self->{outputProtocolFactory}->getProtocol($otrans);
228  
229 0 $self->_clientBegin($iprot, $oprot);
230  
231 0 my $pid = fork();
232  
233 0 ▶ if ($pid)
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 0 0 0
234 {
235 0 $self->_parent($pid, $itrans, $otrans);
236 }
237 else {
238 0 $self->_child($itrans, $otrans, $iprot, $oprot);
239 }
240 };
241 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
242 0 $self->_handleException($@);
243 }
244 }
245  
246 sub _parent
247 0 ▶{
Subroutine _parent: not called CC=1 SCAR=6.9
248 0 my $self = shift;
249 0 my $pid = shift;
250 0 my $itrans = shift;
251 0 my $otrans = shift;
252  
253 # Parent must close socket or the connection may not get closed promptly
254 0 $self->tryClose($itrans);
255 0 $self->tryClose($otrans);
256 }
257  
258 sub _child
259 0 ▶{
Subroutine _child: not called CC=2 SCAR=17.9
260 0 my $self = shift;
261 0 my $itrans = shift;
262 0 my $otrans = shift;
263 0 my $iprot = shift;
264 0 my $oprot = shift;
265  
266 0 my $ecode = 0;
267 0 eval {
268 # THRIFT-4065 ensure child process has normal signal handling in case thrift handler uses it
269 0 $SIG{CHLD} = 'DEFAULT';
270 0 while (1)
271 {
272 0 $self->{processor}->process($iprot, $oprot);
273 }
274 };
275 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
276 0 $ecode = 1;
277 0 $self->_handleException($@);
278 }
279  
280 0 $self->tryClose($itrans);
281 0 $self->tryClose($otrans);
282  
283 0 exit($ecode);
284 }
285  
286 sub tryClose
287 0 ▶{
Subroutine tryClose: not called CC=5 SCAR=34.0
288 0 my $self = shift;
289 0 my $file = shift;
290  
291 0 eval {
292 0 ▶ if (defined $file)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file) 0 0 0
293 {
294 0 $file->close();
295 }
296 };
297 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
298 0 ▶ if ($@->isa('Thrift::TException') and exists $@->{message}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@->isa('Thrift::TException') and exists $@->{'message'}) { } 0 0 0
Condition0%0/3
$@->isa("Thrift::TException") and exists $@->{"message"}
!ll&&!rl&&r
000
MC/DC0%0/2
$@->isa("Thrift::TException") and exists $@->{"message"}
$@->isa("Thrift::TException") exists $@->{"message"}
Truth table0%0/3
A and B
A= $@->isa("Thrift::TException")B= exists $@->{"message"}
ABresult
0X0
100
111
299 0 my $message = $@->{message};
300 0 my $code = $@->{code};
301 0 my $out = $code . ':' . $message;
302  
303 0 warn $out;
304 }
305 else {
306 0 warn $@;
307 }
308 }
309 }
310  
311 1 1;
« blib/lib/Thrift/Exception.pm ↑ Summary blib/lib/Thrift/FramedTransport.pm »