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