Mark Slee | 57cc25e | 2007-02-28 21:43:54 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 2 | |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [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 | |
David Reiss | db893b6 | 2008-02-18 02:11:48 +0000 | [diff] [blame] | 22 | import sys, glob, time |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 23 | sys.path.insert(0, './gen-py') |
| 24 | sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0]) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 25 | |
Mark Slee | 57cc25e | 2007-02-28 21:43:54 +0000 | [diff] [blame] | 26 | from ThriftTest import ThriftTest |
| 27 | from ThriftTest.ttypes import * |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 28 | from thrift.transport import TTransport |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 29 | from thrift.transport import TSocket |
| 30 | from thrift.protocol import TBinaryProtocol |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 31 | from thrift.server import TServer, TNonblockingServer, THttpServer |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 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 | |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 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 | |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 66 | def testException(self, str): |
| 67 | print 'testException(%s)' % str |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 68 | if str == 'Xception': |
| 69 | x = Xception() |
| 70 | x.errorCode = 1001 |
| 71 | x.message = str |
| 72 | raise x |
David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 73 | elif str == "throw_undeclared": |
| 74 | raise ValueError("foo") |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 75 | |
David Reiss | 6ce401d | 2009-03-24 20:01:58 +0000 | [diff] [blame] | 76 | def testOneway(self, seconds): |
| 77 | print 'testOneway(%d) => sleeping...' % seconds |
David Reiss | db893b6 | 2008-02-18 02:11:48 +0000 | [diff] [blame] | 78 | time.sleep(seconds) |
| 79 | print 'done sleeping' |
| 80 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 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 | |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 99 | pfactory = TBinaryProtocol.TBinaryProtocolFactory() |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 100 | handler = TestHandler() |
Mark Slee | 1dd819c | 2006-10-26 04:56:18 +0000 | [diff] [blame] | 101 | processor = ThriftTest.Processor(handler) |
David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 102 | |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 103 | if sys.argv[1] == "THttpServer": |
| 104 | server = THttpServer.THttpServer(processor, ('', 9090), pfactory) |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 105 | else: |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 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 | |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 115 | server.serve() |