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 | db893b6 | 2008-02-18 02:11:48 +0000 | [diff] [blame] | 3 | import sys, glob, time |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 4 | sys.path.insert(0, './gen-py') |
| 5 | sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0]) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 6 | |
Mark Slee | 57cc25e | 2007-02-28 21:43:54 +0000 | [diff] [blame] | 7 | from ThriftTest import ThriftTest |
| 8 | from ThriftTest.ttypes import * |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 9 | from thrift.transport import TTransport |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 10 | from thrift.transport import TSocket |
| 11 | from thrift.protocol import TBinaryProtocol |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 12 | from thrift.server import TServer, TNonblockingServer, THttpServer |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 13 | |
| 14 | class TestHandler: |
| 15 | |
| 16 | def testVoid(self): |
| 17 | print 'testVoid()' |
| 18 | |
| 19 | def testString(self, str): |
| 20 | print 'testString(%s)' % str |
| 21 | return str |
| 22 | |
| 23 | def testByte(self, byte): |
| 24 | print 'testByte(%d)' % byte |
| 25 | return byte |
| 26 | |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 27 | def testI16(self, i16): |
| 28 | print 'testI16(%d)' % i16 |
| 29 | return i16 |
| 30 | |
| 31 | def testI32(self, i32): |
| 32 | print 'testI32(%d)' % i32 |
| 33 | return i32 |
| 34 | |
| 35 | def testI64(self, i64): |
| 36 | print 'testI64(%d)' % i64 |
| 37 | return i64 |
| 38 | |
| 39 | def testDouble(self, dub): |
| 40 | print 'testDouble(%f)' % dub |
| 41 | return dub |
| 42 | |
| 43 | def testStruct(self, thing): |
| 44 | print 'testStruct({%s, %d, %d, %d})' % (thing.string_thing, thing.byte_thing, thing.i32_thing, thing.i64_thing) |
| 45 | return thing |
| 46 | |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 47 | def testException(self, str): |
| 48 | print 'testException(%s)' % str |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 49 | if str == 'Xception': |
| 50 | x = Xception() |
| 51 | x.errorCode = 1001 |
| 52 | x.message = str |
| 53 | raise x |
David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 54 | elif str == "throw_undeclared": |
| 55 | raise ValueError("foo") |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 56 | |
David Reiss | 6ce401d | 2009-03-24 20:01:58 +0000 | [diff] [blame] | 57 | def testOneway(self, seconds): |
| 58 | print 'testOneway(%d) => sleeping...' % seconds |
David Reiss | db893b6 | 2008-02-18 02:11:48 +0000 | [diff] [blame] | 59 | time.sleep(seconds) |
| 60 | print 'done sleeping' |
| 61 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 62 | def testNest(self, thing): |
| 63 | return thing |
| 64 | |
| 65 | def testMap(self, thing): |
| 66 | return thing |
| 67 | |
| 68 | def testSet(self, thing): |
| 69 | return thing |
| 70 | |
| 71 | def testList(self, thing): |
| 72 | return thing |
| 73 | |
| 74 | def testEnum(self, thing): |
| 75 | return thing |
| 76 | |
| 77 | def testTypedef(self, thing): |
| 78 | return thing |
| 79 | |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 80 | pfactory = TBinaryProtocol.TBinaryProtocolFactory() |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 81 | handler = TestHandler() |
Mark Slee | 1dd819c | 2006-10-26 04:56:18 +0000 | [diff] [blame] | 82 | processor = ThriftTest.Processor(handler) |
David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 83 | |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 84 | if sys.argv[1] == "THttpServer": |
| 85 | server = THttpServer.THttpServer(processor, ('', 9090), pfactory) |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 86 | else: |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 87 | transport = TSocket.TServerSocket(9090) |
| 88 | tfactory = TTransport.TBufferedTransportFactory() |
| 89 | |
| 90 | if sys.argv[1] == "TNonblockingServer": |
| 91 | server = TNonblockingServer.TNonblockingServer(processor, transport) |
| 92 | else: |
| 93 | ServerClass = getattr(TServer, sys.argv[1]) |
| 94 | server = ServerClass(processor, transport, tfactory, pfactory) |
| 95 | |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 96 | server.serve() |