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
|
|
|
|
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-21 17:38:22 +00:00
|
|
|
parser = OptionParser()
|
|
|
|
parser.set_defaults(port=9090, verbose=1, proto='binary')
|
|
|
|
parser.add_option("--port", type="int", dest="port",
|
|
|
|
help="port number for server to listen on")
|
|
|
|
parser.add_option('-v', '--verbose', action="store_const",
|
|
|
|
dest="verbose", const=2,
|
|
|
|
help="verbose output")
|
|
|
|
parser.add_option('--proto', dest="proto", type="string",
|
|
|
|
help="protocol to use, one of: accel, binary, compact")
|
|
|
|
options, args = parser.parse_args()
|
|
|
|
|
2006-09-05 17:34:52 +00:00
|
|
|
class TestHandler:
|
|
|
|
|
|
|
|
def testVoid(self):
|
2011-03-21 17:38:22 +00:00
|
|
|
if options.verbose:
|
|
|
|
print 'testVoid()'
|
2006-09-05 17:34:52 +00:00
|
|
|
|
|
|
|
def testString(self, str):
|
2011-03-21 17:38:22 +00:00
|
|
|
if options.verbose:
|
|
|
|
print 'testString(%s)' % str
|
2006-09-05 17:34:52 +00:00
|
|
|
return str
|
|
|
|
|
|
|
|
def testByte(self, byte):
|
2011-03-21 17:38:22 +00:00
|
|
|
if options.verbose:
|
|
|
|
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-21 17:38:22 +00:00
|
|
|
if options.verbose:
|
|
|
|
print 'testI16(%d)' % i16
|
2006-09-06 02:42:25 +00:00
|
|
|
return i16
|
|
|
|
|
|
|
|
def testI32(self, i32):
|
2011-03-21 17:38:22 +00:00
|
|
|
if options.verbose:
|
|
|
|
print 'testI32(%d)' % i32
|
2006-09-06 02:42:25 +00:00
|
|
|
return i32
|
|
|
|
|
|
|
|
def testI64(self, i64):
|
2011-03-21 17:38:22 +00:00
|
|
|
if options.verbose:
|
|
|
|
print 'testI64(%d)' % i64
|
2006-09-06 02:42:25 +00:00
|
|
|
return i64
|
|
|
|
|
|
|
|
def testDouble(self, dub):
|
2011-03-21 17:38:22 +00:00
|
|
|
if options.verbose:
|
|
|
|
print 'testDouble(%f)' % dub
|
2006-09-06 02:42:25 +00:00
|
|
|
return dub
|
|
|
|
|
|
|
|
def testStruct(self, thing):
|
2011-03-21 17:38:22 +00:00
|
|
|
if options.verbose:
|
|
|
|
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-21 17:38:22 +00:00
|
|
|
if options.verbose:
|
|
|
|
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-21 17:38:22 +00:00
|
|
|
if options.verbose:
|
|
|
|
print 'testOneway(%d) => sleeping...' % seconds
|
|
|
|
time.sleep(seconds / 3) # be quick
|
|
|
|
if options.verbose:
|
|
|
|
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-21 17:38:22 +00:00
|
|
|
if options.verbose:
|
|
|
|
print 'testNest(%s)' % thing
|
2008-11-07 23:09:31 +00:00
|
|
|
return thing
|
|
|
|
|
|
|
|
def testMap(self, thing):
|
2011-03-21 17:38:22 +00:00
|
|
|
if options.verbose:
|
|
|
|
print 'testMap(%s)' % thing
|
2008-11-07 23:09:31 +00:00
|
|
|
return thing
|
|
|
|
|
|
|
|
def testSet(self, thing):
|
2011-03-21 17:38:22 +00:00
|
|
|
if options.verbose:
|
|
|
|
print 'testSet(%s)' % thing
|
2008-11-07 23:09:31 +00:00
|
|
|
return thing
|
|
|
|
|
|
|
|
def testList(self, thing):
|
2011-03-21 17:38:22 +00:00
|
|
|
if options.verbose:
|
|
|
|
print 'testList(%s)' % thing
|
2008-11-07 23:09:31 +00:00
|
|
|
return thing
|
|
|
|
|
|
|
|
def testEnum(self, thing):
|
2011-03-21 17:38:22 +00:00
|
|
|
if options.verbose:
|
|
|
|
print 'testEnum(%s)' % thing
|
2008-11-07 23:09:31 +00:00
|
|
|
return thing
|
|
|
|
|
|
|
|
def testTypedef(self, thing):
|
2011-03-21 17:38:22 +00:00
|
|
|
if options.verbose:
|
|
|
|
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):
|
|
|
|
if options.verbose:
|
|
|
|
print 'testMapMap(%s)' % thing
|
|
|
|
return thing
|
|
|
|
|
|
|
|
def testMulti(self, arg0, arg1, arg2, arg3, arg4, arg5):
|
|
|
|
if options.verbose:
|
|
|
|
print 'testMulti(%s)' % [arg0, arg1, arg2, arg3, arg4, arg5]
|
|
|
|
x = Xtruct(byte_thing=arg0, i32_thing=arg1, i64_thing=arg2)
|
|
|
|
return x
|
|
|
|
|
|
|
|
if options.proto == 'binary':
|
|
|
|
pfactory = TBinaryProtocol.TBinaryProtocolFactory()
|
|
|
|
elif options.proto == 'accel':
|
|
|
|
pfactory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory()
|
|
|
|
elif options.proto == 'compact':
|
|
|
|
pfactory = TCompactProtocol.TCompactProtocolFactory()
|
|
|
|
else:
|
|
|
|
raise AssertionError('Unknown --proto option: %s' % options.proto)
|
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-21 17:38:22 +00:00
|
|
|
if args[0] == "THttpServer":
|
|
|
|
server = THttpServer.THttpServer(processor, ('', options.port), pfactory)
|
2008-11-07 23:09:31 +00:00
|
|
|
else:
|
2011-02-08 20:44:22 +00:00
|
|
|
host = None
|
2011-03-21 17:38:22 +00:00
|
|
|
transport = TSocket.TServerSocket(host, options.port)
|
2009-01-31 21:59:32 +00:00
|
|
|
tfactory = TTransport.TBufferedTransportFactory()
|
|
|
|
|
2011-03-21 17:38:22 +00:00
|
|
|
if args[0] == "TNonblockingServer":
|
|
|
|
server = TNonblockingServer.TNonblockingServer(processor, transport, inputProtocolFactory=pfactory)
|
|
|
|
elif args[0] == "TProcessPoolServer":
|
|
|
|
import signal
|
|
|
|
def set_alarm():
|
|
|
|
def clean_shutdown(signum, frame):
|
|
|
|
for worker in server.workers:
|
|
|
|
print 'Terminating worker: %s' % worker
|
|
|
|
worker.terminate()
|
|
|
|
print 'Requesting server to stop()'
|
|
|
|
server.stop()
|
|
|
|
signal.signal(signal.SIGALRM, clean_shutdown)
|
|
|
|
signal.alarm(2)
|
|
|
|
from thrift.server import TProcessPoolServer
|
|
|
|
server = TProcessPoolServer.TProcessPoolServer(processor, transport, tfactory, pfactory)
|
|
|
|
server.setNumWorkers(5)
|
|
|
|
set_alarm()
|
2009-01-31 21:59:32 +00:00
|
|
|
else:
|
2011-03-21 17:38:22 +00:00
|
|
|
ServerClass = getattr(TServer, args[0])
|
2009-01-31 21:59:32 +00:00
|
|
|
server = ServerClass(processor, transport, tfactory, pfactory)
|
|
|
|
|
2006-09-20 01:56:10 +00:00
|
|
|
server.serve()
|