blob: 65317f89c13f5afb8d36165f96df848ecc2773dc [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>
penenin1ab096c2020-05-18 12:27:31 -070042#include <thrift/transport/TWebSocketServer.h>
James E. King IIIb2b767e2018-09-15 20:32:04 +000043#include <thrift/transport/TZlibTransport.h>
James E. King, III58402ff2017-11-17 14:41:46 -050044
45#include "SecondService.h"
Mark Slee95771002006-06-07 06:53:25 +000046#include "ThriftTest.h"
Marc Slemko6be374b2006-08-04 03:16:25 +000047
James E. King, III7edc8fa2017-01-20 10:11:41 -050048#ifdef HAVE_STDINT_H
49#include <stdint.h>
50#endif
51#ifdef HAVE_INTTYPES_H
52#include <inttypes.h>
53#endif
James E. King III9bea32f2018-03-16 16:07:42 -040054#ifdef HAVE_SIGNAL_H
55#include <signal.h>
56#endif
James E. King, III7edc8fa2017-01-20 10:11:41 -050057
Marc Slemko6be374b2006-08-04 03:16:25 +000058#include <iostream>
59#include <stdexcept>
60#include <sstream>
61
James E. King, III58402ff2017-11-17 14:41:46 -050062#include <boost/algorithm/string.hpp>
Roger Meierca142b02011-06-07 17:59:07 +000063#include <boost/program_options.hpp>
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +053064#include <boost/filesystem.hpp>
Roger Meierca142b02011-06-07 17:59:07 +000065
Jake Farrell5d02b802014-01-07 21:42:01 -050066#if _WIN32
Konrad Grochowski16a23a62014-11-13 15:33:38 +010067#include <thrift/windows/TWinsockSingleton.h>
Jake Farrell5d02b802014-01-07 21:42:01 -050068#endif
David Reissbc3dddb2007-08-22 23:20:24 +000069
Mark Sleee8540632006-05-30 09:24:40 +000070using namespace std;
71
T Jake Lucianib5e62212009-01-31 22:36:20 +000072using namespace apache::thrift;
James E. King, III82ae9572017-08-05 12:23:54 -040073using namespace apache::thrift::async;
T Jake Lucianib5e62212009-01-31 22:36:20 +000074using namespace apache::thrift::concurrency;
75using namespace apache::thrift::protocol;
76using namespace apache::thrift::transport;
77using namespace apache::thrift::server;
Marc Slemko6be374b2006-08-04 03:16:25 +000078
Marc Slemkobf4fd192006-08-15 21:29:39 +000079using namespace thrift::test;
80
James E. King III9bea32f2018-03-16 16:07:42 -040081// to handle a controlled shutdown, signal handling is mandatory
82#ifdef HAVE_SIGNAL_H
83apache::thrift::concurrency::Monitor gMonitor;
84void signal_handler(int signum)
85{
86 if (signum == SIGINT) {
87 gMonitor.notifyAll();
88 }
89}
90#endif
91
Mark Sleed2655522006-09-05 22:09:57 +000092class TestHandler : public ThriftTestIf {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010093public:
Sebastian Zenker042580f2019-01-29 15:48:12 +010094 TestHandler() = default;
Mark Sleee8540632006-05-30 09:24:40 +000095
Sebastian Zenker042580f2019-01-29 15:48:12 +010096 void testVoid() override { printf("testVoid()\n"); }
Mark Sleee8540632006-05-30 09:24:40 +000097
Sebastian Zenker042580f2019-01-29 15:48:12 +010098 void testString(string& out, const string& thing) override {
Mark Sleee8540632006-05-30 09:24:40 +000099 printf("testString(\"%s\")\n", thing.c_str());
Mark Slee1921d202007-01-24 19:43:06 +0000100 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000101 }
102
Sebastian Zenker042580f2019-01-29 15:48:12 +0100103 bool testBool(const bool thing) override {
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900104 printf("testBool(%s)\n", thing ? "true" : "false");
105 return thing;
106 }
107
Sebastian Zenker042580f2019-01-29 15:48:12 +0100108 int8_t testByte(const int8_t thing) override {
Mark Sleee8540632006-05-30 09:24:40 +0000109 printf("testByte(%d)\n", (int)thing);
110 return thing;
111 }
112
Sebastian Zenker042580f2019-01-29 15:48:12 +0100113 int32_t testI32(const int32_t thing) override {
Mark Sleee8540632006-05-30 09:24:40 +0000114 printf("testI32(%d)\n", thing);
115 return thing;
116 }
117
Sebastian Zenker042580f2019-01-29 15:48:12 +0100118 int64_t testI64(const int64_t thing) override {
Roger Meier0e814802014-01-17 21:07:58 +0100119 printf("testI64(%" PRId64 ")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +0000120 return thing;
121 }
122
Sebastian Zenker042580f2019-01-29 15:48:12 +0100123 double testDouble(const double thing) override {
Roger Meiera8cef6e2011-07-17 18:55:59 +0000124 printf("testDouble(%f)\n", thing);
Mark Sleec98d0502006-09-06 02:42:25 +0000125 return thing;
126 }
127
Sebastian Zenker042580f2019-01-29 15:48:12 +0100128 void testBinary(std::string& _return, const std::string& thing) override {
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100129 std::ostringstream hexstr;
130 hexstr << std::hex << thing;
James E. King III3ae30422018-03-12 07:33:22 -0400131 printf("testBinary(%lu: %s)\n", safe_numeric_cast<unsigned long>(thing.size()), hexstr.str().c_str());
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100132 _return = thing;
133 }
134
Sebastian Zenker042580f2019-01-29 15:48:12 +0100135 void testStruct(Xtruct& out, const Xtruct& thing) override {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100136 printf("testStruct({\"%s\", %d, %d, %" PRId64 "})\n",
137 thing.string_thing.c_str(),
138 (int)thing.byte_thing,
139 thing.i32_thing,
140 thing.i64_thing);
Mark Slee1921d202007-01-24 19:43:06 +0000141 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000142 }
143
Sebastian Zenker042580f2019-01-29 15:48:12 +0100144 void testNest(Xtruct2& out, const Xtruct2& nest) override {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100145 const Xtruct& thing = nest.struct_thing;
146 printf("testNest({%d, {\"%s\", %d, %d, %" PRId64 "}, %d})\n",
147 (int)nest.byte_thing,
148 thing.string_thing.c_str(),
149 (int)thing.byte_thing,
150 thing.i32_thing,
151 thing.i64_thing,
152 nest.i32_thing);
Mark Slee1921d202007-01-24 19:43:06 +0000153 out = nest;
Mark Sleee8540632006-05-30 09:24:40 +0000154 }
155
Sebastian Zenker042580f2019-01-29 15:48:12 +0100156 void testMap(map<int32_t, int32_t>& out, const map<int32_t, int32_t>& thing) override {
Mark Sleee8540632006-05-30 09:24:40 +0000157 printf("testMap({");
158 map<int32_t, int32_t>::const_iterator m_iter;
159 bool first = true;
160 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
161 if (first) {
162 first = false;
163 } else {
164 printf(", ");
165 }
166 printf("%d => %d", m_iter->first, m_iter->second);
167 }
168 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000169 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000170 }
171
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100172 void testStringMap(map<std::string, std::string>& out,
Sebastian Zenker042580f2019-01-29 15:48:12 +0100173 const map<std::string, std::string>& thing) override {
Roger Meierd3b9dca2011-06-24 14:01:10 +0000174 printf("testMap({");
175 map<std::string, std::string>::const_iterator m_iter;
176 bool first = true;
177 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
178 if (first) {
179 first = false;
180 } else {
181 printf(", ");
182 }
183 printf("%s => %s", (m_iter->first).c_str(), (m_iter->second).c_str());
184 }
185 printf("})\n");
186 out = thing;
187 }
188
Sebastian Zenker042580f2019-01-29 15:48:12 +0100189 void testSet(set<int32_t>& out, const set<int32_t>& thing) override {
Mark Sleee8540632006-05-30 09:24:40 +0000190 printf("testSet({");
191 set<int32_t>::const_iterator s_iter;
192 bool first = true;
193 for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) {
194 if (first) {
195 first = false;
196 } else {
197 printf(", ");
198 }
199 printf("%d", *s_iter);
200 }
201 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000202 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000203 }
204
Sebastian Zenker042580f2019-01-29 15:48:12 +0100205 void testList(vector<int32_t>& out, const vector<int32_t>& thing) override {
Mark Sleee8540632006-05-30 09:24:40 +0000206 printf("testList({");
Mark Sleeb9acf982006-10-10 01:57:32 +0000207 vector<int32_t>::const_iterator l_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000208 bool first = true;
209 for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) {
210 if (first) {
211 first = false;
212 } else {
213 printf(", ");
214 }
215 printf("%d", *l_iter);
216 }
217 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000218 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000219 }
220
Sebastian Zenker042580f2019-01-29 15:48:12 +0100221 Numberz::type testEnum(const Numberz::type thing) override {
Mark Sleee8540632006-05-30 09:24:40 +0000222 printf("testEnum(%d)\n", thing);
223 return thing;
224 }
225
Sebastian Zenker042580f2019-01-29 15:48:12 +0100226 UserId testTypedef(const UserId thing) override {
Roger Meier0e814802014-01-17 21:07:58 +0100227 printf("testTypedef(%" PRId64 ")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +0000228 return thing;
229 }
230
Sebastian Zenker042580f2019-01-29 15:48:12 +0100231 void testMapMap(map<int32_t, map<int32_t, int32_t> >& mapmap, const int32_t hello) override {
Mark Sleee8540632006-05-30 09:24:40 +0000232 printf("testMapMap(%d)\n", hello);
Mark Sleee8540632006-05-30 09:24:40 +0000233
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100234 map<int32_t, int32_t> pos;
235 map<int32_t, int32_t> neg;
Mark Sleee8540632006-05-30 09:24:40 +0000236 for (int i = 1; i < 5; i++) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100237 pos.insert(make_pair(i, i));
238 neg.insert(make_pair(-i, -i));
Mark Sleee8540632006-05-30 09:24:40 +0000239 }
240
241 mapmap.insert(make_pair(4, pos));
242 mapmap.insert(make_pair(-4, neg));
Mark Sleee8540632006-05-30 09:24:40 +0000243 }
244
Sebastian Zenker042580f2019-01-29 15:48:12 +0100245 void testInsanity(map<UserId, map<Numberz::type, Insanity> >& insane, const Insanity& argument) override {
Mark Sleee8540632006-05-30 09:24:40 +0000246 printf("testInsanity()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000247
Mark Sleee8540632006-05-30 09:24:40 +0000248 Insanity looney;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000249 map<Numberz::type, Insanity> first_map;
250 map<Numberz::type, Insanity> second_map;
Mark Sleee8540632006-05-30 09:24:40 +0000251
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900252 first_map.insert(make_pair(Numberz::TWO, argument));
253 first_map.insert(make_pair(Numberz::THREE, argument));
Mark Sleee8540632006-05-30 09:24:40 +0000254
Bryan Duxbury833ae492010-09-27 17:26:02 +0000255 second_map.insert(make_pair(Numberz::SIX, looney));
Mark Sleee8540632006-05-30 09:24:40 +0000256
Mark Sleee8540632006-05-30 09:24:40 +0000257 insane.insert(make_pair(1, first_map));
258 insane.insert(make_pair(2, second_map));
259
260 printf("return");
261 printf(" = {");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100262 map<UserId, map<Numberz::type, Insanity> >::const_iterator i_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000263 for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) {
Roger Meier0e814802014-01-17 21:07:58 +0100264 printf("%" PRId64 " => {", i_iter->first);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100265 map<Numberz::type, Insanity>::const_iterator i2_iter;
266 for (i2_iter = i_iter->second.begin(); i2_iter != i_iter->second.end(); ++i2_iter) {
Mark Sleee8540632006-05-30 09:24:40 +0000267 printf("%d => {", i2_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000268 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
269 map<Numberz::type, UserId>::const_iterator um;
Mark Sleee8540632006-05-30 09:24:40 +0000270 printf("{");
271 for (um = userMap.begin(); um != userMap.end(); ++um) {
Roger Meier0e814802014-01-17 21:07:58 +0100272 printf("%d => %" PRId64 ", ", um->first, um->second);
Mark Sleee8540632006-05-30 09:24:40 +0000273 }
274 printf("}, ");
275
Mark Sleeb9acf982006-10-10 01:57:32 +0000276 vector<Xtruct> xtructs = i2_iter->second.xtructs;
277 vector<Xtruct>::const_iterator x;
Mark Sleee8540632006-05-30 09:24:40 +0000278 printf("{");
279 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100280 printf("{\"%s\", %d, %d, %" PRId64 "}, ",
281 x->string_thing.c_str(),
282 (int)x->byte_thing,
283 x->i32_thing,
284 x->i64_thing);
Mark Sleee8540632006-05-30 09:24:40 +0000285 }
286 printf("}");
287
288 printf("}, ");
289 }
290 printf("}, ");
291 }
292 printf("}\n");
Mark Sleee8540632006-05-30 09:24:40 +0000293 }
Marc Slemkoe6889de2006-08-12 00:32:53 +0000294
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100295 void testMulti(Xtruct& hello,
296 const int8_t arg0,
297 const int32_t arg1,
298 const int64_t arg2,
299 const std::map<int16_t, std::string>& arg3,
300 const Numberz::type arg4,
Sebastian Zenker042580f2019-01-29 15:48:12 +0100301 const UserId arg5) override {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100302 (void)arg3;
303 (void)arg4;
304 (void)arg5;
Jake Farrell5d02b802014-01-07 21:42:01 -0500305
Marc Slemkoe6889de2006-08-12 00:32:53 +0000306 printf("testMulti()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000307
Marc Slemkoe6889de2006-08-12 00:32:53 +0000308 hello.string_thing = "Hello2";
309 hello.byte_thing = arg0;
310 hello.i32_thing = arg1;
311 hello.i64_thing = (int64_t)arg2;
Marc Slemkoe6889de2006-08-12 00:32:53 +0000312 }
Marc Slemkobf4fd192006-08-15 21:29:39 +0000313
Sebastian Zenker042580f2019-01-29 15:48:12 +0100314 void testException(const std::string& arg) override {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000315 printf("testException(%s)\n", arg.c_str());
Mark Sleed3d733a2006-09-01 22:19:06 +0000316 if (arg.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000317 Xception e;
318 e.errorCode = 1001;
David Reiss9813cbe2009-01-31 21:39:11 +0000319 e.message = arg;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000320 throw e;
Roger Meier1f8b48f2012-05-02 22:56:47 +0000321 } else if (arg.compare("TException") == 0) {
T Jake Lucianib5e62212009-01-31 22:36:20 +0000322 apache::thrift::TException e;
David Reiss55ff70f2008-06-11 00:58:25 +0000323 throw e;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000324 } else {
325 Xtruct result;
326 result.string_thing = arg;
327 return;
328 }
329 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000330
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100331 void testMultiException(Xtruct& result,
332 const std::string& arg0,
Sebastian Zenker042580f2019-01-29 15:48:12 +0100333 const std::string& arg1) override {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000334
Marc Slemko71d4e472006-08-15 22:34:04 +0000335 printf("testMultiException(%s, %s)\n", arg0.c_str(), arg1.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000336
Mark Sleef5f2be42006-09-05 21:05:31 +0000337 if (arg0.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000338 Xception e;
339 e.errorCode = 1001;
340 e.message = "This is an Xception";
341 throw e;
Mark Sleef5f2be42006-09-05 21:05:31 +0000342 } else if (arg0.compare("Xception2") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000343 Xception2 e;
344 e.errorCode = 2002;
345 e.struct_thing.string_thing = "This is an Xception2";
346 throw e;
347 } else {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000348 result.string_thing = arg1;
Mark Slee1921d202007-01-24 19:43:06 +0000349 return;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000350 }
351 }
David Reiss2ab6fe82008-02-18 02:11:44 +0000352
Sebastian Zenker042580f2019-01-29 15:48:12 +0100353 void testOneway(const int32_t aNum) override {
James E. King, III58402ff2017-11-17 14:41:46 -0500354 printf("testOneway(%d): call received\n", aNum);
David Reiss2ab6fe82008-02-18 02:11:44 +0000355 }
Mark Sleee8540632006-05-30 09:24:40 +0000356};
357
James E. King, III58402ff2017-11-17 14:41:46 -0500358class SecondHandler : public SecondServiceIf
359{
360 public:
Sebastian Zenker042580f2019-01-29 15:48:12 +0100361 void secondtestString(std::string& result, const std::string& thing) override
James E. King, III58402ff2017-11-17 14:41:46 -0500362 { result = "testString(\"" + thing + "\")"; }
James E. King, III39eaae62017-11-19 20:17:33 -0500363};
James E. King, III58402ff2017-11-17 14:41:46 -0500364
David Reissd7192062010-10-06 17:09:33 +0000365class TestProcessorEventHandler : public TProcessorEventHandler {
Sebastian Zenker042580f2019-01-29 15:48:12 +0100366 void* getContext(const char* fn_name, void* serverContext) override {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100367 (void)serverContext;
David Reissd7192062010-10-06 17:09:33 +0000368 return new std::string(fn_name);
369 }
Sebastian Zenker042580f2019-01-29 15:48:12 +0100370 void freeContext(void* ctx, const char* fn_name) override {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100371 (void)fn_name;
David Reissd7192062010-10-06 17:09:33 +0000372 delete static_cast<std::string*>(ctx);
373 }
Sebastian Zenker042580f2019-01-29 15:48:12 +0100374 void preRead(void* ctx, const char* fn_name) override { communicate("preRead", ctx, fn_name); }
375 void postRead(void* ctx, const char* fn_name, uint32_t bytes) override {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100376 (void)bytes;
David Reissd7192062010-10-06 17:09:33 +0000377 communicate("postRead", ctx, fn_name);
378 }
Sebastian Zenker042580f2019-01-29 15:48:12 +0100379 void preWrite(void* ctx, const char* fn_name) override { communicate("preWrite", ctx, fn_name); }
380 void postWrite(void* ctx, const char* fn_name, uint32_t bytes) override {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100381 (void)bytes;
David Reissd7192062010-10-06 17:09:33 +0000382 communicate("postWrite", ctx, fn_name);
383 }
Sebastian Zenker042580f2019-01-29 15:48:12 +0100384 void asyncComplete(void* ctx, const char* fn_name) override {
David Reissd7192062010-10-06 17:09:33 +0000385 communicate("asyncComplete", ctx, fn_name);
386 }
Sebastian Zenker042580f2019-01-29 15:48:12 +0100387 void handlerError(void* ctx, const char* fn_name) override {
David Reissd7192062010-10-06 17:09:33 +0000388 communicate("handlerError", ctx, fn_name);
389 }
390
391 void communicate(const char* event, void* ctx, const char* fn_name) {
392 std::cout << event << ": " << *static_cast<std::string*>(ctx) << " = " << fn_name << std::endl;
393 }
394};
395
Roger Meier7e056e72011-07-17 07:28:28 +0000396class TestHandlerAsync : public ThriftTestCobSvIf {
397public:
cyy316723a2019-01-05 16:35:14 +0800398 TestHandlerAsync(std::shared_ptr<TestHandler>& handler) : _delegate(handler) {}
Sebastian Zenker042580f2019-01-29 15:48:12 +0100399 ~TestHandlerAsync() override = default;
Roger Meier7e056e72011-07-17 07:28:28 +0000400
Sebastian Zenker042580f2019-01-29 15:48:12 +0100401 void testVoid(std::function<void()> cob) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000402 _delegate->testVoid();
403 cob();
404 }
405
Sebastian Zenker042580f2019-01-29 15:48:12 +0100406 void testString(std::function<void(std::string const& _return)> cob,
407 const std::string& thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000408 std::string res;
409 _delegate->testString(res, thing);
410 cob(res);
411 }
412
Sebastian Zenker042580f2019-01-29 15:48:12 +0100413 void testBool(std::function<void(bool const& _return)> cob, const bool thing) override {
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900414 bool res = _delegate->testBool(thing);
415 cob(res);
416 }
417
Sebastian Zenker042580f2019-01-29 15:48:12 +0100418 void testByte(std::function<void(int8_t const& _return)> cob, const int8_t thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000419 int8_t res = _delegate->testByte(thing);
420 cob(res);
421 }
422
Sebastian Zenker042580f2019-01-29 15:48:12 +0100423 void testI32(std::function<void(int32_t const& _return)> cob, const int32_t thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000424 int32_t res = _delegate->testI32(thing);
425 cob(res);
426 }
427
Sebastian Zenker042580f2019-01-29 15:48:12 +0100428 void testI64(std::function<void(int64_t const& _return)> cob, const int64_t thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000429 int64_t res = _delegate->testI64(thing);
430 cob(res);
431 }
432
Sebastian Zenker042580f2019-01-29 15:48:12 +0100433 void testDouble(std::function<void(double const& _return)> cob, const double thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000434 double res = _delegate->testDouble(thing);
435 cob(res);
436 }
437
Sebastian Zenker042580f2019-01-29 15:48:12 +0100438 void testBinary(std::function<void(std::string const& _return)> cob,
439 const std::string& thing) override {
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100440 std::string res;
441 _delegate->testBinary(res, thing);
442 cob(res);
443 }
444
Sebastian Zenker042580f2019-01-29 15:48:12 +0100445 void testStruct(std::function<void(Xtruct const& _return)> cob, const Xtruct& thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000446 Xtruct res;
447 _delegate->testStruct(res, thing);
448 cob(res);
449 }
450
Sebastian Zenker042580f2019-01-29 15:48:12 +0100451 void testNest(std::function<void(Xtruct2 const& _return)> cob, const Xtruct2& thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000452 Xtruct2 res;
453 _delegate->testNest(res, thing);
454 cob(res);
455 }
456
Sebastian Zenker042580f2019-01-29 15:48:12 +0100457 void testMap(std::function<void(std::map<int32_t, int32_t> const& _return)> cob,
458 const std::map<int32_t, int32_t>& thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000459 std::map<int32_t, int32_t> res;
460 _delegate->testMap(res, thing);
461 cob(res);
462 }
463
Sebastian Zenker042580f2019-01-29 15:48:12 +0100464 void testStringMap(
cyy316723a2019-01-05 16:35:14 +0800465 std::function<void(std::map<std::string, std::string> const& _return)> cob,
Sebastian Zenker042580f2019-01-29 15:48:12 +0100466 const std::map<std::string, std::string>& thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000467 std::map<std::string, std::string> res;
468 _delegate->testStringMap(res, thing);
469 cob(res);
470 }
471
Sebastian Zenker042580f2019-01-29 15:48:12 +0100472 void testSet(std::function<void(std::set<int32_t> const& _return)> cob,
473 const std::set<int32_t>& thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000474 std::set<int32_t> res;
475 _delegate->testSet(res, thing);
476 cob(res);
477 }
478
Sebastian Zenker042580f2019-01-29 15:48:12 +0100479 void testList(std::function<void(std::vector<int32_t> const& _return)> cob,
480 const std::vector<int32_t>& thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000481 std::vector<int32_t> res;
482 _delegate->testList(res, thing);
483 cob(res);
484 }
485
Sebastian Zenker042580f2019-01-29 15:48:12 +0100486 void testEnum(std::function<void(Numberz::type const& _return)> cob,
487 const Numberz::type thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000488 Numberz::type res = _delegate->testEnum(thing);
489 cob(res);
490 }
491
Sebastian Zenker042580f2019-01-29 15:48:12 +0100492 void testTypedef(std::function<void(UserId const& _return)> cob, const UserId thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000493 UserId res = _delegate->testTypedef(thing);
494 cob(res);
495 }
496
Sebastian Zenker042580f2019-01-29 15:48:12 +0100497 void testMapMap(
cyy316723a2019-01-05 16:35:14 +0800498 std::function<void(std::map<int32_t, std::map<int32_t, int32_t> > const& _return)> cob,
Sebastian Zenker042580f2019-01-29 15:48:12 +0100499 const int32_t hello) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000500 std::map<int32_t, std::map<int32_t, int32_t> > res;
501 _delegate->testMapMap(res, hello);
502 cob(res);
503 }
504
Sebastian Zenker042580f2019-01-29 15:48:12 +0100505 void testInsanity(
cyy316723a2019-01-05 16:35:14 +0800506 std::function<void(std::map<UserId, std::map<Numberz::type, Insanity> > const& _return)> cob,
Sebastian Zenker042580f2019-01-29 15:48:12 +0100507 const Insanity& argument) override {
Jake Farrell5d02b802014-01-07 21:42:01 -0500508 std::map<UserId, std::map<Numberz::type, Insanity> > res;
Roger Meier7e056e72011-07-17 07:28:28 +0000509 _delegate->testInsanity(res, argument);
510 cob(res);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100511 }
Roger Meier7e056e72011-07-17 07:28:28 +0000512
Sebastian Zenker042580f2019-01-29 15:48:12 +0100513 void testMulti(std::function<void(Xtruct const& _return)> cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100514 const int8_t arg0,
515 const int32_t arg1,
516 const int64_t arg2,
517 const std::map<int16_t, std::string>& arg3,
518 const Numberz::type arg4,
Sebastian Zenker042580f2019-01-29 15:48:12 +0100519 const UserId arg5) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000520 Xtruct res;
521 _delegate->testMulti(res, arg0, arg1, arg2, arg3, arg4, arg5);
522 cob(res);
523 }
524
Sebastian Zenker042580f2019-01-29 15:48:12 +0100525 void testException(
cyy316723a2019-01-05 16:35:14 +0800526 std::function<void()> cob,
527 std::function<void(::apache::thrift::TDelayedException* _throw)> exn_cob,
Sebastian Zenker042580f2019-01-29 15:48:12 +0100528 const std::string& arg) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000529 try {
530 _delegate->testException(arg);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100531 } catch (const apache::thrift::TException& e) {
Roger Meier7e056e72011-07-17 07:28:28 +0000532 exn_cob(apache::thrift::TDelayedException::delayException(e));
533 return;
534 }
535 cob();
536 }
537
Sebastian Zenker042580f2019-01-29 15:48:12 +0100538 void testMultiException(
cyy316723a2019-01-05 16:35:14 +0800539 std::function<void(Xtruct const& _return)> cob,
540 std::function<void(::apache::thrift::TDelayedException* _throw)> exn_cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100541 const std::string& arg0,
Sebastian Zenker042580f2019-01-29 15:48:12 +0100542 const std::string& arg1) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000543 Xtruct res;
544 try {
545 _delegate->testMultiException(res, arg0, arg1);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100546 } catch (const apache::thrift::TException& e) {
Roger Meier7e056e72011-07-17 07:28:28 +0000547 exn_cob(apache::thrift::TDelayedException::delayException(e));
548 return;
549 }
550 cob(res);
551 }
552
Sebastian Zenker042580f2019-01-29 15:48:12 +0100553 void testOneway(std::function<void()> cob, const int32_t secondsToSleep) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000554 _delegate->testOneway(secondsToSleep);
555 cob();
556 }
557
558protected:
cyy316723a2019-01-05 16:35:14 +0800559 std::shared_ptr<TestHandler> _delegate;
Roger Meier7e056e72011-07-17 07:28:28 +0000560};
561
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900562namespace po = boost::program_options;
563
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100564int main(int argc, char** argv) {
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +0530565
Nobuaki Sukegawac8744082016-03-09 19:55:56 +0900566 string testDir = boost::filesystem::system_complete(argv[0]).parent_path().parent_path().parent_path().string();
567 string certPath = testDir + "/keys/server.crt";
568 string keyPath = testDir + "/keys/server.key";
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +0530569
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100570#if _WIN32
Jake Farrell5d02b802014-01-07 21:42:01 -0500571 transport::TWinsockSingleton::create();
572#endif
Mark Sleee8540632006-05-30 09:24:40 +0000573 int port = 9090;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000574 bool ssl = false;
James E. King IIIb2b767e2018-09-15 20:32:04 +0000575 bool zlib = false;
Roger Meierca142b02011-06-07 17:59:07 +0000576 string transport_type = "buffered";
577 string protocol_type = "binary";
578 string server_type = "simple";
579 string domain_socket = "";
pavlodd08f6e2015-10-08 16:43:56 -0400580 bool abstract_namespace = false;
Roger Meierca142b02011-06-07 17:59:07 +0000581 size_t workers = 4;
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900582 int string_limit = 0;
583 int container_limit = 0;
Marc Slemko6be374b2006-08-04 03:16:25 +0000584
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900585 po::options_description desc("Allowed options");
586 desc.add_options()
587 ("help,h", "produce help message")
588 ("port", po::value<int>(&port)->default_value(port), "Port number to listen")
589 ("domain-socket", po::value<string>(&domain_socket) ->default_value(domain_socket), "Unix Domain Socket (e.g. /tmp/ThriftTest.thrift)")
590 ("abstract-namespace", "Create the domain socket in the Abstract Namespace (no connection with filesystem pathnames)")
591 ("server-type", po::value<string>(&server_type)->default_value(server_type), "type of server, \"simple\", \"thread-pool\", \"threaded\", or \"nonblocking\"")
penenin1ab096c2020-05-18 12:27:31 -0700592 ("transport", po::value<string>(&transport_type)->default_value(transport_type), "transport: buffered, framed, http, websocket, zlib")
James E. King, III58402ff2017-11-17 14:41:46 -0500593 ("protocol", po::value<string>(&protocol_type)->default_value(protocol_type), "protocol: binary, compact, header, json, multi, multic, multih, multij")
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900594 ("ssl", "Encrypted Transport using SSL")
James E. King IIIb2b767e2018-09-15 20:32:04 +0000595 ("zlib", "Wrapped Transport using Zlib")
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900596 ("processor-events", "processor-events")
597 ("workers,n", po::value<size_t>(&workers)->default_value(workers), "Number of thread pools workers. Only valid for thread-pool server type")
598 ("string-limit", po::value<int>(&string_limit))
599 ("container-limit", po::value<int>(&container_limit));
Marc Slemko6be374b2006-08-04 03:16:25 +0000600
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900601 po::variables_map vm;
602 po::store(po::parse_command_line(argc, argv, desc), vm);
603 po::notify(vm);
Marc Slemko6be374b2006-08-04 03:16:25 +0000604
Roger Meierca142b02011-06-07 17:59:07 +0000605 if (vm.count("help")) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100606 cout << desc << "\n";
607 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000608 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500609
Marc Slemko6be374b2006-08-04 03:16:25 +0000610 try {
Roger Meierca142b02011-06-07 17:59:07 +0000611 if (!server_type.empty()) {
612 if (server_type == "simple") {
613 } else if (server_type == "thread-pool") {
614 } else if (server_type == "threaded") {
615 } else if (server_type == "nonblocking") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000616 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100617 throw invalid_argument("Unknown server type " + server_type);
Roger Meierca142b02011-06-07 17:59:07 +0000618 }
619 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500620
Roger Meierca142b02011-06-07 17:59:07 +0000621 if (!protocol_type.empty()) {
622 if (protocol_type == "binary") {
Roger Meier284101c2014-03-11 21:20:35 +0100623 } else if (protocol_type == "compact") {
Roger Meierca142b02011-06-07 17:59:07 +0000624 } else if (protocol_type == "json") {
Dave Watson792db4e2015-01-16 11:22:01 -0800625 } else if (protocol_type == "header") {
James E. King, III39eaae62017-11-19 20:17:33 -0500626 } else if (protocol_type == "multi") { // multiplexed binary
627 } else if (protocol_type == "multic") { // multiplexed compact
628 } else if (protocol_type == "multih") { // multiplexed header
629 } else if (protocol_type == "multij") { // multiplexed json
Roger Meierca142b02011-06-07 17:59:07 +0000630 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100631 throw invalid_argument("Unknown protocol type " + protocol_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000632 }
633 }
634
Roger Meier284101c2014-03-11 21:20:35 +0100635 if (!transport_type.empty()) {
Roger Meierca142b02011-06-07 17:59:07 +0000636 if (transport_type == "buffered") {
637 } else if (transport_type == "framed") {
638 } else if (transport_type == "http") {
penenin1ab096c2020-05-18 12:27:31 -0700639 } else if (transport_type == "websocket") {
James E. King IIIb2b767e2018-09-15 20:32:04 +0000640 } else if (transport_type == "zlib") {
641 // crosstester will pass zlib as a flag and a transport right now...
Marc Slemko6be374b2006-08-04 03:16:25 +0000642 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100643 throw invalid_argument("Unknown transport type " + transport_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000644 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000645 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000646
Bryan Duxbury833ae492010-09-27 17:26:02 +0000647 } catch (std::exception& e) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000648 cerr << e.what() << endl;
Roger Meierca142b02011-06-07 17:59:07 +0000649 cout << desc << "\n";
650 return 1;
Marc Slemko6be374b2006-08-04 03:16:25 +0000651 }
652
Roger Meierca142b02011-06-07 17:59:07 +0000653 if (vm.count("ssl")) {
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000654 ssl = true;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000655 }
656
James E. King IIIb2b767e2018-09-15 20:32:04 +0000657 if (vm.count("zlib")) {
658 zlib = true;
659 }
660
James E. King III9bea32f2018-03-16 16:07:42 -0400661#if defined(HAVE_SIGNAL_H) && defined(SIGPIPE)
662 if (ssl) {
663 signal(SIGPIPE, SIG_IGN); // for OpenSSL, otherwise we end abruptly
664 }
665#endif
666
pavlodd08f6e2015-10-08 16:43:56 -0400667 if (vm.count("abstract-namespace")) {
668 abstract_namespace = true;
669 }
670
Mark Sleee8540632006-05-30 09:24:40 +0000671 // Dispatcher
cyy316723a2019-01-05 16:35:14 +0800672 std::shared_ptr<TProtocolFactory> protocolFactory;
James E. King, III58402ff2017-11-17 14:41:46 -0500673 if (protocol_type == "json" || protocol_type == "multij") {
cyy316723a2019-01-05 16:35:14 +0800674 std::shared_ptr<TProtocolFactory> jsonProtocolFactory(new TJSONProtocolFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000675 protocolFactory = jsonProtocolFactory;
James E. King, III58402ff2017-11-17 14:41:46 -0500676 } else if (protocol_type == "compact" || protocol_type == "multic") {
Sebastian Zenker042580f2019-01-29 15:48:12 +0100677 auto *compactProtocolFactory = new TCompactProtocolFactoryT<TBufferBase>();
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900678 compactProtocolFactory->setContainerSizeLimit(container_limit);
679 compactProtocolFactory->setStringSizeLimit(string_limit);
680 protocolFactory.reset(compactProtocolFactory);
James E. King, III58402ff2017-11-17 14:41:46 -0500681 } else if (protocol_type == "header" || protocol_type == "multih") {
cyy316723a2019-01-05 16:35:14 +0800682 std::shared_ptr<TProtocolFactory> headerProtocolFactory(new THeaderProtocolFactory());
Dave Watson792db4e2015-01-16 11:22:01 -0800683 protocolFactory = headerProtocolFactory;
Roger Meierca142b02011-06-07 17:59:07 +0000684 } else {
Sebastian Zenker042580f2019-01-29 15:48:12 +0100685 auto* binaryProtocolFactory = new TBinaryProtocolFactoryT<TBufferBase>();
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900686 binaryProtocolFactory->setContainerSizeLimit(container_limit);
687 binaryProtocolFactory->setStringSizeLimit(string_limit);
688 protocolFactory.reset(binaryProtocolFactory);
Roger Meierca142b02011-06-07 17:59:07 +0000689 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000690
James E. King, III58402ff2017-11-17 14:41:46 -0500691 // Processors
cyy316723a2019-01-05 16:35:14 +0800692 std::shared_ptr<TestHandler> testHandler(new TestHandler());
693 std::shared_ptr<TProcessor> testProcessor(new ThriftTestProcessor(testHandler));
Jake Farrell5d02b802014-01-07 21:42:01 -0500694
Roger Meierca142b02011-06-07 17:59:07 +0000695 if (vm.count("processor-events")) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100696 testProcessor->setEventHandler(
cyy316723a2019-01-05 16:35:14 +0800697 std::shared_ptr<TProcessorEventHandler>(new TestProcessorEventHandler()));
David Reissd7192062010-10-06 17:09:33 +0000698 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500699
Mark Sleee8540632006-05-30 09:24:40 +0000700 // Transport
cyy316723a2019-01-05 16:35:14 +0800701 std::shared_ptr<TSSLSocketFactory> sslSocketFactory;
702 std::shared_ptr<TServerSocket> serverSocket;
Mark Sleee8540632006-05-30 09:24:40 +0000703
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000704 if (ssl) {
cyy316723a2019-01-05 16:35:14 +0800705 sslSocketFactory = std::shared_ptr<TSSLSocketFactory>(new TSSLSocketFactory());
Nobuaki Sukegawac8744082016-03-09 19:55:56 +0900706 sslSocketFactory->loadCertificate(certPath.c_str());
707 sslSocketFactory->loadPrivateKey(keyPath.c_str());
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000708 sslSocketFactory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
James E. King III70b33fb2018-03-11 10:57:10 -0400709 if (server_type != "nonblocking") {
cyy316723a2019-01-05 16:35:14 +0800710 serverSocket = std::shared_ptr<TServerSocket>(new TSSLServerSocket(port, sslSocketFactory));
James E. King III70b33fb2018-03-11 10:57:10 -0400711 }
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000712 } else {
Roger Meierc94b2932014-02-22 20:07:33 +0100713 if (domain_socket != "") {
pavlodd08f6e2015-10-08 16:43:56 -0400714 if (abstract_namespace) {
715 std::string abstract_socket("\0", 1);
716 abstract_socket += domain_socket;
cyy316723a2019-01-05 16:35:14 +0800717 serverSocket = std::shared_ptr<TServerSocket>(new TServerSocket(abstract_socket));
pavlodd08f6e2015-10-08 16:43:56 -0400718 } else {
719 unlink(domain_socket.c_str());
cyy316723a2019-01-05 16:35:14 +0800720 serverSocket = std::shared_ptr<TServerSocket>(new TServerSocket(domain_socket));
pavlodd08f6e2015-10-08 16:43:56 -0400721 }
Roger Meierc94b2932014-02-22 20:07:33 +0100722 port = 0;
723 } else {
cyy316723a2019-01-05 16:35:14 +0800724 serverSocket = std::shared_ptr<TServerSocket>(new TServerSocket(port));
Roger Meierc94b2932014-02-22 20:07:33 +0100725 }
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000726 }
Roger Meierca142b02011-06-07 17:59:07 +0000727
Mark Sleed788b2e2006-09-07 01:26:35 +0000728 // Factory
cyy316723a2019-01-05 16:35:14 +0800729 std::shared_ptr<TTransportFactory> transportFactory;
Jake Farrell5d02b802014-01-07 21:42:01 -0500730
Roger Meier7e056e72011-07-17 07:28:28 +0000731 if (transport_type == "http" && server_type != "nonblocking") {
cyy316723a2019-01-05 16:35:14 +0800732 transportFactory = std::make_shared<THttpServerTransportFactory>();
penenin1ab096c2020-05-18 12:27:31 -0700733 } else if (transport_type == "websocket" && server_type != "nonblocking") {
734 if (protocol_type == "json" || protocol_type == "multij") {
735 transportFactory = std::make_shared<TTextWebSocketServerTransportFactory>();
736 } else {
737 transportFactory = std::make_shared<TBinaryWebSocketServerTransportFactory>();
738 }
Roger Meierca142b02011-06-07 17:59:07 +0000739 } else if (transport_type == "framed") {
cyy316723a2019-01-05 16:35:14 +0800740 transportFactory = std::make_shared<TFramedTransportFactory>();
Roger Meierca142b02011-06-07 17:59:07 +0000741 } else {
cyy316723a2019-01-05 16:35:14 +0800742 transportFactory = std::make_shared<TBufferedTransportFactory>();
James E. King IIIb2b767e2018-09-15 20:32:04 +0000743 }
744
745 if (zlib) {
Mario Emmenlauer2d2df9e2020-04-24 18:02:18 +0200746 // currently TZlibTransportFactory is the only factory than can wrap another:
747 transportFactory = std::make_shared<TZlibTransportFactory>(transportFactory);
Roger Meierca142b02011-06-07 17:59:07 +0000748 }
Mark Sleed788b2e2006-09-07 01:26:35 +0000749
Roger Meierca142b02011-06-07 17:59:07 +0000750 // Server Info
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100751 cout << "Starting \"" << server_type << "\" server (" << transport_type << "/" << protocol_type
pavlodd08f6e2015-10-08 16:43:56 -0400752 << ") listen on: ";
753 if (abstract_namespace) {
754 cout << '@';
755 }
756 cout << domain_socket;
Roger Meierca142b02011-06-07 17:59:07 +0000757 if (port != 0) {
758 cout << port;
759 }
760 cout << endl;
Mark Sleee8540632006-05-30 09:24:40 +0000761
James E. King, III58402ff2017-11-17 14:41:46 -0500762 // Multiplexed Processor if needed
763 if (boost::starts_with(protocol_type, "multi")) {
cyy316723a2019-01-05 16:35:14 +0800764 std::shared_ptr<SecondHandler> secondHandler(new SecondHandler());
765 std::shared_ptr<SecondServiceProcessor> secondProcessor(new SecondServiceProcessor(secondHandler));
James E. King, III58402ff2017-11-17 14:41:46 -0500766
cyy316723a2019-01-05 16:35:14 +0800767 std::shared_ptr<TMultiplexedProcessor> multiplexedProcessor(new TMultiplexedProcessor());
James E. King, III58402ff2017-11-17 14:41:46 -0500768 multiplexedProcessor->registerDefault(testProcessor); // non-multi clients go to the default processor (multi:binary, multic:compact, ...)
769 multiplexedProcessor->registerProcessor("ThriftTest", testProcessor);
770 multiplexedProcessor->registerProcessor("SecondService", secondProcessor);
cyy316723a2019-01-05 16:35:14 +0800771 testProcessor = std::dynamic_pointer_cast<TProcessor>(multiplexedProcessor);
James E. King, III58402ff2017-11-17 14:41:46 -0500772 }
773
Roger Meierca142b02011-06-07 17:59:07 +0000774 // Server
cyy316723a2019-01-05 16:35:14 +0800775 std::shared_ptr<apache::thrift::server::TServer> server;
Jake Farrell5d02b802014-01-07 21:42:01 -0500776
Roger Meierca142b02011-06-07 17:59:07 +0000777 if (server_type == "simple") {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100778 server.reset(new TSimpleServer(testProcessor, serverSocket, transportFactory, protocolFactory));
Roger Meierca142b02011-06-07 17:59:07 +0000779 } else if (server_type == "thread-pool") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000780
cyyca8af9b2019-01-11 22:13:12 +0800781 std::shared_ptr<ThreadFactory> threadFactory
782 = std::shared_ptr<ThreadFactory>(new ThreadFactory());
Marc Slemko6be374b2006-08-04 03:16:25 +0000783
cyy316723a2019-01-05 16:35:14 +0800784 std::shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workers);
Marc Slemko6be374b2006-08-04 03:16:25 +0000785 threadManager->threadFactory(threadFactory);
Marc Slemko6be374b2006-08-04 03:16:25 +0000786 threadManager->start();
787
Jake Farrell5d02b802014-01-07 21:42:01 -0500788 server.reset(new TThreadPoolServer(testProcessor,
Roger Meierca142b02011-06-07 17:59:07 +0000789 serverSocket,
Mark Sleed788b2e2006-09-07 01:26:35 +0000790 transportFactory,
Mark Sleea3302652006-10-25 19:03:32 +0000791 protocolFactory,
Jake Farrell5d02b802014-01-07 21:42:01 -0500792 threadManager));
Roger Meierca142b02011-06-07 17:59:07 +0000793 } else if (server_type == "threaded") {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100794 server.reset(
795 new TThreadedServer(testProcessor, serverSocket, transportFactory, protocolFactory));
Roger Meierca142b02011-06-07 17:59:07 +0000796 } else if (server_type == "nonblocking") {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100797 if (transport_type == "http") {
cyy316723a2019-01-05 16:35:14 +0800798 std::shared_ptr<TestHandlerAsync> testHandlerAsync(new TestHandlerAsync(testHandler));
799 std::shared_ptr<TAsyncProcessor> testProcessorAsync(
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100800 new ThriftTestAsyncProcessor(testHandlerAsync));
cyy316723a2019-01-05 16:35:14 +0800801 std::shared_ptr<TAsyncBufferProcessor> testBufferProcessor(
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100802 new TAsyncProtocolProcessor(testProcessorAsync, protocolFactory));
Jake Farrell5d02b802014-01-07 21:42:01 -0500803
804 // not loading nonblockingServer into "server" because
805 // TEvhttpServer doesn't inherit from TServer, and doesn't
806 // provide a stop method.
Roger Meier7e056e72011-07-17 07:28:28 +0000807 TEvhttpServer nonblockingServer(testBufferProcessor, port);
808 nonblockingServer.serve();
James E. King III70b33fb2018-03-11 10:57:10 -0400809 } else if (transport_type == "framed") {
cyy316723a2019-01-05 16:35:14 +0800810 std::shared_ptr<transport::TNonblockingServerTransport> nbSocket;
James E. King III9bea32f2018-03-16 16:07:42 -0400811 nbSocket.reset(
812 ssl ? new transport::TNonblockingSSLServerSocket(port, sslSocketFactory)
813 : new transport::TNonblockingServerSocket(port));
Divya Thaluru808d1432017-08-06 16:36:36 -0700814 server.reset(new TNonblockingServer(testProcessor, protocolFactory, nbSocket));
James E. King III70b33fb2018-03-11 10:57:10 -0400815 } else {
James E. King III9bea32f2018-03-16 16:07:42 -0400816 cerr << "server-type nonblocking requires transport of http or framed" << endl;
817 exit(1);
Roger Meier7e056e72011-07-17 07:28:28 +0000818 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000819 }
820
Sebastian Zenker042580f2019-01-29 15:48:12 +0100821 if (server.get() != nullptr) {
Dave Watson792db4e2015-01-16 11:22:01 -0800822 if (protocol_type == "header") {
James E. King, IIIdf899132016-11-12 15:16:30 -0500823 // Tell the server to use the same protocol for input / output
Dave Watson792db4e2015-01-16 11:22:01 -0800824 // if using header
cyy316723a2019-01-05 16:35:14 +0800825 server->setOutputProtocolFactory(std::shared_ptr<TProtocolFactory>());
Dave Watson792db4e2015-01-16 11:22:01 -0800826 }
Mario Emmenlauer2d2df9e2020-04-24 18:02:18 +0200827
cyyca8af9b2019-01-11 22:13:12 +0800828 apache::thrift::concurrency::ThreadFactory factory;
Jake Farrell5d02b802014-01-07 21:42:01 -0500829 factory.setDetached(false);
cyy316723a2019-01-05 16:35:14 +0800830 std::shared_ptr<apache::thrift::concurrency::Runnable> serverThreadRunner(server);
831 std::shared_ptr<apache::thrift::concurrency::Thread> thread
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100832 = factory.newThread(serverThreadRunner);
Jake Farrell5d02b802014-01-07 21:42:01 -0500833
James E. King III9bea32f2018-03-16 16:07:42 -0400834#ifdef HAVE_SIGNAL_H
835 signal(SIGINT, signal_handler);
836#endif
837
838 thread->start();
839 gMonitor.waitForever(); // wait for a shutdown signal
Mario Emmenlauer2d2df9e2020-04-24 18:02:18 +0200840
James E. King III9bea32f2018-03-16 16:07:42 -0400841#ifdef HAVE_SIGNAL_H
842 signal(SIGINT, SIG_DFL);
843#endif
Jake Farrell5d02b802014-01-07 21:42:01 -0500844
845 server->stop();
846 thread->join();
847 server.reset();
848 }
849
Roger Meierca142b02011-06-07 17:59:07 +0000850 cout << "done." << endl;
Mark Sleee8540632006-05-30 09:24:40 +0000851 return 0;
852}