blob: e8c1cfa9e3361f09ffc25e9d385204cc0e5e8071 [file] [log] [blame]
Jim Kingf5f1b352015-06-24 13:47:24 -04001#!/usr/bin/env perl
2
3#
4# Licensed to the Apache Software Foundation (ASF) under one
5# or more contributor license agreements. See the NOTICE file
6# distributed with this work for additional information
7# regarding copyright ownership. The ASF licenses this file
8# to you under the Apache License, Version 2.0 (the
9# "License"); you may not use this file except in compliance
10# with the License. You may obtain a copy of the License at
11#
12# http://www.apache.org/licenses/LICENSE-2.0
13#
14# Unless required by applicable law or agreed to in writing,
15# software distributed under the License is distributed on an
16# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17# KIND, either express or implied. See the License for the
18# specific language governing permissions and limitations
19# under the License.
20#
21
James E. King, III177c37c2017-03-30 17:09:04 -040022use 5.10.0;
Jim Kingf5f1b352015-06-24 13:47:24 -040023use strict;
24use warnings;
25use Data::Dumper;
26use Getopt::Long qw(GetOptions);
27use Time::HiRes qw(gettimeofday);
28
James E. King III9bea32f2018-03-16 16:07:42 -040029$SIG{INT} = \&sigint_handler;
30
Jim Kingf5f1b352015-06-24 13:47:24 -040031use lib '../../lib/perl/lib';
32use lib 'gen-perl';
33
34use Thrift;
35use Thrift::BinaryProtocol;
36use Thrift::BufferedTransport;
37use Thrift::FramedTransport;
James E. King, III20e16bc2017-11-18 22:37:54 -050038use Thrift::MultiplexedProcessor;
Jim Kingf5f1b352015-06-24 13:47:24 -040039use Thrift::SSLServerSocket;
40use Thrift::ServerSocket;
41use Thrift::Server;
James E. King, III49f4dc02015-10-29 15:52:23 -040042use Thrift::UnixServerSocket;
Jim Kingf5f1b352015-06-24 13:47:24 -040043
James E. King, III20e16bc2017-11-18 22:37:54 -050044use ThriftTest::SecondService;
Jim Kingf5f1b352015-06-24 13:47:24 -040045use ThriftTest::ThriftTest;
46use ThriftTest::Types;
47
48$|++;
49
50sub usage {
51 print <<EOF;
52Usage: $0 [OPTIONS]
53
54Options: (default)
55 --ca Certificate authority file (optional).
56 --cert Certificate file.
James E. King, III377719c2017-02-15 14:33:20 -050057 Required if using --ssl.
58 --ciphers Acceptable cipher list.
James E. King, III49f4dc02015-10-29 15:52:23 -040059 --domain-socket <file> Use a unix domain socket.
Jim Kingf5f1b352015-06-24 13:47:24 -040060 --help Show usage.
61 --key Private key file for certificate.
62 Required if using --ssl and private key is
63 not in the certificate file.
64 --port <portnum> 9090 Port to use.
65 --protocol {binary} binary Protocol to use.
66 --ssl If present, use SSL/TLS.
67 --transport {buffered|framed} buffered Transport to use.
James E. King, III377719c2017-02-15 14:33:20 -050068
Jim Kingf5f1b352015-06-24 13:47:24 -040069EOF
70}
71
72my %opts = (
73 'port' => 9090,
74 'protocol' => 'binary',
75 'transport' => 'buffered'
76);
77
78GetOptions(\%opts, qw (
79 ca=s
80 cert=s
James E. King, III377719c2017-02-15 14:33:20 -050081 ciphers=s
James E. King, III49f4dc02015-10-29 15:52:23 -040082 domain-socket=s
Jim Kingf5f1b352015-06-24 13:47:24 -040083 help
84 host=s
85 key=s
86 port=i
87 protocol=s
88 ssl
89 transport=s
90)) || exit 1;
91
92if ($opts{help}) {
93 usage();
94 exit 0;
95}
96
97if ($opts{ssl} and not defined $opts{cert}) {
98 usage();
99 exit 1;
100}
101
102my $handler = new ThriftTestHandler();
James E. King, III20e16bc2017-11-18 22:37:54 -0500103my $handler2 = new SecondServiceHandler();
Jim Kingf5f1b352015-06-24 13:47:24 -0400104my $processor = new ThriftTest::ThriftTestProcessor($handler);
James E. King, III20e16bc2017-11-18 22:37:54 -0500105my $processor2 = new ThriftTest::SecondServiceProcessor($handler2);
Jim Kingf5f1b352015-06-24 13:47:24 -0400106my $serversocket;
James E. King, III49f4dc02015-10-29 15:52:23 -0400107if ($opts{"domain-socket"}) {
108 unlink($opts{"domain-socket"});
109 $serversocket = new Thrift::UnixServerSocket($opts{"domain-socket"});
110} elsif ($opts{ssl}) {
Jim Kingf5f1b352015-06-24 13:47:24 -0400111 $serversocket = new Thrift::SSLServerSocket(\%opts);
112} else {
113 $serversocket = new Thrift::ServerSocket(\%opts);
114}
115my $transport;
116if ($opts{transport} eq 'buffered') {
117 $transport = new Thrift::BufferedTransportFactory();
118} elsif ($opts{transport} eq 'framed') {
119 $transport = new Thrift::FramedTransportFactory();
120} else {
121 usage();
122 exit 1;
123}
124my $protocol;
James E. King, III20e16bc2017-11-18 22:37:54 -0500125if ($opts{protocol} eq 'binary' || $opts{protocol} eq 'multi') {
Jim Kingf5f1b352015-06-24 13:47:24 -0400126 $protocol = new Thrift::BinaryProtocolFactory();
127} else {
128 usage();
129 exit 1;
130}
131
James E. King, III20e16bc2017-11-18 22:37:54 -0500132if (index($opts{protocol}, 'multi') == 0) {
133 my $newProcessor = new Thrift::MultiplexedProcessor($protocol);
134 $newProcessor->defaultProcessor($processor);
135 $newProcessor->registerProcessor("ThriftTest", $processor);
136 $newProcessor->registerProcessor("SecondService", $processor2);
137 $processor = $newProcessor;
138}
139
Jim Kingf5f1b352015-06-24 13:47:24 -0400140my $ssltag = '';
141if ($opts{ssl}) {
142 $ssltag = "(SSL)";
143}
James E. King, III49f4dc02015-10-29 15:52:23 -0400144my $listening_on = "$opts{port} $ssltag";
145if ($opts{"domain-socket"}) {
146 $listening_on = $opts{"domain-socket"};
147}
Jim Kingf5f1b352015-06-24 13:47:24 -0400148my $server = new Thrift::SimpleServer($processor, $serversocket, $transport, $protocol);
James E. King, III49f4dc02015-10-29 15:52:23 -0400149print "Starting \"simple\" server ($opts{transport}/$opts{protocol}) listen on: $listening_on\n";
Jim Kingf5f1b352015-06-24 13:47:24 -0400150$server->serve();
James E. King III9bea32f2018-03-16 16:07:42 -0400151print "done.\n";
152
153sub sigint_handler {
154 print "received SIGINT, stopping...\n";
155 $server->stop();
156}
Jim Kingf5f1b352015-06-24 13:47:24 -0400157
James E. King, III377719c2017-02-15 14:33:20 -0500158###
Jim Kingf5f1b352015-06-24 13:47:24 -0400159### Test server implementation
160###
161
162package ThriftTestHandler;
163
164use base qw( ThriftTest::ThriftTestIf );
165
166sub new {
167 my $classname = shift;
168 my $self = {};
169 return bless($self, $classname);
170}
171
172sub testVoid() {
James E. King, III377719c2017-02-15 14:33:20 -0500173 print("testVoid()\n");
Jim Kingf5f1b352015-06-24 13:47:24 -0400174}
175
176sub testString() {
177 my $self = shift;
178 my $thing = shift;
179 print("testString($thing)\n");
180 return $thing;
181}
182
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900183sub testBool() {
184 my $self = shift;
185 my $thing = shift;
186 my $str = $thing ? "true" : "false";
187 print("testBool($str)\n");
188 return $thing;
189}
190
Jim Kingf5f1b352015-06-24 13:47:24 -0400191sub testByte() {
192 my $self = shift;
193 my $thing = shift;
194 print("testByte($thing)\n");
195 return $thing;
196}
197
198sub testI32() {
199 my $self = shift;
200 my $thing = shift;
201 print("testI32($thing)\n");
202 return $thing;
203}
204
205sub testI64() {
206 my $self = shift;
207 my $thing = shift;
208 print("testI64($thing)\n");
209 return $thing;
210}
211
212sub testDouble() {
213 my $self = shift;
214 my $thing = shift;
215 print("testDouble($thing)\n");
216 return $thing;
217}
218
219sub testBinary() {
220 my $self = shift;
221 my $thing = shift;
222 my @bytes = split //, $thing;
223 print("testBinary(");
224 foreach (@bytes)
225 {
226 printf "%02lx", ord $_;
227 }
228 print(")\n");
229 return $thing;
230}
231
232sub testStruct() {
233 my $self = shift;
234 my $thing = shift;
235 printf("testStruct({\"%s\", %d, %d, %lld})\n",
236 $thing->{string_thing},
237 $thing->{byte_thing},
238 $thing->{i32_thing},
239 $thing->{i64_thing});
240 return $thing;
241}
242
243sub testNest() {
244 my $self = shift;
245 my $nest = shift;
246 my $thing = $nest->{struct_thing};
247 printf("testNest({%d, {\"%s\", %d, %d, %lld}, %d})\n",
248 $nest->{byte_thing},
249 $thing->{string_thing},
250 $thing->{byte_thing},
251 $thing->{i32_thing},
252 $thing->{i64_thing},
253 $nest->{i32_thing});
254 return $nest;
255}
256
257sub testMap() {
258 my $self = shift;
259 my $thing = shift;
260 print("testMap({");
261 my $first = 1;
262 foreach my $key (keys %$thing) {
263 if ($first) {
264 $first = 0;
265 } else {
266 print(", ");
267 }
268 print("$key => $thing->{$key}");
269 }
270 print("})\n");
271 return $thing;
272}
273
274sub testStringMap() {
275 my $self = shift;
276 my $thing = shift;
277 print("testStringMap({");
278 my $first = 1;
279 foreach my $key (keys %$thing) {
280 if ($first) {
281 $first = 0;
282 } else {
283 print(", ");
284 }
285 print("$key => $thing->{$key}");
286 }
287 print("})\n");
288 return $thing;
289}
290
291sub testSet() {
292 my $self = shift;
293 my $thing = shift;
294 my @arr;
295 my $result = \@arr;
296 print("testSet({");
297 my $first = 1;
298 foreach my $key (keys %$thing) {
299 if ($first) {
300 $first = 0;
301 } else {
302 print(", ");
303 }
304 print("$key");
James E. King, III619218c2017-10-29 06:55:00 -0400305 push(@arr, $key);
Jim Kingf5f1b352015-06-24 13:47:24 -0400306 }
307 print("})\n");
308 return $result;
309}
310
311sub testList() {
312 my $self = shift;
313 my $thing = shift;
314 print("testList({");
315 my $first = 1;
316 foreach my $key (@$thing) {
317 if ($first) {
318 $first = 0;
319 } else {
320 print(", ");
321 }
322 print("$key");
323 }
324 print("})\n");
325 return $thing;
326}
327
328sub testEnum() {
329 my $self = shift;
330 my $thing = shift;
331 print("testEnum($thing)\n");
332 return $thing;
333}
334
335sub testTypedef() {
336 my $self = shift;
337 my $thing = shift;
338 print("testTypedef($thing)\n");
339 return $thing;
340}
341
342sub testMapMap() {
343 my $self = shift;
344 my $hello = shift;
James E. King, III377719c2017-02-15 14:33:20 -0500345
Jim Kingf5f1b352015-06-24 13:47:24 -0400346 printf("testMapMap(%d)\n", $hello);
347 my $result = { 4 => { 1 => 1, 2 => 2, 3 => 3, 4 => 4 }, -4 => { -1 => -1, -2 => -2, -3 => -3, -4 => -4 } };
348 return $result;
349}
350
351sub testInsanity() {
352 my $self = shift;
353 my $argument = shift;
354 print("testInsanity()\n");
355
356 my $hello = new ThriftTest::Xtruct({string_thing => "Hello2", byte_thing => 2, i32_thing => 2, i64_thing => 2});
357 my @hellos;
358 push(@hellos, $hello);
359 my $goodbye = new ThriftTest::Xtruct({string_thing => "Goodbye4", byte_thing => 4, i32_thing => 4, i64_thing => 4});
360 my @goodbyes;
361 push(@goodbyes, $goodbye);
362 my $crazy = new ThriftTest::Insanity({userMap => { ThriftTest::Numberz::EIGHT => 8 }, xtructs => \@goodbyes});
Jens Geyerd629ea02015-09-23 21:16:50 +0200363 my $loony = new ThriftTest::Insanity();
364 my $result = { 1 => { ThriftTest::Numberz::TWO => $argument, ThriftTest::Numberz::THREE => $argument },
Jim Kingf5f1b352015-06-24 13:47:24 -0400365 2 => { ThriftTest::Numberz::SIX => $loony } };
366 return $result;
367}
368
369sub testMulti() {
370 my $self = shift;
371 my $arg0 = shift;
372 my $arg1 = shift;
373 my $arg2 = shift;
374 my $arg3 = shift;
375 my $arg4 = shift;
376 my $arg5 = shift;
James E. King, III377719c2017-02-15 14:33:20 -0500377
Jim Kingf5f1b352015-06-24 13:47:24 -0400378 print("testMulti()\n");
379 return new ThriftTest::Xtruct({string_thing => "Hello2", byte_thing => $arg0, i32_thing => $arg1, i64_thing => $arg2});
380}
381
382sub testException() {
383 my $self = shift;
384 my $arg = shift;
385 print("testException($arg)\n");
386 if ($arg eq "Xception") {
387 die new ThriftTest::Xception({errorCode => 1001, message => $arg});
388 } elsif ($arg eq "TException") {
389 die "astring"; # all unhandled exceptions become TExceptions
390 } else {
391 return new ThriftTest::Xtruct({string_thing => $arg});
392 }
393}
394
395sub testMultiException() {
396 my $self = shift;
397 my $arg0 = shift;
398 my $arg1 = shift;
399
400 printf("testMultiException(%s, %s)\n", $arg0, $arg1);
401 if ($arg0 eq "Xception") {
402 die new ThriftTest::Xception({errorCode => 1001, message => "This is an Xception"});
403 } elsif ($arg0 eq "Xception2") {
404 my $struct_thing = new ThriftTest::Xtruct({string_thing => "This is an Xception2"});
405 die new ThriftTest::Xception2({errorCode => 2002, struct_thing => $struct_thing});
406 } else {
407 return new ThriftTest::Xtruct({string_thing => $arg1});
408 }
409}
410
411sub testOneway() {
412 my $self = shift;
James E. King, III20e16bc2017-11-18 22:37:54 -0500413 my $num = shift;
414 print("testOneway($num): received\n");
Jim Kingf5f1b352015-06-24 13:47:24 -0400415}
416
James E. King, III20e16bc2017-11-18 22:37:54 -0500417###
418### Test server implementation
419###
420
421package SecondServiceHandler;
422
423use base qw( ThriftTest::SecondServiceIf );
424
425sub new {
426 my $classname = shift;
427 my $self = {};
428 return bless($self, $classname);
429}
430
431sub secondtestString() {
432 my $self = shift;
433 my $thing = shift;
434 print("testString($thing)\n");
435 return "testString(\"" . $thing . "\")";
436}
Jim Kingf5f1b352015-06-24 13:47:24 -0400437
4381;