Jake Farrell | b95b0ff | 2012-03-22 21:49:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | module thrift_test_server; |
| 20 | |
| 21 | import core.thread : dur, Thread; |
| 22 | import std.algorithm; |
| 23 | import std.exception : enforce; |
| 24 | import std.getopt; |
| 25 | import std.parallelism : totalCPUs; |
| 26 | import std.string; |
| 27 | import std.stdio; |
| 28 | import std.typetuple : TypeTuple, staticMap; |
| 29 | import thrift.base; |
| 30 | import thrift.codegen.processor; |
| 31 | import thrift.protocol.base; |
| 32 | import thrift.protocol.binary; |
| 33 | import thrift.protocol.compact; |
| 34 | import thrift.protocol.json; |
| 35 | import thrift.server.base; |
| 36 | import thrift.server.transport.socket; |
| 37 | import thrift.server.transport.ssl; |
| 38 | import thrift.transport.base; |
| 39 | import thrift.transport.buffered; |
| 40 | import thrift.transport.framed; |
| 41 | import thrift.transport.http; |
| 42 | import thrift.transport.ssl; |
| 43 | import thrift.util.hashset; |
| 44 | import test_utils; |
| 45 | |
| 46 | import thrift_test_common; |
| 47 | import thrift.test.ThriftTest_types; |
| 48 | import thrift.test.ThriftTest; |
| 49 | |
| 50 | class TestHandler : ThriftTest { |
| 51 | this(bool trace) { |
| 52 | trace_ = trace; |
| 53 | } |
| 54 | |
| 55 | override void testVoid() { |
| 56 | if (trace_) writeln("testVoid()"); |
| 57 | } |
| 58 | |
| 59 | override string testString(string thing) { |
| 60 | if (trace_) writefln("testString(\"%s\")", thing); |
| 61 | return thing; |
| 62 | } |
| 63 | |
| 64 | override byte testByte(byte thing) { |
| 65 | if (trace_) writefln("testByte(%s)", thing); |
| 66 | return thing; |
| 67 | } |
| 68 | |
| 69 | override int testI32(int thing) { |
| 70 | if (trace_) writefln("testI32(%s)", thing); |
| 71 | return thing; |
| 72 | } |
| 73 | |
| 74 | override long testI64(long thing) { |
| 75 | if (trace_) writefln("testI64(%s)", thing); |
| 76 | return thing; |
| 77 | } |
| 78 | |
| 79 | override double testDouble(double thing) { |
| 80 | if (trace_) writefln("testDouble(%s)", thing); |
| 81 | return thing; |
| 82 | } |
| 83 | |
Jens Geyer | 8bcfdd9 | 2014-12-14 03:14:26 +0100 | [diff] [blame] | 84 | override string testBinary(string thing) { |
| 85 | if (trace_) writefln("testBinary(\"%s\")", thing); |
| 86 | return thing; |
| 87 | } |
| 88 | |
Jens Geyer | 855cf7f | 2015-10-08 21:12:57 +0200 | [diff] [blame^] | 89 | override bool testBool(bool thing) { |
| 90 | if (trace_) writefln("testBool(\"%s\")", thing); |
| 91 | return thing; |
| 92 | } |
| 93 | |
Jake Farrell | b95b0ff | 2012-03-22 21:49:10 +0000 | [diff] [blame] | 94 | override Xtruct testStruct(ref const(Xtruct) thing) { |
| 95 | if (trace_) writefln("testStruct({\"%s\", %s, %s, %s})", |
| 96 | thing.string_thing, thing.byte_thing, thing.i32_thing, thing.i64_thing); |
| 97 | return thing; |
| 98 | } |
| 99 | |
| 100 | override Xtruct2 testNest(ref const(Xtruct2) nest) { |
| 101 | auto thing = nest.struct_thing; |
| 102 | if (trace_) writefln("testNest({%s, {\"%s\", %s, %s, %s}, %s})", |
| 103 | nest.byte_thing, thing.string_thing, thing.byte_thing, thing.i32_thing, |
| 104 | thing.i64_thing, nest.i32_thing); |
| 105 | return nest; |
| 106 | } |
| 107 | |
| 108 | override int[int] testMap(int[int] thing) { |
| 109 | if (trace_) writefln("testMap({%s})", thing); |
| 110 | return thing; |
| 111 | } |
| 112 | |
| 113 | override HashSet!int testSet(HashSet!int thing) { |
| 114 | if (trace_) writefln("testSet({%s})", |
| 115 | join(map!`to!string(a)`(thing[]), ", ")); |
| 116 | return thing; |
| 117 | } |
| 118 | |
| 119 | override int[] testList(int[] thing) { |
| 120 | if (trace_) writefln("testList(%s)", thing); |
| 121 | return thing; |
| 122 | } |
| 123 | |
| 124 | override Numberz testEnum(Numberz thing) { |
| 125 | if (trace_) writefln("testEnum(%s)", thing); |
| 126 | return thing; |
| 127 | } |
| 128 | |
| 129 | override UserId testTypedef(UserId thing) { |
| 130 | if (trace_) writefln("testTypedef(%s)", thing); |
| 131 | return thing; |
| 132 | } |
| 133 | |
| 134 | override string[string] testStringMap(string[string] thing) { |
| 135 | if (trace_) writefln("testStringMap(%s)", thing); |
| 136 | return thing; |
| 137 | } |
| 138 | |
| 139 | override int[int][int] testMapMap(int hello) { |
| 140 | if (trace_) writefln("testMapMap(%s)", hello); |
| 141 | return testMapMapReturn; |
| 142 | } |
| 143 | |
| 144 | override Insanity[Numberz][UserId] testInsanity(ref const(Insanity) argument) { |
| 145 | if (trace_) writeln("testInsanity()"); |
| 146 | return testInsanityReturn; |
| 147 | } |
| 148 | |
| 149 | override Xtruct testMulti(byte arg0, int arg1, long arg2, string[short] arg3, |
| 150 | Numberz arg4, UserId arg5) |
| 151 | { |
| 152 | if (trace_) writeln("testMulti()"); |
| 153 | return Xtruct("Hello2", arg0, arg1, arg2); |
| 154 | } |
| 155 | |
| 156 | override void testException(string arg) { |
| 157 | if (trace_) writefln("testException(%s)", arg); |
| 158 | if (arg == "Xception") { |
| 159 | auto e = new Xception(); |
| 160 | e.errorCode = 1001; |
| 161 | e.message = arg; |
| 162 | throw e; |
| 163 | } else if (arg == "ApplicationException") { |
| 164 | throw new TException(); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | override Xtruct testMultiException(string arg0, string arg1) { |
| 169 | if (trace_) writefln("testMultiException(%s, %s)", arg0, arg1); |
| 170 | |
| 171 | if (arg0 == "Xception") { |
| 172 | auto e = new Xception(); |
| 173 | e.errorCode = 1001; |
| 174 | e.message = "This is an Xception"; |
| 175 | throw e; |
| 176 | } else if (arg0 == "Xception2") { |
| 177 | auto e = new Xception2(); |
| 178 | e.errorCode = 2002; |
| 179 | e.struct_thing.string_thing = "This is an Xception2"; |
| 180 | throw e; |
| 181 | } else { |
| 182 | return Xtruct(arg1); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | override void testOneway(int sleepFor) { |
| 187 | if (trace_) writefln("testOneway(%s): Sleeping...", sleepFor); |
| 188 | Thread.sleep(dur!"seconds"(sleepFor)); |
| 189 | if (trace_) writefln("testOneway(%s): done sleeping!", sleepFor); |
| 190 | } |
| 191 | |
| 192 | private: |
| 193 | bool trace_; |
| 194 | } |
| 195 | |
| 196 | void main(string[] args) { |
| 197 | ushort port = 9090; |
| 198 | ServerType serverType; |
| 199 | ProtocolType protocolType; |
| 200 | size_t numIOThreads = 1; |
| 201 | TransportType transportType; |
| 202 | bool ssl; |
| 203 | bool trace; |
| 204 | size_t taskPoolSize = totalCPUs; |
| 205 | |
| 206 | getopt(args, "port", &port, "protocol", &protocolType, "server-type", |
| 207 | &serverType, "ssl", &ssl, "num-io-threads", &numIOThreads, |
| 208 | "task-pool-size", &taskPoolSize, "trace", &trace, |
| 209 | "transport", &transportType); |
| 210 | |
| 211 | if (serverType == ServerType.nonblocking || |
| 212 | serverType == ServerType.pooledNonblocking |
| 213 | ) { |
| 214 | enforce(transportType == TransportType.framed, |
| 215 | "Need to use framed transport with non-blocking server."); |
| 216 | enforce(!ssl, "The non-blocking server does not support SSL yet."); |
| 217 | |
| 218 | // Don't wrap the contents into another layer of framing. |
| 219 | transportType = TransportType.raw; |
| 220 | } |
| 221 | |
| 222 | version (ThriftTestTemplates) { |
| 223 | // Only exercise the specialized template code paths if explicitly enabled |
| 224 | // to reduce memory consumption on regular test suite runs – there should |
| 225 | // not be much that can go wrong with that specifically anyway. |
| 226 | alias TypeTuple!(TBufferedTransport, TFramedTransport, TServerHttpTransport) |
| 227 | AvailableTransports; |
| 228 | alias TypeTuple!( |
| 229 | staticMap!(TBinaryProtocol, AvailableTransports), |
| 230 | staticMap!(TCompactProtocol, AvailableTransports) |
| 231 | ) AvailableProtocols; |
| 232 | } else { |
| 233 | alias TypeTuple!() AvailableTransports; |
| 234 | alias TypeTuple!() AvailableProtocols; |
| 235 | } |
| 236 | |
| 237 | TProtocolFactory protocolFactory; |
| 238 | final switch (protocolType) { |
| 239 | case ProtocolType.binary: |
| 240 | protocolFactory = new TBinaryProtocolFactory!AvailableTransports; |
| 241 | break; |
| 242 | case ProtocolType.compact: |
| 243 | protocolFactory = new TCompactProtocolFactory!AvailableTransports; |
| 244 | break; |
| 245 | case ProtocolType.json: |
| 246 | protocolFactory = new TJsonProtocolFactory!AvailableTransports; |
| 247 | break; |
| 248 | } |
| 249 | |
| 250 | auto processor = new TServiceProcessor!(ThriftTest, AvailableProtocols)( |
| 251 | new TestHandler(trace)); |
| 252 | |
| 253 | TServerSocket serverSocket; |
| 254 | if (ssl) { |
| 255 | auto sslContext = new TSSLContext(); |
| 256 | sslContext.serverSide = true; |
| 257 | sslContext.loadCertificate("./server-certificate.pem"); |
| 258 | sslContext.loadPrivateKey("./server-private-key.pem"); |
| 259 | sslContext.ciphers = "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH"; |
| 260 | serverSocket = new TSSLServerSocket(port, sslContext); |
| 261 | } else { |
| 262 | serverSocket = new TServerSocket(port); |
| 263 | } |
| 264 | |
| 265 | auto transportFactory = createTransportFactory(transportType); |
| 266 | |
| 267 | auto server = createServer(serverType, numIOThreads, taskPoolSize, |
| 268 | processor, serverSocket, transportFactory, protocolFactory); |
| 269 | |
| 270 | writefln("Starting %s/%s %s ThriftTest server %son port %s...", protocolType, |
| 271 | transportType, serverType, ssl ? "(using SSL) ": "", port); |
| 272 | server.serve(); |
| 273 | writeln("done."); |
| 274 | } |