Mark Slee | 57cc25e | 2007-02-28 21:43:54 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 2 | |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 3 | # |
| 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 Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 21 | from __future__ import division |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 22 | import sys, glob, time, os |
| 23 | sys.path.insert(0, glob.glob(os.path.join(os.path.dirname(__file__),'../../lib/py/build/lib.*'))[0]) |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 24 | from optparse import OptionParser |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 25 | |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 26 | parser = OptionParser() |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 27 | parser.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 Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 30 | parser.add_option("--port", type="int", dest="port", |
| 31 | help="port number for server to listen on") |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 32 | parser.add_option("--zlib", action="store_true", dest="zlib", |
| 33 | help="use zlib wrapper for compressed transport") |
| 34 | parser.add_option("--ssl", action="store_true", dest="ssl", |
| 35 | help="use SSL for encrypted transport") |
Roger Meier | 879cab2 | 2014-05-03 17:51:21 +0200 | [diff] [blame^] | 36 | parser.add_option("--multiple", action="store_true", dest="multiple", |
| 37 | help="use multiple service") |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 38 | parser.add_option('-v', '--verbose', action="store_const", |
| 39 | dest="verbose", const=2, |
| 40 | help="verbose output") |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 41 | parser.add_option('-q', '--quiet', action="store_const", |
| 42 | dest="verbose", const=0, |
| 43 | help="minimal output") |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 44 | parser.add_option('--proto', dest="proto", type="string", |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 45 | help="protocol to use, one of: accel, binary, compact, json") |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 46 | parser.set_defaults(port=9090, verbose=1, proto='binary') |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 47 | options, args = parser.parse_args() |
| 48 | |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 49 | sys.path.insert(0, options.genpydir) |
| 50 | |
Roger Meier | 879cab2 | 2014-05-03 17:51:21 +0200 | [diff] [blame^] | 51 | from ThriftTest import ThriftTest, SecondService |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 52 | from ThriftTest.ttypes import * |
Roger Meier | 1f554e1 | 2013-01-05 20:38:35 +0100 | [diff] [blame] | 53 | from thrift.Thrift import TException |
Roger Meier | 879cab2 | 2014-05-03 17:51:21 +0200 | [diff] [blame^] | 54 | from thrift import TMultiplexedProcessor |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 55 | from thrift.transport import TTransport |
| 56 | from thrift.transport import TSocket |
| 57 | from thrift.transport import TZlibTransport |
| 58 | from thrift.protocol import TBinaryProtocol |
| 59 | from thrift.protocol import TCompactProtocol |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 60 | from thrift.protocol import TJSONProtocol |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 61 | from thrift.server import TServer, TNonblockingServer, THttpServer |
| 62 | |
| 63 | PROT_FACTORIES = {'binary': TBinaryProtocol.TBinaryProtocolFactory, |
| 64 | 'accel': TBinaryProtocol.TBinaryProtocolAcceleratedFactory, |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 65 | 'compact': TCompactProtocol.TCompactProtocolFactory, |
| 66 | 'json': TJSONProtocol.TJSONProtocolFactory} |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 67 | |
Roger Meier | 879cab2 | 2014-05-03 17:51:21 +0200 | [diff] [blame^] | 68 | class SecondHandler: |
| 69 | |
| 70 | def blahBlah(self): |
| 71 | if options.verbose > 1: |
| 72 | print 'blahBlah()' |
| 73 | |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 74 | class TestHandler: |
| 75 | |
| 76 | def testVoid(self): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 77 | if options.verbose > 1: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 78 | print 'testVoid()' |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 79 | |
| 80 | def testString(self, str): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 81 | if options.verbose > 1: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 82 | print 'testString(%s)' % str |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 83 | return str |
| 84 | |
| 85 | def testByte(self, byte): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 86 | if options.verbose > 1: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 87 | print 'testByte(%d)' % byte |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 88 | return byte |
| 89 | |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 90 | def testI16(self, i16): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 91 | if options.verbose > 1: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 92 | print 'testI16(%d)' % i16 |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 93 | return i16 |
| 94 | |
| 95 | def testI32(self, i32): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 96 | if options.verbose > 1: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 97 | print 'testI32(%d)' % i32 |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 98 | return i32 |
| 99 | |
| 100 | def testI64(self, i64): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 101 | if options.verbose > 1: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 102 | print 'testI64(%d)' % i64 |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 103 | return i64 |
| 104 | |
| 105 | def testDouble(self, dub): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 106 | if options.verbose > 1: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 107 | print 'testDouble(%f)' % dub |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 108 | return dub |
| 109 | |
| 110 | def testStruct(self, thing): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 111 | if options.verbose > 1: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 112 | print 'testStruct({%s, %d, %d, %d})' % (thing.string_thing, thing.byte_thing, thing.i32_thing, thing.i64_thing) |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 113 | return thing |
| 114 | |
Roger Meier | 1f554e1 | 2013-01-05 20:38:35 +0100 | [diff] [blame] | 115 | 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 Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 124 | if options.verbose > 1: |
Roger Meier | 1f554e1 | 2013-01-05 20:38:35 +0100 | [diff] [blame] | 125 | 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 Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 133 | |
David Reiss | 6ce401d | 2009-03-24 20:01:58 +0000 | [diff] [blame] | 134 | def testOneway(self, seconds): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 135 | if options.verbose > 1: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 136 | print 'testOneway(%d) => sleeping...' % seconds |
| 137 | time.sleep(seconds / 3) # be quick |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 138 | if options.verbose > 1: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 139 | print 'done sleeping' |
David Reiss | db893b6 | 2008-02-18 02:11:48 +0000 | [diff] [blame] | 140 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 141 | def testNest(self, thing): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 142 | if options.verbose > 1: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 143 | print 'testNest(%s)' % thing |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 144 | return thing |
| 145 | |
| 146 | def testMap(self, thing): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 147 | if options.verbose > 1: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 148 | print 'testMap(%s)' % thing |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 149 | return thing |
| 150 | |
| 151 | def testSet(self, thing): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 152 | if options.verbose > 1: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 153 | print 'testSet(%s)' % thing |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 154 | return thing |
| 155 | |
| 156 | def testList(self, thing): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 157 | if options.verbose > 1: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 158 | print 'testList(%s)' % thing |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 159 | return thing |
| 160 | |
| 161 | def testEnum(self, thing): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 162 | if options.verbose > 1: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 163 | print 'testEnum(%s)' % thing |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 164 | return thing |
| 165 | |
| 166 | def testTypedef(self, thing): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 167 | if options.verbose > 1: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 168 | print 'testTypedef(%s)' % thing |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 169 | return thing |
| 170 | |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 171 | def testMapMap(self, thing): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 172 | if options.verbose > 1: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 173 | print 'testMapMap(%s)' % thing |
Roger Meier | 1f554e1 | 2013-01-05 20:38:35 +0100 | [diff] [blame] | 174 | 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 Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 180 | |
| 181 | def testMulti(self, arg0, arg1, arg2, arg3, arg4, arg5): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 182 | if options.verbose > 1: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 183 | print 'testMulti(%s)' % [arg0, arg1, arg2, arg3, arg4, arg5] |
Roger Meier | 1f554e1 | 2013-01-05 20:38:35 +0100 | [diff] [blame] | 184 | return Xtruct(string_thing='Hello2', |
| 185 | byte_thing=arg0, i32_thing=arg1, i64_thing=arg2) |
| 186 | |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 187 | |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 188 | # set up the protocol factory form the --proto option |
| 189 | pfactory_cls = PROT_FACTORIES.get(options.proto, None) |
| 190 | if pfactory_cls is None: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 191 | raise AssertionError('Unknown --proto option: %s' % options.proto) |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 192 | pfactory = pfactory_cls() |
| 193 | |
| 194 | # get the server type (TSimpleServer, TNonblockingServer, etc...) |
| 195 | if len(args) > 1: |
| 196 | raise AssertionError('Only one server type may be specified, not multiple types.') |
| 197 | server_type = args[0] |
| 198 | |
| 199 | # Set up the handler and processor objects |
Roger Meier | 879cab2 | 2014-05-03 17:51:21 +0200 | [diff] [blame^] | 200 | if not options.multiple: |
| 201 | handler = TestHandler() |
| 202 | processor = ThriftTest.Processor(handler) |
| 203 | else: |
| 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 Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 209 | |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 210 | # Handle THttpServer as a special case |
| 211 | if 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 |
| 217 | host = None |
| 218 | if options.ssl: |
| 219 | from thrift.transport import TSSLSocket |
Roger Meier | 58bbb70 | 2014-02-19 19:59:25 +0100 | [diff] [blame] | 220 | transport = TSSLSocket.TSSLServerSocket(host, options.port, certfile='../keys/server.pem') |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 221 | else: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 222 | transport = TSocket.TServerSocket(host, options.port) |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 223 | tfactory = TTransport.TBufferedTransportFactory() |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 224 | |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 225 | # if --zlib, then wrap server transport, and use a different transport factory |
| 226 | if options.zlib: |
| 227 | transport = TZlibTransport.TZlibTransport(transport) # wrap with zlib |
| 228 | tfactory = TZlibTransport.TZlibTransportFactory() |
| 229 | |
| 230 | # do server-specific setup here: |
| 231 | if server_type == "TNonblockingServer": |
| 232 | server = TNonblockingServer.TNonblockingServer(processor, transport, inputProtocolFactory=pfactory) |
| 233 | elif 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 Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 242 | print 'Terminating worker: %s' % worker |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 243 | worker.terminate() |
| 244 | if options.verbose > 0: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 245 | print 'Requesting server to stop()' |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 246 | try: |
| 247 | server.stop() |
| 248 | except: |
| 249 | pass |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 250 | signal.signal(signal.SIGALRM, clean_shutdown) |
| 251 | signal.alarm(2) |
| 252 | set_alarm() |
| 253 | else: |
| 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 Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 258 | server.serve() |