Jim King | f5f1b35 | 2015-06-24 13:47:24 -0400 | [diff] [blame] | 1 | #!/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, III | 177c37c | 2017-03-30 17:09:04 -0400 | [diff] [blame] | 22 | use 5.10.0; |
Jim King | f5f1b35 | 2015-06-24 13:47:24 -0400 | [diff] [blame] | 23 | use strict; |
| 24 | use warnings; |
| 25 | use Data::Dumper; |
| 26 | use Getopt::Long qw(GetOptions); |
| 27 | use Time::HiRes qw(gettimeofday); |
| 28 | |
James E. King III | 9bea32f | 2018-03-16 16:07:42 -0400 | [diff] [blame] | 29 | $SIG{INT} = \&sigint_handler; |
| 30 | |
Jim King | f5f1b35 | 2015-06-24 13:47:24 -0400 | [diff] [blame] | 31 | use lib '../../lib/perl/lib'; |
| 32 | use lib 'gen-perl'; |
| 33 | |
| 34 | use Thrift; |
| 35 | use Thrift::BinaryProtocol; |
| 36 | use Thrift::BufferedTransport; |
| 37 | use Thrift::FramedTransport; |
James E. King, III | 20e16bc | 2017-11-18 22:37:54 -0500 | [diff] [blame] | 38 | use Thrift::MultiplexedProcessor; |
Jim King | f5f1b35 | 2015-06-24 13:47:24 -0400 | [diff] [blame] | 39 | use Thrift::SSLServerSocket; |
| 40 | use Thrift::ServerSocket; |
| 41 | use Thrift::Server; |
James E. King, III | 49f4dc0 | 2015-10-29 15:52:23 -0400 | [diff] [blame] | 42 | use Thrift::UnixServerSocket; |
Jim King | f5f1b35 | 2015-06-24 13:47:24 -0400 | [diff] [blame] | 43 | |
James E. King, III | 20e16bc | 2017-11-18 22:37:54 -0500 | [diff] [blame] | 44 | use ThriftTest::SecondService; |
Jim King | f5f1b35 | 2015-06-24 13:47:24 -0400 | [diff] [blame] | 45 | use ThriftTest::ThriftTest; |
| 46 | use ThriftTest::Types; |
| 47 | |
| 48 | $|++; |
| 49 | |
| 50 | sub usage { |
| 51 | print <<EOF; |
| 52 | Usage: $0 [OPTIONS] |
| 53 | |
| 54 | Options: (default) |
| 55 | --ca Certificate authority file (optional). |
| 56 | --cert Certificate file. |
James E. King, III | 377719c | 2017-02-15 14:33:20 -0500 | [diff] [blame] | 57 | Required if using --ssl. |
| 58 | --ciphers Acceptable cipher list. |
James E. King, III | 49f4dc0 | 2015-10-29 15:52:23 -0400 | [diff] [blame] | 59 | --domain-socket <file> Use a unix domain socket. |
Jim King | f5f1b35 | 2015-06-24 13:47:24 -0400 | [diff] [blame] | 60 | --help Show usage. |
| 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. |
James E. King, III | 377719c | 2017-02-15 14:33:20 -0500 | [diff] [blame] | 68 | |
Jim King | f5f1b35 | 2015-06-24 13:47:24 -0400 | [diff] [blame] | 69 | EOF |
| 70 | } |
| 71 | |
| 72 | my %opts = ( |
| 73 | 'port' => 9090, |
| 74 | 'protocol' => 'binary', |
| 75 | 'transport' => 'buffered' |
| 76 | ); |
| 77 | |
| 78 | GetOptions(\%opts, qw ( |
| 79 | ca=s |
| 80 | cert=s |
James E. King, III | 377719c | 2017-02-15 14:33:20 -0500 | [diff] [blame] | 81 | ciphers=s |
James E. King, III | 49f4dc0 | 2015-10-29 15:52:23 -0400 | [diff] [blame] | 82 | domain-socket=s |
Jim King | f5f1b35 | 2015-06-24 13:47:24 -0400 | [diff] [blame] | 83 | help |
| 84 | host=s |
| 85 | key=s |
| 86 | port=i |
| 87 | protocol=s |
| 88 | ssl |
| 89 | transport=s |
| 90 | )) || exit 1; |
| 91 | |
| 92 | if ($opts{help}) { |
| 93 | usage(); |
| 94 | exit 0; |
| 95 | } |
| 96 | |
| 97 | if ($opts{ssl} and not defined $opts{cert}) { |
| 98 | usage(); |
| 99 | exit 1; |
| 100 | } |
| 101 | |
| 102 | my $handler = new ThriftTestHandler(); |
James E. King, III | 20e16bc | 2017-11-18 22:37:54 -0500 | [diff] [blame] | 103 | my $handler2 = new SecondServiceHandler(); |
Jim King | f5f1b35 | 2015-06-24 13:47:24 -0400 | [diff] [blame] | 104 | my $processor = new ThriftTest::ThriftTestProcessor($handler); |
James E. King, III | 20e16bc | 2017-11-18 22:37:54 -0500 | [diff] [blame] | 105 | my $processor2 = new ThriftTest::SecondServiceProcessor($handler2); |
Jim King | f5f1b35 | 2015-06-24 13:47:24 -0400 | [diff] [blame] | 106 | my $serversocket; |
James E. King, III | 49f4dc0 | 2015-10-29 15:52:23 -0400 | [diff] [blame] | 107 | if ($opts{"domain-socket"}) { |
| 108 | unlink($opts{"domain-socket"}); |
| 109 | $serversocket = new Thrift::UnixServerSocket($opts{"domain-socket"}); |
| 110 | } elsif ($opts{ssl}) { |
Jim King | f5f1b35 | 2015-06-24 13:47:24 -0400 | [diff] [blame] | 111 | $serversocket = new Thrift::SSLServerSocket(\%opts); |
| 112 | } else { |
| 113 | $serversocket = new Thrift::ServerSocket(\%opts); |
| 114 | } |
| 115 | my $transport; |
| 116 | if ($opts{transport} eq 'buffered') { |
| 117 | $transport = new Thrift::BufferedTransportFactory(); |
| 118 | } elsif ($opts{transport} eq 'framed') { |
| 119 | $transport = new Thrift::FramedTransportFactory(); |
| 120 | } else { |
| 121 | usage(); |
| 122 | exit 1; |
| 123 | } |
| 124 | my $protocol; |
James E. King, III | 20e16bc | 2017-11-18 22:37:54 -0500 | [diff] [blame] | 125 | if ($opts{protocol} eq 'binary' || $opts{protocol} eq 'multi') { |
Jim King | f5f1b35 | 2015-06-24 13:47:24 -0400 | [diff] [blame] | 126 | $protocol = new Thrift::BinaryProtocolFactory(); |
| 127 | } else { |
| 128 | usage(); |
| 129 | exit 1; |
| 130 | } |
| 131 | |
James E. King, III | 20e16bc | 2017-11-18 22:37:54 -0500 | [diff] [blame] | 132 | if (index($opts{protocol}, 'multi') == 0) { |
| 133 | my $newProcessor = new Thrift::MultiplexedProcessor($protocol); |
| 134 | $newProcessor->defaultProcessor($processor); |
| 135 | $newProcessor->registerProcessor("ThriftTest", $processor); |
| 136 | $newProcessor->registerProcessor("SecondService", $processor2); |
| 137 | $processor = $newProcessor; |
| 138 | } |
| 139 | |
Jim King | f5f1b35 | 2015-06-24 13:47:24 -0400 | [diff] [blame] | 140 | my $ssltag = ''; |
| 141 | if ($opts{ssl}) { |
| 142 | $ssltag = "(SSL)"; |
| 143 | } |
James E. King, III | 49f4dc0 | 2015-10-29 15:52:23 -0400 | [diff] [blame] | 144 | my $listening_on = "$opts{port} $ssltag"; |
| 145 | if ($opts{"domain-socket"}) { |
| 146 | $listening_on = $opts{"domain-socket"}; |
| 147 | } |
Jim King | f5f1b35 | 2015-06-24 13:47:24 -0400 | [diff] [blame] | 148 | my $server = new Thrift::SimpleServer($processor, $serversocket, $transport, $protocol); |
James E. King, III | 49f4dc0 | 2015-10-29 15:52:23 -0400 | [diff] [blame] | 149 | print "Starting \"simple\" server ($opts{transport}/$opts{protocol}) listen on: $listening_on\n"; |
Jim King | f5f1b35 | 2015-06-24 13:47:24 -0400 | [diff] [blame] | 150 | $server->serve(); |
James E. King III | 9bea32f | 2018-03-16 16:07:42 -0400 | [diff] [blame] | 151 | print "done.\n"; |
| 152 | |
| 153 | sub sigint_handler { |
| 154 | print "received SIGINT, stopping...\n"; |
| 155 | $server->stop(); |
| 156 | } |
Jim King | f5f1b35 | 2015-06-24 13:47:24 -0400 | [diff] [blame] | 157 | |
James E. King, III | 377719c | 2017-02-15 14:33:20 -0500 | [diff] [blame] | 158 | ### |
Jim King | f5f1b35 | 2015-06-24 13:47:24 -0400 | [diff] [blame] | 159 | ### Test server implementation |
| 160 | ### |
| 161 | |
| 162 | package ThriftTestHandler; |
| 163 | |
| 164 | use base qw( ThriftTest::ThriftTestIf ); |
| 165 | |
| 166 | sub new { |
| 167 | my $classname = shift; |
| 168 | my $self = {}; |
| 169 | return bless($self, $classname); |
| 170 | } |
| 171 | |
| 172 | sub testVoid() { |
James E. King, III | 377719c | 2017-02-15 14:33:20 -0500 | [diff] [blame] | 173 | print("testVoid()\n"); |
Jim King | f5f1b35 | 2015-06-24 13:47:24 -0400 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | sub testString() { |
| 177 | my $self = shift; |
| 178 | my $thing = shift; |
| 179 | print("testString($thing)\n"); |
| 180 | return $thing; |
| 181 | } |
| 182 | |
Nobuaki Sukegawa | a649e74 | 2015-09-21 13:53:25 +0900 | [diff] [blame] | 183 | sub testBool() { |
| 184 | my $self = shift; |
| 185 | my $thing = shift; |
| 186 | my $str = $thing ? "true" : "false"; |
| 187 | print("testBool($str)\n"); |
| 188 | return $thing; |
| 189 | } |
| 190 | |
Jim King | f5f1b35 | 2015-06-24 13:47:24 -0400 | [diff] [blame] | 191 | sub testByte() { |
| 192 | my $self = shift; |
| 193 | my $thing = shift; |
| 194 | print("testByte($thing)\n"); |
| 195 | return $thing; |
| 196 | } |
| 197 | |
| 198 | sub testI32() { |
| 199 | my $self = shift; |
| 200 | my $thing = shift; |
| 201 | print("testI32($thing)\n"); |
| 202 | return $thing; |
| 203 | } |
| 204 | |
| 205 | sub testI64() { |
| 206 | my $self = shift; |
| 207 | my $thing = shift; |
| 208 | print("testI64($thing)\n"); |
| 209 | return $thing; |
| 210 | } |
| 211 | |
| 212 | sub testDouble() { |
| 213 | my $self = shift; |
| 214 | my $thing = shift; |
| 215 | print("testDouble($thing)\n"); |
| 216 | return $thing; |
| 217 | } |
| 218 | |
| 219 | sub testBinary() { |
| 220 | my $self = shift; |
| 221 | my $thing = shift; |
| 222 | my @bytes = split //, $thing; |
| 223 | print("testBinary("); |
| 224 | foreach (@bytes) |
| 225 | { |
| 226 | printf "%02lx", ord $_; |
| 227 | } |
| 228 | print(")\n"); |
| 229 | return $thing; |
| 230 | } |
| 231 | |
| 232 | sub testStruct() { |
| 233 | my $self = shift; |
| 234 | my $thing = shift; |
| 235 | printf("testStruct({\"%s\", %d, %d, %lld})\n", |
| 236 | $thing->{string_thing}, |
| 237 | $thing->{byte_thing}, |
| 238 | $thing->{i32_thing}, |
| 239 | $thing->{i64_thing}); |
| 240 | return $thing; |
| 241 | } |
| 242 | |
| 243 | sub testNest() { |
| 244 | my $self = shift; |
| 245 | my $nest = shift; |
| 246 | my $thing = $nest->{struct_thing}; |
| 247 | printf("testNest({%d, {\"%s\", %d, %d, %lld}, %d})\n", |
| 248 | $nest->{byte_thing}, |
| 249 | $thing->{string_thing}, |
| 250 | $thing->{byte_thing}, |
| 251 | $thing->{i32_thing}, |
| 252 | $thing->{i64_thing}, |
| 253 | $nest->{i32_thing}); |
| 254 | return $nest; |
| 255 | } |
| 256 | |
| 257 | sub testMap() { |
| 258 | my $self = shift; |
| 259 | my $thing = shift; |
| 260 | print("testMap({"); |
| 261 | my $first = 1; |
| 262 | foreach my $key (keys %$thing) { |
| 263 | if ($first) { |
| 264 | $first = 0; |
| 265 | } else { |
| 266 | print(", "); |
| 267 | } |
| 268 | print("$key => $thing->{$key}"); |
| 269 | } |
| 270 | print("})\n"); |
| 271 | return $thing; |
| 272 | } |
| 273 | |
| 274 | sub testStringMap() { |
| 275 | my $self = shift; |
| 276 | my $thing = shift; |
| 277 | print("testStringMap({"); |
| 278 | my $first = 1; |
| 279 | foreach my $key (keys %$thing) { |
| 280 | if ($first) { |
| 281 | $first = 0; |
| 282 | } else { |
| 283 | print(", "); |
| 284 | } |
| 285 | print("$key => $thing->{$key}"); |
| 286 | } |
| 287 | print("})\n"); |
| 288 | return $thing; |
| 289 | } |
| 290 | |
| 291 | sub testSet() { |
| 292 | my $self = shift; |
| 293 | my $thing = shift; |
| 294 | my @arr; |
| 295 | my $result = \@arr; |
| 296 | print("testSet({"); |
| 297 | my $first = 1; |
| 298 | foreach my $key (keys %$thing) { |
| 299 | if ($first) { |
| 300 | $first = 0; |
| 301 | } else { |
| 302 | print(", "); |
| 303 | } |
| 304 | print("$key"); |
James E. King, III | 619218c | 2017-10-29 06:55:00 -0400 | [diff] [blame] | 305 | push(@arr, $key); |
Jim King | f5f1b35 | 2015-06-24 13:47:24 -0400 | [diff] [blame] | 306 | } |
| 307 | print("})\n"); |
| 308 | return $result; |
| 309 | } |
| 310 | |
| 311 | sub testList() { |
| 312 | my $self = shift; |
| 313 | my $thing = shift; |
| 314 | print("testList({"); |
| 315 | my $first = 1; |
| 316 | foreach my $key (@$thing) { |
| 317 | if ($first) { |
| 318 | $first = 0; |
| 319 | } else { |
| 320 | print(", "); |
| 321 | } |
| 322 | print("$key"); |
| 323 | } |
| 324 | print("})\n"); |
| 325 | return $thing; |
| 326 | } |
| 327 | |
| 328 | sub testEnum() { |
| 329 | my $self = shift; |
| 330 | my $thing = shift; |
| 331 | print("testEnum($thing)\n"); |
| 332 | return $thing; |
| 333 | } |
| 334 | |
| 335 | sub testTypedef() { |
| 336 | my $self = shift; |
| 337 | my $thing = shift; |
| 338 | print("testTypedef($thing)\n"); |
| 339 | return $thing; |
| 340 | } |
| 341 | |
| 342 | sub testMapMap() { |
| 343 | my $self = shift; |
| 344 | my $hello = shift; |
James E. King, III | 377719c | 2017-02-15 14:33:20 -0500 | [diff] [blame] | 345 | |
Jim King | f5f1b35 | 2015-06-24 13:47:24 -0400 | [diff] [blame] | 346 | printf("testMapMap(%d)\n", $hello); |
| 347 | my $result = { 4 => { 1 => 1, 2 => 2, 3 => 3, 4 => 4 }, -4 => { -1 => -1, -2 => -2, -3 => -3, -4 => -4 } }; |
| 348 | return $result; |
| 349 | } |
| 350 | |
| 351 | sub testInsanity() { |
| 352 | my $self = shift; |
| 353 | my $argument = shift; |
| 354 | print("testInsanity()\n"); |
| 355 | |
| 356 | my $hello = new ThriftTest::Xtruct({string_thing => "Hello2", byte_thing => 2, i32_thing => 2, i64_thing => 2}); |
| 357 | my @hellos; |
| 358 | push(@hellos, $hello); |
| 359 | my $goodbye = new ThriftTest::Xtruct({string_thing => "Goodbye4", byte_thing => 4, i32_thing => 4, i64_thing => 4}); |
| 360 | my @goodbyes; |
| 361 | push(@goodbyes, $goodbye); |
| 362 | my $crazy = new ThriftTest::Insanity({userMap => { ThriftTest::Numberz::EIGHT => 8 }, xtructs => \@goodbyes}); |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 363 | my $loony = new ThriftTest::Insanity(); |
| 364 | my $result = { 1 => { ThriftTest::Numberz::TWO => $argument, ThriftTest::Numberz::THREE => $argument }, |
Jim King | f5f1b35 | 2015-06-24 13:47:24 -0400 | [diff] [blame] | 365 | 2 => { ThriftTest::Numberz::SIX => $loony } }; |
| 366 | return $result; |
| 367 | } |
| 368 | |
| 369 | sub testMulti() { |
| 370 | my $self = shift; |
| 371 | my $arg0 = shift; |
| 372 | my $arg1 = shift; |
| 373 | my $arg2 = shift; |
| 374 | my $arg3 = shift; |
| 375 | my $arg4 = shift; |
| 376 | my $arg5 = shift; |
James E. King, III | 377719c | 2017-02-15 14:33:20 -0500 | [diff] [blame] | 377 | |
Jim King | f5f1b35 | 2015-06-24 13:47:24 -0400 | [diff] [blame] | 378 | print("testMulti()\n"); |
| 379 | return new ThriftTest::Xtruct({string_thing => "Hello2", byte_thing => $arg0, i32_thing => $arg1, i64_thing => $arg2}); |
| 380 | } |
| 381 | |
| 382 | sub testException() { |
| 383 | my $self = shift; |
| 384 | my $arg = shift; |
| 385 | print("testException($arg)\n"); |
| 386 | if ($arg eq "Xception") { |
| 387 | die new ThriftTest::Xception({errorCode => 1001, message => $arg}); |
| 388 | } elsif ($arg eq "TException") { |
| 389 | die "astring"; # all unhandled exceptions become TExceptions |
| 390 | } else { |
| 391 | return new ThriftTest::Xtruct({string_thing => $arg}); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | sub testMultiException() { |
| 396 | my $self = shift; |
| 397 | my $arg0 = shift; |
| 398 | my $arg1 = shift; |
| 399 | |
| 400 | printf("testMultiException(%s, %s)\n", $arg0, $arg1); |
| 401 | if ($arg0 eq "Xception") { |
| 402 | die new ThriftTest::Xception({errorCode => 1001, message => "This is an Xception"}); |
| 403 | } elsif ($arg0 eq "Xception2") { |
| 404 | my $struct_thing = new ThriftTest::Xtruct({string_thing => "This is an Xception2"}); |
| 405 | die new ThriftTest::Xception2({errorCode => 2002, struct_thing => $struct_thing}); |
| 406 | } else { |
| 407 | return new ThriftTest::Xtruct({string_thing => $arg1}); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | sub testOneway() { |
| 412 | my $self = shift; |
James E. King, III | 20e16bc | 2017-11-18 22:37:54 -0500 | [diff] [blame] | 413 | my $num = shift; |
| 414 | print("testOneway($num): received\n"); |
Jim King | f5f1b35 | 2015-06-24 13:47:24 -0400 | [diff] [blame] | 415 | } |
| 416 | |
James E. King, III | 20e16bc | 2017-11-18 22:37:54 -0500 | [diff] [blame] | 417 | ### |
| 418 | ### Test server implementation |
| 419 | ### |
| 420 | |
| 421 | package SecondServiceHandler; |
| 422 | |
| 423 | use base qw( ThriftTest::SecondServiceIf ); |
| 424 | |
| 425 | sub new { |
| 426 | my $classname = shift; |
| 427 | my $self = {}; |
| 428 | return bless($self, $classname); |
| 429 | } |
| 430 | |
| 431 | sub secondtestString() { |
| 432 | my $self = shift; |
| 433 | my $thing = shift; |
| 434 | print("testString($thing)\n"); |
| 435 | return "testString(\"" . $thing . "\")"; |
| 436 | } |
Jim King | f5f1b35 | 2015-06-24 13:47:24 -0400 | [diff] [blame] | 437 | |
| 438 | 1; |