Roger Meier | 32f3982 | 2014-06-18 22:43:17 +0200 | [diff] [blame] | 1 | #!/usr/bin/env ruby |
| 2 | |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 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 | $:.push File.dirname(__FILE__) + '/..' |
| 23 | |
| 24 | require 'test_helper' |
| 25 | require 'thrift' |
| 26 | require 'thrift_test' |
| 27 | |
| 28 | class SimpleHandler |
| 29 | [:testVoid, :testString, :testByte, :testI32, :testI64, :testDouble, |
| 30 | :testStruct, :testMap, :testSet, :testList, :testNest, |
| 31 | :testEnum, :testTypedef, :testMultiException].each do |meth| |
| 32 | |
| 33 | define_method(meth) do |thing| |
| 34 | thing |
| 35 | end |
| 36 | |
| 37 | end |
| 38 | |
| 39 | def testVoid() |
| 40 | end |
| 41 | |
| 42 | def testInsanity(thing) |
| 43 | num, uid = thing.userMap.find { true } |
| 44 | return {uid => {num => thing}} |
| 45 | end |
| 46 | |
| 47 | def testMapMap(thing) |
| 48 | return {thing => {thing => thing}} |
| 49 | end |
| 50 | |
| 51 | def testEnum(thing) |
| 52 | return thing |
| 53 | end |
| 54 | |
| 55 | def testTypedef(thing) |
| 56 | return thing |
| 57 | end |
| 58 | |
| 59 | def testException(thing) |
| 60 | if thing == "Xception" |
| 61 | raise Thrift::Test::Xception, :message => thing |
| 62 | elsif thing == "TException" |
| 63 | raise Thrift::Test::TException, :message => thing |
| 64 | else |
| 65 | return arg1 |
| 66 | end |
| 67 | end |
| 68 | |
| 69 | def testMultiException(arg0, arg1) |
| 70 | if arg0 == "Xception2" |
| 71 | raise Thrift::Test::Xception2, :message => 'This is an Xception2' |
| 72 | elsif arg0 == "Xception" |
| 73 | raise Thrift::Test::Xception, :message => 'This is an Xception' |
| 74 | else |
| 75 | return arg1 |
| 76 | end |
| 77 | end |
| 78 | |
| 79 | end |
| 80 | |
| 81 | protocol = "binary" |
| 82 | port = 9090 |
| 83 | transport = "buffered" |
| 84 | @transportFactory = Thrift::BufferedTransportFactory.new |
| 85 | @protocolFactory = Thrift::BinaryProtocolFactory.new |
| 86 | ARGV.each do|a| |
| 87 | if a == "--help" |
| 88 | puts "Allowed options:" |
| 89 | puts "\t -h [ --help ] \t produce help message" |
| 90 | puts "\t--port arg (=9090) \t Port number to listen" |
| 91 | puts "\t--protocol arg (=binary) \t protocol: binary, accel" |
| 92 | puts "\t--transport arg (=buffered) transport: buffered, framed, http" |
| 93 | exit |
| 94 | elsif a.start_with?("--protocol") |
| 95 | protocol = a.split("=")[1] |
| 96 | elsif a.start_with?("--transport") |
| 97 | transport = a.split("=")[1] |
| 98 | elsif a.start_with?("--port") |
| 99 | port = a.split("=")[1].to_i |
| 100 | end |
| 101 | end |
| 102 | |
| 103 | if protocol == "binary" |
| 104 | @protocolFactory = Thrift::BinaryProtocolFactory.new |
| 105 | elsif protocol == "" |
| 106 | @protocolFactory = Thrift::BinaryProtocolFactory.new |
| 107 | elsif protocol == "compact" |
| 108 | @protocolFactory = Thrift::CompactProtocolFactory.new |
| 109 | elsif protocol == "json" |
| 110 | @protocolFactory = Thrift::JsonProtocolFactory.new |
| 111 | elsif protocol == "accel" |
| 112 | @protocolFactory = Thrift::BinaryProtocolAcceleratedFactory.new |
| 113 | else |
| 114 | raise 'Unknown protocol type' |
| 115 | end |
| 116 | |
| 117 | if transport == "buffered" |
| 118 | @transportFactory = Thrift::BufferedTransportFactory.new |
| 119 | elsif transport == "" |
| 120 | @transportFactory = Thrift::BufferedTransportFactory.new |
| 121 | elsif transport == "framed" |
| 122 | @transportFactory = Thrift::FramedTransportFactory.new |
| 123 | else |
| 124 | raise 'Unknown transport type' |
| 125 | end |
| 126 | |
| 127 | @handler = SimpleHandler.new |
| 128 | @processor = Thrift::Test::ThriftTest::Processor.new(@handler) |
| 129 | @transport = Thrift::ServerSocket.new(port) |
| 130 | @server = Thrift::ThreadedServer.new(@processor, @transport, @transportFactory, @protocolFactory) |
| 131 | @server.serve |