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