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