blob: 323f873547ad43ac709552f10fff4521bb674ef1 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +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 */
19
James E. King, III58402ff2017-11-17 14:41:46 -050020#include <thrift/async/TAsyncBufferProcessor.h>
21#include <thrift/async/TAsyncProtocolProcessor.h>
22#include <thrift/async/TEvhttpServer.h>
Roger Meier49ff8b12012-04-13 09:12:31 +000023#include <thrift/concurrency/PlatformThreadFactory.h>
James E. King, III58402ff2017-11-17 14:41:46 -050024#include <thrift/concurrency/ThreadManager.h>
25#include <thrift/processor/TMultiplexedProcessor.h>
Roger Meier49ff8b12012-04-13 09:12:31 +000026#include <thrift/protocol/TBinaryProtocol.h>
Roger Meier023192f2014-02-12 09:35:12 +010027#include <thrift/protocol/TCompactProtocol.h>
Dave Watson792db4e2015-01-16 11:22:01 -080028#include <thrift/protocol/THeaderProtocol.h>
Roger Meier49ff8b12012-04-13 09:12:31 +000029#include <thrift/protocol/TJSONProtocol.h>
Roger Meier49ff8b12012-04-13 09:12:31 +000030#include <thrift/server/TNonblockingServer.h>
James E. King, III58402ff2017-11-17 14:41:46 -050031#include <thrift/server/TSimpleServer.h>
32#include <thrift/server/TThreadPoolServer.h>
33#include <thrift/server/TThreadedServer.h>
Roger Meier49ff8b12012-04-13 09:12:31 +000034#include <thrift/transport/THttpServer.h>
35#include <thrift/transport/THttpTransport.h>
James E. King III70b33fb2018-03-11 10:57:10 -040036#include <thrift/transport/TNonblockingSSLServerSocket.h>
James E. King, III58402ff2017-11-17 14:41:46 -050037#include <thrift/transport/TNonblockingServerSocket.h>
38#include <thrift/transport/TSSLServerSocket.h>
39#include <thrift/transport/TSSLSocket.h>
40#include <thrift/transport/TServerSocket.h>
Roger Meier49ff8b12012-04-13 09:12:31 +000041#include <thrift/transport/TTransportUtils.h>
James E. King IIIb2b767e2018-09-15 20:32:04 +000042#include <thrift/transport/TZlibTransport.h>
James E. King, III58402ff2017-11-17 14:41:46 -050043
44#include "SecondService.h"
Mark Slee95771002006-06-07 06:53:25 +000045#include "ThriftTest.h"
Marc Slemko6be374b2006-08-04 03:16:25 +000046
James E. King, III7edc8fa2017-01-20 10:11:41 -050047#ifdef HAVE_STDINT_H
48#include <stdint.h>
49#endif
50#ifdef HAVE_INTTYPES_H
51#include <inttypes.h>
52#endif
James E. King III9bea32f2018-03-16 16:07:42 -040053#ifdef HAVE_SIGNAL_H
54#include <signal.h>
55#endif
James E. King, III7edc8fa2017-01-20 10:11:41 -050056
Marc Slemko6be374b2006-08-04 03:16:25 +000057#include <iostream>
58#include <stdexcept>
59#include <sstream>
60
James E. King, III58402ff2017-11-17 14:41:46 -050061#include <boost/algorithm/string.hpp>
Roger Meierca142b02011-06-07 17:59:07 +000062#include <boost/program_options.hpp>
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +053063#include <boost/filesystem.hpp>
James E. King, III82ae9572017-08-05 12:23:54 -040064#include <thrift/stdcxx.h>
Roger Meierca142b02011-06-07 17:59:07 +000065
Jake Farrell5d02b802014-01-07 21:42:01 -050066#if _WIN32
Konrad Grochowski16a23a62014-11-13 15:33:38 +010067#include <thrift/windows/TWinsockSingleton.h>
Jake Farrell5d02b802014-01-07 21:42:01 -050068#endif
David Reissbc3dddb2007-08-22 23:20:24 +000069
Mark Sleee8540632006-05-30 09:24:40 +000070using namespace std;
71
T Jake Lucianib5e62212009-01-31 22:36:20 +000072using namespace apache::thrift;
James E. King, III82ae9572017-08-05 12:23:54 -040073using namespace apache::thrift::async;
T Jake Lucianib5e62212009-01-31 22:36:20 +000074using namespace apache::thrift::concurrency;
75using namespace apache::thrift::protocol;
76using namespace apache::thrift::transport;
77using namespace apache::thrift::server;
Marc Slemko6be374b2006-08-04 03:16:25 +000078
Marc Slemkobf4fd192006-08-15 21:29:39 +000079using namespace thrift::test;
80
James E. King III9bea32f2018-03-16 16:07:42 -040081// to handle a controlled shutdown, signal handling is mandatory
82#ifdef HAVE_SIGNAL_H
83apache::thrift::concurrency::Monitor gMonitor;
84void signal_handler(int signum)
85{
86 if (signum == SIGINT) {
87 gMonitor.notifyAll();
88 }
89}
90#endif
91
Mark Sleed2655522006-09-05 22:09:57 +000092class TestHandler : public ThriftTestIf {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010093public:
Mark Sleed2655522006-09-05 22:09:57 +000094 TestHandler() {}
Mark Sleee8540632006-05-30 09:24:40 +000095
Konrad Grochowski16a23a62014-11-13 15:33:38 +010096 void testVoid() { printf("testVoid()\n"); }
Mark Sleee8540632006-05-30 09:24:40 +000097
Konrad Grochowski16a23a62014-11-13 15:33:38 +010098 void testString(string& out, const string& thing) {
Mark Sleee8540632006-05-30 09:24:40 +000099 printf("testString(\"%s\")\n", thing.c_str());
Mark Slee1921d202007-01-24 19:43:06 +0000100 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000101 }
102
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900103 bool testBool(const bool thing) {
104 printf("testBool(%s)\n", thing ? "true" : "false");
105 return thing;
106 }
107
Mark Slee1921d202007-01-24 19:43:06 +0000108 int8_t testByte(const int8_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000109 printf("testByte(%d)\n", (int)thing);
110 return thing;
111 }
112
Mark Slee1921d202007-01-24 19:43:06 +0000113 int32_t testI32(const int32_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000114 printf("testI32(%d)\n", thing);
115 return thing;
116 }
117
Mark Slee1921d202007-01-24 19:43:06 +0000118 int64_t testI64(const int64_t thing) {
Roger Meier0e814802014-01-17 21:07:58 +0100119 printf("testI64(%" PRId64 ")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +0000120 return thing;
121 }
122
Mark Slee1921d202007-01-24 19:43:06 +0000123 double testDouble(const double thing) {
Roger Meiera8cef6e2011-07-17 18:55:59 +0000124 printf("testDouble(%f)\n", thing);
Mark Sleec98d0502006-09-06 02:42:25 +0000125 return thing;
126 }
127
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100128 void testBinary(std::string& _return, const std::string& thing) {
129 std::ostringstream hexstr;
130 hexstr << std::hex << thing;
James E. King III3ae30422018-03-12 07:33:22 -0400131 printf("testBinary(%lu: %s)\n", safe_numeric_cast<unsigned long>(thing.size()), hexstr.str().c_str());
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100132 _return = thing;
133 }
134
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100135 void testStruct(Xtruct& out, const Xtruct& thing) {
136 printf("testStruct({\"%s\", %d, %d, %" PRId64 "})\n",
137 thing.string_thing.c_str(),
138 (int)thing.byte_thing,
139 thing.i32_thing,
140 thing.i64_thing);
Mark Slee1921d202007-01-24 19:43:06 +0000141 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000142 }
143
Mark Slee1921d202007-01-24 19:43:06 +0000144 void testNest(Xtruct2& out, const Xtruct2& nest) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100145 const Xtruct& thing = nest.struct_thing;
146 printf("testNest({%d, {\"%s\", %d, %d, %" PRId64 "}, %d})\n",
147 (int)nest.byte_thing,
148 thing.string_thing.c_str(),
149 (int)thing.byte_thing,
150 thing.i32_thing,
151 thing.i64_thing,
152 nest.i32_thing);
Mark Slee1921d202007-01-24 19:43:06 +0000153 out = nest;
Mark Sleee8540632006-05-30 09:24:40 +0000154 }
155
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100156 void testMap(map<int32_t, int32_t>& out, const map<int32_t, int32_t>& thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000157 printf("testMap({");
158 map<int32_t, int32_t>::const_iterator m_iter;
159 bool first = true;
160 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
161 if (first) {
162 first = false;
163 } else {
164 printf(", ");
165 }
166 printf("%d => %d", m_iter->first, m_iter->second);
167 }
168 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000169 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000170 }
171
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100172 void testStringMap(map<std::string, std::string>& out,
173 const map<std::string, std::string>& thing) {
Roger Meierd3b9dca2011-06-24 14:01:10 +0000174 printf("testMap({");
175 map<std::string, std::string>::const_iterator m_iter;
176 bool first = true;
177 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
178 if (first) {
179 first = false;
180 } else {
181 printf(", ");
182 }
183 printf("%s => %s", (m_iter->first).c_str(), (m_iter->second).c_str());
184 }
185 printf("})\n");
186 out = thing;
187 }
188
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100189 void testSet(set<int32_t>& out, const set<int32_t>& thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000190 printf("testSet({");
191 set<int32_t>::const_iterator s_iter;
192 bool first = true;
193 for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) {
194 if (first) {
195 first = false;
196 } else {
197 printf(", ");
198 }
199 printf("%d", *s_iter);
200 }
201 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000202 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000203 }
204
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100205 void testList(vector<int32_t>& out, const vector<int32_t>& thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000206 printf("testList({");
Mark Sleeb9acf982006-10-10 01:57:32 +0000207 vector<int32_t>::const_iterator l_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000208 bool first = true;
209 for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) {
210 if (first) {
211 first = false;
212 } else {
213 printf(", ");
214 }
215 printf("%d", *l_iter);
216 }
217 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000218 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000219 }
220
Bryan Duxbury833ae492010-09-27 17:26:02 +0000221 Numberz::type testEnum(const Numberz::type thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000222 printf("testEnum(%d)\n", thing);
223 return thing;
224 }
225
Mark Slee1921d202007-01-24 19:43:06 +0000226 UserId testTypedef(const UserId thing) {
Roger Meier0e814802014-01-17 21:07:58 +0100227 printf("testTypedef(%" PRId64 ")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +0000228 return thing;
229 }
230
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100231 void testMapMap(map<int32_t, map<int32_t, int32_t> >& mapmap, const int32_t hello) {
Mark Sleee8540632006-05-30 09:24:40 +0000232 printf("testMapMap(%d)\n", hello);
Mark Sleee8540632006-05-30 09:24:40 +0000233
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100234 map<int32_t, int32_t> pos;
235 map<int32_t, int32_t> neg;
Mark Sleee8540632006-05-30 09:24:40 +0000236 for (int i = 1; i < 5; i++) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100237 pos.insert(make_pair(i, i));
238 neg.insert(make_pair(-i, -i));
Mark Sleee8540632006-05-30 09:24:40 +0000239 }
240
241 mapmap.insert(make_pair(4, pos));
242 mapmap.insert(make_pair(-4, neg));
Mark Sleee8540632006-05-30 09:24:40 +0000243 }
244
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100245 void testInsanity(map<UserId, map<Numberz::type, Insanity> >& insane, const Insanity& argument) {
Mark Sleee8540632006-05-30 09:24:40 +0000246 printf("testInsanity()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000247
Mark Sleee8540632006-05-30 09:24:40 +0000248 Insanity looney;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000249 map<Numberz::type, Insanity> first_map;
250 map<Numberz::type, Insanity> second_map;
Mark Sleee8540632006-05-30 09:24:40 +0000251
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900252 first_map.insert(make_pair(Numberz::TWO, argument));
253 first_map.insert(make_pair(Numberz::THREE, argument));
Mark Sleee8540632006-05-30 09:24:40 +0000254
Bryan Duxbury833ae492010-09-27 17:26:02 +0000255 second_map.insert(make_pair(Numberz::SIX, looney));
Mark Sleee8540632006-05-30 09:24:40 +0000256
Mark Sleee8540632006-05-30 09:24:40 +0000257 insane.insert(make_pair(1, first_map));
258 insane.insert(make_pair(2, second_map));
259
260 printf("return");
261 printf(" = {");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100262 map<UserId, map<Numberz::type, Insanity> >::const_iterator i_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000263 for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) {
Roger Meier0e814802014-01-17 21:07:58 +0100264 printf("%" PRId64 " => {", i_iter->first);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100265 map<Numberz::type, Insanity>::const_iterator i2_iter;
266 for (i2_iter = i_iter->second.begin(); i2_iter != i_iter->second.end(); ++i2_iter) {
Mark Sleee8540632006-05-30 09:24:40 +0000267 printf("%d => {", i2_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000268 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
269 map<Numberz::type, UserId>::const_iterator um;
Mark Sleee8540632006-05-30 09:24:40 +0000270 printf("{");
271 for (um = userMap.begin(); um != userMap.end(); ++um) {
Roger Meier0e814802014-01-17 21:07:58 +0100272 printf("%d => %" PRId64 ", ", um->first, um->second);
Mark Sleee8540632006-05-30 09:24:40 +0000273 }
274 printf("}, ");
275
Mark Sleeb9acf982006-10-10 01:57:32 +0000276 vector<Xtruct> xtructs = i2_iter->second.xtructs;
277 vector<Xtruct>::const_iterator x;
Mark Sleee8540632006-05-30 09:24:40 +0000278 printf("{");
279 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100280 printf("{\"%s\", %d, %d, %" PRId64 "}, ",
281 x->string_thing.c_str(),
282 (int)x->byte_thing,
283 x->i32_thing,
284 x->i64_thing);
Mark Sleee8540632006-05-30 09:24:40 +0000285 }
286 printf("}");
287
288 printf("}, ");
289 }
290 printf("}, ");
291 }
292 printf("}\n");
Mark Sleee8540632006-05-30 09:24:40 +0000293 }
Marc Slemkoe6889de2006-08-12 00:32:53 +0000294
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100295 void testMulti(Xtruct& hello,
296 const int8_t arg0,
297 const int32_t arg1,
298 const int64_t arg2,
299 const std::map<int16_t, std::string>& arg3,
300 const Numberz::type arg4,
301 const UserId arg5) {
302 (void)arg3;
303 (void)arg4;
304 (void)arg5;
Jake Farrell5d02b802014-01-07 21:42:01 -0500305
Marc Slemkoe6889de2006-08-12 00:32:53 +0000306 printf("testMulti()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000307
Marc Slemkoe6889de2006-08-12 00:32:53 +0000308 hello.string_thing = "Hello2";
309 hello.byte_thing = arg0;
310 hello.i32_thing = arg1;
311 hello.i64_thing = (int64_t)arg2;
Marc Slemkoe6889de2006-08-12 00:32:53 +0000312 }
Marc Slemkobf4fd192006-08-15 21:29:39 +0000313
ben-craigfae08e72015-07-15 11:34:47 -0500314 void testException(const std::string& arg) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000315 printf("testException(%s)\n", arg.c_str());
Mark Sleed3d733a2006-09-01 22:19:06 +0000316 if (arg.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000317 Xception e;
318 e.errorCode = 1001;
David Reiss9813cbe2009-01-31 21:39:11 +0000319 e.message = arg;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000320 throw e;
Roger Meier1f8b48f2012-05-02 22:56:47 +0000321 } else if (arg.compare("TException") == 0) {
T Jake Lucianib5e62212009-01-31 22:36:20 +0000322 apache::thrift::TException e;
David Reiss55ff70f2008-06-11 00:58:25 +0000323 throw e;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000324 } else {
325 Xtruct result;
326 result.string_thing = arg;
327 return;
328 }
329 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000330
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100331 void testMultiException(Xtruct& result,
332 const std::string& arg0,
ben-craigfae08e72015-07-15 11:34:47 -0500333 const std::string& arg1) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000334
Marc Slemko71d4e472006-08-15 22:34:04 +0000335 printf("testMultiException(%s, %s)\n", arg0.c_str(), arg1.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000336
Mark Sleef5f2be42006-09-05 21:05:31 +0000337 if (arg0.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000338 Xception e;
339 e.errorCode = 1001;
340 e.message = "This is an Xception";
341 throw e;
Mark Sleef5f2be42006-09-05 21:05:31 +0000342 } else if (arg0.compare("Xception2") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000343 Xception2 e;
344 e.errorCode = 2002;
345 e.struct_thing.string_thing = "This is an Xception2";
346 throw e;
347 } else {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000348 result.string_thing = arg1;
Mark Slee1921d202007-01-24 19:43:06 +0000349 return;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000350 }
351 }
David Reiss2ab6fe82008-02-18 02:11:44 +0000352
James E. King, III58402ff2017-11-17 14:41:46 -0500353 void testOneway(const int32_t aNum) {
354 printf("testOneway(%d): call received\n", aNum);
David Reiss2ab6fe82008-02-18 02:11:44 +0000355 }
Mark Sleee8540632006-05-30 09:24:40 +0000356};
357
James E. King, III58402ff2017-11-17 14:41:46 -0500358class SecondHandler : public SecondServiceIf
359{
360 public:
361 void secondtestString(std::string& result, const std::string& thing)
362 { result = "testString(\"" + thing + "\")"; }
James E. King, III39eaae62017-11-19 20:17:33 -0500363};
James E. King, III58402ff2017-11-17 14:41:46 -0500364
David Reissd7192062010-10-06 17:09:33 +0000365class TestProcessorEventHandler : public TProcessorEventHandler {
David Reiss23248712010-10-06 17:10:08 +0000366 virtual void* getContext(const char* fn_name, void* serverContext) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100367 (void)serverContext;
David Reissd7192062010-10-06 17:09:33 +0000368 return new std::string(fn_name);
369 }
370 virtual void freeContext(void* ctx, const char* fn_name) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100371 (void)fn_name;
David Reissd7192062010-10-06 17:09:33 +0000372 delete static_cast<std::string*>(ctx);
373 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100374 virtual void preRead(void* ctx, const char* fn_name) { communicate("preRead", ctx, fn_name); }
David Reissef7200f2010-10-06 17:09:42 +0000375 virtual void postRead(void* ctx, const char* fn_name, uint32_t bytes) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100376 (void)bytes;
David Reissd7192062010-10-06 17:09:33 +0000377 communicate("postRead", ctx, fn_name);
378 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100379 virtual void preWrite(void* ctx, const char* fn_name) { communicate("preWrite", ctx, fn_name); }
David Reissef7200f2010-10-06 17:09:42 +0000380 virtual void postWrite(void* ctx, const char* fn_name, uint32_t bytes) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100381 (void)bytes;
David Reissd7192062010-10-06 17:09:33 +0000382 communicate("postWrite", ctx, fn_name);
383 }
384 virtual void asyncComplete(void* ctx, const char* fn_name) {
385 communicate("asyncComplete", ctx, fn_name);
386 }
387 virtual void handlerError(void* ctx, const char* fn_name) {
388 communicate("handlerError", ctx, fn_name);
389 }
390
391 void communicate(const char* event, void* ctx, const char* fn_name) {
392 std::cout << event << ": " << *static_cast<std::string*>(ctx) << " = " << fn_name << std::endl;
393 }
394};
395
Roger Meier7e056e72011-07-17 07:28:28 +0000396class TestHandlerAsync : public ThriftTestCobSvIf {
397public:
James E. King, III82ae9572017-08-05 12:23:54 -0400398 TestHandlerAsync(stdcxx::shared_ptr<TestHandler>& handler) : _delegate(handler) {}
Roger Meier7e056e72011-07-17 07:28:28 +0000399 virtual ~TestHandlerAsync() {}
400
James E. King, III82ae9572017-08-05 12:23:54 -0400401 virtual void testVoid(stdcxx::function<void()> cob) {
Roger Meier7e056e72011-07-17 07:28:28 +0000402 _delegate->testVoid();
403 cob();
404 }
405
James E. King, III82ae9572017-08-05 12:23:54 -0400406 virtual void testString(stdcxx::function<void(std::string const& _return)> cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100407 const std::string& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000408 std::string res;
409 _delegate->testString(res, thing);
410 cob(res);
411 }
412
James E. King, III82ae9572017-08-05 12:23:54 -0400413 virtual void testBool(stdcxx::function<void(bool const& _return)> cob, const bool thing) {
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900414 bool res = _delegate->testBool(thing);
415 cob(res);
416 }
417
James E. King, III82ae9572017-08-05 12:23:54 -0400418 virtual void testByte(stdcxx::function<void(int8_t const& _return)> cob, const int8_t thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000419 int8_t res = _delegate->testByte(thing);
420 cob(res);
421 }
422
James E. King, III82ae9572017-08-05 12:23:54 -0400423 virtual void testI32(stdcxx::function<void(int32_t const& _return)> cob, const int32_t thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000424 int32_t res = _delegate->testI32(thing);
425 cob(res);
426 }
427
James E. King, III82ae9572017-08-05 12:23:54 -0400428 virtual void testI64(stdcxx::function<void(int64_t const& _return)> cob, const int64_t thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000429 int64_t res = _delegate->testI64(thing);
430 cob(res);
431 }
432
James E. King, III82ae9572017-08-05 12:23:54 -0400433 virtual void testDouble(stdcxx::function<void(double const& _return)> cob, const double thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000434 double res = _delegate->testDouble(thing);
435 cob(res);
436 }
437
James E. King, III82ae9572017-08-05 12:23:54 -0400438 virtual void testBinary(stdcxx::function<void(std::string const& _return)> cob,
Konrad Grochowski1f6e3802015-05-18 18:10:06 +0200439 const std::string& thing) {
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100440 std::string res;
441 _delegate->testBinary(res, thing);
442 cob(res);
443 }
444
James E. King, III82ae9572017-08-05 12:23:54 -0400445 virtual void testStruct(stdcxx::function<void(Xtruct const& _return)> cob, const Xtruct& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000446 Xtruct res;
447 _delegate->testStruct(res, thing);
448 cob(res);
449 }
450
James E. King, III82ae9572017-08-05 12:23:54 -0400451 virtual void testNest(stdcxx::function<void(Xtruct2 const& _return)> cob, const Xtruct2& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000452 Xtruct2 res;
453 _delegate->testNest(res, thing);
454 cob(res);
455 }
456
James E. King, III82ae9572017-08-05 12:23:54 -0400457 virtual void testMap(stdcxx::function<void(std::map<int32_t, int32_t> const& _return)> cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100458 const std::map<int32_t, int32_t>& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000459 std::map<int32_t, int32_t> res;
460 _delegate->testMap(res, thing);
461 cob(res);
462 }
463
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100464 virtual void testStringMap(
James E. King, III82ae9572017-08-05 12:23:54 -0400465 stdcxx::function<void(std::map<std::string, std::string> const& _return)> cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100466 const std::map<std::string, std::string>& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000467 std::map<std::string, std::string> res;
468 _delegate->testStringMap(res, thing);
469 cob(res);
470 }
471
James E. King, III82ae9572017-08-05 12:23:54 -0400472 virtual void testSet(stdcxx::function<void(std::set<int32_t> const& _return)> cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100473 const std::set<int32_t>& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000474 std::set<int32_t> res;
475 _delegate->testSet(res, thing);
476 cob(res);
477 }
478
James E. King, III82ae9572017-08-05 12:23:54 -0400479 virtual void testList(stdcxx::function<void(std::vector<int32_t> const& _return)> cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100480 const std::vector<int32_t>& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000481 std::vector<int32_t> res;
482 _delegate->testList(res, thing);
483 cob(res);
484 }
485
James E. King, III82ae9572017-08-05 12:23:54 -0400486 virtual void testEnum(stdcxx::function<void(Numberz::type const& _return)> cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100487 const Numberz::type thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000488 Numberz::type res = _delegate->testEnum(thing);
489 cob(res);
490 }
491
James E. King, III82ae9572017-08-05 12:23:54 -0400492 virtual void testTypedef(stdcxx::function<void(UserId const& _return)> cob, const UserId thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000493 UserId res = _delegate->testTypedef(thing);
494 cob(res);
495 }
496
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100497 virtual void testMapMap(
James E. King, III82ae9572017-08-05 12:23:54 -0400498 stdcxx::function<void(std::map<int32_t, std::map<int32_t, int32_t> > const& _return)> cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100499 const int32_t hello) {
Roger Meier7e056e72011-07-17 07:28:28 +0000500 std::map<int32_t, std::map<int32_t, int32_t> > res;
501 _delegate->testMapMap(res, hello);
502 cob(res);
503 }
504
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100505 virtual void testInsanity(
James E. King, III82ae9572017-08-05 12:23:54 -0400506 stdcxx::function<void(std::map<UserId, std::map<Numberz::type, Insanity> > const& _return)> cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100507 const Insanity& argument) {
Jake Farrell5d02b802014-01-07 21:42:01 -0500508 std::map<UserId, std::map<Numberz::type, Insanity> > res;
Roger Meier7e056e72011-07-17 07:28:28 +0000509 _delegate->testInsanity(res, argument);
510 cob(res);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100511 }
Roger Meier7e056e72011-07-17 07:28:28 +0000512
James E. King, III82ae9572017-08-05 12:23:54 -0400513 virtual void testMulti(stdcxx::function<void(Xtruct const& _return)> cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100514 const int8_t arg0,
515 const int32_t arg1,
516 const int64_t arg2,
517 const std::map<int16_t, std::string>& arg3,
518 const Numberz::type arg4,
519 const UserId arg5) {
Roger Meier7e056e72011-07-17 07:28:28 +0000520 Xtruct res;
521 _delegate->testMulti(res, arg0, arg1, arg2, arg3, arg4, arg5);
522 cob(res);
523 }
524
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100525 virtual void testException(
James E. King, III82ae9572017-08-05 12:23:54 -0400526 stdcxx::function<void()> cob,
527 stdcxx::function<void(::apache::thrift::TDelayedException* _throw)> exn_cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100528 const std::string& arg) {
Roger Meier7e056e72011-07-17 07:28:28 +0000529 try {
530 _delegate->testException(arg);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100531 } catch (const apache::thrift::TException& e) {
Roger Meier7e056e72011-07-17 07:28:28 +0000532 exn_cob(apache::thrift::TDelayedException::delayException(e));
533 return;
534 }
535 cob();
536 }
537
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100538 virtual void testMultiException(
James E. King, III82ae9572017-08-05 12:23:54 -0400539 stdcxx::function<void(Xtruct const& _return)> cob,
540 stdcxx::function<void(::apache::thrift::TDelayedException* _throw)> exn_cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100541 const std::string& arg0,
542 const std::string& arg1) {
Roger Meier7e056e72011-07-17 07:28:28 +0000543 Xtruct res;
544 try {
545 _delegate->testMultiException(res, arg0, arg1);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100546 } catch (const apache::thrift::TException& e) {
Roger Meier7e056e72011-07-17 07:28:28 +0000547 exn_cob(apache::thrift::TDelayedException::delayException(e));
548 return;
549 }
550 cob(res);
551 }
552
James E. King, III82ae9572017-08-05 12:23:54 -0400553 virtual void testOneway(stdcxx::function<void()> cob, const int32_t secondsToSleep) {
Roger Meier7e056e72011-07-17 07:28:28 +0000554 _delegate->testOneway(secondsToSleep);
555 cob();
556 }
557
558protected:
James E. King, III82ae9572017-08-05 12:23:54 -0400559 stdcxx::shared_ptr<TestHandler> _delegate;
Roger Meier7e056e72011-07-17 07:28:28 +0000560};
561
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900562namespace po = boost::program_options;
563
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100564int main(int argc, char** argv) {
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +0530565
Nobuaki Sukegawac8744082016-03-09 19:55:56 +0900566 string testDir = boost::filesystem::system_complete(argv[0]).parent_path().parent_path().parent_path().string();
567 string certPath = testDir + "/keys/server.crt";
568 string keyPath = testDir + "/keys/server.key";
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +0530569
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100570#if _WIN32
Jake Farrell5d02b802014-01-07 21:42:01 -0500571 transport::TWinsockSingleton::create();
572#endif
Mark Sleee8540632006-05-30 09:24:40 +0000573 int port = 9090;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000574 bool ssl = false;
James E. King IIIb2b767e2018-09-15 20:32:04 +0000575 bool zlib = false;
Roger Meierca142b02011-06-07 17:59:07 +0000576 string transport_type = "buffered";
577 string protocol_type = "binary";
578 string server_type = "simple";
579 string domain_socket = "";
pavlodd08f6e2015-10-08 16:43:56 -0400580 bool abstract_namespace = false;
Roger Meierca142b02011-06-07 17:59:07 +0000581 size_t workers = 4;
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900582 int string_limit = 0;
583 int container_limit = 0;
Marc Slemko6be374b2006-08-04 03:16:25 +0000584
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900585 po::options_description desc("Allowed options");
586 desc.add_options()
587 ("help,h", "produce help message")
588 ("port", po::value<int>(&port)->default_value(port), "Port number to listen")
589 ("domain-socket", po::value<string>(&domain_socket) ->default_value(domain_socket), "Unix Domain Socket (e.g. /tmp/ThriftTest.thrift)")
590 ("abstract-namespace", "Create the domain socket in the Abstract Namespace (no connection with filesystem pathnames)")
591 ("server-type", po::value<string>(&server_type)->default_value(server_type), "type of server, \"simple\", \"thread-pool\", \"threaded\", or \"nonblocking\"")
James E. King IIIb2b767e2018-09-15 20:32:04 +0000592 ("transport", po::value<string>(&transport_type)->default_value(transport_type), "transport: buffered, framed, http, zlib")
James E. King, III58402ff2017-11-17 14:41:46 -0500593 ("protocol", po::value<string>(&protocol_type)->default_value(protocol_type), "protocol: binary, compact, header, json, multi, multic, multih, multij")
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900594 ("ssl", "Encrypted Transport using SSL")
James E. King IIIb2b767e2018-09-15 20:32:04 +0000595 ("zlib", "Wrapped Transport using Zlib")
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900596 ("processor-events", "processor-events")
597 ("workers,n", po::value<size_t>(&workers)->default_value(workers), "Number of thread pools workers. Only valid for thread-pool server type")
598 ("string-limit", po::value<int>(&string_limit))
599 ("container-limit", po::value<int>(&container_limit));
Marc Slemko6be374b2006-08-04 03:16:25 +0000600
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900601 po::variables_map vm;
602 po::store(po::parse_command_line(argc, argv, desc), vm);
603 po::notify(vm);
Marc Slemko6be374b2006-08-04 03:16:25 +0000604
Roger Meierca142b02011-06-07 17:59:07 +0000605 if (vm.count("help")) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100606 cout << desc << "\n";
607 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000608 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500609
Marc Slemko6be374b2006-08-04 03:16:25 +0000610 try {
Roger Meierca142b02011-06-07 17:59:07 +0000611 if (!server_type.empty()) {
612 if (server_type == "simple") {
613 } else if (server_type == "thread-pool") {
614 } else if (server_type == "threaded") {
615 } else if (server_type == "nonblocking") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000616 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100617 throw invalid_argument("Unknown server type " + server_type);
Roger Meierca142b02011-06-07 17:59:07 +0000618 }
619 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500620
Roger Meierca142b02011-06-07 17:59:07 +0000621 if (!protocol_type.empty()) {
622 if (protocol_type == "binary") {
Roger Meier284101c2014-03-11 21:20:35 +0100623 } else if (protocol_type == "compact") {
Roger Meierca142b02011-06-07 17:59:07 +0000624 } else if (protocol_type == "json") {
Dave Watson792db4e2015-01-16 11:22:01 -0800625 } else if (protocol_type == "header") {
James E. King, III39eaae62017-11-19 20:17:33 -0500626 } else if (protocol_type == "multi") { // multiplexed binary
627 } else if (protocol_type == "multic") { // multiplexed compact
628 } else if (protocol_type == "multih") { // multiplexed header
629 } else if (protocol_type == "multij") { // multiplexed json
Roger Meierca142b02011-06-07 17:59:07 +0000630 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100631 throw invalid_argument("Unknown protocol type " + protocol_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000632 }
633 }
634
Roger Meier284101c2014-03-11 21:20:35 +0100635 if (!transport_type.empty()) {
Roger Meierca142b02011-06-07 17:59:07 +0000636 if (transport_type == "buffered") {
637 } else if (transport_type == "framed") {
638 } else if (transport_type == "http") {
James E. King IIIb2b767e2018-09-15 20:32:04 +0000639 } else if (transport_type == "zlib") {
640 // crosstester will pass zlib as a flag and a transport right now...
Marc Slemko6be374b2006-08-04 03:16:25 +0000641 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100642 throw invalid_argument("Unknown transport type " + transport_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000643 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000644 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000645
Bryan Duxbury833ae492010-09-27 17:26:02 +0000646 } catch (std::exception& e) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000647 cerr << e.what() << endl;
Roger Meierca142b02011-06-07 17:59:07 +0000648 cout << desc << "\n";
649 return 1;
Marc Slemko6be374b2006-08-04 03:16:25 +0000650 }
651
Roger Meierca142b02011-06-07 17:59:07 +0000652 if (vm.count("ssl")) {
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000653 ssl = true;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000654 }
655
James E. King IIIb2b767e2018-09-15 20:32:04 +0000656 if (vm.count("zlib")) {
657 zlib = true;
658 }
659
James E. King III9bea32f2018-03-16 16:07:42 -0400660#if defined(HAVE_SIGNAL_H) && defined(SIGPIPE)
661 if (ssl) {
662 signal(SIGPIPE, SIG_IGN); // for OpenSSL, otherwise we end abruptly
663 }
664#endif
665
pavlodd08f6e2015-10-08 16:43:56 -0400666 if (vm.count("abstract-namespace")) {
667 abstract_namespace = true;
668 }
669
Mark Sleee8540632006-05-30 09:24:40 +0000670 // Dispatcher
James E. King, III82ae9572017-08-05 12:23:54 -0400671 stdcxx::shared_ptr<TProtocolFactory> protocolFactory;
James E. King, III58402ff2017-11-17 14:41:46 -0500672 if (protocol_type == "json" || protocol_type == "multij") {
James E. King, III82ae9572017-08-05 12:23:54 -0400673 stdcxx::shared_ptr<TProtocolFactory> jsonProtocolFactory(new TJSONProtocolFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000674 protocolFactory = jsonProtocolFactory;
James E. King, III58402ff2017-11-17 14:41:46 -0500675 } else if (protocol_type == "compact" || protocol_type == "multic") {
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900676 TCompactProtocolFactoryT<TBufferBase> *compactProtocolFactory = new TCompactProtocolFactoryT<TBufferBase>();
677 compactProtocolFactory->setContainerSizeLimit(container_limit);
678 compactProtocolFactory->setStringSizeLimit(string_limit);
679 protocolFactory.reset(compactProtocolFactory);
James E. King, III58402ff2017-11-17 14:41:46 -0500680 } else if (protocol_type == "header" || protocol_type == "multih") {
James E. King, III82ae9572017-08-05 12:23:54 -0400681 stdcxx::shared_ptr<TProtocolFactory> headerProtocolFactory(new THeaderProtocolFactory());
Dave Watson792db4e2015-01-16 11:22:01 -0800682 protocolFactory = headerProtocolFactory;
Roger Meierca142b02011-06-07 17:59:07 +0000683 } else {
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900684 TBinaryProtocolFactoryT<TBufferBase>* binaryProtocolFactory = new TBinaryProtocolFactoryT<TBufferBase>();
685 binaryProtocolFactory->setContainerSizeLimit(container_limit);
686 binaryProtocolFactory->setStringSizeLimit(string_limit);
687 protocolFactory.reset(binaryProtocolFactory);
Roger Meierca142b02011-06-07 17:59:07 +0000688 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000689
James E. King, III58402ff2017-11-17 14:41:46 -0500690 // Processors
James E. King, III82ae9572017-08-05 12:23:54 -0400691 stdcxx::shared_ptr<TestHandler> testHandler(new TestHandler());
James E. King, III58402ff2017-11-17 14:41:46 -0500692 stdcxx::shared_ptr<TProcessor> testProcessor(new ThriftTestProcessor(testHandler));
Jake Farrell5d02b802014-01-07 21:42:01 -0500693
Roger Meierca142b02011-06-07 17:59:07 +0000694 if (vm.count("processor-events")) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100695 testProcessor->setEventHandler(
James E. King, III82ae9572017-08-05 12:23:54 -0400696 stdcxx::shared_ptr<TProcessorEventHandler>(new TestProcessorEventHandler()));
David Reissd7192062010-10-06 17:09:33 +0000697 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500698
Mark Sleee8540632006-05-30 09:24:40 +0000699 // Transport
James E. King, III82ae9572017-08-05 12:23:54 -0400700 stdcxx::shared_ptr<TSSLSocketFactory> sslSocketFactory;
701 stdcxx::shared_ptr<TServerSocket> serverSocket;
Mark Sleee8540632006-05-30 09:24:40 +0000702
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000703 if (ssl) {
James E. King, III82ae9572017-08-05 12:23:54 -0400704 sslSocketFactory = stdcxx::shared_ptr<TSSLSocketFactory>(new TSSLSocketFactory());
Nobuaki Sukegawac8744082016-03-09 19:55:56 +0900705 sslSocketFactory->loadCertificate(certPath.c_str());
706 sslSocketFactory->loadPrivateKey(keyPath.c_str());
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000707 sslSocketFactory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
James E. King III70b33fb2018-03-11 10:57:10 -0400708 if (server_type != "nonblocking") {
709 serverSocket = stdcxx::shared_ptr<TServerSocket>(new TSSLServerSocket(port, sslSocketFactory));
710 }
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000711 } else {
Roger Meierc94b2932014-02-22 20:07:33 +0100712 if (domain_socket != "") {
pavlodd08f6e2015-10-08 16:43:56 -0400713 if (abstract_namespace) {
714 std::string abstract_socket("\0", 1);
715 abstract_socket += domain_socket;
James E. King, III82ae9572017-08-05 12:23:54 -0400716 serverSocket = stdcxx::shared_ptr<TServerSocket>(new TServerSocket(abstract_socket));
pavlodd08f6e2015-10-08 16:43:56 -0400717 } else {
718 unlink(domain_socket.c_str());
James E. King, III82ae9572017-08-05 12:23:54 -0400719 serverSocket = stdcxx::shared_ptr<TServerSocket>(new TServerSocket(domain_socket));
pavlodd08f6e2015-10-08 16:43:56 -0400720 }
Roger Meierc94b2932014-02-22 20:07:33 +0100721 port = 0;
722 } else {
James E. King, III82ae9572017-08-05 12:23:54 -0400723 serverSocket = stdcxx::shared_ptr<TServerSocket>(new TServerSocket(port));
Roger Meierc94b2932014-02-22 20:07:33 +0100724 }
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000725 }
Roger Meierca142b02011-06-07 17:59:07 +0000726
Mark Sleed788b2e2006-09-07 01:26:35 +0000727 // Factory
James E. King, III82ae9572017-08-05 12:23:54 -0400728 stdcxx::shared_ptr<TTransportFactory> transportFactory;
Jake Farrell5d02b802014-01-07 21:42:01 -0500729
Roger Meier7e056e72011-07-17 07:28:28 +0000730 if (transport_type == "http" && server_type != "nonblocking") {
James E. King IIIb2b767e2018-09-15 20:32:04 +0000731 transportFactory = stdcxx::make_shared<THttpServerTransportFactory>();
Roger Meierca142b02011-06-07 17:59:07 +0000732 } else if (transport_type == "framed") {
James E. King IIIb2b767e2018-09-15 20:32:04 +0000733 transportFactory = stdcxx::make_shared<TFramedTransportFactory>();
Roger Meierca142b02011-06-07 17:59:07 +0000734 } else {
James E. King IIIb2b767e2018-09-15 20:32:04 +0000735 transportFactory = stdcxx::make_shared<TBufferedTransportFactory>();
736 }
737
738 if (zlib) {
739 // hmm.. doesn't seem to be a way to make it wrap the others...
740 transportFactory = stdcxx::make_shared<TZlibTransportFactory>();
Roger Meierca142b02011-06-07 17:59:07 +0000741 }
Mark Sleed788b2e2006-09-07 01:26:35 +0000742
Roger Meierca142b02011-06-07 17:59:07 +0000743 // Server Info
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100744 cout << "Starting \"" << server_type << "\" server (" << transport_type << "/" << protocol_type
pavlodd08f6e2015-10-08 16:43:56 -0400745 << ") listen on: ";
746 if (abstract_namespace) {
747 cout << '@';
748 }
749 cout << domain_socket;
Roger Meierca142b02011-06-07 17:59:07 +0000750 if (port != 0) {
751 cout << port;
752 }
753 cout << endl;
Mark Sleee8540632006-05-30 09:24:40 +0000754
James E. King, III58402ff2017-11-17 14:41:46 -0500755 // Multiplexed Processor if needed
756 if (boost::starts_with(protocol_type, "multi")) {
757 stdcxx::shared_ptr<SecondHandler> secondHandler(new SecondHandler());
758 stdcxx::shared_ptr<SecondServiceProcessor> secondProcessor(new SecondServiceProcessor(secondHandler));
759
760 stdcxx::shared_ptr<TMultiplexedProcessor> multiplexedProcessor(new TMultiplexedProcessor());
761 multiplexedProcessor->registerDefault(testProcessor); // non-multi clients go to the default processor (multi:binary, multic:compact, ...)
762 multiplexedProcessor->registerProcessor("ThriftTest", testProcessor);
763 multiplexedProcessor->registerProcessor("SecondService", secondProcessor);
764 testProcessor = stdcxx::dynamic_pointer_cast<TProcessor>(multiplexedProcessor);
765 }
766
Roger Meierca142b02011-06-07 17:59:07 +0000767 // Server
James E. King, III82ae9572017-08-05 12:23:54 -0400768 stdcxx::shared_ptr<apache::thrift::server::TServer> server;
Jake Farrell5d02b802014-01-07 21:42:01 -0500769
Roger Meierca142b02011-06-07 17:59:07 +0000770 if (server_type == "simple") {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100771 server.reset(new TSimpleServer(testProcessor, serverSocket, transportFactory, protocolFactory));
Roger Meierca142b02011-06-07 17:59:07 +0000772 } else if (server_type == "thread-pool") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000773
James E. King, III82ae9572017-08-05 12:23:54 -0400774 stdcxx::shared_ptr<PlatformThreadFactory> threadFactory
775 = stdcxx::shared_ptr<PlatformThreadFactory>(new PlatformThreadFactory());
Marc Slemko6be374b2006-08-04 03:16:25 +0000776
James E. King III70b33fb2018-03-11 10:57:10 -0400777 stdcxx::shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workers);
Marc Slemko6be374b2006-08-04 03:16:25 +0000778 threadManager->threadFactory(threadFactory);
Marc Slemko6be374b2006-08-04 03:16:25 +0000779 threadManager->start();
780
Jake Farrell5d02b802014-01-07 21:42:01 -0500781 server.reset(new TThreadPoolServer(testProcessor,
Roger Meierca142b02011-06-07 17:59:07 +0000782 serverSocket,
Mark Sleed788b2e2006-09-07 01:26:35 +0000783 transportFactory,
Mark Sleea3302652006-10-25 19:03:32 +0000784 protocolFactory,
Jake Farrell5d02b802014-01-07 21:42:01 -0500785 threadManager));
Roger Meierca142b02011-06-07 17:59:07 +0000786 } else if (server_type == "threaded") {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100787 server.reset(
788 new TThreadedServer(testProcessor, serverSocket, transportFactory, protocolFactory));
Roger Meierca142b02011-06-07 17:59:07 +0000789 } else if (server_type == "nonblocking") {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100790 if (transport_type == "http") {
James E. King, III82ae9572017-08-05 12:23:54 -0400791 stdcxx::shared_ptr<TestHandlerAsync> testHandlerAsync(new TestHandlerAsync(testHandler));
792 stdcxx::shared_ptr<TAsyncProcessor> testProcessorAsync(
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100793 new ThriftTestAsyncProcessor(testHandlerAsync));
James E. King, III82ae9572017-08-05 12:23:54 -0400794 stdcxx::shared_ptr<TAsyncBufferProcessor> testBufferProcessor(
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100795 new TAsyncProtocolProcessor(testProcessorAsync, protocolFactory));
Jake Farrell5d02b802014-01-07 21:42:01 -0500796
797 // not loading nonblockingServer into "server" because
798 // TEvhttpServer doesn't inherit from TServer, and doesn't
799 // provide a stop method.
Roger Meier7e056e72011-07-17 07:28:28 +0000800 TEvhttpServer nonblockingServer(testBufferProcessor, port);
801 nonblockingServer.serve();
James E. King III70b33fb2018-03-11 10:57:10 -0400802 } else if (transport_type == "framed") {
James E. King III9bea32f2018-03-16 16:07:42 -0400803 stdcxx::shared_ptr<transport::TNonblockingServerTransport> nbSocket;
804 nbSocket.reset(
805 ssl ? new transport::TNonblockingSSLServerSocket(port, sslSocketFactory)
806 : new transport::TNonblockingServerSocket(port));
Divya Thaluru808d1432017-08-06 16:36:36 -0700807 server.reset(new TNonblockingServer(testProcessor, protocolFactory, nbSocket));
James E. King III70b33fb2018-03-11 10:57:10 -0400808 } else {
James E. King III9bea32f2018-03-16 16:07:42 -0400809 cerr << "server-type nonblocking requires transport of http or framed" << endl;
810 exit(1);
Roger Meier7e056e72011-07-17 07:28:28 +0000811 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000812 }
813
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100814 if (server.get() != NULL) {
Dave Watson792db4e2015-01-16 11:22:01 -0800815 if (protocol_type == "header") {
James E. King, IIIdf899132016-11-12 15:16:30 -0500816 // Tell the server to use the same protocol for input / output
Dave Watson792db4e2015-01-16 11:22:01 -0800817 // if using header
James E. King, III82ae9572017-08-05 12:23:54 -0400818 server->setOutputProtocolFactory(stdcxx::shared_ptr<TProtocolFactory>());
Dave Watson792db4e2015-01-16 11:22:01 -0800819 }
James E. King III9bea32f2018-03-16 16:07:42 -0400820
Jake Farrell5d02b802014-01-07 21:42:01 -0500821 apache::thrift::concurrency::PlatformThreadFactory factory;
822 factory.setDetached(false);
James E. King, III82ae9572017-08-05 12:23:54 -0400823 stdcxx::shared_ptr<apache::thrift::concurrency::Runnable> serverThreadRunner(server);
824 stdcxx::shared_ptr<apache::thrift::concurrency::Thread> thread
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100825 = factory.newThread(serverThreadRunner);
Jake Farrell5d02b802014-01-07 21:42:01 -0500826
James E. King III9bea32f2018-03-16 16:07:42 -0400827#ifdef HAVE_SIGNAL_H
828 signal(SIGINT, signal_handler);
829#endif
830
831 thread->start();
832 gMonitor.waitForever(); // wait for a shutdown signal
833
834#ifdef HAVE_SIGNAL_H
835 signal(SIGINT, SIG_DFL);
836#endif
Jake Farrell5d02b802014-01-07 21:42:01 -0500837
838 server->stop();
839 thread->join();
840 server.reset();
841 }
842
Roger Meierca142b02011-06-07 17:59:07 +0000843 cout << "done." << endl;
Mark Sleee8540632006-05-30 09:24:40 +0000844 return 0;
845}
James E. King III9bea32f2018-03-16 16:07:42 -0400846