| Gavin McDonald | 0b75e1a | 2010-10-28 02:12:01 +0000 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 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 | import sys, glob, time |
| 23 | sys.path.insert(0, './gen-py') |
| 24 | sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0]) |
| 25 | |
| 26 | from ThriftTest import ThriftTest |
| 27 | from ThriftTest.ttypes import * |
| 28 | from thrift.transport import TTransport |
| 29 | from thrift.transport import TSocket |
| 30 | from thrift.protocol import TBinaryProtocol |
| 31 | from thrift.server import TServer, TNonblockingServer, THttpServer |
| 32 | |
| 33 | class TestHandler: |
| 34 | |
| 35 | def testVoid(self): |
| 36 | print 'testVoid()' |
| 37 | |
| 38 | def testString(self, str): |
| 39 | print 'testString(%s)' % str |
| 40 | return str |
| 41 | |
| 42 | def testByte(self, byte): |
| 43 | print 'testByte(%d)' % byte |
| 44 | return byte |
| 45 | |
| 46 | def testI16(self, i16): |
| 47 | print 'testI16(%d)' % i16 |
| 48 | return i16 |
| 49 | |
| 50 | def testI32(self, i32): |
| 51 | print 'testI32(%d)' % i32 |
| 52 | return i32 |
| 53 | |
| 54 | def testI64(self, i64): |
| 55 | print 'testI64(%d)' % i64 |
| 56 | return i64 |
| 57 | |
| 58 | def testDouble(self, dub): |
| 59 | print 'testDouble(%f)' % dub |
| 60 | return dub |
| 61 | |
| 62 | def testStruct(self, thing): |
| 63 | print 'testStruct({%s, %d, %d, %d})' % (thing.string_thing, thing.byte_thing, thing.i32_thing, thing.i64_thing) |
| 64 | return thing |
| 65 | |
| 66 | def testException(self, str): |
| 67 | print 'testException(%s)' % str |
| 68 | if str == 'Xception': |
| 69 | x = Xception() |
| 70 | x.errorCode = 1001 |
| 71 | x.message = str |
| 72 | raise x |
| 73 | elif str == "throw_undeclared": |
| 74 | raise ValueError("foo") |
| 75 | |
| 76 | def testOneway(self, seconds): |
| 77 | print 'testOneway(%d) => sleeping...' % seconds |
| 78 | time.sleep(seconds) |
| 79 | print 'done sleeping' |
| 80 | |
| 81 | def testNest(self, thing): |
| 82 | return thing |
| 83 | |
| 84 | def testMap(self, thing): |
| 85 | return thing |
| 86 | |
| 87 | def testSet(self, thing): |
| 88 | return thing |
| 89 | |
| 90 | def testList(self, thing): |
| 91 | return thing |
| 92 | |
| 93 | def testEnum(self, thing): |
| 94 | return thing |
| 95 | |
| 96 | def testTypedef(self, thing): |
| 97 | return thing |
| 98 | |
| 99 | pfactory = TBinaryProtocol.TBinaryProtocolFactory() |
| 100 | handler = TestHandler() |
| 101 | processor = ThriftTest.Processor(handler) |
| 102 | |
| 103 | if sys.argv[1] == "THttpServer": |
| 104 | server = THttpServer.THttpServer(processor, ('', 9090), pfactory) |
| 105 | else: |
| 106 | transport = TSocket.TServerSocket(9090) |
| 107 | tfactory = TTransport.TBufferedTransportFactory() |
| 108 | |
| 109 | if sys.argv[1] == "TNonblockingServer": |
| 110 | server = TNonblockingServer.TNonblockingServer(processor, transport) |
| 111 | else: |
| 112 | ServerClass = getattr(TServer, sys.argv[1]) |
| 113 | server = ServerClass(processor, transport, tfactory, pfactory) |
| 114 | |
| 115 | server.serve() |