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