blob: 3d379eaeba8638646696ad36bc966210b95f05aa [file] [log] [blame]
Mark Slee57cc25e2007-02-28 21:43:54 +00001#!/usr/bin/env python
Mark Sleec9676562006-09-05 17:34:52 +00002
David Reissea2cba82009-03-30 21:35:00 +00003#
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 Reissdb893b62008-02-18 02:11:48 +000022import sys, glob, time
Mark Slee5299a952007-10-05 00:13:24 +000023sys.path.insert(0, './gen-py')
24sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0])
Mark Sleec9676562006-09-05 17:34:52 +000025
Mark Slee57cc25e2007-02-28 21:43:54 +000026from ThriftTest import ThriftTest
27from ThriftTest.ttypes import *
Mark Sleed788b2e2006-09-07 01:26:35 +000028from thrift.transport import TTransport
Mark Sleec9676562006-09-05 17:34:52 +000029from thrift.transport import TSocket
30from thrift.protocol import TBinaryProtocol
David Reissf78ec2b2009-01-31 21:59:32 +000031from thrift.server import TServer, TNonblockingServer, THttpServer
Mark Sleec9676562006-09-05 17:34:52 +000032
33class 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 Sleec98d0502006-09-06 02:42:25 +000046 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 Sleec9676562006-09-05 17:34:52 +000066 def testException(self, str):
67 print 'testException(%s)' % str
Mark Sleec98d0502006-09-06 02:42:25 +000068 if str == 'Xception':
69 x = Xception()
70 x.errorCode = 1001
71 x.message = str
72 raise x
David Reissbcaa2ad2008-06-10 22:55:26 +000073 elif str == "throw_undeclared":
74 raise ValueError("foo")
Mark Sleec9676562006-09-05 17:34:52 +000075
David Reiss6ce401d2009-03-24 20:01:58 +000076 def testOneway(self, seconds):
77 print 'testOneway(%d) => sleeping...' % seconds
David Reissdb893b62008-02-18 02:11:48 +000078 time.sleep(seconds)
79 print 'done sleeping'
80
David Reiss74421272008-11-07 23:09:31 +000081 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 Reissf78ec2b2009-01-31 21:59:32 +000099pfactory = TBinaryProtocol.TBinaryProtocolFactory()
Mark Sleec9676562006-09-05 17:34:52 +0000100handler = TestHandler()
Mark Slee1dd819c2006-10-26 04:56:18 +0000101processor = ThriftTest.Processor(handler)
David Reissbcaa2ad2008-06-10 22:55:26 +0000102
David Reissf78ec2b2009-01-31 21:59:32 +0000103if sys.argv[1] == "THttpServer":
104 server = THttpServer.THttpServer(processor, ('', 9090), pfactory)
David Reiss74421272008-11-07 23:09:31 +0000105else:
David Reissf78ec2b2009-01-31 21:59:32 +0000106 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 Slee794993d2006-09-20 01:56:10 +0000115server.serve()