blob: 57a136766f5b9f5fdb8128b652dffac6e08698a1 [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
151sub testByte() {
152 my $self = shift;
153 my $thing = shift;
154 print("testByte($thing)\n");
155 return $thing;
156}
157
158sub testI32() {
159 my $self = shift;
160 my $thing = shift;
161 print("testI32($thing)\n");
162 return $thing;
163}
164
165sub testI64() {
166 my $self = shift;
167 my $thing = shift;
168 print("testI64($thing)\n");
169 return $thing;
170}
171
172sub testDouble() {
173 my $self = shift;
174 my $thing = shift;
175 print("testDouble($thing)\n");
176 return $thing;
177}
178
179sub testBinary() {
180 my $self = shift;
181 my $thing = shift;
182 my @bytes = split //, $thing;
183 print("testBinary(");
184 foreach (@bytes)
185 {
186 printf "%02lx", ord $_;
187 }
188 print(")\n");
189 return $thing;
190}
191
192sub testStruct() {
193 my $self = shift;
194 my $thing = shift;
195 printf("testStruct({\"%s\", %d, %d, %lld})\n",
196 $thing->{string_thing},
197 $thing->{byte_thing},
198 $thing->{i32_thing},
199 $thing->{i64_thing});
200 return $thing;
201}
202
203sub testNest() {
204 my $self = shift;
205 my $nest = shift;
206 my $thing = $nest->{struct_thing};
207 printf("testNest({%d, {\"%s\", %d, %d, %lld}, %d})\n",
208 $nest->{byte_thing},
209 $thing->{string_thing},
210 $thing->{byte_thing},
211 $thing->{i32_thing},
212 $thing->{i64_thing},
213 $nest->{i32_thing});
214 return $nest;
215}
216
217sub testMap() {
218 my $self = shift;
219 my $thing = shift;
220 print("testMap({");
221 my $first = 1;
222 foreach my $key (keys %$thing) {
223 if ($first) {
224 $first = 0;
225 } else {
226 print(", ");
227 }
228 print("$key => $thing->{$key}");
229 }
230 print("})\n");
231 return $thing;
232}
233
234sub testStringMap() {
235 my $self = shift;
236 my $thing = shift;
237 print("testStringMap({");
238 my $first = 1;
239 foreach my $key (keys %$thing) {
240 if ($first) {
241 $first = 0;
242 } else {
243 print(", ");
244 }
245 print("$key => $thing->{$key}");
246 }
247 print("})\n");
248 return $thing;
249}
250
251sub testSet() {
252 my $self = shift;
253 my $thing = shift;
254 my @arr;
255 my $result = \@arr;
256 print("testSet({");
257 my $first = 1;
258 foreach my $key (keys %$thing) {
259 if ($first) {
260 $first = 0;
261 } else {
262 print(", ");
263 }
264 print("$key");
265 push($result, $key);
266 }
267 print("})\n");
268 return $result;
269}
270
271sub testList() {
272 my $self = shift;
273 my $thing = shift;
274 print("testList({");
275 my $first = 1;
276 foreach my $key (@$thing) {
277 if ($first) {
278 $first = 0;
279 } else {
280 print(", ");
281 }
282 print("$key");
283 }
284 print("})\n");
285 return $thing;
286}
287
288sub testEnum() {
289 my $self = shift;
290 my $thing = shift;
291 print("testEnum($thing)\n");
292 return $thing;
293}
294
295sub testTypedef() {
296 my $self = shift;
297 my $thing = shift;
298 print("testTypedef($thing)\n");
299 return $thing;
300}
301
302sub testMapMap() {
303 my $self = shift;
304 my $hello = shift;
305
306 printf("testMapMap(%d)\n", $hello);
307 my $result = { 4 => { 1 => 1, 2 => 2, 3 => 3, 4 => 4 }, -4 => { -1 => -1, -2 => -2, -3 => -3, -4 => -4 } };
308 return $result;
309}
310
311sub testInsanity() {
312 my $self = shift;
313 my $argument = shift;
314 print("testInsanity()\n");
315
316 my $hello = new ThriftTest::Xtruct({string_thing => "Hello2", byte_thing => 2, i32_thing => 2, i64_thing => 2});
317 my @hellos;
318 push(@hellos, $hello);
319 my $goodbye = new ThriftTest::Xtruct({string_thing => "Goodbye4", byte_thing => 4, i32_thing => 4, i64_thing => 4});
320 my @goodbyes;
321 push(@goodbyes, $goodbye);
322 my $crazy = new ThriftTest::Insanity({userMap => { ThriftTest::Numberz::EIGHT => 8 }, xtructs => \@goodbyes});
323 my $loony = new ThriftTest::Insanity({userMap => { ThriftTest::Numberz::FIVE => 5 }, xtructs => \@hellos});
324 my $result = { 1 => { ThriftTest::Numberz::TWO => $crazy, ThriftTest::Numberz::THREE => $crazy },
325 2 => { ThriftTest::Numberz::SIX => $loony } };
326 return $result;
327}
328
329sub testMulti() {
330 my $self = shift;
331 my $arg0 = shift;
332 my $arg1 = shift;
333 my $arg2 = shift;
334 my $arg3 = shift;
335 my $arg4 = shift;
336 my $arg5 = shift;
337
338 print("testMulti()\n");
339 return new ThriftTest::Xtruct({string_thing => "Hello2", byte_thing => $arg0, i32_thing => $arg1, i64_thing => $arg2});
340}
341
342sub testException() {
343 my $self = shift;
344 my $arg = shift;
345 print("testException($arg)\n");
346 if ($arg eq "Xception") {
347 die new ThriftTest::Xception({errorCode => 1001, message => $arg});
348 } elsif ($arg eq "TException") {
349 die "astring"; # all unhandled exceptions become TExceptions
350 } else {
351 return new ThriftTest::Xtruct({string_thing => $arg});
352 }
353}
354
355sub testMultiException() {
356 my $self = shift;
357 my $arg0 = shift;
358 my $arg1 = shift;
359
360 printf("testMultiException(%s, %s)\n", $arg0, $arg1);
361 if ($arg0 eq "Xception") {
362 die new ThriftTest::Xception({errorCode => 1001, message => "This is an Xception"});
363 } elsif ($arg0 eq "Xception2") {
364 my $struct_thing = new ThriftTest::Xtruct({string_thing => "This is an Xception2"});
365 die new ThriftTest::Xception2({errorCode => 2002, struct_thing => $struct_thing});
366 } else {
367 return new ThriftTest::Xtruct({string_thing => $arg1});
368 }
369}
370
371sub testOneway() {
372 my $self = shift;
373 my $sleepFor = shift;
374 print("testOneway($sleepFor): Sleeping...\n");
375 sleep $sleepFor;
376 print("testOneway($sleepFor): done sleeping!\n");
377}
378
379
3801;