blob: 7d8f929b44725c6920746c890a80e2662b4e06b8 [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
29use lib '../../lib/perl/lib';
30use lib 'gen-perl';
31
32use Thrift;
33use Thrift::BinaryProtocol;
34use Thrift::BufferedTransport;
35use Thrift::FramedTransport;
James E. King, III20e16bc2017-11-18 22:37:54 -050036use Thrift::MultiplexedProcessor;
Jim Kingf5f1b352015-06-24 13:47:24 -040037use Thrift::SSLServerSocket;
38use Thrift::ServerSocket;
39use Thrift::Server;
James E. King, III49f4dc02015-10-29 15:52:23 -040040use Thrift::UnixServerSocket;
Jim Kingf5f1b352015-06-24 13:47:24 -040041
James E. King, III20e16bc2017-11-18 22:37:54 -050042use ThriftTest::SecondService;
Jim Kingf5f1b352015-06-24 13:47:24 -040043use ThriftTest::ThriftTest;
44use ThriftTest::Types;
45
46$|++;
47
48sub usage {
49 print <<EOF;
50Usage: $0 [OPTIONS]
51
52Options: (default)
53 --ca Certificate authority file (optional).
54 --cert Certificate file.
James E. King, III377719c2017-02-15 14:33:20 -050055 Required if using --ssl.
56 --ciphers Acceptable cipher list.
James E. King, III49f4dc02015-10-29 15:52:23 -040057 --domain-socket <file> Use a unix domain socket.
Jim Kingf5f1b352015-06-24 13:47:24 -040058 --help Show usage.
59 --key Private key file for certificate.
60 Required if using --ssl and private key is
61 not in the certificate file.
62 --port <portnum> 9090 Port to use.
63 --protocol {binary} binary Protocol to use.
64 --ssl If present, use SSL/TLS.
65 --transport {buffered|framed} buffered Transport to use.
James E. King, III377719c2017-02-15 14:33:20 -050066
Jim Kingf5f1b352015-06-24 13:47:24 -040067EOF
68}
69
70my %opts = (
71 'port' => 9090,
72 'protocol' => 'binary',
73 'transport' => 'buffered'
74);
75
76GetOptions(\%opts, qw (
77 ca=s
78 cert=s
James E. King, III377719c2017-02-15 14:33:20 -050079 ciphers=s
James E. King, III49f4dc02015-10-29 15:52:23 -040080 domain-socket=s
Jim Kingf5f1b352015-06-24 13:47:24 -040081 help
82 host=s
83 key=s
84 port=i
85 protocol=s
86 ssl
87 transport=s
88)) || exit 1;
89
90if ($opts{help}) {
91 usage();
92 exit 0;
93}
94
95if ($opts{ssl} and not defined $opts{cert}) {
96 usage();
97 exit 1;
98}
99
100my $handler = new ThriftTestHandler();
James E. King, III20e16bc2017-11-18 22:37:54 -0500101my $handler2 = new SecondServiceHandler();
Jim Kingf5f1b352015-06-24 13:47:24 -0400102my $processor = new ThriftTest::ThriftTestProcessor($handler);
James E. King, III20e16bc2017-11-18 22:37:54 -0500103my $processor2 = new ThriftTest::SecondServiceProcessor($handler2);
Jim Kingf5f1b352015-06-24 13:47:24 -0400104my $serversocket;
James E. King, III49f4dc02015-10-29 15:52:23 -0400105if ($opts{"domain-socket"}) {
106 unlink($opts{"domain-socket"});
107 $serversocket = new Thrift::UnixServerSocket($opts{"domain-socket"});
108} elsif ($opts{ssl}) {
Jim Kingf5f1b352015-06-24 13:47:24 -0400109 $serversocket = new Thrift::SSLServerSocket(\%opts);
110} else {
111 $serversocket = new Thrift::ServerSocket(\%opts);
112}
113my $transport;
114if ($opts{transport} eq 'buffered') {
115 $transport = new Thrift::BufferedTransportFactory();
116} elsif ($opts{transport} eq 'framed') {
117 $transport = new Thrift::FramedTransportFactory();
118} else {
119 usage();
120 exit 1;
121}
122my $protocol;
James E. King, III20e16bc2017-11-18 22:37:54 -0500123if ($opts{protocol} eq 'binary' || $opts{protocol} eq 'multi') {
Jim Kingf5f1b352015-06-24 13:47:24 -0400124 $protocol = new Thrift::BinaryProtocolFactory();
125} else {
126 usage();
127 exit 1;
128}
129
James E. King, III20e16bc2017-11-18 22:37:54 -0500130if (index($opts{protocol}, 'multi') == 0) {
131 my $newProcessor = new Thrift::MultiplexedProcessor($protocol);
132 $newProcessor->defaultProcessor($processor);
133 $newProcessor->registerProcessor("ThriftTest", $processor);
134 $newProcessor->registerProcessor("SecondService", $processor2);
135 $processor = $newProcessor;
136}
137
Jim Kingf5f1b352015-06-24 13:47:24 -0400138my $ssltag = '';
139if ($opts{ssl}) {
140 $ssltag = "(SSL)";
141}
James E. King, III49f4dc02015-10-29 15:52:23 -0400142my $listening_on = "$opts{port} $ssltag";
143if ($opts{"domain-socket"}) {
144 $listening_on = $opts{"domain-socket"};
145}
Jim Kingf5f1b352015-06-24 13:47:24 -0400146my $server = new Thrift::SimpleServer($processor, $serversocket, $transport, $protocol);
James E. King, III49f4dc02015-10-29 15:52:23 -0400147print "Starting \"simple\" server ($opts{transport}/$opts{protocol}) listen on: $listening_on\n";
Jim Kingf5f1b352015-06-24 13:47:24 -0400148$server->serve();
149
James E. King, III377719c2017-02-15 14:33:20 -0500150###
Jim Kingf5f1b352015-06-24 13:47:24 -0400151### Test server implementation
152###
153
154package ThriftTestHandler;
155
156use base qw( ThriftTest::ThriftTestIf );
157
158sub new {
159 my $classname = shift;
160 my $self = {};
161 return bless($self, $classname);
162}
163
164sub testVoid() {
James E. King, III377719c2017-02-15 14:33:20 -0500165 print("testVoid()\n");
Jim Kingf5f1b352015-06-24 13:47:24 -0400166}
167
168sub testString() {
169 my $self = shift;
170 my $thing = shift;
171 print("testString($thing)\n");
172 return $thing;
173}
174
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900175sub testBool() {
176 my $self = shift;
177 my $thing = shift;
178 my $str = $thing ? "true" : "false";
179 print("testBool($str)\n");
180 return $thing;
181}
182
Jim Kingf5f1b352015-06-24 13:47:24 -0400183sub testByte() {
184 my $self = shift;
185 my $thing = shift;
186 print("testByte($thing)\n");
187 return $thing;
188}
189
190sub testI32() {
191 my $self = shift;
192 my $thing = shift;
193 print("testI32($thing)\n");
194 return $thing;
195}
196
197sub testI64() {
198 my $self = shift;
199 my $thing = shift;
200 print("testI64($thing)\n");
201 return $thing;
202}
203
204sub testDouble() {
205 my $self = shift;
206 my $thing = shift;
207 print("testDouble($thing)\n");
208 return $thing;
209}
210
211sub testBinary() {
212 my $self = shift;
213 my $thing = shift;
214 my @bytes = split //, $thing;
215 print("testBinary(");
216 foreach (@bytes)
217 {
218 printf "%02lx", ord $_;
219 }
220 print(")\n");
221 return $thing;
222}
223
224sub testStruct() {
225 my $self = shift;
226 my $thing = shift;
227 printf("testStruct({\"%s\", %d, %d, %lld})\n",
228 $thing->{string_thing},
229 $thing->{byte_thing},
230 $thing->{i32_thing},
231 $thing->{i64_thing});
232 return $thing;
233}
234
235sub testNest() {
236 my $self = shift;
237 my $nest = shift;
238 my $thing = $nest->{struct_thing};
239 printf("testNest({%d, {\"%s\", %d, %d, %lld}, %d})\n",
240 $nest->{byte_thing},
241 $thing->{string_thing},
242 $thing->{byte_thing},
243 $thing->{i32_thing},
244 $thing->{i64_thing},
245 $nest->{i32_thing});
246 return $nest;
247}
248
249sub testMap() {
250 my $self = shift;
251 my $thing = shift;
252 print("testMap({");
253 my $first = 1;
254 foreach my $key (keys %$thing) {
255 if ($first) {
256 $first = 0;
257 } else {
258 print(", ");
259 }
260 print("$key => $thing->{$key}");
261 }
262 print("})\n");
263 return $thing;
264}
265
266sub testStringMap() {
267 my $self = shift;
268 my $thing = shift;
269 print("testStringMap({");
270 my $first = 1;
271 foreach my $key (keys %$thing) {
272 if ($first) {
273 $first = 0;
274 } else {
275 print(", ");
276 }
277 print("$key => $thing->{$key}");
278 }
279 print("})\n");
280 return $thing;
281}
282
283sub testSet() {
284 my $self = shift;
285 my $thing = shift;
286 my @arr;
287 my $result = \@arr;
288 print("testSet({");
289 my $first = 1;
290 foreach my $key (keys %$thing) {
291 if ($first) {
292 $first = 0;
293 } else {
294 print(", ");
295 }
296 print("$key");
James E. King, III619218c2017-10-29 06:55:00 -0400297 push(@arr, $key);
Jim Kingf5f1b352015-06-24 13:47:24 -0400298 }
299 print("})\n");
300 return $result;
301}
302
303sub testList() {
304 my $self = shift;
305 my $thing = shift;
306 print("testList({");
307 my $first = 1;
308 foreach my $key (@$thing) {
309 if ($first) {
310 $first = 0;
311 } else {
312 print(", ");
313 }
314 print("$key");
315 }
316 print("})\n");
317 return $thing;
318}
319
320sub testEnum() {
321 my $self = shift;
322 my $thing = shift;
323 print("testEnum($thing)\n");
324 return $thing;
325}
326
327sub testTypedef() {
328 my $self = shift;
329 my $thing = shift;
330 print("testTypedef($thing)\n");
331 return $thing;
332}
333
334sub testMapMap() {
335 my $self = shift;
336 my $hello = shift;
James E. King, III377719c2017-02-15 14:33:20 -0500337
Jim Kingf5f1b352015-06-24 13:47:24 -0400338 printf("testMapMap(%d)\n", $hello);
339 my $result = { 4 => { 1 => 1, 2 => 2, 3 => 3, 4 => 4 }, -4 => { -1 => -1, -2 => -2, -3 => -3, -4 => -4 } };
340 return $result;
341}
342
343sub testInsanity() {
344 my $self = shift;
345 my $argument = shift;
346 print("testInsanity()\n");
347
348 my $hello = new ThriftTest::Xtruct({string_thing => "Hello2", byte_thing => 2, i32_thing => 2, i64_thing => 2});
349 my @hellos;
350 push(@hellos, $hello);
351 my $goodbye = new ThriftTest::Xtruct({string_thing => "Goodbye4", byte_thing => 4, i32_thing => 4, i64_thing => 4});
352 my @goodbyes;
353 push(@goodbyes, $goodbye);
354 my $crazy = new ThriftTest::Insanity({userMap => { ThriftTest::Numberz::EIGHT => 8 }, xtructs => \@goodbyes});
Jens Geyerd629ea02015-09-23 21:16:50 +0200355 my $loony = new ThriftTest::Insanity();
356 my $result = { 1 => { ThriftTest::Numberz::TWO => $argument, ThriftTest::Numberz::THREE => $argument },
Jim Kingf5f1b352015-06-24 13:47:24 -0400357 2 => { ThriftTest::Numberz::SIX => $loony } };
358 return $result;
359}
360
361sub testMulti() {
362 my $self = shift;
363 my $arg0 = shift;
364 my $arg1 = shift;
365 my $arg2 = shift;
366 my $arg3 = shift;
367 my $arg4 = shift;
368 my $arg5 = shift;
James E. King, III377719c2017-02-15 14:33:20 -0500369
Jim Kingf5f1b352015-06-24 13:47:24 -0400370 print("testMulti()\n");
371 return new ThriftTest::Xtruct({string_thing => "Hello2", byte_thing => $arg0, i32_thing => $arg1, i64_thing => $arg2});
372}
373
374sub testException() {
375 my $self = shift;
376 my $arg = shift;
377 print("testException($arg)\n");
378 if ($arg eq "Xception") {
379 die new ThriftTest::Xception({errorCode => 1001, message => $arg});
380 } elsif ($arg eq "TException") {
381 die "astring"; # all unhandled exceptions become TExceptions
382 } else {
383 return new ThriftTest::Xtruct({string_thing => $arg});
384 }
385}
386
387sub testMultiException() {
388 my $self = shift;
389 my $arg0 = shift;
390 my $arg1 = shift;
391
392 printf("testMultiException(%s, %s)\n", $arg0, $arg1);
393 if ($arg0 eq "Xception") {
394 die new ThriftTest::Xception({errorCode => 1001, message => "This is an Xception"});
395 } elsif ($arg0 eq "Xception2") {
396 my $struct_thing = new ThriftTest::Xtruct({string_thing => "This is an Xception2"});
397 die new ThriftTest::Xception2({errorCode => 2002, struct_thing => $struct_thing});
398 } else {
399 return new ThriftTest::Xtruct({string_thing => $arg1});
400 }
401}
402
403sub testOneway() {
404 my $self = shift;
James E. King, III20e16bc2017-11-18 22:37:54 -0500405 my $num = shift;
406 print("testOneway($num): received\n");
Jim Kingf5f1b352015-06-24 13:47:24 -0400407}
408
James E. King, III20e16bc2017-11-18 22:37:54 -0500409###
410### Test server implementation
411###
412
413package SecondServiceHandler;
414
415use base qw( ThriftTest::SecondServiceIf );
416
417sub new {
418 my $classname = shift;
419 my $self = {};
420 return bless($self, $classname);
421}
422
423sub secondtestString() {
424 my $self = shift;
425 my $thing = shift;
426 print("testString($thing)\n");
427 return "testString(\"" . $thing . "\")";
428}
Jim Kingf5f1b352015-06-24 13:47:24 -0400429
4301;