blob: 1c381241027f6a930c38ddea21399c9ed1daca37 [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, III58402ff2017-11-17 14:41:46 -050042
43#include "SecondService.h"
Mark Slee95771002006-06-07 06:53:25 +000044#include "ThriftTest.h"
Marc Slemko6be374b2006-08-04 03:16:25 +000045
James E. King, III7edc8fa2017-01-20 10:11:41 -050046#ifdef HAVE_STDINT_H
47#include <stdint.h>
48#endif
49#ifdef HAVE_INTTYPES_H
50#include <inttypes.h>
51#endif
James E. King III9bea32f2018-03-16 16:07:42 -040052#ifdef HAVE_SIGNAL_H
53#include <signal.h>
54#endif
James E. King, III7edc8fa2017-01-20 10:11:41 -050055
Marc Slemko6be374b2006-08-04 03:16:25 +000056#include <iostream>
57#include <stdexcept>
58#include <sstream>
59
James E. King, III58402ff2017-11-17 14:41:46 -050060#include <boost/algorithm/string.hpp>
Roger Meierca142b02011-06-07 17:59:07 +000061#include <boost/program_options.hpp>
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +053062#include <boost/filesystem.hpp>
James E. King, III82ae9572017-08-05 12:23:54 -040063#include <thrift/stdcxx.h>
Roger Meierca142b02011-06-07 17:59:07 +000064
Jake Farrell5d02b802014-01-07 21:42:01 -050065#if _WIN32
Konrad Grochowski16a23a62014-11-13 15:33:38 +010066#include <thrift/windows/TWinsockSingleton.h>
Jake Farrell5d02b802014-01-07 21:42:01 -050067#endif
David Reissbc3dddb2007-08-22 23:20:24 +000068
Mark Sleee8540632006-05-30 09:24:40 +000069using namespace std;
70
T Jake Lucianib5e62212009-01-31 22:36:20 +000071using namespace apache::thrift;
James E. King, III82ae9572017-08-05 12:23:54 -040072using namespace apache::thrift::async;
T Jake Lucianib5e62212009-01-31 22:36:20 +000073using namespace apache::thrift::concurrency;
74using namespace apache::thrift::protocol;
75using namespace apache::thrift::transport;
76using namespace apache::thrift::server;
Marc Slemko6be374b2006-08-04 03:16:25 +000077
Marc Slemkobf4fd192006-08-15 21:29:39 +000078using namespace thrift::test;
79
James E. King III9bea32f2018-03-16 16:07:42 -040080// to handle a controlled shutdown, signal handling is mandatory
81#ifdef HAVE_SIGNAL_H
82apache::thrift::concurrency::Monitor gMonitor;
83void signal_handler(int signum)
84{
85 if (signum == SIGINT) {
86 gMonitor.notifyAll();
87 }
88}
89#endif
90
Mark Sleed2655522006-09-05 22:09:57 +000091class TestHandler : public ThriftTestIf {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010092public:
Mark Sleed2655522006-09-05 22:09:57 +000093 TestHandler() {}
Mark Sleee8540632006-05-30 09:24:40 +000094
Konrad Grochowski16a23a62014-11-13 15:33:38 +010095 void testVoid() { printf("testVoid()\n"); }
Mark Sleee8540632006-05-30 09:24:40 +000096
Konrad Grochowski16a23a62014-11-13 15:33:38 +010097 void testString(string& out, const string& thing) {
Mark Sleee8540632006-05-30 09:24:40 +000098 printf("testString(\"%s\")\n", thing.c_str());
Mark Slee1921d202007-01-24 19:43:06 +000099 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000100 }
101
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900102 bool testBool(const bool thing) {
103 printf("testBool(%s)\n", thing ? "true" : "false");
104 return thing;
105 }
106
Mark Slee1921d202007-01-24 19:43:06 +0000107 int8_t testByte(const int8_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000108 printf("testByte(%d)\n", (int)thing);
109 return thing;
110 }
111
Mark Slee1921d202007-01-24 19:43:06 +0000112 int32_t testI32(const int32_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000113 printf("testI32(%d)\n", thing);
114 return thing;
115 }
116
Mark Slee1921d202007-01-24 19:43:06 +0000117 int64_t testI64(const int64_t thing) {
Roger Meier0e814802014-01-17 21:07:58 +0100118 printf("testI64(%" PRId64 ")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +0000119 return thing;
120 }
121
Mark Slee1921d202007-01-24 19:43:06 +0000122 double testDouble(const double thing) {
Roger Meiera8cef6e2011-07-17 18:55:59 +0000123 printf("testDouble(%f)\n", thing);
Mark Sleec98d0502006-09-06 02:42:25 +0000124 return thing;
125 }
126
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100127 void testBinary(std::string& _return, const std::string& thing) {
128 std::ostringstream hexstr;
129 hexstr << std::hex << thing;
James E. King III3ae30422018-03-12 07:33:22 -0400130 printf("testBinary(%lu: %s)\n", safe_numeric_cast<unsigned long>(thing.size()), hexstr.str().c_str());
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100131 _return = thing;
132 }
133
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100134 void testStruct(Xtruct& out, const Xtruct& thing) {
135 printf("testStruct({\"%s\", %d, %d, %" PRId64 "})\n",
136 thing.string_thing.c_str(),
137 (int)thing.byte_thing,
138 thing.i32_thing,
139 thing.i64_thing);
Mark Slee1921d202007-01-24 19:43:06 +0000140 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000141 }
142
Mark Slee1921d202007-01-24 19:43:06 +0000143 void testNest(Xtruct2& out, const Xtruct2& nest) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100144 const Xtruct& thing = nest.struct_thing;
145 printf("testNest({%d, {\"%s\", %d, %d, %" PRId64 "}, %d})\n",
146 (int)nest.byte_thing,
147 thing.string_thing.c_str(),
148 (int)thing.byte_thing,
149 thing.i32_thing,
150 thing.i64_thing,
151 nest.i32_thing);
Mark Slee1921d202007-01-24 19:43:06 +0000152 out = nest;
Mark Sleee8540632006-05-30 09:24:40 +0000153 }
154
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100155 void testMap(map<int32_t, int32_t>& out, const map<int32_t, int32_t>& thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000156 printf("testMap({");
157 map<int32_t, int32_t>::const_iterator m_iter;
158 bool first = true;
159 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
160 if (first) {
161 first = false;
162 } else {
163 printf(", ");
164 }
165 printf("%d => %d", m_iter->first, m_iter->second);
166 }
167 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000168 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000169 }
170
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100171 void testStringMap(map<std::string, std::string>& out,
172 const map<std::string, std::string>& thing) {
Roger Meierd3b9dca2011-06-24 14:01:10 +0000173 printf("testMap({");
174 map<std::string, std::string>::const_iterator m_iter;
175 bool first = true;
176 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
177 if (first) {
178 first = false;
179 } else {
180 printf(", ");
181 }
182 printf("%s => %s", (m_iter->first).c_str(), (m_iter->second).c_str());
183 }
184 printf("})\n");
185 out = thing;
186 }
187
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100188 void testSet(set<int32_t>& out, const set<int32_t>& thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000189 printf("testSet({");
190 set<int32_t>::const_iterator s_iter;
191 bool first = true;
192 for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) {
193 if (first) {
194 first = false;
195 } else {
196 printf(", ");
197 }
198 printf("%d", *s_iter);
199 }
200 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000201 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000202 }
203
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100204 void testList(vector<int32_t>& out, const vector<int32_t>& thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000205 printf("testList({");
Mark Sleeb9acf982006-10-10 01:57:32 +0000206 vector<int32_t>::const_iterator l_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000207 bool first = true;
208 for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) {
209 if (first) {
210 first = false;
211 } else {
212 printf(", ");
213 }
214 printf("%d", *l_iter);
215 }
216 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000217 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000218 }
219
Bryan Duxbury833ae492010-09-27 17:26:02 +0000220 Numberz::type testEnum(const Numberz::type thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000221 printf("testEnum(%d)\n", thing);
222 return thing;
223 }
224
Mark Slee1921d202007-01-24 19:43:06 +0000225 UserId testTypedef(const UserId thing) {
Roger Meier0e814802014-01-17 21:07:58 +0100226 printf("testTypedef(%" PRId64 ")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +0000227 return thing;
228 }
229
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100230 void testMapMap(map<int32_t, map<int32_t, int32_t> >& mapmap, const int32_t hello) {
Mark Sleee8540632006-05-30 09:24:40 +0000231 printf("testMapMap(%d)\n", hello);
Mark Sleee8540632006-05-30 09:24:40 +0000232
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100233 map<int32_t, int32_t> pos;
234 map<int32_t, int32_t> neg;
Mark Sleee8540632006-05-30 09:24:40 +0000235 for (int i = 1; i < 5; i++) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100236 pos.insert(make_pair(i, i));
237 neg.insert(make_pair(-i, -i));
Mark Sleee8540632006-05-30 09:24:40 +0000238 }
239
240 mapmap.insert(make_pair(4, pos));
241 mapmap.insert(make_pair(-4, neg));
Mark Sleee8540632006-05-30 09:24:40 +0000242 }
243
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100244 void testInsanity(map<UserId, map<Numberz::type, Insanity> >& insane, const Insanity& argument) {
Mark Sleee8540632006-05-30 09:24:40 +0000245 printf("testInsanity()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000246
Mark Sleee8540632006-05-30 09:24:40 +0000247 Insanity looney;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000248 map<Numberz::type, Insanity> first_map;
249 map<Numberz::type, Insanity> second_map;
Mark Sleee8540632006-05-30 09:24:40 +0000250
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900251 first_map.insert(make_pair(Numberz::TWO, argument));
252 first_map.insert(make_pair(Numberz::THREE, argument));
Mark Sleee8540632006-05-30 09:24:40 +0000253
Bryan Duxbury833ae492010-09-27 17:26:02 +0000254 second_map.insert(make_pair(Numberz::SIX, looney));
Mark Sleee8540632006-05-30 09:24:40 +0000255
Mark Sleee8540632006-05-30 09:24:40 +0000256 insane.insert(make_pair(1, first_map));
257 insane.insert(make_pair(2, second_map));
258
259 printf("return");
260 printf(" = {");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100261 map<UserId, map<Numberz::type, Insanity> >::const_iterator i_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000262 for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) {
Roger Meier0e814802014-01-17 21:07:58 +0100263 printf("%" PRId64 " => {", i_iter->first);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100264 map<Numberz::type, Insanity>::const_iterator i2_iter;
265 for (i2_iter = i_iter->second.begin(); i2_iter != i_iter->second.end(); ++i2_iter) {
Mark Sleee8540632006-05-30 09:24:40 +0000266 printf("%d => {", i2_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000267 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
268 map<Numberz::type, UserId>::const_iterator um;
Mark Sleee8540632006-05-30 09:24:40 +0000269 printf("{");
270 for (um = userMap.begin(); um != userMap.end(); ++um) {
Roger Meier0e814802014-01-17 21:07:58 +0100271 printf("%d => %" PRId64 ", ", um->first, um->second);
Mark Sleee8540632006-05-30 09:24:40 +0000272 }
273 printf("}, ");
274
Mark Sleeb9acf982006-10-10 01:57:32 +0000275 vector<Xtruct> xtructs = i2_iter->second.xtructs;
276 vector<Xtruct>::const_iterator x;
Mark Sleee8540632006-05-30 09:24:40 +0000277 printf("{");
278 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100279 printf("{\"%s\", %d, %d, %" PRId64 "}, ",
280 x->string_thing.c_str(),
281 (int)x->byte_thing,
282 x->i32_thing,
283 x->i64_thing);
Mark Sleee8540632006-05-30 09:24:40 +0000284 }
285 printf("}");
286
287 printf("}, ");
288 }
289 printf("}, ");
290 }
291 printf("}\n");
Mark Sleee8540632006-05-30 09:24:40 +0000292 }
Marc Slemkoe6889de2006-08-12 00:32:53 +0000293
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100294 void testMulti(Xtruct& hello,
295 const int8_t arg0,
296 const int32_t arg1,
297 const int64_t arg2,
298 const std::map<int16_t, std::string>& arg3,
299 const Numberz::type arg4,
300 const UserId arg5) {
301 (void)arg3;
302 (void)arg4;
303 (void)arg5;
Jake Farrell5d02b802014-01-07 21:42:01 -0500304
Marc Slemkoe6889de2006-08-12 00:32:53 +0000305 printf("testMulti()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000306
Marc Slemkoe6889de2006-08-12 00:32:53 +0000307 hello.string_thing = "Hello2";
308 hello.byte_thing = arg0;
309 hello.i32_thing = arg1;
310 hello.i64_thing = (int64_t)arg2;
Marc Slemkoe6889de2006-08-12 00:32:53 +0000311 }
Marc Slemkobf4fd192006-08-15 21:29:39 +0000312
ben-craigfae08e72015-07-15 11:34:47 -0500313 void testException(const std::string& arg) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000314 printf("testException(%s)\n", arg.c_str());
Mark Sleed3d733a2006-09-01 22:19:06 +0000315 if (arg.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000316 Xception e;
317 e.errorCode = 1001;
David Reiss9813cbe2009-01-31 21:39:11 +0000318 e.message = arg;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000319 throw e;
Roger Meier1f8b48f2012-05-02 22:56:47 +0000320 } else if (arg.compare("TException") == 0) {
T Jake Lucianib5e62212009-01-31 22:36:20 +0000321 apache::thrift::TException e;
David Reiss55ff70f2008-06-11 00:58:25 +0000322 throw e;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000323 } else {
324 Xtruct result;
325 result.string_thing = arg;
326 return;
327 }
328 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000329
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100330 void testMultiException(Xtruct& result,
331 const std::string& arg0,
ben-craigfae08e72015-07-15 11:34:47 -0500332 const std::string& arg1) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000333
Marc Slemko71d4e472006-08-15 22:34:04 +0000334 printf("testMultiException(%s, %s)\n", arg0.c_str(), arg1.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000335
Mark Sleef5f2be42006-09-05 21:05:31 +0000336 if (arg0.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000337 Xception e;
338 e.errorCode = 1001;
339 e.message = "This is an Xception";
340 throw e;
Mark Sleef5f2be42006-09-05 21:05:31 +0000341 } else if (arg0.compare("Xception2") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000342 Xception2 e;
343 e.errorCode = 2002;
344 e.struct_thing.string_thing = "This is an Xception2";
345 throw e;
346 } else {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000347 result.string_thing = arg1;
Mark Slee1921d202007-01-24 19:43:06 +0000348 return;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000349 }
350 }
David Reiss2ab6fe82008-02-18 02:11:44 +0000351
James E. King, III58402ff2017-11-17 14:41:46 -0500352 void testOneway(const int32_t aNum) {
353 printf("testOneway(%d): call received\n", aNum);
David Reiss2ab6fe82008-02-18 02:11:44 +0000354 }
Mark Sleee8540632006-05-30 09:24:40 +0000355};
356
James E. King, III58402ff2017-11-17 14:41:46 -0500357class SecondHandler : public SecondServiceIf
358{
359 public:
360 void secondtestString(std::string& result, const std::string& thing)
361 { result = "testString(\"" + thing + "\")"; }
James E. King, III39eaae62017-11-19 20:17:33 -0500362};
James E. King, III58402ff2017-11-17 14:41:46 -0500363
David Reissd7192062010-10-06 17:09:33 +0000364class TestProcessorEventHandler : public TProcessorEventHandler {
David Reiss23248712010-10-06 17:10:08 +0000365 virtual void* getContext(const char* fn_name, void* serverContext) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100366 (void)serverContext;
David Reissd7192062010-10-06 17:09:33 +0000367 return new std::string(fn_name);
368 }
369 virtual void freeContext(void* ctx, const char* fn_name) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100370 (void)fn_name;
David Reissd7192062010-10-06 17:09:33 +0000371 delete static_cast<std::string*>(ctx);
372 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100373 virtual void preRead(void* ctx, const char* fn_name) { communicate("preRead", ctx, fn_name); }
David Reissef7200f2010-10-06 17:09:42 +0000374 virtual void postRead(void* ctx, const char* fn_name, uint32_t bytes) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100375 (void)bytes;
David Reissd7192062010-10-06 17:09:33 +0000376 communicate("postRead", ctx, fn_name);
377 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100378 virtual void preWrite(void* ctx, const char* fn_name) { communicate("preWrite", ctx, fn_name); }
David Reissef7200f2010-10-06 17:09:42 +0000379 virtual void postWrite(void* ctx, const char* fn_name, uint32_t bytes) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100380 (void)bytes;
David Reissd7192062010-10-06 17:09:33 +0000381 communicate("postWrite", ctx, fn_name);
382 }
383 virtual void asyncComplete(void* ctx, const char* fn_name) {
384 communicate("asyncComplete", ctx, fn_name);
385 }
386 virtual void handlerError(void* ctx, const char* fn_name) {
387 communicate("handlerError", ctx, fn_name);
388 }
389
390 void communicate(const char* event, void* ctx, const char* fn_name) {
391 std::cout << event << ": " << *static_cast<std::string*>(ctx) << " = " << fn_name << std::endl;
392 }
393};
394
Roger Meier7e056e72011-07-17 07:28:28 +0000395class TestHandlerAsync : public ThriftTestCobSvIf {
396public:
James E. King, III82ae9572017-08-05 12:23:54 -0400397 TestHandlerAsync(stdcxx::shared_ptr<TestHandler>& handler) : _delegate(handler) {}
Roger Meier7e056e72011-07-17 07:28:28 +0000398 virtual ~TestHandlerAsync() {}
399
James E. King, III82ae9572017-08-05 12:23:54 -0400400 virtual void testVoid(stdcxx::function<void()> cob) {
Roger Meier7e056e72011-07-17 07:28:28 +0000401 _delegate->testVoid();
402 cob();
403 }
404
James E. King, III82ae9572017-08-05 12:23:54 -0400405 virtual void testString(stdcxx::function<void(std::string const& _return)> cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100406 const std::string& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000407 std::string res;
408 _delegate->testString(res, thing);
409 cob(res);
410 }
411
James E. King, III82ae9572017-08-05 12:23:54 -0400412 virtual void testBool(stdcxx::function<void(bool const& _return)> cob, const bool thing) {
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900413 bool res = _delegate->testBool(thing);
414 cob(res);
415 }
416
James E. King, III82ae9572017-08-05 12:23:54 -0400417 virtual void testByte(stdcxx::function<void(int8_t const& _return)> cob, const int8_t thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000418 int8_t res = _delegate->testByte(thing);
419 cob(res);
420 }
421
James E. King, III82ae9572017-08-05 12:23:54 -0400422 virtual void testI32(stdcxx::function<void(int32_t const& _return)> cob, const int32_t thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000423 int32_t res = _delegate->testI32(thing);
424 cob(res);
425 }
426
James E. King, III82ae9572017-08-05 12:23:54 -0400427 virtual void testI64(stdcxx::function<void(int64_t const& _return)> cob, const int64_t thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000428 int64_t res = _delegate->testI64(thing);
429 cob(res);
430 }
431
James E. King, III82ae9572017-08-05 12:23:54 -0400432 virtual void testDouble(stdcxx::function<void(double const& _return)> cob, const double thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000433 double res = _delegate->testDouble(thing);
434 cob(res);
435 }
436
James E. King, III82ae9572017-08-05 12:23:54 -0400437 virtual void testBinary(stdcxx::function<void(std::string const& _return)> cob,
Konrad Grochowski1f6e3802015-05-18 18:10:06 +0200438 const std::string& thing) {
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100439 std::string res;
440 _delegate->testBinary(res, thing);
441 cob(res);
442 }
443
James E. King, III82ae9572017-08-05 12:23:54 -0400444 virtual void testStruct(stdcxx::function<void(Xtruct const& _return)> cob, const Xtruct& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000445 Xtruct res;
446 _delegate->testStruct(res, thing);
447 cob(res);
448 }
449
James E. King, III82ae9572017-08-05 12:23:54 -0400450 virtual void testNest(stdcxx::function<void(Xtruct2 const& _return)> cob, const Xtruct2& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000451 Xtruct2 res;
452 _delegate->testNest(res, thing);
453 cob(res);
454 }
455
James E. King, III82ae9572017-08-05 12:23:54 -0400456 virtual void testMap(stdcxx::function<void(std::map<int32_t, int32_t> const& _return)> cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100457 const std::map<int32_t, int32_t>& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000458 std::map<int32_t, int32_t> res;
459 _delegate->testMap(res, thing);
460 cob(res);
461 }
462
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100463 virtual void testStringMap(
James E. King, III82ae9572017-08-05 12:23:54 -0400464 stdcxx::function<void(std::map<std::string, std::string> const& _return)> cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100465 const std::map<std::string, std::string>& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000466 std::map<std::string, std::string> res;
467 _delegate->testStringMap(res, thing);
468 cob(res);
469 }
470
James E. King, III82ae9572017-08-05 12:23:54 -0400471 virtual void testSet(stdcxx::function<void(std::set<int32_t> const& _return)> cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100472 const std::set<int32_t>& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000473 std::set<int32_t> res;
474 _delegate->testSet(res, thing);
475 cob(res);
476 }
477
James E. King, III82ae9572017-08-05 12:23:54 -0400478 virtual void testList(stdcxx::function<void(std::vector<int32_t> const& _return)> cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100479 const std::vector<int32_t>& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000480 std::vector<int32_t> res;
481 _delegate->testList(res, thing);
482 cob(res);
483 }
484
James E. King, III82ae9572017-08-05 12:23:54 -0400485 virtual void testEnum(stdcxx::function<void(Numberz::type const& _return)> cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100486 const Numberz::type thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000487 Numberz::type res = _delegate->testEnum(thing);
488 cob(res);
489 }
490
James E. King, III82ae9572017-08-05 12:23:54 -0400491 virtual void testTypedef(stdcxx::function<void(UserId const& _return)> cob, const UserId thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000492 UserId res = _delegate->testTypedef(thing);
493 cob(res);
494 }
495
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100496 virtual void testMapMap(
James E. King, III82ae9572017-08-05 12:23:54 -0400497 stdcxx::function<void(std::map<int32_t, std::map<int32_t, int32_t> > const& _return)> cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100498 const int32_t hello) {
Roger Meier7e056e72011-07-17 07:28:28 +0000499 std::map<int32_t, std::map<int32_t, int32_t> > res;
500 _delegate->testMapMap(res, hello);
501 cob(res);
502 }
503
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100504 virtual void testInsanity(
James E. King, III82ae9572017-08-05 12:23:54 -0400505 stdcxx::function<void(std::map<UserId, std::map<Numberz::type, Insanity> > const& _return)> cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100506 const Insanity& argument) {
Jake Farrell5d02b802014-01-07 21:42:01 -0500507 std::map<UserId, std::map<Numberz::type, Insanity> > res;
Roger Meier7e056e72011-07-17 07:28:28 +0000508 _delegate->testInsanity(res, argument);
509 cob(res);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100510 }
Roger Meier7e056e72011-07-17 07:28:28 +0000511
James E. King, III82ae9572017-08-05 12:23:54 -0400512 virtual void testMulti(stdcxx::function<void(Xtruct const& _return)> cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100513 const int8_t arg0,
514 const int32_t arg1,
515 const int64_t arg2,
516 const std::map<int16_t, std::string>& arg3,
517 const Numberz::type arg4,
518 const UserId arg5) {
Roger Meier7e056e72011-07-17 07:28:28 +0000519 Xtruct res;
520 _delegate->testMulti(res, arg0, arg1, arg2, arg3, arg4, arg5);
521 cob(res);
522 }
523
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100524 virtual void testException(
James E. King, III82ae9572017-08-05 12:23:54 -0400525 stdcxx::function<void()> cob,
526 stdcxx::function<void(::apache::thrift::TDelayedException* _throw)> exn_cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100527 const std::string& arg) {
Roger Meier7e056e72011-07-17 07:28:28 +0000528 try {
529 _delegate->testException(arg);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100530 } catch (const apache::thrift::TException& e) {
Roger Meier7e056e72011-07-17 07:28:28 +0000531 exn_cob(apache::thrift::TDelayedException::delayException(e));
532 return;
533 }
534 cob();
535 }
536
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100537 virtual void testMultiException(
James E. King, III82ae9572017-08-05 12:23:54 -0400538 stdcxx::function<void(Xtruct const& _return)> cob,
539 stdcxx::function<void(::apache::thrift::TDelayedException* _throw)> exn_cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100540 const std::string& arg0,
541 const std::string& arg1) {
Roger Meier7e056e72011-07-17 07:28:28 +0000542 Xtruct res;
543 try {
544 _delegate->testMultiException(res, arg0, arg1);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100545 } catch (const apache::thrift::TException& e) {
Roger Meier7e056e72011-07-17 07:28:28 +0000546 exn_cob(apache::thrift::TDelayedException::delayException(e));
547 return;
548 }
549 cob(res);
550 }
551
James E. King, III82ae9572017-08-05 12:23:54 -0400552 virtual void testOneway(stdcxx::function<void()> cob, const int32_t secondsToSleep) {
Roger Meier7e056e72011-07-17 07:28:28 +0000553 _delegate->testOneway(secondsToSleep);
554 cob();
555 }
556
557protected:
James E. King, III82ae9572017-08-05 12:23:54 -0400558 stdcxx::shared_ptr<TestHandler> _delegate;
Roger Meier7e056e72011-07-17 07:28:28 +0000559};
560
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900561namespace po = boost::program_options;
562
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100563int main(int argc, char** argv) {
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +0530564
Nobuaki Sukegawac8744082016-03-09 19:55:56 +0900565 string testDir = boost::filesystem::system_complete(argv[0]).parent_path().parent_path().parent_path().string();
566 string certPath = testDir + "/keys/server.crt";
567 string keyPath = testDir + "/keys/server.key";
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +0530568
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100569#if _WIN32
Jake Farrell5d02b802014-01-07 21:42:01 -0500570 transport::TWinsockSingleton::create();
571#endif
Mark Sleee8540632006-05-30 09:24:40 +0000572 int port = 9090;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000573 bool ssl = false;
Roger Meierca142b02011-06-07 17:59:07 +0000574 string transport_type = "buffered";
575 string protocol_type = "binary";
576 string server_type = "simple";
577 string domain_socket = "";
pavlodd08f6e2015-10-08 16:43:56 -0400578 bool abstract_namespace = false;
Roger Meierca142b02011-06-07 17:59:07 +0000579 size_t workers = 4;
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900580 int string_limit = 0;
581 int container_limit = 0;
Marc Slemko6be374b2006-08-04 03:16:25 +0000582
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900583 po::options_description desc("Allowed options");
584 desc.add_options()
585 ("help,h", "produce help message")
586 ("port", po::value<int>(&port)->default_value(port), "Port number to listen")
587 ("domain-socket", po::value<string>(&domain_socket) ->default_value(domain_socket), "Unix Domain Socket (e.g. /tmp/ThriftTest.thrift)")
588 ("abstract-namespace", "Create the domain socket in the Abstract Namespace (no connection with filesystem pathnames)")
589 ("server-type", po::value<string>(&server_type)->default_value(server_type), "type of server, \"simple\", \"thread-pool\", \"threaded\", or \"nonblocking\"")
590 ("transport", po::value<string>(&transport_type)->default_value(transport_type), "transport: buffered, framed, http")
James E. King, III58402ff2017-11-17 14:41:46 -0500591 ("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 +0900592 ("ssl", "Encrypted Transport using SSL")
593 ("processor-events", "processor-events")
594 ("workers,n", po::value<size_t>(&workers)->default_value(workers), "Number of thread pools workers. Only valid for thread-pool server type")
595 ("string-limit", po::value<int>(&string_limit))
596 ("container-limit", po::value<int>(&container_limit));
Marc Slemko6be374b2006-08-04 03:16:25 +0000597
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900598 po::variables_map vm;
599 po::store(po::parse_command_line(argc, argv, desc), vm);
600 po::notify(vm);
Marc Slemko6be374b2006-08-04 03:16:25 +0000601
Roger Meierca142b02011-06-07 17:59:07 +0000602 if (vm.count("help")) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100603 cout << desc << "\n";
604 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000605 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500606
Marc Slemko6be374b2006-08-04 03:16:25 +0000607 try {
Roger Meierca142b02011-06-07 17:59:07 +0000608 if (!server_type.empty()) {
609 if (server_type == "simple") {
610 } else if (server_type == "thread-pool") {
611 } else if (server_type == "threaded") {
612 } else if (server_type == "nonblocking") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000613 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100614 throw invalid_argument("Unknown server type " + server_type);
Roger Meierca142b02011-06-07 17:59:07 +0000615 }
616 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500617
Roger Meierca142b02011-06-07 17:59:07 +0000618 if (!protocol_type.empty()) {
619 if (protocol_type == "binary") {
Roger Meier284101c2014-03-11 21:20:35 +0100620 } else if (protocol_type == "compact") {
Roger Meierca142b02011-06-07 17:59:07 +0000621 } else if (protocol_type == "json") {
Dave Watson792db4e2015-01-16 11:22:01 -0800622 } else if (protocol_type == "header") {
James E. King, III39eaae62017-11-19 20:17:33 -0500623 } else if (protocol_type == "multi") { // multiplexed binary
624 } else if (protocol_type == "multic") { // multiplexed compact
625 } else if (protocol_type == "multih") { // multiplexed header
626 } else if (protocol_type == "multij") { // multiplexed json
Roger Meierca142b02011-06-07 17:59:07 +0000627 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100628 throw invalid_argument("Unknown protocol type " + protocol_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000629 }
630 }
631
Roger Meier284101c2014-03-11 21:20:35 +0100632 if (!transport_type.empty()) {
Roger Meierca142b02011-06-07 17:59:07 +0000633 if (transport_type == "buffered") {
634 } else if (transport_type == "framed") {
635 } else if (transport_type == "http") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000636 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100637 throw invalid_argument("Unknown transport type " + transport_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000638 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000639 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000640
Bryan Duxbury833ae492010-09-27 17:26:02 +0000641 } catch (std::exception& e) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000642 cerr << e.what() << endl;
Roger Meierca142b02011-06-07 17:59:07 +0000643 cout << desc << "\n";
644 return 1;
Marc Slemko6be374b2006-08-04 03:16:25 +0000645 }
646
Roger Meierca142b02011-06-07 17:59:07 +0000647 if (vm.count("ssl")) {
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000648 ssl = true;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000649 }
650
James E. King III9bea32f2018-03-16 16:07:42 -0400651#if defined(HAVE_SIGNAL_H) && defined(SIGPIPE)
652 if (ssl) {
653 signal(SIGPIPE, SIG_IGN); // for OpenSSL, otherwise we end abruptly
654 }
655#endif
656
pavlodd08f6e2015-10-08 16:43:56 -0400657 if (vm.count("abstract-namespace")) {
658 abstract_namespace = true;
659 }
660
Mark Sleee8540632006-05-30 09:24:40 +0000661 // Dispatcher
James E. King, III82ae9572017-08-05 12:23:54 -0400662 stdcxx::shared_ptr<TProtocolFactory> protocolFactory;
James E. King, III58402ff2017-11-17 14:41:46 -0500663 if (protocol_type == "json" || protocol_type == "multij") {
James E. King, III82ae9572017-08-05 12:23:54 -0400664 stdcxx::shared_ptr<TProtocolFactory> jsonProtocolFactory(new TJSONProtocolFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000665 protocolFactory = jsonProtocolFactory;
James E. King, III58402ff2017-11-17 14:41:46 -0500666 } else if (protocol_type == "compact" || protocol_type == "multic") {
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900667 TCompactProtocolFactoryT<TBufferBase> *compactProtocolFactory = new TCompactProtocolFactoryT<TBufferBase>();
668 compactProtocolFactory->setContainerSizeLimit(container_limit);
669 compactProtocolFactory->setStringSizeLimit(string_limit);
670 protocolFactory.reset(compactProtocolFactory);
James E. King, III58402ff2017-11-17 14:41:46 -0500671 } else if (protocol_type == "header" || protocol_type == "multih") {
James E. King, III82ae9572017-08-05 12:23:54 -0400672 stdcxx::shared_ptr<TProtocolFactory> headerProtocolFactory(new THeaderProtocolFactory());
Dave Watson792db4e2015-01-16 11:22:01 -0800673 protocolFactory = headerProtocolFactory;
Roger Meierca142b02011-06-07 17:59:07 +0000674 } else {
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900675 TBinaryProtocolFactoryT<TBufferBase>* binaryProtocolFactory = new TBinaryProtocolFactoryT<TBufferBase>();
676 binaryProtocolFactory->setContainerSizeLimit(container_limit);
677 binaryProtocolFactory->setStringSizeLimit(string_limit);
678 protocolFactory.reset(binaryProtocolFactory);
Roger Meierca142b02011-06-07 17:59:07 +0000679 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000680
James E. King, III58402ff2017-11-17 14:41:46 -0500681 // Processors
James E. King, III82ae9572017-08-05 12:23:54 -0400682 stdcxx::shared_ptr<TestHandler> testHandler(new TestHandler());
James E. King, III58402ff2017-11-17 14:41:46 -0500683 stdcxx::shared_ptr<TProcessor> testProcessor(new ThriftTestProcessor(testHandler));
Jake Farrell5d02b802014-01-07 21:42:01 -0500684
Roger Meierca142b02011-06-07 17:59:07 +0000685 if (vm.count("processor-events")) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100686 testProcessor->setEventHandler(
James E. King, III82ae9572017-08-05 12:23:54 -0400687 stdcxx::shared_ptr<TProcessorEventHandler>(new TestProcessorEventHandler()));
David Reissd7192062010-10-06 17:09:33 +0000688 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500689
Mark Sleee8540632006-05-30 09:24:40 +0000690 // Transport
James E. King, III82ae9572017-08-05 12:23:54 -0400691 stdcxx::shared_ptr<TSSLSocketFactory> sslSocketFactory;
692 stdcxx::shared_ptr<TServerSocket> serverSocket;
Mark Sleee8540632006-05-30 09:24:40 +0000693
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000694 if (ssl) {
James E. King, III82ae9572017-08-05 12:23:54 -0400695 sslSocketFactory = stdcxx::shared_ptr<TSSLSocketFactory>(new TSSLSocketFactory());
Nobuaki Sukegawac8744082016-03-09 19:55:56 +0900696 sslSocketFactory->loadCertificate(certPath.c_str());
697 sslSocketFactory->loadPrivateKey(keyPath.c_str());
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000698 sslSocketFactory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
James E. King III70b33fb2018-03-11 10:57:10 -0400699 if (server_type != "nonblocking") {
700 serverSocket = stdcxx::shared_ptr<TServerSocket>(new TSSLServerSocket(port, sslSocketFactory));
701 }
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000702 } else {
Roger Meierc94b2932014-02-22 20:07:33 +0100703 if (domain_socket != "") {
pavlodd08f6e2015-10-08 16:43:56 -0400704 if (abstract_namespace) {
705 std::string abstract_socket("\0", 1);
706 abstract_socket += domain_socket;
James E. King, III82ae9572017-08-05 12:23:54 -0400707 serverSocket = stdcxx::shared_ptr<TServerSocket>(new TServerSocket(abstract_socket));
pavlodd08f6e2015-10-08 16:43:56 -0400708 } else {
709 unlink(domain_socket.c_str());
James E. King, III82ae9572017-08-05 12:23:54 -0400710 serverSocket = stdcxx::shared_ptr<TServerSocket>(new TServerSocket(domain_socket));
pavlodd08f6e2015-10-08 16:43:56 -0400711 }
Roger Meierc94b2932014-02-22 20:07:33 +0100712 port = 0;
713 } else {
James E. King, III82ae9572017-08-05 12:23:54 -0400714 serverSocket = stdcxx::shared_ptr<TServerSocket>(new TServerSocket(port));
Roger Meierc94b2932014-02-22 20:07:33 +0100715 }
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000716 }
Roger Meierca142b02011-06-07 17:59:07 +0000717
Mark Sleed788b2e2006-09-07 01:26:35 +0000718 // Factory
James E. King, III82ae9572017-08-05 12:23:54 -0400719 stdcxx::shared_ptr<TTransportFactory> transportFactory;
Jake Farrell5d02b802014-01-07 21:42:01 -0500720
Roger Meier7e056e72011-07-17 07:28:28 +0000721 if (transport_type == "http" && server_type != "nonblocking") {
James E. King, III82ae9572017-08-05 12:23:54 -0400722 stdcxx::shared_ptr<TTransportFactory> httpTransportFactory(new THttpServerTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000723 transportFactory = httpTransportFactory;
724 } else if (transport_type == "framed") {
James E. King, III82ae9572017-08-05 12:23:54 -0400725 stdcxx::shared_ptr<TTransportFactory> framedTransportFactory(new TFramedTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000726 transportFactory = framedTransportFactory;
727 } else {
James E. King, III82ae9572017-08-05 12:23:54 -0400728 stdcxx::shared_ptr<TTransportFactory> bufferedTransportFactory(new TBufferedTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000729 transportFactory = bufferedTransportFactory;
730 }
Mark Sleed788b2e2006-09-07 01:26:35 +0000731
Roger Meierca142b02011-06-07 17:59:07 +0000732 // Server Info
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100733 cout << "Starting \"" << server_type << "\" server (" << transport_type << "/" << protocol_type
pavlodd08f6e2015-10-08 16:43:56 -0400734 << ") listen on: ";
735 if (abstract_namespace) {
736 cout << '@';
737 }
738 cout << domain_socket;
Roger Meierca142b02011-06-07 17:59:07 +0000739 if (port != 0) {
740 cout << port;
741 }
742 cout << endl;
Mark Sleee8540632006-05-30 09:24:40 +0000743
James E. King, III58402ff2017-11-17 14:41:46 -0500744 // Multiplexed Processor if needed
745 if (boost::starts_with(protocol_type, "multi")) {
746 stdcxx::shared_ptr<SecondHandler> secondHandler(new SecondHandler());
747 stdcxx::shared_ptr<SecondServiceProcessor> secondProcessor(new SecondServiceProcessor(secondHandler));
748
749 stdcxx::shared_ptr<TMultiplexedProcessor> multiplexedProcessor(new TMultiplexedProcessor());
750 multiplexedProcessor->registerDefault(testProcessor); // non-multi clients go to the default processor (multi:binary, multic:compact, ...)
751 multiplexedProcessor->registerProcessor("ThriftTest", testProcessor);
752 multiplexedProcessor->registerProcessor("SecondService", secondProcessor);
753 testProcessor = stdcxx::dynamic_pointer_cast<TProcessor>(multiplexedProcessor);
754 }
755
Roger Meierca142b02011-06-07 17:59:07 +0000756 // Server
James E. King, III82ae9572017-08-05 12:23:54 -0400757 stdcxx::shared_ptr<apache::thrift::server::TServer> server;
Jake Farrell5d02b802014-01-07 21:42:01 -0500758
Roger Meierca142b02011-06-07 17:59:07 +0000759 if (server_type == "simple") {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100760 server.reset(new TSimpleServer(testProcessor, serverSocket, transportFactory, protocolFactory));
Roger Meierca142b02011-06-07 17:59:07 +0000761 } else if (server_type == "thread-pool") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000762
James E. King, III82ae9572017-08-05 12:23:54 -0400763 stdcxx::shared_ptr<PlatformThreadFactory> threadFactory
764 = stdcxx::shared_ptr<PlatformThreadFactory>(new PlatformThreadFactory());
Marc Slemko6be374b2006-08-04 03:16:25 +0000765
James E. King III70b33fb2018-03-11 10:57:10 -0400766 stdcxx::shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workers);
Marc Slemko6be374b2006-08-04 03:16:25 +0000767 threadManager->threadFactory(threadFactory);
Marc Slemko6be374b2006-08-04 03:16:25 +0000768 threadManager->start();
769
Jake Farrell5d02b802014-01-07 21:42:01 -0500770 server.reset(new TThreadPoolServer(testProcessor,
Roger Meierca142b02011-06-07 17:59:07 +0000771 serverSocket,
Mark Sleed788b2e2006-09-07 01:26:35 +0000772 transportFactory,
Mark Sleea3302652006-10-25 19:03:32 +0000773 protocolFactory,
Jake Farrell5d02b802014-01-07 21:42:01 -0500774 threadManager));
Roger Meierca142b02011-06-07 17:59:07 +0000775 } else if (server_type == "threaded") {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100776 server.reset(
777 new TThreadedServer(testProcessor, serverSocket, transportFactory, protocolFactory));
Roger Meierca142b02011-06-07 17:59:07 +0000778 } else if (server_type == "nonblocking") {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100779 if (transport_type == "http") {
James E. King, III82ae9572017-08-05 12:23:54 -0400780 stdcxx::shared_ptr<TestHandlerAsync> testHandlerAsync(new TestHandlerAsync(testHandler));
781 stdcxx::shared_ptr<TAsyncProcessor> testProcessorAsync(
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100782 new ThriftTestAsyncProcessor(testHandlerAsync));
James E. King, III82ae9572017-08-05 12:23:54 -0400783 stdcxx::shared_ptr<TAsyncBufferProcessor> testBufferProcessor(
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100784 new TAsyncProtocolProcessor(testProcessorAsync, protocolFactory));
Jake Farrell5d02b802014-01-07 21:42:01 -0500785
786 // not loading nonblockingServer into "server" because
787 // TEvhttpServer doesn't inherit from TServer, and doesn't
788 // provide a stop method.
Roger Meier7e056e72011-07-17 07:28:28 +0000789 TEvhttpServer nonblockingServer(testBufferProcessor, port);
790 nonblockingServer.serve();
James E. King III70b33fb2018-03-11 10:57:10 -0400791 } else if (transport_type == "framed") {
James E. King III9bea32f2018-03-16 16:07:42 -0400792 stdcxx::shared_ptr<transport::TNonblockingServerTransport> nbSocket;
793 nbSocket.reset(
794 ssl ? new transport::TNonblockingSSLServerSocket(port, sslSocketFactory)
795 : new transport::TNonblockingServerSocket(port));
Divya Thaluru808d1432017-08-06 16:36:36 -0700796 server.reset(new TNonblockingServer(testProcessor, protocolFactory, nbSocket));
James E. King III70b33fb2018-03-11 10:57:10 -0400797 } else {
James E. King III9bea32f2018-03-16 16:07:42 -0400798 cerr << "server-type nonblocking requires transport of http or framed" << endl;
799 exit(1);
Roger Meier7e056e72011-07-17 07:28:28 +0000800 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000801 }
802
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100803 if (server.get() != NULL) {
Dave Watson792db4e2015-01-16 11:22:01 -0800804 if (protocol_type == "header") {
James E. King, IIIdf899132016-11-12 15:16:30 -0500805 // Tell the server to use the same protocol for input / output
Dave Watson792db4e2015-01-16 11:22:01 -0800806 // if using header
James E. King, III82ae9572017-08-05 12:23:54 -0400807 server->setOutputProtocolFactory(stdcxx::shared_ptr<TProtocolFactory>());
Dave Watson792db4e2015-01-16 11:22:01 -0800808 }
James E. King III9bea32f2018-03-16 16:07:42 -0400809
Jake Farrell5d02b802014-01-07 21:42:01 -0500810 apache::thrift::concurrency::PlatformThreadFactory factory;
811 factory.setDetached(false);
James E. King, III82ae9572017-08-05 12:23:54 -0400812 stdcxx::shared_ptr<apache::thrift::concurrency::Runnable> serverThreadRunner(server);
813 stdcxx::shared_ptr<apache::thrift::concurrency::Thread> thread
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100814 = factory.newThread(serverThreadRunner);
Jake Farrell5d02b802014-01-07 21:42:01 -0500815
James E. King III9bea32f2018-03-16 16:07:42 -0400816#ifdef HAVE_SIGNAL_H
817 signal(SIGINT, signal_handler);
818#endif
819
820 thread->start();
821 gMonitor.waitForever(); // wait for a shutdown signal
822
823#ifdef HAVE_SIGNAL_H
824 signal(SIGINT, SIG_DFL);
825#endif
Jake Farrell5d02b802014-01-07 21:42:01 -0500826
827 server->stop();
828 thread->join();
829 server.reset();
830 }
831
Roger Meierca142b02011-06-07 17:59:07 +0000832 cout << "done." << endl;
Mark Sleee8540632006-05-30 09:24:40 +0000833 return 0;
834}
James E. King III9bea32f2018-03-16 16:07:42 -0400835