blob: 8022341392601fae2926dfd0e41e726ee0cc9d6f [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#
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000021from __future__ import division
Roger Meier85fb6de2012-11-02 00:05:42 +000022import sys, glob, time, os
23sys.path.insert(0, glob.glob(os.path.join(os.path.dirname(__file__),'../../lib/py/build/lib.*'))[0])
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000024from optparse import OptionParser
Mark Sleec9676562006-09-05 17:34:52 +000025
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000026parser = OptionParser()
Roger Meierf4eec7a2011-09-11 18:16:21 +000027parser.add_option('--genpydir', type='string', dest='genpydir',
28 default='gen-py',
29 help='include this local directory in sys.path for locating generated code')
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000030parser.add_option("--port", type="int", dest="port",
31 help="port number for server to listen on")
Bryan Duxbury16066592011-03-22 18:06:04 +000032parser.add_option("--zlib", action="store_true", dest="zlib",
33 help="use zlib wrapper for compressed transport")
34parser.add_option("--ssl", action="store_true", dest="ssl",
35 help="use SSL for encrypted transport")
Roger Meier879cab22014-05-03 17:51:21 +020036parser.add_option("--multiple", action="store_true", dest="multiple",
37 help="use multiple service")
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000038parser.add_option('-v', '--verbose', action="store_const",
39 dest="verbose", const=2,
40 help="verbose output")
Bryan Duxbury16066592011-03-22 18:06:04 +000041parser.add_option('-q', '--quiet', action="store_const",
42 dest="verbose", const=0,
43 help="minimal output")
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000044parser.add_option('--proto', dest="proto", type="string",
Roger Meier85fb6de2012-11-02 00:05:42 +000045 help="protocol to use, one of: accel, binary, compact, json")
Bryan Duxbury16066592011-03-22 18:06:04 +000046parser.set_defaults(port=9090, verbose=1, proto='binary')
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000047options, args = parser.parse_args()
48
Roger Meierf4eec7a2011-09-11 18:16:21 +000049sys.path.insert(0, options.genpydir)
50
Roger Meier879cab22014-05-03 17:51:21 +020051from ThriftTest import ThriftTest, SecondService
Roger Meierf4eec7a2011-09-11 18:16:21 +000052from ThriftTest.ttypes import *
Roger Meier1f554e12013-01-05 20:38:35 +010053from thrift.Thrift import TException
Roger Meier879cab22014-05-03 17:51:21 +020054from thrift import TMultiplexedProcessor
Roger Meierf4eec7a2011-09-11 18:16:21 +000055from thrift.transport import TTransport
56from thrift.transport import TSocket
57from thrift.transport import TZlibTransport
58from thrift.protocol import TBinaryProtocol
59from thrift.protocol import TCompactProtocol
Roger Meier85fb6de2012-11-02 00:05:42 +000060from thrift.protocol import TJSONProtocol
Roger Meierf4eec7a2011-09-11 18:16:21 +000061from thrift.server import TServer, TNonblockingServer, THttpServer
62
63PROT_FACTORIES = {'binary': TBinaryProtocol.TBinaryProtocolFactory,
64 'accel': TBinaryProtocol.TBinaryProtocolAcceleratedFactory,
Roger Meier85fb6de2012-11-02 00:05:42 +000065 'compact': TCompactProtocol.TCompactProtocolFactory,
66 'json': TJSONProtocol.TJSONProtocolFactory}
Roger Meierf4eec7a2011-09-11 18:16:21 +000067
Roger Meier879cab22014-05-03 17:51:21 +020068class SecondHandler:
69
70 def blahBlah(self):
71 if options.verbose > 1:
72 print 'blahBlah()'
73
Mark Sleec9676562006-09-05 17:34:52 +000074class TestHandler:
75
76 def testVoid(self):
Bryan Duxbury16066592011-03-22 18:06:04 +000077 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000078 print 'testVoid()'
Mark Sleec9676562006-09-05 17:34:52 +000079
80 def testString(self, str):
Bryan Duxbury16066592011-03-22 18:06:04 +000081 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000082 print 'testString(%s)' % str
Mark Sleec9676562006-09-05 17:34:52 +000083 return str
84
85 def testByte(self, byte):
Bryan Duxbury16066592011-03-22 18:06:04 +000086 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000087 print 'testByte(%d)' % byte
Mark Sleec9676562006-09-05 17:34:52 +000088 return byte
89
Mark Sleec98d0502006-09-06 02:42:25 +000090 def testI16(self, i16):
Bryan Duxbury16066592011-03-22 18:06:04 +000091 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000092 print 'testI16(%d)' % i16
Mark Sleec98d0502006-09-06 02:42:25 +000093 return i16
94
95 def testI32(self, i32):
Bryan Duxbury16066592011-03-22 18:06:04 +000096 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000097 print 'testI32(%d)' % i32
Mark Sleec98d0502006-09-06 02:42:25 +000098 return i32
99
100 def testI64(self, i64):
Bryan Duxbury16066592011-03-22 18:06:04 +0000101 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000102 print 'testI64(%d)' % i64
Mark Sleec98d0502006-09-06 02:42:25 +0000103 return i64
104
105 def testDouble(self, dub):
Bryan Duxbury16066592011-03-22 18:06:04 +0000106 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000107 print 'testDouble(%f)' % dub
Mark Sleec98d0502006-09-06 02:42:25 +0000108 return dub
109
110 def testStruct(self, thing):
Bryan Duxbury16066592011-03-22 18:06:04 +0000111 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000112 print 'testStruct({%s, %d, %d, %d})' % (thing.string_thing, thing.byte_thing, thing.i32_thing, thing.i64_thing)
Mark Sleec98d0502006-09-06 02:42:25 +0000113 return thing
114
Roger Meier1f554e12013-01-05 20:38:35 +0100115 def testException(self, arg):
116 #if options.verbose > 1:
117 print 'testException(%s)' % arg
118 if arg == 'Xception':
119 raise Xception(errorCode=1001, message=arg)
120 elif arg == 'TException':
121 raise TException(message='This is a TException')
122
123 def testMultiException(self, arg0, arg1):
Bryan Duxbury16066592011-03-22 18:06:04 +0000124 if options.verbose > 1:
Roger Meier1f554e12013-01-05 20:38:35 +0100125 print 'testMultiException(%s, %s)' % (arg0, arg1)
126 if arg0 == 'Xception':
127 raise Xception(errorCode=1001, message='This is an Xception')
128 elif arg0 == 'Xception2':
129 raise Xception2(
130 errorCode=2002,
131 struct_thing=Xtruct(string_thing='This is an Xception2'))
132 return Xtruct(string_thing=arg1)
Mark Sleec9676562006-09-05 17:34:52 +0000133
David Reiss6ce401d2009-03-24 20:01:58 +0000134 def testOneway(self, seconds):
Bryan Duxbury16066592011-03-22 18:06:04 +0000135 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000136 print 'testOneway(%d) => sleeping...' % seconds
137 time.sleep(seconds / 3) # be quick
Bryan Duxbury16066592011-03-22 18:06:04 +0000138 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000139 print 'done sleeping'
David Reissdb893b62008-02-18 02:11:48 +0000140
David Reiss74421272008-11-07 23:09:31 +0000141 def testNest(self, thing):
Bryan Duxbury16066592011-03-22 18:06:04 +0000142 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000143 print 'testNest(%s)' % thing
David Reiss74421272008-11-07 23:09:31 +0000144 return thing
145
146 def testMap(self, thing):
Bryan Duxbury16066592011-03-22 18:06:04 +0000147 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000148 print 'testMap(%s)' % thing
David Reiss74421272008-11-07 23:09:31 +0000149 return thing
150
151 def testSet(self, thing):
Bryan Duxbury16066592011-03-22 18:06:04 +0000152 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000153 print 'testSet(%s)' % thing
David Reiss74421272008-11-07 23:09:31 +0000154 return thing
155
156 def testList(self, thing):
Bryan Duxbury16066592011-03-22 18:06:04 +0000157 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000158 print 'testList(%s)' % thing
David Reiss74421272008-11-07 23:09:31 +0000159 return thing
160
161 def testEnum(self, thing):
Bryan Duxbury16066592011-03-22 18:06:04 +0000162 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000163 print 'testEnum(%s)' % thing
David Reiss74421272008-11-07 23:09:31 +0000164 return thing
165
166 def testTypedef(self, thing):
Bryan Duxbury16066592011-03-22 18:06:04 +0000167 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000168 print 'testTypedef(%s)' % thing
David Reiss74421272008-11-07 23:09:31 +0000169 return thing
170
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000171 def testMapMap(self, thing):
Bryan Duxbury16066592011-03-22 18:06:04 +0000172 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000173 print 'testMapMap(%s)' % thing
Roger Meier1f554e12013-01-05 20:38:35 +0100174 return {thing: {thing: thing}}
175
176 def testInsanity(self, argument):
177 if options.verbose > 1:
178 print 'testInsanity(%s)' % argument
179 return {123489: {Numberz.ONE:argument}}
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000180
181 def testMulti(self, arg0, arg1, arg2, arg3, arg4, arg5):
Bryan Duxbury16066592011-03-22 18:06:04 +0000182 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000183 print 'testMulti(%s)' % [arg0, arg1, arg2, arg3, arg4, arg5]
Roger Meier1f554e12013-01-05 20:38:35 +0100184 return Xtruct(string_thing='Hello2',
185 byte_thing=arg0, i32_thing=arg1, i64_thing=arg2)
186
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000187
Bryan Duxbury16066592011-03-22 18:06:04 +0000188# set up the protocol factory form the --proto option
189pfactory_cls = PROT_FACTORIES.get(options.proto, None)
190if pfactory_cls is None:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000191 raise AssertionError('Unknown --proto option: %s' % options.proto)
Bryan Duxbury16066592011-03-22 18:06:04 +0000192pfactory = pfactory_cls()
193
194# get the server type (TSimpleServer, TNonblockingServer, etc...)
195if len(args) > 1:
196 raise AssertionError('Only one server type may be specified, not multiple types.')
197server_type = args[0]
198
199# Set up the handler and processor objects
Roger Meier879cab22014-05-03 17:51:21 +0200200if not options.multiple:
201 handler = TestHandler()
202 processor = ThriftTest.Processor(handler)
203else:
204 processor = TMultiplexedProcessor.TMultiplexedProcessor()
205 handler = TestHandler()
206 processor.registerProcessor("ThriftTest", ThriftTest.Processor(handler))
207 handler = SecondHandler()
208 processor.registerProcessor("SecondService", SecondService.Processor(handler))
David Reissbcaa2ad2008-06-10 22:55:26 +0000209
Bryan Duxbury16066592011-03-22 18:06:04 +0000210# Handle THttpServer as a special case
211if server_type == 'THttpServer':
212 server =THttpServer.THttpServer(processor, ('', options.port), pfactory)
213 server.serve()
214 sys.exit(0)
215
216# set up server transport and transport factory
217host = None
218if options.ssl:
219 from thrift.transport import TSSLSocket
Roger Meier58bbb702014-02-19 19:59:25 +0100220 transport = TSSLSocket.TSSLServerSocket(host, options.port, certfile='../keys/server.pem')
David Reiss74421272008-11-07 23:09:31 +0000221else:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000222 transport = TSocket.TServerSocket(host, options.port)
Bryan Duxbury16066592011-03-22 18:06:04 +0000223tfactory = TTransport.TBufferedTransportFactory()
David Reissf78ec2b2009-01-31 21:59:32 +0000224
Bryan Duxbury16066592011-03-22 18:06:04 +0000225# if --zlib, then wrap server transport, and use a different transport factory
226if options.zlib:
227 transport = TZlibTransport.TZlibTransport(transport) # wrap with zlib
228 tfactory = TZlibTransport.TZlibTransportFactory()
229
230# do server-specific setup here:
231if server_type == "TNonblockingServer":
232 server = TNonblockingServer.TNonblockingServer(processor, transport, inputProtocolFactory=pfactory)
233elif server_type == "TProcessPoolServer":
234 import signal
235 from thrift.server import TProcessPoolServer
236 server = TProcessPoolServer.TProcessPoolServer(processor, transport, tfactory, pfactory)
237 server.setNumWorkers(5)
238 def set_alarm():
239 def clean_shutdown(signum, frame):
240 for worker in server.workers:
241 if options.verbose > 0:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000242 print 'Terminating worker: %s' % worker
Bryan Duxbury16066592011-03-22 18:06:04 +0000243 worker.terminate()
244 if options.verbose > 0:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000245 print 'Requesting server to stop()'
Roger Meierf4eec7a2011-09-11 18:16:21 +0000246 try:
247 server.stop()
248 except:
249 pass
Bryan Duxbury16066592011-03-22 18:06:04 +0000250 signal.signal(signal.SIGALRM, clean_shutdown)
251 signal.alarm(2)
252 set_alarm()
253else:
254 # look up server class dynamically to instantiate server
255 ServerClass = getattr(TServer, server_type)
256 server = ServerClass(processor, transport, tfactory, pfactory)
257# enter server main loop
Mark Slee794993d2006-09-20 01:56:10 +0000258server.serve()