blob: 6ee03992e19cb7673f5ccd06450a13b471c92e29 [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")
Roger Meier76150722014-05-31 22:22:07 +020038parser.add_option('-v', '--verbose', action="store_const",
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000039 dest="verbose", const=2,
40 help="verbose output")
Roger Meier76150722014-05-31 22:22:07 +020041parser.add_option('-q', '--quiet', action="store_const",
Bryan Duxbury16066592011-03-22 18:06:04 +000042 dest="verbose", const=0,
43 help="minimal output")
Roger Meier76150722014-05-31 22:22:07 +020044parser.add_option('--protocol', dest="proto", type="string",
Roger Meier85fb6de2012-11-02 00:05:42 +000045 help="protocol to use, one of: accel, binary, compact, json")
Roger Meier76150722014-05-31 22:22:07 +020046parser.add_option('--transport', dest="trans", type="string",
47 help="transport to use, one of: buffered, framed")
Bryan Duxbury16066592011-03-22 18:06:04 +000048parser.set_defaults(port=9090, verbose=1, proto='binary')
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000049options, args = parser.parse_args()
50
Roger Meierf4eec7a2011-09-11 18:16:21 +000051sys.path.insert(0, options.genpydir)
52
Roger Meier879cab22014-05-03 17:51:21 +020053from ThriftTest import ThriftTest, SecondService
Roger Meierf4eec7a2011-09-11 18:16:21 +000054from ThriftTest.ttypes import *
Roger Meier1f554e12013-01-05 20:38:35 +010055from thrift.Thrift import TException
Roger Meier879cab22014-05-03 17:51:21 +020056from thrift import TMultiplexedProcessor
Roger Meierf4eec7a2011-09-11 18:16:21 +000057from thrift.transport import TTransport
58from thrift.transport import TSocket
59from thrift.transport import TZlibTransport
60from thrift.protocol import TBinaryProtocol
61from thrift.protocol import TCompactProtocol
Roger Meier85fb6de2012-11-02 00:05:42 +000062from thrift.protocol import TJSONProtocol
Roger Meierf4eec7a2011-09-11 18:16:21 +000063from thrift.server import TServer, TNonblockingServer, THttpServer
64
65PROT_FACTORIES = {'binary': TBinaryProtocol.TBinaryProtocolFactory,
66 'accel': TBinaryProtocol.TBinaryProtocolAcceleratedFactory,
Roger Meier85fb6de2012-11-02 00:05:42 +000067 'compact': TCompactProtocol.TCompactProtocolFactory,
68 'json': TJSONProtocol.TJSONProtocolFactory}
Roger Meierf4eec7a2011-09-11 18:16:21 +000069
Roger Meier879cab22014-05-03 17:51:21 +020070class SecondHandler:
71
72 def blahBlah(self):
73 if options.verbose > 1:
74 print 'blahBlah()'
75
Mark Sleec9676562006-09-05 17:34:52 +000076class TestHandler:
77
78 def testVoid(self):
Bryan Duxbury16066592011-03-22 18:06:04 +000079 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000080 print 'testVoid()'
Mark Sleec9676562006-09-05 17:34:52 +000081
82 def testString(self, str):
Bryan Duxbury16066592011-03-22 18:06:04 +000083 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000084 print 'testString(%s)' % str
Mark Sleec9676562006-09-05 17:34:52 +000085 return str
86
87 def testByte(self, byte):
Bryan Duxbury16066592011-03-22 18:06:04 +000088 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000089 print 'testByte(%d)' % byte
Mark Sleec9676562006-09-05 17:34:52 +000090 return byte
91
Mark Sleec98d0502006-09-06 02:42:25 +000092 def testI16(self, i16):
Bryan Duxbury16066592011-03-22 18:06:04 +000093 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000094 print 'testI16(%d)' % i16
Mark Sleec98d0502006-09-06 02:42:25 +000095 return i16
96
97 def testI32(self, i32):
Bryan Duxbury16066592011-03-22 18:06:04 +000098 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000099 print 'testI32(%d)' % i32
Mark Sleec98d0502006-09-06 02:42:25 +0000100 return i32
101
102 def testI64(self, i64):
Bryan Duxbury16066592011-03-22 18:06:04 +0000103 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000104 print 'testI64(%d)' % i64
Mark Sleec98d0502006-09-06 02:42:25 +0000105 return i64
106
107 def testDouble(self, dub):
Bryan Duxbury16066592011-03-22 18:06:04 +0000108 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000109 print 'testDouble(%f)' % dub
Mark Sleec98d0502006-09-06 02:42:25 +0000110 return dub
111
112 def testStruct(self, thing):
Bryan Duxbury16066592011-03-22 18:06:04 +0000113 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000114 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 +0000115 return thing
116
Roger Meier1f554e12013-01-05 20:38:35 +0100117 def testException(self, arg):
118 #if options.verbose > 1:
119 print 'testException(%s)' % arg
120 if arg == 'Xception':
121 raise Xception(errorCode=1001, message=arg)
122 elif arg == 'TException':
123 raise TException(message='This is a TException')
124
125 def testMultiException(self, arg0, arg1):
Bryan Duxbury16066592011-03-22 18:06:04 +0000126 if options.verbose > 1:
Roger Meier1f554e12013-01-05 20:38:35 +0100127 print 'testMultiException(%s, %s)' % (arg0, arg1)
128 if arg0 == 'Xception':
129 raise Xception(errorCode=1001, message='This is an Xception')
130 elif arg0 == 'Xception2':
131 raise Xception2(
132 errorCode=2002,
133 struct_thing=Xtruct(string_thing='This is an Xception2'))
134 return Xtruct(string_thing=arg1)
Mark Sleec9676562006-09-05 17:34:52 +0000135
David Reiss6ce401d2009-03-24 20:01:58 +0000136 def testOneway(self, seconds):
Bryan Duxbury16066592011-03-22 18:06:04 +0000137 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000138 print 'testOneway(%d) => sleeping...' % seconds
139 time.sleep(seconds / 3) # be quick
Bryan Duxbury16066592011-03-22 18:06:04 +0000140 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000141 print 'done sleeping'
David Reissdb893b62008-02-18 02:11:48 +0000142
David Reiss74421272008-11-07 23:09:31 +0000143 def testNest(self, thing):
Bryan Duxbury16066592011-03-22 18:06:04 +0000144 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000145 print 'testNest(%s)' % thing
David Reiss74421272008-11-07 23:09:31 +0000146 return thing
147
148 def testMap(self, thing):
Bryan Duxbury16066592011-03-22 18:06:04 +0000149 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000150 print 'testMap(%s)' % thing
David Reiss74421272008-11-07 23:09:31 +0000151 return thing
152
153 def testSet(self, thing):
Bryan Duxbury16066592011-03-22 18:06:04 +0000154 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000155 print 'testSet(%s)' % thing
David Reiss74421272008-11-07 23:09:31 +0000156 return thing
157
158 def testList(self, thing):
Bryan Duxbury16066592011-03-22 18:06:04 +0000159 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000160 print 'testList(%s)' % thing
David Reiss74421272008-11-07 23:09:31 +0000161 return thing
162
163 def testEnum(self, thing):
Bryan Duxbury16066592011-03-22 18:06:04 +0000164 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000165 print 'testEnum(%s)' % thing
David Reiss74421272008-11-07 23:09:31 +0000166 return thing
167
168 def testTypedef(self, thing):
Bryan Duxbury16066592011-03-22 18:06:04 +0000169 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000170 print 'testTypedef(%s)' % thing
David Reiss74421272008-11-07 23:09:31 +0000171 return thing
172
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000173 def testMapMap(self, thing):
Bryan Duxbury16066592011-03-22 18:06:04 +0000174 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000175 print 'testMapMap(%s)' % thing
Roger Meier1f554e12013-01-05 20:38:35 +0100176 return {thing: {thing: thing}}
177
178 def testInsanity(self, argument):
179 if options.verbose > 1:
180 print 'testInsanity(%s)' % argument
181 return {123489: {Numberz.ONE:argument}}
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000182
183 def testMulti(self, arg0, arg1, arg2, arg3, arg4, arg5):
Bryan Duxbury16066592011-03-22 18:06:04 +0000184 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000185 print 'testMulti(%s)' % [arg0, arg1, arg2, arg3, arg4, arg5]
Roger Meier1f554e12013-01-05 20:38:35 +0100186 return Xtruct(string_thing='Hello2',
187 byte_thing=arg0, i32_thing=arg1, i64_thing=arg2)
188
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000189
Roger Meier76150722014-05-31 22:22:07 +0200190# set up the protocol factory form the --protocol option
Bryan Duxbury16066592011-03-22 18:06:04 +0000191pfactory_cls = PROT_FACTORIES.get(options.proto, None)
192if pfactory_cls is None:
Roger Meier76150722014-05-31 22:22:07 +0200193 raise AssertionError('Unknown --protocol option: %s' % options.proto)
Bryan Duxbury16066592011-03-22 18:06:04 +0000194pfactory = pfactory_cls()
195
196# get the server type (TSimpleServer, TNonblockingServer, etc...)
197if len(args) > 1:
198 raise AssertionError('Only one server type may be specified, not multiple types.')
199server_type = args[0]
200
201# Set up the handler and processor objects
Roger Meier879cab22014-05-03 17:51:21 +0200202if not options.multiple:
203 handler = TestHandler()
204 processor = ThriftTest.Processor(handler)
205else:
206 processor = TMultiplexedProcessor.TMultiplexedProcessor()
207 handler = TestHandler()
208 processor.registerProcessor("ThriftTest", ThriftTest.Processor(handler))
209 handler = SecondHandler()
210 processor.registerProcessor("SecondService", SecondService.Processor(handler))
David Reissbcaa2ad2008-06-10 22:55:26 +0000211
Bryan Duxbury16066592011-03-22 18:06:04 +0000212# Handle THttpServer as a special case
213if server_type == 'THttpServer':
214 server =THttpServer.THttpServer(processor, ('', options.port), pfactory)
215 server.serve()
216 sys.exit(0)
217
218# set up server transport and transport factory
Roger Meier76150722014-05-31 22:22:07 +0200219
220script_dir = os.path.dirname(__file__) #<-- absolute dir the script is in
221rel_path = "../keys/server.pem"
222abs_key_path = os.path.join(script_dir, rel_path)
223
Bryan Duxbury16066592011-03-22 18:06:04 +0000224host = None
225if options.ssl:
226 from thrift.transport import TSSLSocket
Roger Meier76150722014-05-31 22:22:07 +0200227 transport = TSSLSocket.TSSLServerSocket(host, options.port, certfile=abs_key_path)
David Reiss74421272008-11-07 23:09:31 +0000228else:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000229 transport = TSocket.TServerSocket(host, options.port)
Bryan Duxbury16066592011-03-22 18:06:04 +0000230tfactory = TTransport.TBufferedTransportFactory()
Roger Meier76150722014-05-31 22:22:07 +0200231if options.trans == 'buffered':
232 tfactory = TTransport.TBufferedTransportFactory()
233elif options.trans == 'framed':
234 tfactory = TTransport.TFramedTransportFactory()
235elif options.trans == '':
236 raise AssertionError('Unknown --transport option: %s' % options.trans)
237else:
238 tfactory = TTransport.TBufferedTransportFactory()
Bryan Duxbury16066592011-03-22 18:06:04 +0000239# if --zlib, then wrap server transport, and use a different transport factory
240if options.zlib:
241 transport = TZlibTransport.TZlibTransport(transport) # wrap with zlib
242 tfactory = TZlibTransport.TZlibTransportFactory()
243
244# do server-specific setup here:
245if server_type == "TNonblockingServer":
246 server = TNonblockingServer.TNonblockingServer(processor, transport, inputProtocolFactory=pfactory)
247elif server_type == "TProcessPoolServer":
248 import signal
249 from thrift.server import TProcessPoolServer
250 server = TProcessPoolServer.TProcessPoolServer(processor, transport, tfactory, pfactory)
251 server.setNumWorkers(5)
252 def set_alarm():
253 def clean_shutdown(signum, frame):
254 for worker in server.workers:
255 if options.verbose > 0:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000256 print 'Terminating worker: %s' % worker
Bryan Duxbury16066592011-03-22 18:06:04 +0000257 worker.terminate()
258 if options.verbose > 0:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000259 print 'Requesting server to stop()'
Roger Meierf4eec7a2011-09-11 18:16:21 +0000260 try:
261 server.stop()
262 except:
263 pass
Bryan Duxbury16066592011-03-22 18:06:04 +0000264 signal.signal(signal.SIGALRM, clean_shutdown)
265 signal.alarm(2)
266 set_alarm()
267else:
268 # look up server class dynamically to instantiate server
269 ServerClass = getattr(TServer, server_type)
270 server = ServerClass(processor, transport, tfactory, pfactory)
271# enter server main loop
Mark Slee794993d2006-09-20 01:56:10 +0000272server.serve()