blob: d2b9a38ab3316a5c4daa55eb0d252f41e11f7ce8 [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;
Dean Hamstead8a130f62018-10-17 18:48:42 +110030
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 {
Dean Hamstead8a130f62018-10-17 18:48:42 +110051 print <<"EOF";
Jim Kingf5f1b352015-06-24 13:47:24 -040052Usage: $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
Dean Hamstead8a130f62018-10-17 18:48:42 +1100102my $handler = ThriftTestHandler->new();
103my $handler2 = SecondServiceHandler->new();
104my $processor = ThriftTest::ThriftTestProcessor->new($handler);
105my $processor2 = ThriftTest::SecondServiceProcessor->new($handler2);
106
Jim Kingf5f1b352015-06-24 13:47:24 -0400107my $serversocket;
Dean Hamstead8a130f62018-10-17 18:48:42 +1100108if ($opts{'domain-socket'}) {
109 unlink($opts{'domain-socket'});
110 $serversocket = Thrift::UnixServerSocket->new($opts{'domain-socket'});
111}
112elsif ($opts{ssl}) {
113 $serversocket = Thrift::SSLServerSocket->new(\%opts);
114}
115else {
116 $serversocket = Thrift::ServerSocket->new(\%opts);
Jim Kingf5f1b352015-06-24 13:47:24 -0400117}
118my $transport;
119if ($opts{transport} eq 'buffered') {
Dean Hamstead8a130f62018-10-17 18:48:42 +1100120 $transport = Thrift::BufferedTransportFactory->new();
121}
122elsif ($opts{transport} eq 'framed') {
123 $transport = Thrift::FramedTransportFactory->new();
124}
125else {
Jim Kingf5f1b352015-06-24 13:47:24 -0400126 usage();
127 exit 1;
128}
129my $protocol;
James E. King, III20e16bc2017-11-18 22:37:54 -0500130if ($opts{protocol} eq 'binary' || $opts{protocol} eq 'multi') {
Dean Hamstead8a130f62018-10-17 18:48:42 +1100131 $protocol = Thrift::BinaryProtocolFactory->new();
132}
133else {
Jim Kingf5f1b352015-06-24 13:47:24 -0400134 usage();
135 exit 1;
136}
137
James E. King, III20e16bc2017-11-18 22:37:54 -0500138if (index($opts{protocol}, 'multi') == 0) {
Dean Hamstead8a130f62018-10-17 18:48:42 +1100139 my $newProcessor = Thrift::MultiplexedProcessor->new($protocol);
James E. King, III20e16bc2017-11-18 22:37:54 -0500140 $newProcessor->defaultProcessor($processor);
Dean Hamstead8a130f62018-10-17 18:48:42 +1100141 $newProcessor->registerProcessor('ThriftTest', $processor);
142 $newProcessor->registerProcessor('SecondService', $processor2);
James E. King, III20e16bc2017-11-18 22:37:54 -0500143 $processor = $newProcessor;
144}
145
Jim Kingf5f1b352015-06-24 13:47:24 -0400146my $ssltag = '';
147if ($opts{ssl}) {
Dean Hamstead8a130f62018-10-17 18:48:42 +1100148 $ssltag = '(SSL)';
Jim Kingf5f1b352015-06-24 13:47:24 -0400149}
James E. King, III49f4dc02015-10-29 15:52:23 -0400150my $listening_on = "$opts{port} $ssltag";
Dean Hamstead8a130f62018-10-17 18:48:42 +1100151if ($opts{'domain-socket'}) {
152 $listening_on = $opts{'domain-socket'};
James E. King, III49f4dc02015-10-29 15:52:23 -0400153}
Dean Hamstead8a130f62018-10-17 18:48:42 +1100154my $server = Thrift::SimpleServer->new($processor, $serversocket, $transport, $protocol);
155print qq|Starting "simple" server ($opts{transport}/$opts{protocol}) listen on: $listening_on\n|;
Jim Kingf5f1b352015-06-24 13:47:24 -0400156$server->serve();
James E. King III9bea32f2018-03-16 16:07:42 -0400157print "done.\n";
158
159sub sigint_handler {
160 print "received SIGINT, stopping...\n";
161 $server->stop();
162}
Jim Kingf5f1b352015-06-24 13:47:24 -0400163
James E. King, III377719c2017-02-15 14:33:20 -0500164###
Jim Kingf5f1b352015-06-24 13:47:24 -0400165### Test server implementation
166###
167
168package ThriftTestHandler;
169
170use base qw( ThriftTest::ThriftTestIf );
171
172sub new {
173 my $classname = shift;
174 my $self = {};
175 return bless($self, $classname);
176}
177
Dean Hamstead8a130f62018-10-17 18:48:42 +1100178sub testVoid {
James E. King, III377719c2017-02-15 14:33:20 -0500179 print("testVoid()\n");
Jim Kingf5f1b352015-06-24 13:47:24 -0400180}
181
Dean Hamstead8a130f62018-10-17 18:48:42 +1100182sub testString {
Jim Kingf5f1b352015-06-24 13:47:24 -0400183 my $self = shift;
184 my $thing = shift;
185 print("testString($thing)\n");
186 return $thing;
187}
188
Dean Hamstead8a130f62018-10-17 18:48:42 +1100189sub testBool {
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900190 my $self = shift;
191 my $thing = shift;
Dean Hamstead8a130f62018-10-17 18:48:42 +1100192 my $str = $thing ? 'true' : 'false';
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900193 print("testBool($str)\n");
194 return $thing;
195}
196
Dean Hamstead8a130f62018-10-17 18:48:42 +1100197sub testByte {
Jim Kingf5f1b352015-06-24 13:47:24 -0400198 my $self = shift;
199 my $thing = shift;
200 print("testByte($thing)\n");
201 return $thing;
202}
203
Dean Hamstead8a130f62018-10-17 18:48:42 +1100204sub testI32 {
Jim Kingf5f1b352015-06-24 13:47:24 -0400205 my $self = shift;
206 my $thing = shift;
207 print("testI32($thing)\n");
208 return $thing;
209}
210
Dean Hamstead8a130f62018-10-17 18:48:42 +1100211sub testI64 {
Jim Kingf5f1b352015-06-24 13:47:24 -0400212 my $self = shift;
213 my $thing = shift;
214 print("testI64($thing)\n");
215 return $thing;
216}
217
Dean Hamstead8a130f62018-10-17 18:48:42 +1100218sub testDouble {
Jim Kingf5f1b352015-06-24 13:47:24 -0400219 my $self = shift;
220 my $thing = shift;
221 print("testDouble($thing)\n");
222 return $thing;
223}
224
Dean Hamstead8a130f62018-10-17 18:48:42 +1100225sub testBinary {
Jim Kingf5f1b352015-06-24 13:47:24 -0400226 my $self = shift;
227 my $thing = shift;
228 my @bytes = split //, $thing;
Dean Hamstead8a130f62018-10-17 18:48:42 +1100229 print 'testBinary(';
230 printf( '%02lx', ord $_ ) foreach (@bytes);
231 print ")\n";
Jim Kingf5f1b352015-06-24 13:47:24 -0400232 return $thing;
233}
234
Dean Hamstead8a130f62018-10-17 18:48:42 +1100235sub testStruct {
Jim Kingf5f1b352015-06-24 13:47:24 -0400236 my $self = shift;
237 my $thing = shift;
Dean Hamstead8a130f62018-10-17 18:48:42 +1100238 printf(qq|testStruct({"%s", %d, %d, %lld})\n|,
Jim Kingf5f1b352015-06-24 13:47:24 -0400239 $thing->{string_thing},
240 $thing->{byte_thing},
241 $thing->{i32_thing},
242 $thing->{i64_thing});
243 return $thing;
244}
245
Dean Hamstead8a130f62018-10-17 18:48:42 +1100246sub testNest {
Jim Kingf5f1b352015-06-24 13:47:24 -0400247 my $self = shift;
248 my $nest = shift;
249 my $thing = $nest->{struct_thing};
Dean Hamstead8a130f62018-10-17 18:48:42 +1100250 printf(qq|testNest({%d, {"%s", %d, %d, %lld}, %d})\n|,
Jim Kingf5f1b352015-06-24 13:47:24 -0400251 $nest->{byte_thing},
252 $thing->{string_thing},
253 $thing->{byte_thing},
254 $thing->{i32_thing},
255 $thing->{i64_thing},
256 $nest->{i32_thing});
257 return $nest;
258}
259
Dean Hamstead8a130f62018-10-17 18:48:42 +1100260sub testMap {
Jim Kingf5f1b352015-06-24 13:47:24 -0400261 my $self = shift;
262 my $thing = shift;
Dean Hamstead8a130f62018-10-17 18:48:42 +1100263 printf "testMap({%s})\n",
264 join( ', ',
265 map { $_ . ' => ' . $thing->{$_} }
266 sort keys %$thing
267 );
Jim Kingf5f1b352015-06-24 13:47:24 -0400268 return $thing;
269}
270
Dean Hamstead8a130f62018-10-17 18:48:42 +1100271sub testStringMap {
Jim Kingf5f1b352015-06-24 13:47:24 -0400272 my $self = shift;
273 my $thing = shift;
Dean Hamstead8a130f62018-10-17 18:48:42 +1100274 printf "testStringMap({%s})\n",
275 join( ', ',
276 map { $_ . ' => ' . $thing->{$_} }
277 sort keys %$thing
278 );
Jim Kingf5f1b352015-06-24 13:47:24 -0400279 return $thing;
280}
281
Dean Hamstead8a130f62018-10-17 18:48:42 +1100282sub testSet {
Jim Kingf5f1b352015-06-24 13:47:24 -0400283 my $self = shift;
284 my $thing = shift;
Dean Hamstead8a130f62018-10-17 18:48:42 +1100285 my @result = sort keys %$thing;
286 printf "testSet({%s})\n", join(', ', @result );
287 return \@result;
Jim Kingf5f1b352015-06-24 13:47:24 -0400288}
289
Dean Hamstead8a130f62018-10-17 18:48:42 +1100290sub testList {
Jim Kingf5f1b352015-06-24 13:47:24 -0400291 my $self = shift;
292 my $thing = shift;
Dean Hamstead8a130f62018-10-17 18:48:42 +1100293 print "testList({%s})\n", join(', ', @$thing);
Jim Kingf5f1b352015-06-24 13:47:24 -0400294 return $thing;
295}
296
Dean Hamstead8a130f62018-10-17 18:48:42 +1100297sub testEnum {
Jim Kingf5f1b352015-06-24 13:47:24 -0400298 my $self = shift;
299 my $thing = shift;
Dean Hamstead8a130f62018-10-17 18:48:42 +1100300 print "testEnum($thing)\n";
Jim Kingf5f1b352015-06-24 13:47:24 -0400301 return $thing;
302}
303
Dean Hamstead8a130f62018-10-17 18:48:42 +1100304sub testTypedef {
Jim Kingf5f1b352015-06-24 13:47:24 -0400305 my $self = shift;
306 my $thing = shift;
307 print("testTypedef($thing)\n");
308 return $thing;
309}
310
Dean Hamstead8a130f62018-10-17 18:48:42 +1100311sub testMapMap {
Jim Kingf5f1b352015-06-24 13:47:24 -0400312 my $self = shift;
313 my $hello = shift;
James E. King, III377719c2017-02-15 14:33:20 -0500314
Jim Kingf5f1b352015-06-24 13:47:24 -0400315 printf("testMapMap(%d)\n", $hello);
316 my $result = { 4 => { 1 => 1, 2 => 2, 3 => 3, 4 => 4 }, -4 => { -1 => -1, -2 => -2, -3 => -3, -4 => -4 } };
317 return $result;
318}
319
Dean Hamstead8a130f62018-10-17 18:48:42 +1100320sub testInsanity {
Jim Kingf5f1b352015-06-24 13:47:24 -0400321 my $self = shift;
322 my $argument = shift;
323 print("testInsanity()\n");
324
Dean Hamstead8a130f62018-10-17 18:48:42 +1100325 my $hello = ThriftTest::Xtruct->new({string_thing => 'Hello2', byte_thing => 2, i32_thing => 2, i64_thing => 2});
Jim Kingf5f1b352015-06-24 13:47:24 -0400326 my @hellos;
327 push(@hellos, $hello);
Dean Hamstead8a130f62018-10-17 18:48:42 +1100328 my $goodbye = ThriftTest::Xtruct->new({string_thing => 'Goodbye4', byte_thing => 4, i32_thing => 4, i64_thing => 4});
Jim Kingf5f1b352015-06-24 13:47:24 -0400329 my @goodbyes;
330 push(@goodbyes, $goodbye);
Dean Hamstead8a130f62018-10-17 18:48:42 +1100331 my $crazy = ThriftTest::Insanity->new({userMap => { ThriftTest::Numberz::EIGHT => 8 }, xtructs => \@goodbyes});
332 my $loony = ThriftTest::Insanity->new();
Jens Geyerd629ea02015-09-23 21:16:50 +0200333 my $result = { 1 => { ThriftTest::Numberz::TWO => $argument, ThriftTest::Numberz::THREE => $argument },
Jim Kingf5f1b352015-06-24 13:47:24 -0400334 2 => { ThriftTest::Numberz::SIX => $loony } };
335 return $result;
336}
337
Dean Hamstead8a130f62018-10-17 18:48:42 +1100338sub testMulti {
Jim Kingf5f1b352015-06-24 13:47:24 -0400339 my $self = shift;
340 my $arg0 = shift;
341 my $arg1 = shift;
342 my $arg2 = shift;
343 my $arg3 = shift;
344 my $arg4 = shift;
345 my $arg5 = shift;
James E. King, III377719c2017-02-15 14:33:20 -0500346
Jim Kingf5f1b352015-06-24 13:47:24 -0400347 print("testMulti()\n");
Dean Hamstead8a130f62018-10-17 18:48:42 +1100348 return ThriftTest::Xtruct->new({string_thing => 'Hello2', byte_thing => $arg0, i32_thing => $arg1, i64_thing => $arg2});
Jim Kingf5f1b352015-06-24 13:47:24 -0400349}
350
Dean Hamstead8a130f62018-10-17 18:48:42 +1100351sub testException {
Jim Kingf5f1b352015-06-24 13:47:24 -0400352 my $self = shift;
353 my $arg = shift;
354 print("testException($arg)\n");
Dean Hamstead8a130f62018-10-17 18:48:42 +1100355 if ($arg eq 'Xception') {
356 die ThriftTest::Xception->new({errorCode => 1001, message => $arg});
357 }
358 elsif ($arg eq 'TException') {
359 die 'astring'; # all unhandled exceptions become TExceptions
360 }
361 else {
362 return ThriftTest::Xtruct->new({string_thing => $arg});
Jim Kingf5f1b352015-06-24 13:47:24 -0400363 }
364}
365
Dean Hamstead8a130f62018-10-17 18:48:42 +1100366sub testMultiException {
Jim Kingf5f1b352015-06-24 13:47:24 -0400367 my $self = shift;
368 my $arg0 = shift;
369 my $arg1 = shift;
370
371 printf("testMultiException(%s, %s)\n", $arg0, $arg1);
Dean Hamstead8a130f62018-10-17 18:48:42 +1100372 if ($arg0 eq 'Xception') {
373 die ThriftTest::Xception->new({errorCode => 1001, message => 'This is an Xception'});
374 }
375 elsif ($arg0 eq 'Xception2') {
376 my $struct_thing = ThriftTest::Xtruct->new({string_thing => 'This is an Xception2'});
377 die ThriftTest::Xception2->new({errorCode => 2002, struct_thing => $struct_thing});
378 }
379 else {
380 return ThriftTest::Xtruct->new({string_thing => $arg1});
Jim Kingf5f1b352015-06-24 13:47:24 -0400381 }
382}
383
Dean Hamstead8a130f62018-10-17 18:48:42 +1100384sub testOneway {
Jim Kingf5f1b352015-06-24 13:47:24 -0400385 my $self = shift;
James E. King, III20e16bc2017-11-18 22:37:54 -0500386 my $num = shift;
387 print("testOneway($num): received\n");
Jim Kingf5f1b352015-06-24 13:47:24 -0400388}
389
James E. King, III20e16bc2017-11-18 22:37:54 -0500390###
391### Test server implementation
392###
393
394package SecondServiceHandler;
395
396use base qw( ThriftTest::SecondServiceIf );
397
398sub new {
399 my $classname = shift;
400 my $self = {};
401 return bless($self, $classname);
402}
403
Dean Hamstead8a130f62018-10-17 18:48:42 +1100404sub secondtestString {
James E. King, III20e16bc2017-11-18 22:37:54 -0500405 my $self = shift;
406 my $thing = shift;
407 print("testString($thing)\n");
Dean Hamstead8a130f62018-10-17 18:48:42 +1100408 return qq|testString("$thing")|;
James E. King, III20e16bc2017-11-18 22:37:54 -0500409}
Jim Kingf5f1b352015-06-24 13:47:24 -0400410
4111;