blob: 1e23ce84a56b6d33492b7bb471b56caab888fa95 [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;
36use Thrift::SSLServerSocket;
37use Thrift::ServerSocket;
38use Thrift::Server;
James E. King, III49f4dc02015-10-29 15:52:23 -040039use Thrift::UnixServerSocket;
Jim Kingf5f1b352015-06-24 13:47:24 -040040
41use ThriftTest::ThriftTest;
42use ThriftTest::Types;
43
44$|++;
45
46sub usage {
47 print <<EOF;
48Usage: $0 [OPTIONS]
49
50Options: (default)
51 --ca Certificate authority file (optional).
52 --cert Certificate file.
James E. King, III377719c2017-02-15 14:33:20 -050053 Required if using --ssl.
54 --ciphers Acceptable cipher list.
James E. King, III49f4dc02015-10-29 15:52:23 -040055 --domain-socket <file> Use a unix domain socket.
Jim Kingf5f1b352015-06-24 13:47:24 -040056 --help Show usage.
57 --key Private key file for certificate.
58 Required if using --ssl and private key is
59 not in the certificate file.
60 --port <portnum> 9090 Port to use.
61 --protocol {binary} binary Protocol to use.
62 --ssl If present, use SSL/TLS.
63 --transport {buffered|framed} buffered Transport to use.
James E. King, III377719c2017-02-15 14:33:20 -050064
Jim Kingf5f1b352015-06-24 13:47:24 -040065EOF
66}
67
68my %opts = (
69 'port' => 9090,
70 'protocol' => 'binary',
71 'transport' => 'buffered'
72);
73
74GetOptions(\%opts, qw (
75 ca=s
76 cert=s
James E. King, III377719c2017-02-15 14:33:20 -050077 ciphers=s
James E. King, III49f4dc02015-10-29 15:52:23 -040078 domain-socket=s
Jim Kingf5f1b352015-06-24 13:47:24 -040079 help
80 host=s
81 key=s
82 port=i
83 protocol=s
84 ssl
85 transport=s
86)) || exit 1;
87
88if ($opts{help}) {
89 usage();
90 exit 0;
91}
92
93if ($opts{ssl} and not defined $opts{cert}) {
94 usage();
95 exit 1;
96}
97
98my $handler = new ThriftTestHandler();
99my $processor = new ThriftTest::ThriftTestProcessor($handler);
100my $serversocket;
James E. King, III49f4dc02015-10-29 15:52:23 -0400101if ($opts{"domain-socket"}) {
102 unlink($opts{"domain-socket"});
103 $serversocket = new Thrift::UnixServerSocket($opts{"domain-socket"});
104} elsif ($opts{ssl}) {
Jim Kingf5f1b352015-06-24 13:47:24 -0400105 $serversocket = new Thrift::SSLServerSocket(\%opts);
106} else {
107 $serversocket = new Thrift::ServerSocket(\%opts);
108}
109my $transport;
110if ($opts{transport} eq 'buffered') {
111 $transport = new Thrift::BufferedTransportFactory();
112} elsif ($opts{transport} eq 'framed') {
113 $transport = new Thrift::FramedTransportFactory();
114} else {
115 usage();
116 exit 1;
117}
118my $protocol;
119if ($opts{protocol} eq 'binary') {
120 $protocol = new Thrift::BinaryProtocolFactory();
121} else {
122 usage();
123 exit 1;
124}
125
126my $ssltag = '';
127if ($opts{ssl}) {
128 $ssltag = "(SSL)";
129}
James E. King, III49f4dc02015-10-29 15:52:23 -0400130my $listening_on = "$opts{port} $ssltag";
131if ($opts{"domain-socket"}) {
132 $listening_on = $opts{"domain-socket"};
133}
Jim Kingf5f1b352015-06-24 13:47:24 -0400134my $server = new Thrift::SimpleServer($processor, $serversocket, $transport, $protocol);
James E. King, III49f4dc02015-10-29 15:52:23 -0400135print "Starting \"simple\" server ($opts{transport}/$opts{protocol}) listen on: $listening_on\n";
Jim Kingf5f1b352015-06-24 13:47:24 -0400136$server->serve();
137
James E. King, III377719c2017-02-15 14:33:20 -0500138###
Jim Kingf5f1b352015-06-24 13:47:24 -0400139### Test server implementation
140###
141
142package ThriftTestHandler;
143
144use base qw( ThriftTest::ThriftTestIf );
145
146sub new {
147 my $classname = shift;
148 my $self = {};
149 return bless($self, $classname);
150}
151
152sub testVoid() {
James E. King, III377719c2017-02-15 14:33:20 -0500153 print("testVoid()\n");
Jim Kingf5f1b352015-06-24 13:47:24 -0400154}
155
156sub testString() {
157 my $self = shift;
158 my $thing = shift;
159 print("testString($thing)\n");
160 return $thing;
161}
162
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900163sub testBool() {
164 my $self = shift;
165 my $thing = shift;
166 my $str = $thing ? "true" : "false";
167 print("testBool($str)\n");
168 return $thing;
169}
170
Jim Kingf5f1b352015-06-24 13:47:24 -0400171sub testByte() {
172 my $self = shift;
173 my $thing = shift;
174 print("testByte($thing)\n");
175 return $thing;
176}
177
178sub testI32() {
179 my $self = shift;
180 my $thing = shift;
181 print("testI32($thing)\n");
182 return $thing;
183}
184
185sub testI64() {
186 my $self = shift;
187 my $thing = shift;
188 print("testI64($thing)\n");
189 return $thing;
190}
191
192sub testDouble() {
193 my $self = shift;
194 my $thing = shift;
195 print("testDouble($thing)\n");
196 return $thing;
197}
198
199sub testBinary() {
200 my $self = shift;
201 my $thing = shift;
202 my @bytes = split //, $thing;
203 print("testBinary(");
204 foreach (@bytes)
205 {
206 printf "%02lx", ord $_;
207 }
208 print(")\n");
209 return $thing;
210}
211
212sub testStruct() {
213 my $self = shift;
214 my $thing = shift;
215 printf("testStruct({\"%s\", %d, %d, %lld})\n",
216 $thing->{string_thing},
217 $thing->{byte_thing},
218 $thing->{i32_thing},
219 $thing->{i64_thing});
220 return $thing;
221}
222
223sub testNest() {
224 my $self = shift;
225 my $nest = shift;
226 my $thing = $nest->{struct_thing};
227 printf("testNest({%d, {\"%s\", %d, %d, %lld}, %d})\n",
228 $nest->{byte_thing},
229 $thing->{string_thing},
230 $thing->{byte_thing},
231 $thing->{i32_thing},
232 $thing->{i64_thing},
233 $nest->{i32_thing});
234 return $nest;
235}
236
237sub testMap() {
238 my $self = shift;
239 my $thing = shift;
240 print("testMap({");
241 my $first = 1;
242 foreach my $key (keys %$thing) {
243 if ($first) {
244 $first = 0;
245 } else {
246 print(", ");
247 }
248 print("$key => $thing->{$key}");
249 }
250 print("})\n");
251 return $thing;
252}
253
254sub testStringMap() {
255 my $self = shift;
256 my $thing = shift;
257 print("testStringMap({");
258 my $first = 1;
259 foreach my $key (keys %$thing) {
260 if ($first) {
261 $first = 0;
262 } else {
263 print(", ");
264 }
265 print("$key => $thing->{$key}");
266 }
267 print("})\n");
268 return $thing;
269}
270
271sub testSet() {
272 my $self = shift;
273 my $thing = shift;
274 my @arr;
275 my $result = \@arr;
276 print("testSet({");
277 my $first = 1;
278 foreach my $key (keys %$thing) {
279 if ($first) {
280 $first = 0;
281 } else {
282 print(", ");
283 }
284 print("$key");
James E. King, III619218c2017-10-29 06:55:00 -0400285 push(@arr, $key);
Jim Kingf5f1b352015-06-24 13:47:24 -0400286 }
287 print("})\n");
288 return $result;
289}
290
291sub testList() {
292 my $self = shift;
293 my $thing = shift;
294 print("testList({");
295 my $first = 1;
296 foreach my $key (@$thing) {
297 if ($first) {
298 $first = 0;
299 } else {
300 print(", ");
301 }
302 print("$key");
303 }
304 print("})\n");
305 return $thing;
306}
307
308sub testEnum() {
309 my $self = shift;
310 my $thing = shift;
311 print("testEnum($thing)\n");
312 return $thing;
313}
314
315sub testTypedef() {
316 my $self = shift;
317 my $thing = shift;
318 print("testTypedef($thing)\n");
319 return $thing;
320}
321
322sub testMapMap() {
323 my $self = shift;
324 my $hello = shift;
James E. King, III377719c2017-02-15 14:33:20 -0500325
Jim Kingf5f1b352015-06-24 13:47:24 -0400326 printf("testMapMap(%d)\n", $hello);
327 my $result = { 4 => { 1 => 1, 2 => 2, 3 => 3, 4 => 4 }, -4 => { -1 => -1, -2 => -2, -3 => -3, -4 => -4 } };
328 return $result;
329}
330
331sub testInsanity() {
332 my $self = shift;
333 my $argument = shift;
334 print("testInsanity()\n");
335
336 my $hello = new ThriftTest::Xtruct({string_thing => "Hello2", byte_thing => 2, i32_thing => 2, i64_thing => 2});
337 my @hellos;
338 push(@hellos, $hello);
339 my $goodbye = new ThriftTest::Xtruct({string_thing => "Goodbye4", byte_thing => 4, i32_thing => 4, i64_thing => 4});
340 my @goodbyes;
341 push(@goodbyes, $goodbye);
342 my $crazy = new ThriftTest::Insanity({userMap => { ThriftTest::Numberz::EIGHT => 8 }, xtructs => \@goodbyes});
Jens Geyerd629ea02015-09-23 21:16:50 +0200343 my $loony = new ThriftTest::Insanity();
344 my $result = { 1 => { ThriftTest::Numberz::TWO => $argument, ThriftTest::Numberz::THREE => $argument },
Jim Kingf5f1b352015-06-24 13:47:24 -0400345 2 => { ThriftTest::Numberz::SIX => $loony } };
346 return $result;
347}
348
349sub testMulti() {
350 my $self = shift;
351 my $arg0 = shift;
352 my $arg1 = shift;
353 my $arg2 = shift;
354 my $arg3 = shift;
355 my $arg4 = shift;
356 my $arg5 = shift;
James E. King, III377719c2017-02-15 14:33:20 -0500357
Jim Kingf5f1b352015-06-24 13:47:24 -0400358 print("testMulti()\n");
359 return new ThriftTest::Xtruct({string_thing => "Hello2", byte_thing => $arg0, i32_thing => $arg1, i64_thing => $arg2});
360}
361
362sub testException() {
363 my $self = shift;
364 my $arg = shift;
365 print("testException($arg)\n");
366 if ($arg eq "Xception") {
367 die new ThriftTest::Xception({errorCode => 1001, message => $arg});
368 } elsif ($arg eq "TException") {
369 die "astring"; # all unhandled exceptions become TExceptions
370 } else {
371 return new ThriftTest::Xtruct({string_thing => $arg});
372 }
373}
374
375sub testMultiException() {
376 my $self = shift;
377 my $arg0 = shift;
378 my $arg1 = shift;
379
380 printf("testMultiException(%s, %s)\n", $arg0, $arg1);
381 if ($arg0 eq "Xception") {
382 die new ThriftTest::Xception({errorCode => 1001, message => "This is an Xception"});
383 } elsif ($arg0 eq "Xception2") {
384 my $struct_thing = new ThriftTest::Xtruct({string_thing => "This is an Xception2"});
385 die new ThriftTest::Xception2({errorCode => 2002, struct_thing => $struct_thing});
386 } else {
387 return new ThriftTest::Xtruct({string_thing => $arg1});
388 }
389}
390
391sub testOneway() {
392 my $self = shift;
393 my $sleepFor = shift;
394 print("testOneway($sleepFor): Sleeping...\n");
395 sleep $sleepFor;
396 print("testOneway($sleepFor): done sleeping!\n");
397}
398
399
4001;