blob: 993c063852c38f916d552beb0f7c14fc196f8b5c [file] [log] [blame]
Jake Farrellb95b0ff2012-03-22 21:49:10 +00001/*
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 */
19module thrift_test_server;
20
21import core.thread : dur, Thread;
22import std.algorithm;
23import std.exception : enforce;
24import std.getopt;
25import std.parallelism : totalCPUs;
26import std.string;
27import std.stdio;
28import std.typetuple : TypeTuple, staticMap;
29import thrift.base;
30import thrift.codegen.processor;
31import thrift.protocol.base;
32import thrift.protocol.binary;
33import thrift.protocol.compact;
34import thrift.protocol.json;
35import thrift.server.base;
36import thrift.server.transport.socket;
37import thrift.server.transport.ssl;
38import thrift.transport.base;
39import thrift.transport.buffered;
40import thrift.transport.framed;
41import thrift.transport.http;
42import thrift.transport.ssl;
43import thrift.util.hashset;
44import test_utils;
45
46import thrift_test_common;
47import thrift.test.ThriftTest_types;
48import thrift.test.ThriftTest;
49
50class 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
84 override Xtruct testStruct(ref const(Xtruct) thing) {
85 if (trace_) writefln("testStruct({\"%s\", %s, %s, %s})",
86 thing.string_thing, thing.byte_thing, thing.i32_thing, thing.i64_thing);
87 return thing;
88 }
89
90 override Xtruct2 testNest(ref const(Xtruct2) nest) {
91 auto thing = nest.struct_thing;
92 if (trace_) writefln("testNest({%s, {\"%s\", %s, %s, %s}, %s})",
93 nest.byte_thing, thing.string_thing, thing.byte_thing, thing.i32_thing,
94 thing.i64_thing, nest.i32_thing);
95 return nest;
96 }
97
98 override int[int] testMap(int[int] thing) {
99 if (trace_) writefln("testMap({%s})", thing);
100 return thing;
101 }
102
103 override HashSet!int testSet(HashSet!int thing) {
104 if (trace_) writefln("testSet({%s})",
105 join(map!`to!string(a)`(thing[]), ", "));
106 return thing;
107 }
108
109 override int[] testList(int[] thing) {
110 if (trace_) writefln("testList(%s)", thing);
111 return thing;
112 }
113
114 override Numberz testEnum(Numberz thing) {
115 if (trace_) writefln("testEnum(%s)", thing);
116 return thing;
117 }
118
119 override UserId testTypedef(UserId thing) {
120 if (trace_) writefln("testTypedef(%s)", thing);
121 return thing;
122 }
123
124 override string[string] testStringMap(string[string] thing) {
125 if (trace_) writefln("testStringMap(%s)", thing);
126 return thing;
127 }
128
129 override int[int][int] testMapMap(int hello) {
130 if (trace_) writefln("testMapMap(%s)", hello);
131 return testMapMapReturn;
132 }
133
134 override Insanity[Numberz][UserId] testInsanity(ref const(Insanity) argument) {
135 if (trace_) writeln("testInsanity()");
136 return testInsanityReturn;
137 }
138
139 override Xtruct testMulti(byte arg0, int arg1, long arg2, string[short] arg3,
140 Numberz arg4, UserId arg5)
141 {
142 if (trace_) writeln("testMulti()");
143 return Xtruct("Hello2", arg0, arg1, arg2);
144 }
145
146 override void testException(string arg) {
147 if (trace_) writefln("testException(%s)", arg);
148 if (arg == "Xception") {
149 auto e = new Xception();
150 e.errorCode = 1001;
151 e.message = arg;
152 throw e;
153 } else if (arg == "ApplicationException") {
154 throw new TException();
155 }
156 }
157
158 override Xtruct testMultiException(string arg0, string arg1) {
159 if (trace_) writefln("testMultiException(%s, %s)", arg0, arg1);
160
161 if (arg0 == "Xception") {
162 auto e = new Xception();
163 e.errorCode = 1001;
164 e.message = "This is an Xception";
165 throw e;
166 } else if (arg0 == "Xception2") {
167 auto e = new Xception2();
168 e.errorCode = 2002;
169 e.struct_thing.string_thing = "This is an Xception2";
170 throw e;
171 } else {
172 return Xtruct(arg1);
173 }
174 }
175
176 override void testOneway(int sleepFor) {
177 if (trace_) writefln("testOneway(%s): Sleeping...", sleepFor);
178 Thread.sleep(dur!"seconds"(sleepFor));
179 if (trace_) writefln("testOneway(%s): done sleeping!", sleepFor);
180 }
181
182private:
183 bool trace_;
184}
185
186void main(string[] args) {
187 ushort port = 9090;
188 ServerType serverType;
189 ProtocolType protocolType;
190 size_t numIOThreads = 1;
191 TransportType transportType;
192 bool ssl;
193 bool trace;
194 size_t taskPoolSize = totalCPUs;
195
196 getopt(args, "port", &port, "protocol", &protocolType, "server-type",
197 &serverType, "ssl", &ssl, "num-io-threads", &numIOThreads,
198 "task-pool-size", &taskPoolSize, "trace", &trace,
199 "transport", &transportType);
200
201 if (serverType == ServerType.nonblocking ||
202 serverType == ServerType.pooledNonblocking
203 ) {
204 enforce(transportType == TransportType.framed,
205 "Need to use framed transport with non-blocking server.");
206 enforce(!ssl, "The non-blocking server does not support SSL yet.");
207
208 // Don't wrap the contents into another layer of framing.
209 transportType = TransportType.raw;
210 }
211
212 version (ThriftTestTemplates) {
213 // Only exercise the specialized template code paths if explicitly enabled
214 // to reduce memory consumption on regular test suite runs – there should
215 // not be much that can go wrong with that specifically anyway.
216 alias TypeTuple!(TBufferedTransport, TFramedTransport, TServerHttpTransport)
217 AvailableTransports;
218 alias TypeTuple!(
219 staticMap!(TBinaryProtocol, AvailableTransports),
220 staticMap!(TCompactProtocol, AvailableTransports)
221 ) AvailableProtocols;
222 } else {
223 alias TypeTuple!() AvailableTransports;
224 alias TypeTuple!() AvailableProtocols;
225 }
226
227 TProtocolFactory protocolFactory;
228 final switch (protocolType) {
229 case ProtocolType.binary:
230 protocolFactory = new TBinaryProtocolFactory!AvailableTransports;
231 break;
232 case ProtocolType.compact:
233 protocolFactory = new TCompactProtocolFactory!AvailableTransports;
234 break;
235 case ProtocolType.json:
236 protocolFactory = new TJsonProtocolFactory!AvailableTransports;
237 break;
238 }
239
240 auto processor = new TServiceProcessor!(ThriftTest, AvailableProtocols)(
241 new TestHandler(trace));
242
243 TServerSocket serverSocket;
244 if (ssl) {
245 auto sslContext = new TSSLContext();
246 sslContext.serverSide = true;
247 sslContext.loadCertificate("./server-certificate.pem");
248 sslContext.loadPrivateKey("./server-private-key.pem");
249 sslContext.ciphers = "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH";
250 serverSocket = new TSSLServerSocket(port, sslContext);
251 } else {
252 serverSocket = new TServerSocket(port);
253 }
254
255 auto transportFactory = createTransportFactory(transportType);
256
257 auto server = createServer(serverType, numIOThreads, taskPoolSize,
258 processor, serverSocket, transportFactory, protocolFactory);
259
260 writefln("Starting %s/%s %s ThriftTest server %son port %s...", protocolType,
261 transportType, serverType, ssl ? "(using SSL) ": "", port);
262 server.serve();
263 writeln("done.");
264}