blob: ab9666604abc408926072ff7575b6749c01092ef [file] [log] [blame]
Mark Slee57cc25e2007-02-28 21:43:54 +00001#!/usr/bin/env python
Mark Sleec9676562006-09-05 17:34:52 +00002
David Reissdb893b62008-02-18 02:11:48 +00003import sys, glob, time
Mark Slee5299a952007-10-05 00:13:24 +00004sys.path.insert(0, './gen-py')
5sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0])
Mark Sleec9676562006-09-05 17:34:52 +00006
Mark Slee57cc25e2007-02-28 21:43:54 +00007from ThriftTest import ThriftTest
8from ThriftTest.ttypes import *
Mark Sleed788b2e2006-09-07 01:26:35 +00009from thrift.transport import TTransport
Mark Sleec9676562006-09-05 17:34:52 +000010from thrift.transport import TSocket
11from thrift.protocol import TBinaryProtocol
David Reissf78ec2b2009-01-31 21:59:32 +000012from thrift.server import TServer, TNonblockingServer, THttpServer
Mark Sleec9676562006-09-05 17:34:52 +000013
14class 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 Sleec98d0502006-09-06 02:42:25 +000027 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 Sleec9676562006-09-05 17:34:52 +000047 def testException(self, str):
48 print 'testException(%s)' % str
Mark Sleec98d0502006-09-06 02:42:25 +000049 if str == 'Xception':
50 x = Xception()
51 x.errorCode = 1001
52 x.message = str
53 raise x
David Reissbcaa2ad2008-06-10 22:55:26 +000054 elif str == "throw_undeclared":
55 raise ValueError("foo")
Mark Sleec9676562006-09-05 17:34:52 +000056
David Reiss6ce401d2009-03-24 20:01:58 +000057 def testOneway(self, seconds):
58 print 'testOneway(%d) => sleeping...' % seconds
David Reissdb893b62008-02-18 02:11:48 +000059 time.sleep(seconds)
60 print 'done sleeping'
61
David Reiss74421272008-11-07 23:09:31 +000062 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 Reissf78ec2b2009-01-31 21:59:32 +000080pfactory = TBinaryProtocol.TBinaryProtocolFactory()
Mark Sleec9676562006-09-05 17:34:52 +000081handler = TestHandler()
Mark Slee1dd819c2006-10-26 04:56:18 +000082processor = ThriftTest.Processor(handler)
David Reissbcaa2ad2008-06-10 22:55:26 +000083
David Reissf78ec2b2009-01-31 21:59:32 +000084if sys.argv[1] == "THttpServer":
85 server = THttpServer.THttpServer(processor, ('', 9090), pfactory)
David Reiss74421272008-11-07 23:09:31 +000086else:
David Reissf78ec2b2009-01-31 21:59:32 +000087 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 Slee794993d2006-09-20 01:56:10 +000096server.serve()