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
12 # http://www.apache.org/licenses/LICENSE-2.0
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
26 use Getopt
::Long
qw(GetOptions);
27 use Time
::HiRes
qw(gettimeofday);
29 $SIG{INT
} = \
&sigint_handler
;
31 use lib
'../../lib/perl/lib';
35 use Thrift
::BinaryProtocol
;
36 use Thrift
::BufferedTransport
;
37 use Thrift
::FramedTransport
;
38 use Thrift
::MultiplexedProcessor
;
39 use Thrift
::SSLServerSocket
;
40 use Thrift
::ServerSocket
;
42 use Thrift
::UnixServerSocket
;
44 use ThriftTest
::SecondService
;
45 use ThriftTest
::ThriftTest
;
46 use ThriftTest
::Types
;
55 --ca Certificate authority file (optional).
56 --cert Certificate file.
57 Required if using --ssl.
58 --ciphers Acceptable cipher list.
59 --domain-socket <file> Use a unix domain socket.
61 --key Private key file for certificate.
62 Required if using --ssl and private key is
63 not in the certificate file.
64 --port <portnum> 9090 Port to use.
65 --protocol {binary} binary Protocol to use.
66 --ssl If present, use SSL/TLS.
67 --transport {buffered|framed} buffered Transport to use.
74 'protocol' => 'binary',
75 'transport' => 'buffered'
78 GetOptions
(\
%opts, qw
(
97 if ($opts{ssl
} and not defined $opts{cert
}) {
102 my $handler = ThriftTestHandler-
>new();
103 my $handler2 = SecondServiceHandler-
>new();
104 my $processor = ThriftTest
::ThriftTestProcessor-
>new($handler);
105 my $processor2 = ThriftTest
::SecondServiceProcessor-
>new($handler2);
108 if ($opts{'domain-socket'}) {
109 unlink($opts{'domain-socket'});
110 $serversocket = Thrift
::UnixServerSocket-
>new($opts{'domain-socket'});
113 $serversocket = Thrift
::SSLServerSocket-
>new(\
%opts);
116 $serversocket = Thrift
::ServerSocket-
>new(\
%opts);
119 if ($opts{transport
} eq 'buffered') {
120 $transport = Thrift
::BufferedTransportFactory-
>new();
122 elsif ($opts{transport
} eq 'framed') {
123 $transport = Thrift
::FramedTransportFactory-
>new();
130 if ($opts{protocol
} eq 'binary' || $opts{protocol
} eq 'multi') {
131 $protocol = Thrift
::BinaryProtocolFactory-
>new();
138 if (index($opts{protocol
}, 'multi') == 0) {
139 my $newProcessor = Thrift
::MultiplexedProcessor-
>new($protocol);
140 $newProcessor->defaultProcessor($processor);
141 $newProcessor->registerProcessor('ThriftTest', $processor);
142 $newProcessor->registerProcessor('SecondService', $processor2);
143 $processor = $newProcessor;
150 my $listening_on = "$opts{port} $ssltag";
151 if ($opts{'domain-socket'}) {
152 $listening_on = $opts{'domain-socket'};
154 my $server = Thrift
::SimpleServer-
>new($processor, $serversocket, $transport, $protocol);
155 print qq
|Starting
"simple" server
($opts{transport
}/$opts{protocol
}) listen on
: $listening_on\n|;
160 print "received SIGINT, stopping...\n";
165 ### Test server implementation
168 package ThriftTestHandler
;
170 use base
qw( ThriftTest::ThriftTestIf );
173 my $classname = shift;
175 return bless($self, $classname);
179 print("testVoid()\n");
185 print("testString($thing)\n");
192 my $str = $thing ?
'true' : 'false';
193 print("testBool($str)\n");
200 print("testByte($thing)\n");
207 print("testI32($thing)\n");
214 print("testI64($thing)\n");
221 print("testDouble($thing)\n");
228 my @bytes = split //, $thing;
230 printf( '%02lx', ord $_ ) foreach (@bytes);
238 printf(qq
|testStruct
({"%s", %d, %d, %lld})\n|,
239 $thing->{string_thing
},
240 $thing->{byte_thing
},
242 $thing->{i64_thing
});
249 my $thing = $nest->{struct_thing
};
250 printf(qq
|testNest
({%d, {"%s", %d, %d, %lld}, %d})\n|,
252 $thing->{string_thing
},
253 $thing->{byte_thing
},
263 printf "testMap({%s})\n",
265 map { $_ . ' => ' . $thing->{$_} }
274 printf "testStringMap({%s})\n",
276 map { $_ . ' => ' . $thing->{$_} }
285 my @result = sort keys %$thing;
286 printf "testSet({%s})\n", join(', ', @result );
293 print "testList({%s})\n", join(', ', @$thing);
300 print "testEnum($thing)\n";
307 print("testTypedef($thing)\n");
315 printf("testMapMap(%d)\n", $hello);
316 my $result = { 4 => { 1 => 1, 2 => 2, 3 => 3, 4 => 4 }, -4 => { -1 => -1, -2 => -2, -3 => -3, -4 => -4 } };
322 my $argument = shift;
323 print("testInsanity()\n");
325 my $hello = ThriftTest
::Xtruct-
>new({string_thing
=> 'Hello2', byte_thing
=> 2, i32_thing
=> 2, i64_thing
=> 2});
327 push(@hellos, $hello);
328 my $goodbye = ThriftTest
::Xtruct-
>new({string_thing
=> 'Goodbye4', byte_thing
=> 4, i32_thing
=> 4, i64_thing
=> 4});
330 push(@goodbyes, $goodbye);
331 my $crazy = ThriftTest
::Insanity-
>new({userMap
=> { ThriftTest
::Numberz
::EIGHT
=> 8 }, xtructs
=> \
@goodbyes});
332 my $loony = ThriftTest
::Insanity-
>new();
333 my $result = { 1 => { ThriftTest
::Numberz
::TWO
=> $argument, ThriftTest
::Numberz
::THREE
=> $argument },
334 2 => { ThriftTest
::Numberz
::SIX
=> $loony } };
347 print("testMulti()\n");
348 return ThriftTest
::Xtruct-
>new({string_thing
=> 'Hello2', byte_thing
=> $arg0, i32_thing
=> $arg1, i64_thing
=> $arg2});
354 print("testException($arg)\n");
355 if ($arg eq 'Xception') {
356 die ThriftTest
::Xception-
>new({errorCode
=> 1001, message
=> $arg});
358 elsif ($arg eq 'TException') {
359 die 'astring'; # all unhandled exceptions become TExceptions
362 return ThriftTest
::Xtruct-
>new({string_thing
=> $arg});
366 sub testMultiException
{
371 printf("testMultiException(%s, %s)\n", $arg0, $arg1);
372 if ($arg0 eq 'Xception') {
373 die ThriftTest
::Xception-
>new({errorCode
=> 1001, message
=> 'This is an Xception'});
375 elsif ($arg0 eq 'Xception2') {
376 my $struct_thing = ThriftTest
::Xtruct-
>new({string_thing
=> 'This is an Xception2'});
377 die ThriftTest
::Xception2-
>new({errorCode
=> 2002, struct_thing
=> $struct_thing});
380 return ThriftTest
::Xtruct-
>new({string_thing
=> $arg1});
387 print("testOneway($num): received\n");
391 ### Test server implementation
394 package SecondServiceHandler
;
396 use base
qw( ThriftTest::SecondServiceIf );
399 my $classname = shift;
401 return bless($self, $classname);
404 sub secondtestString
{
407 print("testString($thing)\n");
408 return qq
|testString
("$thing")|;