2007-02-28 21:43:54 +00:00
|
|
|
#!/usr/bin/env python
|
2006-09-05 17:34:52 +00:00
|
|
|
|
2009-03-30 21:35:00 +00:00
|
|
|
#
|
|
|
|
# Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
# or more contributor license agreements. See the NOTICE file
|
|
|
|
# distributed with this work for additional information
|
|
|
|
# regarding copyright ownership. The ASF licenses this file
|
|
|
|
# to you under the Apache License, Version 2.0 (the
|
|
|
|
# "License"); you may not use this file except in compliance
|
|
|
|
# with the License. You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing,
|
|
|
|
# software distributed under the License is distributed on an
|
|
|
|
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
# KIND, either express or implied. See the License for the
|
|
|
|
# specific language governing permissions and limitations
|
|
|
|
# under the License.
|
|
|
|
#
|
2011-03-21 17:38:22 +00:00
|
|
|
from __future__ import division
|
2008-02-18 02:11:48 +00:00
|
|
|
import sys, glob, time
|
2007-10-05 00:13:24 +00:00
|
|
|
sys.path.insert(0, './gen-py')
|
|
|
|
sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0])
|
2011-03-21 17:38:22 +00:00
|
|
|
from optparse import OptionParser
|
2006-09-05 17:34:52 +00:00
|
|
|
|
2007-02-28 21:43:54 +00:00
|
|
|
from ThriftTest import ThriftTest
|
|
|
|
from ThriftTest.ttypes import *
|
2006-09-07 01:26:35 +00:00
|
|
|
from thrift.transport import TTransport
|
2006-09-05 17:34:52 +00:00
|
|
|
from thrift.transport import TSocket
|
2011-03-22 18:06:04 +00:00
|
|
|
from thrift.transport import TZlibTransport
|
2006-09-05 17:34:52 +00:00
|
|
|
from thrift.protocol import TBinaryProtocol
|
2011-03-21 17:38:22 +00:00
|
|
|
from thrift.protocol import TCompactProtocol
|
2009-01-31 21:59:32 +00:00
|
|
|
from thrift.server import TServer, TNonblockingServer, THttpServer
|
2006-09-05 17:34:52 +00:00
|
|
|
|
2011-03-22 18:06:04 +00:00
|
|
|
PROT_FACTORIES = {'binary': TBinaryProtocol.TBinaryProtocolFactory,
|
|
|
|
'accel': TBinaryProtocol.TBinaryProtocolAcceleratedFactory,
|
|
|
|
'compact': TCompactProtocol.TCompactProtocolFactory}
|
|
|
|
|
2011-03-21 17:38:22 +00:00
|
|
|
parser = OptionParser()
|
|
|
|
parser.add_option("--port", type="int", dest="port",
|
|
|
|
help="port number for server to listen on")
|
2011-03-22 18:06:04 +00:00
|
|
|
parser.add_option("--zlib", action="store_true", dest="zlib",
|
|
|
|
help="use zlib wrapper for compressed transport")
|
|
|
|
parser.add_option("--ssl", action="store_true", dest="ssl",
|
|
|
|
help="use SSL for encrypted transport")
|
2011-03-21 17:38:22 +00:00
|
|
|
parser.add_option('-v', '--verbose', action="store_const",
|
|
|
|
dest="verbose", const=2,
|
|
|
|
help="verbose output")
|
2011-03-22 18:06:04 +00:00
|
|
|
parser.add_option('-q', '--quiet', action="store_const",
|
|
|
|
dest="verbose", const=0,
|
|
|
|
help="minimal output")
|
2011-03-21 17:38:22 +00:00
|
|
|
parser.add_option('--proto', dest="proto", type="string",
|
|
|
|
help="protocol to use, one of: accel, binary, compact")
|
2011-03-22 18:06:04 +00:00
|
|
|
parser.set_defaults(port=9090, verbose=1, proto='binary')
|
2011-03-21 17:38:22 +00:00
|
|
|
options, args = parser.parse_args()
|
|
|
|
|
2006-09-05 17:34:52 +00:00
|
|
|
class TestHandler:
|
|
|
|
|
|
|
|
def testVoid(self):
|
2011-03-22 18:06:04 +00:00
|
|
|
if options.verbose > 1:
|
2011-03-21 17:38:22 +00:00
|
|
|
print 'testVoid()'
|
2006-09-05 17:34:52 +00:00
|
|
|
|
|
|
|
def testString(self, str):
|
2011-03-22 18:06:04 +00:00
|
|
|
if options.verbose > 1:
|
2011-03-21 17:38:22 +00:00
|
|
|
print 'testString(%s)' % str
|
2006-09-05 17:34:52 +00:00
|
|
|
return str
|
|
|
|
|
|
|
|
def testByte(self, byte):
|
2011-03-22 18:06:04 +00:00
|
|
|
if options.verbose > 1:
|
2011-03-21 17:38:22 +00:00
|
|
|
print 'testByte(%d)' % byte
|
2006-09-05 17:34:52 +00:00
|
|
|
return byte
|
|
|
|
|
2006-09-06 02:42:25 +00:00
|
|
|
def testI16(self, i16):
|
2011-03-22 18:06:04 +00:00
|
|
|
if options.verbose > 1:
|
2011-03-21 17:38:22 +00:00
|
|
|
print 'testI16(%d)' % i16
|
2006-09-06 02:42:25 +00:00
|
|
|
return i16
|
|
|
|
|
|
|
|
def testI32(self, i32):
|
2011-03-22 18:06:04 +00:00
|
|
|
if options.verbose > 1:
|
2011-03-21 17:38:22 +00:00
|
|
|
print 'testI32(%d)' % i32
|
2006-09-06 02:42:25 +00:00
|
|
|
return i32
|
|
|
|
|
|
|
|
def testI64(self, i64):
|
2011-03-22 18:06:04 +00:00
|
|
|
if options.verbose > 1:
|
2011-03-21 17:38:22 +00:00
|
|
|
print 'testI64(%d)' % i64
|
2006-09-06 02:42:25 +00:00
|
|
|
return i64
|
|
|
|
|
|
|
|
def testDouble(self, dub):
|
2011-03-22 18:06:04 +00:00
|
|
|
if options.verbose > 1:
|
2011-03-21 17:38:22 +00:00
|
|
|
print 'testDouble(%f)' % dub
|
2006-09-06 02:42:25 +00:00
|
|
|
return dub
|
|
|
|
|
|
|
|
def testStruct(self, thing):
|
2011-03-22 18:06:04 +00:00
|
|
|
if options.verbose > 1:
|
2011-03-21 17:38:22 +00:00
|
|
|
print 'testStruct({%s, %d, %d, %d})' % (thing.string_thing, thing.byte_thing, thing.i32_thing, thing.i64_thing)
|
2006-09-06 02:42:25 +00:00
|
|
|
return thing
|
|
|
|
|
2006-09-05 17:34:52 +00:00
|
|
|
def testException(self, str):
|
2011-03-22 18:06:04 +00:00
|
|
|
if options.verbose > 1:
|
2011-03-21 17:38:22 +00:00
|
|
|
print 'testException(%s)' % str
|
2006-09-06 02:42:25 +00:00
|
|
|
if str == 'Xception':
|
|
|
|
x = Xception()
|
|
|
|
x.errorCode = 1001
|
|
|
|
x.message = str
|
|
|
|
raise x
|
2008-06-10 22:55:26 +00:00
|
|
|
elif str == "throw_undeclared":
|
|
|
|
raise ValueError("foo")
|
2006-09-05 17:34:52 +00:00
|
|
|
|
2009-03-24 20:01:58 +00:00
|
|
|
def testOneway(self, seconds):
|
2011-03-22 18:06:04 +00:00
|
|
|
if options.verbose > 1:
|
2011-03-21 17:38:22 +00:00
|
|
|
print 'testOneway(%d) => sleeping...' % seconds
|
|
|
|
time.sleep(seconds / 3) # be quick
|
2011-03-22 18:06:04 +00:00
|
|
|
if options.verbose > 1:
|
2011-03-21 17:38:22 +00:00
|
|
|
print 'done sleeping'
|
2008-02-18 02:11:48 +00:00
|
|
|
|
2008-11-07 23:09:31 +00:00
|
|
|
def testNest(self, thing):
|
2011-03-22 18:06:04 +00:00
|
|
|
if options.verbose > 1:
|
2011-03-21 17:38:22 +00:00
|
|
|
print 'testNest(%s)' % thing
|
2008-11-07 23:09:31 +00:00
|
|
|
return thing
|
|
|
|
|
|
|
|
def testMap(self, thing):
|
2011-03-22 18:06:04 +00:00
|
|
|
if options.verbose > 1:
|
2011-03-21 17:38:22 +00:00
|
|
|
print 'testMap(%s)' % thing
|
2008-11-07 23:09:31 +00:00
|
|
|
return thing
|
|
|
|
|
|
|
|
def testSet(self, thing):
|
2011-03-22 18:06:04 +00:00
|
|
|
if options.verbose > 1:
|
2011-03-21 17:38:22 +00:00
|
|
|
print 'testSet(%s)' % thing
|
2008-11-07 23:09:31 +00:00
|
|
|
return thing
|
|
|
|
|
|
|
|
def testList(self, thing):
|
2011-03-22 18:06:04 +00:00
|
|
|
if options.verbose > 1:
|
2011-03-21 17:38:22 +00:00
|
|
|
print 'testList(%s)' % thing
|
2008-11-07 23:09:31 +00:00
|
|
|
return thing
|
|
|
|
|
|
|
|
def testEnum(self, thing):
|
2011-03-22 18:06:04 +00:00
|
|
|
if options.verbose > 1:
|
2011-03-21 17:38:22 +00:00
|
|
|
print 'testEnum(%s)' % thing
|
2008-11-07 23:09:31 +00:00
|
|
|
return thing
|
|
|
|
|
|
|
|
def testTypedef(self, thing):
|
2011-03-22 18:06:04 +00:00
|
|
|
if options.verbose > 1:
|
2011-03-21 17:38:22 +00:00
|
|
|
print 'testTypedef(%s)' % thing
|
2008-11-07 23:09:31 +00:00
|
|
|
return thing
|
|
|
|
|
2011-03-21 17:38:22 +00:00
|
|
|
def testMapMap(self, thing):
|
2011-03-22 18:06:04 +00:00
|
|
|
if options.verbose > 1:
|
2011-03-21 17:38:22 +00:00
|
|
|
print 'testMapMap(%s)' % thing
|
|
|
|
return thing
|
|
|
|
|
|
|
|
def testMulti(self, arg0, arg1, arg2, arg3, arg4, arg5):
|
2011-03-22 18:06:04 +00:00
|
|
|
if options.verbose > 1:
|
2011-03-21 17:38:22 +00:00
|
|
|
print 'testMulti(%s)' % [arg0, arg1, arg2, arg3, arg4, arg5]
|
|
|
|
x = Xtruct(byte_thing=arg0, i32_thing=arg1, i64_thing=arg2)
|
|
|
|
return x
|
|
|
|
|
2011-03-22 18:06:04 +00:00
|
|
|
# set up the protocol factory form the --proto option
|
|
|
|
pfactory_cls = PROT_FACTORIES.get(options.proto, None)
|
|
|
|
if pfactory_cls is None:
|
2011-03-21 17:38:22 +00:00
|
|
|
raise AssertionError('Unknown --proto option: %s' % options.proto)
|
2011-03-22 18:06:04 +00:00
|
|
|
pfactory = pfactory_cls()
|
|
|
|
|
|
|
|
# get the server type (TSimpleServer, TNonblockingServer, etc...)
|
|
|
|
if len(args) > 1:
|
|
|
|
raise AssertionError('Only one server type may be specified, not multiple types.')
|
|
|
|
server_type = args[0]
|
|
|
|
|
|
|
|
# Set up the handler and processor objects
|
2006-09-05 17:34:52 +00:00
|
|
|
handler = TestHandler()
|
2006-10-26 04:56:18 +00:00
|
|
|
processor = ThriftTest.Processor(handler)
|
2008-06-10 22:55:26 +00:00
|
|
|
|
2011-03-22 18:06:04 +00:00
|
|
|
# Handle THttpServer as a special case
|
|
|
|
if server_type == 'THttpServer':
|
|
|
|
server =THttpServer.THttpServer(processor, ('', options.port), pfactory)
|
|
|
|
server.serve()
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
# set up server transport and transport factory
|
|
|
|
host = None
|
|
|
|
if options.ssl:
|
|
|
|
from thrift.transport import TSSLSocket
|
|
|
|
transport = TSSLSocket.TSSLServerSocket(host, options.port, certfile='test_cert.pem')
|
2008-11-07 23:09:31 +00:00
|
|
|
else:
|
2011-03-21 17:38:22 +00:00
|
|
|
transport = TSocket.TServerSocket(host, options.port)
|
2011-03-22 18:06:04 +00:00
|
|
|
tfactory = TTransport.TBufferedTransportFactory()
|
|
|
|
|
|
|
|
# if --zlib, then wrap server transport, and use a different transport factory
|
|
|
|
if options.zlib:
|
|
|
|
transport = TZlibTransport.TZlibTransport(transport) # wrap with zlib
|
|
|
|
tfactory = TZlibTransport.TZlibTransportFactory()
|
|
|
|
|
|
|
|
# do server-specific setup here:
|
|
|
|
if server_type == "TNonblockingServer":
|
|
|
|
server = TNonblockingServer.TNonblockingServer(processor, transport, inputProtocolFactory=pfactory)
|
|
|
|
elif server_type == "TProcessPoolServer":
|
|
|
|
import signal
|
|
|
|
from thrift.server import TProcessPoolServer
|
|
|
|
server = TProcessPoolServer.TProcessPoolServer(processor, transport, tfactory, pfactory)
|
|
|
|
server.setNumWorkers(5)
|
|
|
|
def set_alarm():
|
|
|
|
def clean_shutdown(signum, frame):
|
|
|
|
for worker in server.workers:
|
|
|
|
if options.verbose > 0:
|
2011-03-21 17:38:22 +00:00
|
|
|
print 'Terminating worker: %s' % worker
|
2011-03-22 18:06:04 +00:00
|
|
|
worker.terminate()
|
|
|
|
if options.verbose > 0:
|
2011-03-21 17:38:22 +00:00
|
|
|
print 'Requesting server to stop()'
|
2011-03-22 18:06:04 +00:00
|
|
|
server.stop()
|
|
|
|
signal.signal(signal.SIGALRM, clean_shutdown)
|
|
|
|
signal.alarm(2)
|
|
|
|
set_alarm()
|
|
|
|
else:
|
|
|
|
# look up server class dynamically to instantiate server
|
|
|
|
ServerClass = getattr(TServer, server_type)
|
|
|
|
server = ServerClass(processor, transport, tfactory, pfactory)
|
|
|
|
# enter server main loop
|
2006-09-20 01:56:10 +00:00
|
|
|
server.serve()
|