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 |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 22 | import glob |
| 23 | import logging |
| 24 | import os |
| 25 | import sys |
| 26 | import time |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 27 | from optparse import OptionParser |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 28 | |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 29 | # Print TServer log to stdout so that the test-runner can redirect it to log files |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 30 | logging.basicConfig(level=logging.DEBUG) |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 31 | |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 32 | parser = OptionParser() |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 33 | parser.add_option('--genpydir', type='string', dest='genpydir', |
| 34 | default='gen-py', |
| 35 | help='include this local directory in sys.path for locating generated code') |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 36 | parser.add_option("--port", type="int", dest="port", |
| 37 | help="port number for server to listen on") |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 38 | parser.add_option("--zlib", action="store_true", dest="zlib", |
| 39 | help="use zlib wrapper for compressed transport") |
| 40 | parser.add_option("--ssl", action="store_true", dest="ssl", |
| 41 | help="use SSL for encrypted transport") |
Roger Meier | 7615072 | 2014-05-31 22:22:07 +0200 | [diff] [blame] | 42 | parser.add_option('-v', '--verbose', action="store_const", |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 43 | dest="verbose", const=2, |
| 44 | help="verbose output") |
Roger Meier | 7615072 | 2014-05-31 22:22:07 +0200 | [diff] [blame] | 45 | parser.add_option('-q', '--quiet', action="store_const", |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 46 | dest="verbose", const=0, |
| 47 | help="minimal output") |
Roger Meier | 7615072 | 2014-05-31 22:22:07 +0200 | [diff] [blame] | 48 | parser.add_option('--protocol', dest="proto", type="string", |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 49 | help="protocol to use, one of: accel, binary, compact, json") |
Roger Meier | 7615072 | 2014-05-31 22:22:07 +0200 | [diff] [blame] | 50 | parser.add_option('--transport', dest="trans", type="string", |
| 51 | help="transport to use, one of: buffered, framed") |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 52 | parser.set_defaults(port=9090, verbose=1, proto='binary') |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 53 | options, args = parser.parse_args() |
| 54 | |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 55 | script_dir = os.path.realpath(os.path.dirname(__file__)) # <-- absolute dir the script is in |
| 56 | lib_dir = os.path.join(os.path.dirname(os.path.dirname(script_dir)), 'lib', 'py', 'build', 'lib.*') |
| 57 | |
cdwijayarathna | 0d4072b | 2014-08-09 21:32:21 +0530 | [diff] [blame] | 58 | sys.path.insert(0, os.path.join(script_dir, options.genpydir)) |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 59 | sys.path.insert(0, glob.glob(lib_dir)[0]) |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 60 | |
jfarrell | dbf2bb5 | 2014-07-09 23:37:12 -0400 | [diff] [blame] | 61 | from ThriftTest import ThriftTest |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 62 | from ThriftTest.ttypes import * |
Roger Meier | 1f554e1 | 2013-01-05 20:38:35 +0100 | [diff] [blame] | 63 | from thrift.Thrift import TException |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 64 | from thrift.transport import TTransport |
| 65 | from thrift.transport import TSocket |
| 66 | from thrift.transport import TZlibTransport |
| 67 | from thrift.protocol import TBinaryProtocol |
| 68 | from thrift.protocol import TCompactProtocol |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 69 | from thrift.protocol import TJSONProtocol |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 70 | from thrift.server import TServer, TNonblockingServer, THttpServer |
| 71 | |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 72 | PROT_FACTORIES = { |
| 73 | 'binary': TBinaryProtocol.TBinaryProtocolFactory, |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 74 | 'accel': TBinaryProtocol.TBinaryProtocolAcceleratedFactory, |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 75 | 'compact': TCompactProtocol.TCompactProtocolFactory, |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 76 | 'json': TJSONProtocol.TJSONProtocolFactory, |
| 77 | } |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 78 | |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 79 | |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 80 | class TestHandler(object): |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 81 | def testVoid(self): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 82 | if options.verbose > 1: |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 83 | logging.info('testVoid()') |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 84 | |
| 85 | def testString(self, str): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 86 | if options.verbose > 1: |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 87 | logging.info('testString(%s)' % str) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 88 | return str |
| 89 | |
Nobuaki Sukegawa | a649e74 | 2015-09-21 13:53:25 +0900 | [diff] [blame] | 90 | def testBool(self, boolean): |
| 91 | if options.verbose > 1: |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 92 | logging.info('testBool(%s)' % str(boolean).lower()) |
Nobuaki Sukegawa | a649e74 | 2015-09-21 13:53:25 +0900 | [diff] [blame] | 93 | return boolean |
| 94 | |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 95 | def testByte(self, byte): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 96 | if options.verbose > 1: |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 97 | logging.info('testByte(%d)' % byte) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 98 | return byte |
| 99 | |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 100 | def testI16(self, i16): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 101 | if options.verbose > 1: |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 102 | logging.info('testI16(%d)' % i16) |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 103 | return i16 |
| 104 | |
| 105 | def testI32(self, i32): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 106 | if options.verbose > 1: |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 107 | logging.info('testI32(%d)' % i32) |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 108 | return i32 |
| 109 | |
| 110 | def testI64(self, i64): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 111 | if options.verbose > 1: |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 112 | logging.info('testI64(%d)' % i64) |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 113 | return i64 |
| 114 | |
| 115 | def testDouble(self, dub): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 116 | if options.verbose > 1: |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 117 | logging.info('testDouble(%f)' % dub) |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 118 | return dub |
| 119 | |
Jens Geyer | 8bcfdd9 | 2014-12-14 03:14:26 +0100 | [diff] [blame] | 120 | def testBinary(self, thing): |
| 121 | if options.verbose > 1: |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 122 | logging.info('testBinary()') # TODO: hex output |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 123 | return thing |
| 124 | |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 125 | def testStruct(self, thing): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 126 | if options.verbose > 1: |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 127 | logging.info('testStruct({%s, %s, %s, %s})' % (thing.string_thing, thing.byte_thing, thing.i32_thing, thing.i64_thing)) |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 128 | return thing |
| 129 | |
Roger Meier | 1f554e1 | 2013-01-05 20:38:35 +0100 | [diff] [blame] | 130 | def testException(self, arg): |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 131 | # if options.verbose > 1: |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 132 | logging.info('testException(%s)' % arg) |
Roger Meier | 1f554e1 | 2013-01-05 20:38:35 +0100 | [diff] [blame] | 133 | if arg == 'Xception': |
| 134 | raise Xception(errorCode=1001, message=arg) |
| 135 | elif arg == 'TException': |
| 136 | raise TException(message='This is a TException') |
| 137 | |
| 138 | def testMultiException(self, arg0, arg1): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 139 | if options.verbose > 1: |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 140 | logging.info('testMultiException(%s, %s)' % (arg0, arg1)) |
Roger Meier | 1f554e1 | 2013-01-05 20:38:35 +0100 | [diff] [blame] | 141 | if arg0 == 'Xception': |
| 142 | raise Xception(errorCode=1001, message='This is an Xception') |
| 143 | elif arg0 == 'Xception2': |
| 144 | raise Xception2( |
| 145 | errorCode=2002, |
| 146 | struct_thing=Xtruct(string_thing='This is an Xception2')) |
| 147 | return Xtruct(string_thing=arg1) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 148 | |
David Reiss | 6ce401d | 2009-03-24 20:01:58 +0000 | [diff] [blame] | 149 | def testOneway(self, seconds): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 150 | if options.verbose > 1: |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 151 | logging.info('testOneway(%d) => sleeping...' % seconds) |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 152 | time.sleep(seconds / 3) # be quick |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 153 | if options.verbose > 1: |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 154 | logging.info('done sleeping') |
David Reiss | db893b6 | 2008-02-18 02:11:48 +0000 | [diff] [blame] | 155 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 156 | def testNest(self, thing): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 157 | if options.verbose > 1: |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 158 | logging.info('testNest(%s)' % thing) |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 159 | return thing |
| 160 | |
| 161 | def testMap(self, thing): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 162 | if options.verbose > 1: |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 163 | logging.info('testMap(%s)' % thing) |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 164 | return thing |
| 165 | |
| 166 | def testStringMap(self, thing): |
| 167 | if options.verbose > 1: |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 168 | logging.info('testStringMap(%s)' % thing) |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 169 | return thing |
| 170 | |
| 171 | def testSet(self, thing): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 172 | if options.verbose > 1: |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 173 | logging.info('testSet(%s)' % thing) |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 174 | return thing |
| 175 | |
| 176 | def testList(self, thing): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 177 | if options.verbose > 1: |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 178 | logging.info('testList(%s)' % thing) |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 179 | return thing |
| 180 | |
| 181 | def testEnum(self, thing): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 182 | if options.verbose > 1: |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 183 | logging.info('testEnum(%s)' % thing) |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 184 | return thing |
| 185 | |
| 186 | def testTypedef(self, thing): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 187 | if options.verbose > 1: |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 188 | logging.info('testTypedef(%s)' % thing) |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 189 | return thing |
| 190 | |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 191 | def testMapMap(self, thing): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 192 | if options.verbose > 1: |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 193 | logging.info('testMapMap(%s)' % thing) |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 194 | return { |
| 195 | -4: { |
| 196 | -4: -4, |
| 197 | -3: -3, |
| 198 | -2: -2, |
| 199 | -1: -1, |
| 200 | }, |
| 201 | 4: { |
| 202 | 4: 4, |
| 203 | 3: 3, |
| 204 | 2: 2, |
| 205 | 1: 1, |
| 206 | }, |
| 207 | } |
Roger Meier | 1f554e1 | 2013-01-05 20:38:35 +0100 | [diff] [blame] | 208 | |
| 209 | def testInsanity(self, argument): |
| 210 | if options.verbose > 1: |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 211 | logging.info('testInsanity(%s)' % argument) |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 212 | return { |
| 213 | 1: { |
| 214 | 2: argument, |
| 215 | 3: argument, |
| 216 | }, |
| 217 | 2: {6: Insanity()}, |
| 218 | } |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 219 | |
| 220 | def testMulti(self, arg0, arg1, arg2, arg3, arg4, arg5): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 221 | if options.verbose > 1: |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 222 | logging.info('testMulti(%s)' % [arg0, arg1, arg2, arg3, arg4, arg5]) |
Roger Meier | 1f554e1 | 2013-01-05 20:38:35 +0100 | [diff] [blame] | 223 | return Xtruct(string_thing='Hello2', |
| 224 | byte_thing=arg0, i32_thing=arg1, i64_thing=arg2) |
| 225 | |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 226 | |
Roger Meier | 7615072 | 2014-05-31 22:22:07 +0200 | [diff] [blame] | 227 | # set up the protocol factory form the --protocol option |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 228 | pfactory_cls = PROT_FACTORIES.get(options.proto, None) |
| 229 | if pfactory_cls is None: |
Roger Meier | 7615072 | 2014-05-31 22:22:07 +0200 | [diff] [blame] | 230 | raise AssertionError('Unknown --protocol option: %s' % options.proto) |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 231 | pfactory = pfactory_cls() |
| 232 | |
| 233 | # get the server type (TSimpleServer, TNonblockingServer, etc...) |
| 234 | if len(args) > 1: |
| 235 | raise AssertionError('Only one server type may be specified, not multiple types.') |
| 236 | server_type = args[0] |
| 237 | |
| 238 | # Set up the handler and processor objects |
jfarrell | dbf2bb5 | 2014-07-09 23:37:12 -0400 | [diff] [blame] | 239 | handler = TestHandler() |
| 240 | processor = ThriftTest.Processor(handler) |
David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 241 | |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 242 | # Handle THttpServer as a special case |
| 243 | if server_type == 'THttpServer': |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 244 | server = THttpServer.THttpServer(processor, ('', options.port), pfactory) |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 245 | server.serve() |
| 246 | sys.exit(0) |
| 247 | |
| 248 | # set up server transport and transport factory |
Roger Meier | 7615072 | 2014-05-31 22:22:07 +0200 | [diff] [blame] | 249 | |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 250 | abs_key_path = os.path.join(os.path.dirname(script_dir), 'keys', 'server.pem') |
Roger Meier | 7615072 | 2014-05-31 22:22:07 +0200 | [diff] [blame] | 251 | |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 252 | host = None |
| 253 | if options.ssl: |
| 254 | from thrift.transport import TSSLSocket |
Roger Meier | 7615072 | 2014-05-31 22:22:07 +0200 | [diff] [blame] | 255 | transport = TSSLSocket.TSSLServerSocket(host, options.port, certfile=abs_key_path) |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 256 | else: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 257 | transport = TSocket.TServerSocket(host, options.port) |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 258 | tfactory = TTransport.TBufferedTransportFactory() |
Roger Meier | 7615072 | 2014-05-31 22:22:07 +0200 | [diff] [blame] | 259 | if options.trans == 'buffered': |
| 260 | tfactory = TTransport.TBufferedTransportFactory() |
| 261 | elif options.trans == 'framed': |
| 262 | tfactory = TTransport.TFramedTransportFactory() |
| 263 | elif options.trans == '': |
| 264 | raise AssertionError('Unknown --transport option: %s' % options.trans) |
| 265 | else: |
| 266 | tfactory = TTransport.TBufferedTransportFactory() |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 267 | # if --zlib, then wrap server transport, and use a different transport factory |
| 268 | if options.zlib: |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 269 | transport = TZlibTransport.TZlibTransport(transport) # wrap with zlib |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 270 | tfactory = TZlibTransport.TZlibTransportFactory() |
| 271 | |
| 272 | # do server-specific setup here: |
| 273 | if server_type == "TNonblockingServer": |
| 274 | server = TNonblockingServer.TNonblockingServer(processor, transport, inputProtocolFactory=pfactory) |
| 275 | elif server_type == "TProcessPoolServer": |
| 276 | import signal |
| 277 | from thrift.server import TProcessPoolServer |
| 278 | server = TProcessPoolServer.TProcessPoolServer(processor, transport, tfactory, pfactory) |
| 279 | server.setNumWorkers(5) |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 280 | |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 281 | def set_alarm(): |
| 282 | def clean_shutdown(signum, frame): |
| 283 | for worker in server.workers: |
| 284 | if options.verbose > 0: |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 285 | logging.info('Terminating worker: %s' % worker) |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 286 | worker.terminate() |
| 287 | if options.verbose > 0: |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 288 | logging.info('Requesting server to stop()') |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 289 | try: |
| 290 | server.stop() |
| 291 | except: |
| 292 | pass |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 293 | signal.signal(signal.SIGALRM, clean_shutdown) |
Jens Geyer | f7d327a | 2015-07-25 15:44:20 +0200 | [diff] [blame] | 294 | signal.alarm(4) |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 295 | set_alarm() |
| 296 | else: |
| 297 | # look up server class dynamically to instantiate server |
| 298 | ServerClass = getattr(TServer, server_type) |
| 299 | server = ServerClass(processor, transport, tfactory, pfactory) |
| 300 | # enter server main loop |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 301 | server.serve() |