blob: 8d5b4d93e2fede9c625b4dc9c797460e14557a1d [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>
cyyca8af9b2019-01-11 22:13:12 +080023#include <thrift/concurrency/ThreadFactory.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>
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:
Sebastian Zenker042580f2019-01-29 15:48:12 +010093 TestHandler() = default;
Mark Sleee8540632006-05-30 09:24:40 +000094
Sebastian Zenker042580f2019-01-29 15:48:12 +010095 void testVoid() override { printf("testVoid()\n"); }
Mark Sleee8540632006-05-30 09:24:40 +000096
Sebastian Zenker042580f2019-01-29 15:48:12 +010097 void testString(string& out, const string& thing) override {
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
Sebastian Zenker042580f2019-01-29 15:48:12 +0100102 bool testBool(const bool thing) override {
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900103 printf("testBool(%s)\n", thing ? "true" : "false");
104 return thing;
105 }
106
Sebastian Zenker042580f2019-01-29 15:48:12 +0100107 int8_t testByte(const int8_t thing) override {
Mark Sleee8540632006-05-30 09:24:40 +0000108 printf("testByte(%d)\n", (int)thing);
109 return thing;
110 }
111
Sebastian Zenker042580f2019-01-29 15:48:12 +0100112 int32_t testI32(const int32_t thing) override {
Mark Sleee8540632006-05-30 09:24:40 +0000113 printf("testI32(%d)\n", thing);
114 return thing;
115 }
116
Sebastian Zenker042580f2019-01-29 15:48:12 +0100117 int64_t testI64(const int64_t thing) override {
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
Sebastian Zenker042580f2019-01-29 15:48:12 +0100122 double testDouble(const double thing) override {
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
Sebastian Zenker042580f2019-01-29 15:48:12 +0100127 void testBinary(std::string& _return, const std::string& thing) override {
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100128 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
Sebastian Zenker042580f2019-01-29 15:48:12 +0100134 void testStruct(Xtruct& out, const Xtruct& thing) override {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100135 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
Sebastian Zenker042580f2019-01-29 15:48:12 +0100143 void testNest(Xtruct2& out, const Xtruct2& nest) override {
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
Sebastian Zenker042580f2019-01-29 15:48:12 +0100155 void testMap(map<int32_t, int32_t>& out, const map<int32_t, int32_t>& thing) override {
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,
Sebastian Zenker042580f2019-01-29 15:48:12 +0100172 const map<std::string, std::string>& thing) override {
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
Sebastian Zenker042580f2019-01-29 15:48:12 +0100188 void testSet(set<int32_t>& out, const set<int32_t>& thing) override {
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
Sebastian Zenker042580f2019-01-29 15:48:12 +0100204 void testList(vector<int32_t>& out, const vector<int32_t>& thing) override {
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
Sebastian Zenker042580f2019-01-29 15:48:12 +0100220 Numberz::type testEnum(const Numberz::type thing) override {
Mark Sleee8540632006-05-30 09:24:40 +0000221 printf("testEnum(%d)\n", thing);
222 return thing;
223 }
224
Sebastian Zenker042580f2019-01-29 15:48:12 +0100225 UserId testTypedef(const UserId thing) override {
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
Sebastian Zenker042580f2019-01-29 15:48:12 +0100230 void testMapMap(map<int32_t, map<int32_t, int32_t> >& mapmap, const int32_t hello) override {
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
Sebastian Zenker042580f2019-01-29 15:48:12 +0100244 void testInsanity(map<UserId, map<Numberz::type, Insanity> >& insane, const Insanity& argument) override {
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,
Sebastian Zenker042580f2019-01-29 15:48:12 +0100300 const UserId arg5) override {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100301 (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
Sebastian Zenker042580f2019-01-29 15:48:12 +0100313 void testException(const std::string& arg) override {
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,
Sebastian Zenker042580f2019-01-29 15:48:12 +0100332 const std::string& arg1) override {
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
Sebastian Zenker042580f2019-01-29 15:48:12 +0100352 void testOneway(const int32_t aNum) override {
James E. King, III58402ff2017-11-17 14:41:46 -0500353 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:
Sebastian Zenker042580f2019-01-29 15:48:12 +0100360 void secondtestString(std::string& result, const std::string& thing) override
James E. King, III58402ff2017-11-17 14:41:46 -0500361 { 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 {
Sebastian Zenker042580f2019-01-29 15:48:12 +0100365 void* getContext(const char* fn_name, void* serverContext) override {
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 }
Sebastian Zenker042580f2019-01-29 15:48:12 +0100369 void freeContext(void* ctx, const char* fn_name) override {
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 }
Sebastian Zenker042580f2019-01-29 15:48:12 +0100373 void preRead(void* ctx, const char* fn_name) override { communicate("preRead", ctx, fn_name); }
374 void postRead(void* ctx, const char* fn_name, uint32_t bytes) override {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100375 (void)bytes;
David Reissd7192062010-10-06 17:09:33 +0000376 communicate("postRead", ctx, fn_name);
377 }
Sebastian Zenker042580f2019-01-29 15:48:12 +0100378 void preWrite(void* ctx, const char* fn_name) override { communicate("preWrite", ctx, fn_name); }
379 void postWrite(void* ctx, const char* fn_name, uint32_t bytes) override {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100380 (void)bytes;
David Reissd7192062010-10-06 17:09:33 +0000381 communicate("postWrite", ctx, fn_name);
382 }
Sebastian Zenker042580f2019-01-29 15:48:12 +0100383 void asyncComplete(void* ctx, const char* fn_name) override {
David Reissd7192062010-10-06 17:09:33 +0000384 communicate("asyncComplete", ctx, fn_name);
385 }
Sebastian Zenker042580f2019-01-29 15:48:12 +0100386 void handlerError(void* ctx, const char* fn_name) override {
David Reissd7192062010-10-06 17:09:33 +0000387 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:
cyy316723a2019-01-05 16:35:14 +0800397 TestHandlerAsync(std::shared_ptr<TestHandler>& handler) : _delegate(handler) {}
Sebastian Zenker042580f2019-01-29 15:48:12 +0100398 ~TestHandlerAsync() override = default;
Roger Meier7e056e72011-07-17 07:28:28 +0000399
Sebastian Zenker042580f2019-01-29 15:48:12 +0100400 void testVoid(std::function<void()> cob) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000401 _delegate->testVoid();
402 cob();
403 }
404
Sebastian Zenker042580f2019-01-29 15:48:12 +0100405 void testString(std::function<void(std::string const& _return)> cob,
406 const std::string& thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000407 std::string res;
408 _delegate->testString(res, thing);
409 cob(res);
410 }
411
Sebastian Zenker042580f2019-01-29 15:48:12 +0100412 void testBool(std::function<void(bool const& _return)> cob, const bool thing) override {
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900413 bool res = _delegate->testBool(thing);
414 cob(res);
415 }
416
Sebastian Zenker042580f2019-01-29 15:48:12 +0100417 void testByte(std::function<void(int8_t const& _return)> cob, const int8_t thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000418 int8_t res = _delegate->testByte(thing);
419 cob(res);
420 }
421
Sebastian Zenker042580f2019-01-29 15:48:12 +0100422 void testI32(std::function<void(int32_t const& _return)> cob, const int32_t thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000423 int32_t res = _delegate->testI32(thing);
424 cob(res);
425 }
426
Sebastian Zenker042580f2019-01-29 15:48:12 +0100427 void testI64(std::function<void(int64_t const& _return)> cob, const int64_t thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000428 int64_t res = _delegate->testI64(thing);
429 cob(res);
430 }
431
Sebastian Zenker042580f2019-01-29 15:48:12 +0100432 void testDouble(std::function<void(double const& _return)> cob, const double thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000433 double res = _delegate->testDouble(thing);
434 cob(res);
435 }
436
Sebastian Zenker042580f2019-01-29 15:48:12 +0100437 void testBinary(std::function<void(std::string const& _return)> cob,
438 const std::string& thing) override {
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100439 std::string res;
440 _delegate->testBinary(res, thing);
441 cob(res);
442 }
443
Sebastian Zenker042580f2019-01-29 15:48:12 +0100444 void testStruct(std::function<void(Xtruct const& _return)> cob, const Xtruct& thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000445 Xtruct res;
446 _delegate->testStruct(res, thing);
447 cob(res);
448 }
449
Sebastian Zenker042580f2019-01-29 15:48:12 +0100450 void testNest(std::function<void(Xtruct2 const& _return)> cob, const Xtruct2& thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000451 Xtruct2 res;
452 _delegate->testNest(res, thing);
453 cob(res);
454 }
455
Sebastian Zenker042580f2019-01-29 15:48:12 +0100456 void testMap(std::function<void(std::map<int32_t, int32_t> const& _return)> cob,
457 const std::map<int32_t, int32_t>& thing) override {
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
Sebastian Zenker042580f2019-01-29 15:48:12 +0100463 void testStringMap(
cyy316723a2019-01-05 16:35:14 +0800464 std::function<void(std::map<std::string, std::string> const& _return)> cob,
Sebastian Zenker042580f2019-01-29 15:48:12 +0100465 const std::map<std::string, std::string>& thing) override {
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
Sebastian Zenker042580f2019-01-29 15:48:12 +0100471 void testSet(std::function<void(std::set<int32_t> const& _return)> cob,
472 const std::set<int32_t>& thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000473 std::set<int32_t> res;
474 _delegate->testSet(res, thing);
475 cob(res);
476 }
477
Sebastian Zenker042580f2019-01-29 15:48:12 +0100478 void testList(std::function<void(std::vector<int32_t> const& _return)> cob,
479 const std::vector<int32_t>& thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000480 std::vector<int32_t> res;
481 _delegate->testList(res, thing);
482 cob(res);
483 }
484
Sebastian Zenker042580f2019-01-29 15:48:12 +0100485 void testEnum(std::function<void(Numberz::type const& _return)> cob,
486 const Numberz::type thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000487 Numberz::type res = _delegate->testEnum(thing);
488 cob(res);
489 }
490
Sebastian Zenker042580f2019-01-29 15:48:12 +0100491 void testTypedef(std::function<void(UserId const& _return)> cob, const UserId thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000492 UserId res = _delegate->testTypedef(thing);
493 cob(res);
494 }
495
Sebastian Zenker042580f2019-01-29 15:48:12 +0100496 void testMapMap(
cyy316723a2019-01-05 16:35:14 +0800497 std::function<void(std::map<int32_t, std::map<int32_t, int32_t> > const& _return)> cob,
Sebastian Zenker042580f2019-01-29 15:48:12 +0100498 const int32_t hello) override {
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
Sebastian Zenker042580f2019-01-29 15:48:12 +0100504 void testInsanity(
cyy316723a2019-01-05 16:35:14 +0800505 std::function<void(std::map<UserId, std::map<Numberz::type, Insanity> > const& _return)> cob,
Sebastian Zenker042580f2019-01-29 15:48:12 +0100506 const Insanity& argument) override {
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
Sebastian Zenker042580f2019-01-29 15:48:12 +0100512 void testMulti(std::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,
Sebastian Zenker042580f2019-01-29 15:48:12 +0100518 const UserId arg5) override {
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
Sebastian Zenker042580f2019-01-29 15:48:12 +0100524 void testException(
cyy316723a2019-01-05 16:35:14 +0800525 std::function<void()> cob,
526 std::function<void(::apache::thrift::TDelayedException* _throw)> exn_cob,
Sebastian Zenker042580f2019-01-29 15:48:12 +0100527 const std::string& arg) override {
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
Sebastian Zenker042580f2019-01-29 15:48:12 +0100537 void testMultiException(
cyy316723a2019-01-05 16:35:14 +0800538 std::function<void(Xtruct const& _return)> cob,
539 std::function<void(::apache::thrift::TDelayedException* _throw)> exn_cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100540 const std::string& arg0,
Sebastian Zenker042580f2019-01-29 15:48:12 +0100541 const std::string& arg1) override {
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
Sebastian Zenker042580f2019-01-29 15:48:12 +0100552 void testOneway(std::function<void()> cob, const int32_t secondsToSleep) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000553 _delegate->testOneway(secondsToSleep);
554 cob();
555 }
556
557protected:
cyy316723a2019-01-05 16:35:14 +0800558 std::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;
James E. King IIIb2b767e2018-09-15 20:32:04 +0000574 bool zlib = false;
Roger Meierca142b02011-06-07 17:59:07 +0000575 string transport_type = "buffered";
576 string protocol_type = "binary";
577 string server_type = "simple";
578 string domain_socket = "";
pavlodd08f6e2015-10-08 16:43:56 -0400579 bool abstract_namespace = false;
Roger Meierca142b02011-06-07 17:59:07 +0000580 size_t workers = 4;
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900581 int string_limit = 0;
582 int container_limit = 0;
Marc Slemko6be374b2006-08-04 03:16:25 +0000583
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900584 po::options_description desc("Allowed options");
585 desc.add_options()
586 ("help,h", "produce help message")
587 ("port", po::value<int>(&port)->default_value(port), "Port number to listen")
588 ("domain-socket", po::value<string>(&domain_socket) ->default_value(domain_socket), "Unix Domain Socket (e.g. /tmp/ThriftTest.thrift)")
589 ("abstract-namespace", "Create the domain socket in the Abstract Namespace (no connection with filesystem pathnames)")
590 ("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 +0000591 ("transport", po::value<string>(&transport_type)->default_value(transport_type), "transport: buffered, framed, http, zlib")
James E. King, III58402ff2017-11-17 14:41:46 -0500592 ("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 +0900593 ("ssl", "Encrypted Transport using SSL")
James E. King IIIb2b767e2018-09-15 20:32:04 +0000594 ("zlib", "Wrapped Transport using Zlib")
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900595 ("processor-events", "processor-events")
596 ("workers,n", po::value<size_t>(&workers)->default_value(workers), "Number of thread pools workers. Only valid for thread-pool server type")
597 ("string-limit", po::value<int>(&string_limit))
598 ("container-limit", po::value<int>(&container_limit));
Marc Slemko6be374b2006-08-04 03:16:25 +0000599
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900600 po::variables_map vm;
601 po::store(po::parse_command_line(argc, argv, desc), vm);
602 po::notify(vm);
Marc Slemko6be374b2006-08-04 03:16:25 +0000603
Roger Meierca142b02011-06-07 17:59:07 +0000604 if (vm.count("help")) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100605 cout << desc << "\n";
606 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000607 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500608
Marc Slemko6be374b2006-08-04 03:16:25 +0000609 try {
Roger Meierca142b02011-06-07 17:59:07 +0000610 if (!server_type.empty()) {
611 if (server_type == "simple") {
612 } else if (server_type == "thread-pool") {
613 } else if (server_type == "threaded") {
614 } else if (server_type == "nonblocking") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000615 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100616 throw invalid_argument("Unknown server type " + server_type);
Roger Meierca142b02011-06-07 17:59:07 +0000617 }
618 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500619
Roger Meierca142b02011-06-07 17:59:07 +0000620 if (!protocol_type.empty()) {
621 if (protocol_type == "binary") {
Roger Meier284101c2014-03-11 21:20:35 +0100622 } else if (protocol_type == "compact") {
Roger Meierca142b02011-06-07 17:59:07 +0000623 } else if (protocol_type == "json") {
Dave Watson792db4e2015-01-16 11:22:01 -0800624 } else if (protocol_type == "header") {
James E. King, III39eaae62017-11-19 20:17:33 -0500625 } else if (protocol_type == "multi") { // multiplexed binary
626 } else if (protocol_type == "multic") { // multiplexed compact
627 } else if (protocol_type == "multih") { // multiplexed header
628 } else if (protocol_type == "multij") { // multiplexed json
Roger Meierca142b02011-06-07 17:59:07 +0000629 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100630 throw invalid_argument("Unknown protocol type " + protocol_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000631 }
632 }
633
Roger Meier284101c2014-03-11 21:20:35 +0100634 if (!transport_type.empty()) {
Roger Meierca142b02011-06-07 17:59:07 +0000635 if (transport_type == "buffered") {
636 } else if (transport_type == "framed") {
637 } else if (transport_type == "http") {
James E. King IIIb2b767e2018-09-15 20:32:04 +0000638 } else if (transport_type == "zlib") {
639 // crosstester will pass zlib as a flag and a transport right now...
Marc Slemko6be374b2006-08-04 03:16:25 +0000640 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100641 throw invalid_argument("Unknown transport type " + transport_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000642 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000643 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000644
Bryan Duxbury833ae492010-09-27 17:26:02 +0000645 } catch (std::exception& e) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000646 cerr << e.what() << endl;
Roger Meierca142b02011-06-07 17:59:07 +0000647 cout << desc << "\n";
648 return 1;
Marc Slemko6be374b2006-08-04 03:16:25 +0000649 }
650
Roger Meierca142b02011-06-07 17:59:07 +0000651 if (vm.count("ssl")) {
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000652 ssl = true;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000653 }
654
James E. King IIIb2b767e2018-09-15 20:32:04 +0000655 if (vm.count("zlib")) {
656 zlib = true;
657 }
658
James E. King III9bea32f2018-03-16 16:07:42 -0400659#if defined(HAVE_SIGNAL_H) && defined(SIGPIPE)
660 if (ssl) {
661 signal(SIGPIPE, SIG_IGN); // for OpenSSL, otherwise we end abruptly
662 }
663#endif
664
pavlodd08f6e2015-10-08 16:43:56 -0400665 if (vm.count("abstract-namespace")) {
666 abstract_namespace = true;
667 }
668
Mark Sleee8540632006-05-30 09:24:40 +0000669 // Dispatcher
cyy316723a2019-01-05 16:35:14 +0800670 std::shared_ptr<TProtocolFactory> protocolFactory;
James E. King, III58402ff2017-11-17 14:41:46 -0500671 if (protocol_type == "json" || protocol_type == "multij") {
cyy316723a2019-01-05 16:35:14 +0800672 std::shared_ptr<TProtocolFactory> jsonProtocolFactory(new TJSONProtocolFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000673 protocolFactory = jsonProtocolFactory;
James E. King, III58402ff2017-11-17 14:41:46 -0500674 } else if (protocol_type == "compact" || protocol_type == "multic") {
Sebastian Zenker042580f2019-01-29 15:48:12 +0100675 auto *compactProtocolFactory = new TCompactProtocolFactoryT<TBufferBase>();
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900676 compactProtocolFactory->setContainerSizeLimit(container_limit);
677 compactProtocolFactory->setStringSizeLimit(string_limit);
678 protocolFactory.reset(compactProtocolFactory);
James E. King, III58402ff2017-11-17 14:41:46 -0500679 } else if (protocol_type == "header" || protocol_type == "multih") {
cyy316723a2019-01-05 16:35:14 +0800680 std::shared_ptr<TProtocolFactory> headerProtocolFactory(new THeaderProtocolFactory());
Dave Watson792db4e2015-01-16 11:22:01 -0800681 protocolFactory = headerProtocolFactory;
Roger Meierca142b02011-06-07 17:59:07 +0000682 } else {
Sebastian Zenker042580f2019-01-29 15:48:12 +0100683 auto* binaryProtocolFactory = new TBinaryProtocolFactoryT<TBufferBase>();
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900684 binaryProtocolFactory->setContainerSizeLimit(container_limit);
685 binaryProtocolFactory->setStringSizeLimit(string_limit);
686 protocolFactory.reset(binaryProtocolFactory);
Roger Meierca142b02011-06-07 17:59:07 +0000687 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000688
James E. King, III58402ff2017-11-17 14:41:46 -0500689 // Processors
cyy316723a2019-01-05 16:35:14 +0800690 std::shared_ptr<TestHandler> testHandler(new TestHandler());
691 std::shared_ptr<TProcessor> testProcessor(new ThriftTestProcessor(testHandler));
Jake Farrell5d02b802014-01-07 21:42:01 -0500692
Roger Meierca142b02011-06-07 17:59:07 +0000693 if (vm.count("processor-events")) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100694 testProcessor->setEventHandler(
cyy316723a2019-01-05 16:35:14 +0800695 std::shared_ptr<TProcessorEventHandler>(new TestProcessorEventHandler()));
David Reissd7192062010-10-06 17:09:33 +0000696 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500697
Mark Sleee8540632006-05-30 09:24:40 +0000698 // Transport
cyy316723a2019-01-05 16:35:14 +0800699 std::shared_ptr<TSSLSocketFactory> sslSocketFactory;
700 std::shared_ptr<TServerSocket> serverSocket;
Mark Sleee8540632006-05-30 09:24:40 +0000701
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000702 if (ssl) {
cyy316723a2019-01-05 16:35:14 +0800703 sslSocketFactory = std::shared_ptr<TSSLSocketFactory>(new TSSLSocketFactory());
Nobuaki Sukegawac8744082016-03-09 19:55:56 +0900704 sslSocketFactory->loadCertificate(certPath.c_str());
705 sslSocketFactory->loadPrivateKey(keyPath.c_str());
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000706 sslSocketFactory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
James E. King III70b33fb2018-03-11 10:57:10 -0400707 if (server_type != "nonblocking") {
cyy316723a2019-01-05 16:35:14 +0800708 serverSocket = std::shared_ptr<TServerSocket>(new TSSLServerSocket(port, sslSocketFactory));
James E. King III70b33fb2018-03-11 10:57:10 -0400709 }
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000710 } else {
Roger Meierc94b2932014-02-22 20:07:33 +0100711 if (domain_socket != "") {
pavlodd08f6e2015-10-08 16:43:56 -0400712 if (abstract_namespace) {
713 std::string abstract_socket("\0", 1);
714 abstract_socket += domain_socket;
cyy316723a2019-01-05 16:35:14 +0800715 serverSocket = std::shared_ptr<TServerSocket>(new TServerSocket(abstract_socket));
pavlodd08f6e2015-10-08 16:43:56 -0400716 } else {
717 unlink(domain_socket.c_str());
cyy316723a2019-01-05 16:35:14 +0800718 serverSocket = std::shared_ptr<TServerSocket>(new TServerSocket(domain_socket));
pavlodd08f6e2015-10-08 16:43:56 -0400719 }
Roger Meierc94b2932014-02-22 20:07:33 +0100720 port = 0;
721 } else {
cyy316723a2019-01-05 16:35:14 +0800722 serverSocket = std::shared_ptr<TServerSocket>(new TServerSocket(port));
Roger Meierc94b2932014-02-22 20:07:33 +0100723 }
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000724 }
Roger Meierca142b02011-06-07 17:59:07 +0000725
Mark Sleed788b2e2006-09-07 01:26:35 +0000726 // Factory
cyy316723a2019-01-05 16:35:14 +0800727 std::shared_ptr<TTransportFactory> transportFactory;
Jake Farrell5d02b802014-01-07 21:42:01 -0500728
Roger Meier7e056e72011-07-17 07:28:28 +0000729 if (transport_type == "http" && server_type != "nonblocking") {
cyy316723a2019-01-05 16:35:14 +0800730 transportFactory = std::make_shared<THttpServerTransportFactory>();
Roger Meierca142b02011-06-07 17:59:07 +0000731 } else if (transport_type == "framed") {
cyy316723a2019-01-05 16:35:14 +0800732 transportFactory = std::make_shared<TFramedTransportFactory>();
Roger Meierca142b02011-06-07 17:59:07 +0000733 } else {
cyy316723a2019-01-05 16:35:14 +0800734 transportFactory = std::make_shared<TBufferedTransportFactory>();
James E. King IIIb2b767e2018-09-15 20:32:04 +0000735 }
736
737 if (zlib) {
738 // hmm.. doesn't seem to be a way to make it wrap the others...
cyy316723a2019-01-05 16:35:14 +0800739 transportFactory = std::make_shared<TZlibTransportFactory>();
Roger Meierca142b02011-06-07 17:59:07 +0000740 }
Mark Sleed788b2e2006-09-07 01:26:35 +0000741
Roger Meierca142b02011-06-07 17:59:07 +0000742 // Server Info
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100743 cout << "Starting \"" << server_type << "\" server (" << transport_type << "/" << protocol_type
pavlodd08f6e2015-10-08 16:43:56 -0400744 << ") listen on: ";
745 if (abstract_namespace) {
746 cout << '@';
747 }
748 cout << domain_socket;
Roger Meierca142b02011-06-07 17:59:07 +0000749 if (port != 0) {
750 cout << port;
751 }
752 cout << endl;
Mark Sleee8540632006-05-30 09:24:40 +0000753
James E. King, III58402ff2017-11-17 14:41:46 -0500754 // Multiplexed Processor if needed
755 if (boost::starts_with(protocol_type, "multi")) {
cyy316723a2019-01-05 16:35:14 +0800756 std::shared_ptr<SecondHandler> secondHandler(new SecondHandler());
757 std::shared_ptr<SecondServiceProcessor> secondProcessor(new SecondServiceProcessor(secondHandler));
James E. King, III58402ff2017-11-17 14:41:46 -0500758
cyy316723a2019-01-05 16:35:14 +0800759 std::shared_ptr<TMultiplexedProcessor> multiplexedProcessor(new TMultiplexedProcessor());
James E. King, III58402ff2017-11-17 14:41:46 -0500760 multiplexedProcessor->registerDefault(testProcessor); // non-multi clients go to the default processor (multi:binary, multic:compact, ...)
761 multiplexedProcessor->registerProcessor("ThriftTest", testProcessor);
762 multiplexedProcessor->registerProcessor("SecondService", secondProcessor);
cyy316723a2019-01-05 16:35:14 +0800763 testProcessor = std::dynamic_pointer_cast<TProcessor>(multiplexedProcessor);
James E. King, III58402ff2017-11-17 14:41:46 -0500764 }
765
Roger Meierca142b02011-06-07 17:59:07 +0000766 // Server
cyy316723a2019-01-05 16:35:14 +0800767 std::shared_ptr<apache::thrift::server::TServer> server;
Jake Farrell5d02b802014-01-07 21:42:01 -0500768
Roger Meierca142b02011-06-07 17:59:07 +0000769 if (server_type == "simple") {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100770 server.reset(new TSimpleServer(testProcessor, serverSocket, transportFactory, protocolFactory));
Roger Meierca142b02011-06-07 17:59:07 +0000771 } else if (server_type == "thread-pool") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000772
cyyca8af9b2019-01-11 22:13:12 +0800773 std::shared_ptr<ThreadFactory> threadFactory
774 = std::shared_ptr<ThreadFactory>(new ThreadFactory());
Marc Slemko6be374b2006-08-04 03:16:25 +0000775
cyy316723a2019-01-05 16:35:14 +0800776 std::shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workers);
Marc Slemko6be374b2006-08-04 03:16:25 +0000777 threadManager->threadFactory(threadFactory);
Marc Slemko6be374b2006-08-04 03:16:25 +0000778 threadManager->start();
779
Jake Farrell5d02b802014-01-07 21:42:01 -0500780 server.reset(new TThreadPoolServer(testProcessor,
Roger Meierca142b02011-06-07 17:59:07 +0000781 serverSocket,
Mark Sleed788b2e2006-09-07 01:26:35 +0000782 transportFactory,
Mark Sleea3302652006-10-25 19:03:32 +0000783 protocolFactory,
Jake Farrell5d02b802014-01-07 21:42:01 -0500784 threadManager));
Roger Meierca142b02011-06-07 17:59:07 +0000785 } else if (server_type == "threaded") {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100786 server.reset(
787 new TThreadedServer(testProcessor, serverSocket, transportFactory, protocolFactory));
Roger Meierca142b02011-06-07 17:59:07 +0000788 } else if (server_type == "nonblocking") {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100789 if (transport_type == "http") {
cyy316723a2019-01-05 16:35:14 +0800790 std::shared_ptr<TestHandlerAsync> testHandlerAsync(new TestHandlerAsync(testHandler));
791 std::shared_ptr<TAsyncProcessor> testProcessorAsync(
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100792 new ThriftTestAsyncProcessor(testHandlerAsync));
cyy316723a2019-01-05 16:35:14 +0800793 std::shared_ptr<TAsyncBufferProcessor> testBufferProcessor(
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100794 new TAsyncProtocolProcessor(testProcessorAsync, protocolFactory));
Jake Farrell5d02b802014-01-07 21:42:01 -0500795
796 // not loading nonblockingServer into "server" because
797 // TEvhttpServer doesn't inherit from TServer, and doesn't
798 // provide a stop method.
Roger Meier7e056e72011-07-17 07:28:28 +0000799 TEvhttpServer nonblockingServer(testBufferProcessor, port);
800 nonblockingServer.serve();
James E. King III70b33fb2018-03-11 10:57:10 -0400801 } else if (transport_type == "framed") {
cyy316723a2019-01-05 16:35:14 +0800802 std::shared_ptr<transport::TNonblockingServerTransport> nbSocket;
James E. King III9bea32f2018-03-16 16:07:42 -0400803 nbSocket.reset(
804 ssl ? new transport::TNonblockingSSLServerSocket(port, sslSocketFactory)
805 : new transport::TNonblockingServerSocket(port));
Divya Thaluru808d1432017-08-06 16:36:36 -0700806 server.reset(new TNonblockingServer(testProcessor, protocolFactory, nbSocket));
James E. King III70b33fb2018-03-11 10:57:10 -0400807 } else {
James E. King III9bea32f2018-03-16 16:07:42 -0400808 cerr << "server-type nonblocking requires transport of http or framed" << endl;
809 exit(1);
Roger Meier7e056e72011-07-17 07:28:28 +0000810 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000811 }
812
Sebastian Zenker042580f2019-01-29 15:48:12 +0100813 if (server.get() != nullptr) {
Dave Watson792db4e2015-01-16 11:22:01 -0800814 if (protocol_type == "header") {
James E. King, IIIdf899132016-11-12 15:16:30 -0500815 // Tell the server to use the same protocol for input / output
Dave Watson792db4e2015-01-16 11:22:01 -0800816 // if using header
cyy316723a2019-01-05 16:35:14 +0800817 server->setOutputProtocolFactory(std::shared_ptr<TProtocolFactory>());
Dave Watson792db4e2015-01-16 11:22:01 -0800818 }
James E. King III9bea32f2018-03-16 16:07:42 -0400819
cyyca8af9b2019-01-11 22:13:12 +0800820 apache::thrift::concurrency::ThreadFactory factory;
Jake Farrell5d02b802014-01-07 21:42:01 -0500821 factory.setDetached(false);
cyy316723a2019-01-05 16:35:14 +0800822 std::shared_ptr<apache::thrift::concurrency::Runnable> serverThreadRunner(server);
823 std::shared_ptr<apache::thrift::concurrency::Thread> thread
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100824 = factory.newThread(serverThreadRunner);
Jake Farrell5d02b802014-01-07 21:42:01 -0500825
James E. King III9bea32f2018-03-16 16:07:42 -0400826#ifdef HAVE_SIGNAL_H
827 signal(SIGINT, signal_handler);
828#endif
829
830 thread->start();
831 gMonitor.waitForever(); // wait for a shutdown signal
832
833#ifdef HAVE_SIGNAL_H
834 signal(SIGINT, SIG_DFL);
835#endif
Jake Farrell5d02b802014-01-07 21:42:01 -0500836
837 server->stop();
838 thread->join();
839 server.reset();
840 }
841
Roger Meierca142b02011-06-07 17:59:07 +0000842 cout << "done." << endl;
Mark Sleee8540632006-05-30 09:24:40 +0000843 return 0;
844}
James E. King III9bea32f2018-03-16 16:07:42 -0400845