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