blob: dc6e69f6f0d69cde6a4eea1af1c97f728a595776 [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
CJCombrink4b909092024-04-27 19:51:39 +0200135 TUuid testUuid(const TUuid thing) override {
136 printf("testUuid(\"{%s}\")\n", to_string(thing).c_str());
CJCombrink1d886ca2024-03-23 21:32:28 +0100137 return thing;
138 }
139
Sebastian Zenker042580f2019-01-29 15:48:12 +0100140 void testStruct(Xtruct& out, const Xtruct& thing) override {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100141 printf("testStruct({\"%s\", %d, %d, %" PRId64 "})\n",
142 thing.string_thing.c_str(),
143 (int)thing.byte_thing,
144 thing.i32_thing,
145 thing.i64_thing);
Mark Slee1921d202007-01-24 19:43:06 +0000146 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000147 }
148
Sebastian Zenker042580f2019-01-29 15:48:12 +0100149 void testNest(Xtruct2& out, const Xtruct2& nest) override {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100150 const Xtruct& thing = nest.struct_thing;
151 printf("testNest({%d, {\"%s\", %d, %d, %" PRId64 "}, %d})\n",
152 (int)nest.byte_thing,
153 thing.string_thing.c_str(),
154 (int)thing.byte_thing,
155 thing.i32_thing,
156 thing.i64_thing,
157 nest.i32_thing);
Mark Slee1921d202007-01-24 19:43:06 +0000158 out = nest;
Mark Sleee8540632006-05-30 09:24:40 +0000159 }
160
Sebastian Zenker042580f2019-01-29 15:48:12 +0100161 void testMap(map<int32_t, int32_t>& out, const map<int32_t, int32_t>& thing) override {
Mark Sleee8540632006-05-30 09:24:40 +0000162 printf("testMap({");
163 map<int32_t, int32_t>::const_iterator m_iter;
164 bool first = true;
165 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
166 if (first) {
167 first = false;
168 } else {
169 printf(", ");
170 }
171 printf("%d => %d", m_iter->first, m_iter->second);
172 }
173 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000174 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000175 }
176
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100177 void testStringMap(map<std::string, std::string>& out,
Sebastian Zenker042580f2019-01-29 15:48:12 +0100178 const map<std::string, std::string>& thing) override {
Roger Meierd3b9dca2011-06-24 14:01:10 +0000179 printf("testMap({");
180 map<std::string, std::string>::const_iterator m_iter;
181 bool first = true;
182 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
183 if (first) {
184 first = false;
185 } else {
186 printf(", ");
187 }
188 printf("%s => %s", (m_iter->first).c_str(), (m_iter->second).c_str());
189 }
190 printf("})\n");
191 out = thing;
192 }
193
Sebastian Zenker042580f2019-01-29 15:48:12 +0100194 void testSet(set<int32_t>& out, const set<int32_t>& thing) override {
Mark Sleee8540632006-05-30 09:24:40 +0000195 printf("testSet({");
196 set<int32_t>::const_iterator s_iter;
197 bool first = true;
198 for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) {
199 if (first) {
200 first = false;
201 } else {
202 printf(", ");
203 }
204 printf("%d", *s_iter);
205 }
206 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000207 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000208 }
209
Sebastian Zenker042580f2019-01-29 15:48:12 +0100210 void testList(vector<int32_t>& out, const vector<int32_t>& thing) override {
Mark Sleee8540632006-05-30 09:24:40 +0000211 printf("testList({");
Mark Sleeb9acf982006-10-10 01:57:32 +0000212 vector<int32_t>::const_iterator l_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000213 bool first = true;
214 for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) {
215 if (first) {
216 first = false;
217 } else {
218 printf(", ");
219 }
220 printf("%d", *l_iter);
221 }
222 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000223 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000224 }
225
Sebastian Zenker042580f2019-01-29 15:48:12 +0100226 Numberz::type testEnum(const Numberz::type thing) override {
Mark Sleee8540632006-05-30 09:24:40 +0000227 printf("testEnum(%d)\n", thing);
228 return thing;
229 }
230
Sebastian Zenker042580f2019-01-29 15:48:12 +0100231 UserId testTypedef(const UserId thing) override {
Roger Meier0e814802014-01-17 21:07:58 +0100232 printf("testTypedef(%" PRId64 ")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +0000233 return thing;
234 }
235
Sebastian Zenker042580f2019-01-29 15:48:12 +0100236 void testMapMap(map<int32_t, map<int32_t, int32_t> >& mapmap, const int32_t hello) override {
Mark Sleee8540632006-05-30 09:24:40 +0000237 printf("testMapMap(%d)\n", hello);
Mark Sleee8540632006-05-30 09:24:40 +0000238
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100239 map<int32_t, int32_t> pos;
240 map<int32_t, int32_t> neg;
Mark Sleee8540632006-05-30 09:24:40 +0000241 for (int i = 1; i < 5; i++) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100242 pos.insert(make_pair(i, i));
243 neg.insert(make_pair(-i, -i));
Mark Sleee8540632006-05-30 09:24:40 +0000244 }
245
246 mapmap.insert(make_pair(4, pos));
247 mapmap.insert(make_pair(-4, neg));
Mark Sleee8540632006-05-30 09:24:40 +0000248 }
249
Sebastian Zenker042580f2019-01-29 15:48:12 +0100250 void testInsanity(map<UserId, map<Numberz::type, Insanity> >& insane, const Insanity& argument) override {
Mark Sleee8540632006-05-30 09:24:40 +0000251 printf("testInsanity()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000252
Mark Sleee8540632006-05-30 09:24:40 +0000253 Insanity looney;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000254 map<Numberz::type, Insanity> first_map;
255 map<Numberz::type, Insanity> second_map;
Mark Sleee8540632006-05-30 09:24:40 +0000256
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900257 first_map.insert(make_pair(Numberz::TWO, argument));
258 first_map.insert(make_pair(Numberz::THREE, argument));
Mark Sleee8540632006-05-30 09:24:40 +0000259
Bryan Duxbury833ae492010-09-27 17:26:02 +0000260 second_map.insert(make_pair(Numberz::SIX, looney));
Mark Sleee8540632006-05-30 09:24:40 +0000261
Mark Sleee8540632006-05-30 09:24:40 +0000262 insane.insert(make_pair(1, first_map));
263 insane.insert(make_pair(2, second_map));
264
265 printf("return");
266 printf(" = {");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100267 map<UserId, map<Numberz::type, Insanity> >::const_iterator i_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000268 for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) {
Roger Meier0e814802014-01-17 21:07:58 +0100269 printf("%" PRId64 " => {", i_iter->first);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100270 map<Numberz::type, Insanity>::const_iterator i2_iter;
271 for (i2_iter = i_iter->second.begin(); i2_iter != i_iter->second.end(); ++i2_iter) {
Mark Sleee8540632006-05-30 09:24:40 +0000272 printf("%d => {", i2_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000273 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
274 map<Numberz::type, UserId>::const_iterator um;
Mark Sleee8540632006-05-30 09:24:40 +0000275 printf("{");
276 for (um = userMap.begin(); um != userMap.end(); ++um) {
Roger Meier0e814802014-01-17 21:07:58 +0100277 printf("%d => %" PRId64 ", ", um->first, um->second);
Mark Sleee8540632006-05-30 09:24:40 +0000278 }
279 printf("}, ");
280
Mark Sleeb9acf982006-10-10 01:57:32 +0000281 vector<Xtruct> xtructs = i2_iter->second.xtructs;
282 vector<Xtruct>::const_iterator x;
Mark Sleee8540632006-05-30 09:24:40 +0000283 printf("{");
284 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100285 printf("{\"%s\", %d, %d, %" PRId64 "}, ",
286 x->string_thing.c_str(),
287 (int)x->byte_thing,
288 x->i32_thing,
289 x->i64_thing);
Mark Sleee8540632006-05-30 09:24:40 +0000290 }
291 printf("}");
292
293 printf("}, ");
294 }
295 printf("}, ");
296 }
297 printf("}\n");
Mark Sleee8540632006-05-30 09:24:40 +0000298 }
Marc Slemkoe6889de2006-08-12 00:32:53 +0000299
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100300 void testMulti(Xtruct& hello,
301 const int8_t arg0,
302 const int32_t arg1,
303 const int64_t arg2,
304 const std::map<int16_t, std::string>& arg3,
305 const Numberz::type arg4,
Sebastian Zenker042580f2019-01-29 15:48:12 +0100306 const UserId arg5) override {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100307 (void)arg3;
308 (void)arg4;
309 (void)arg5;
Jake Farrell5d02b802014-01-07 21:42:01 -0500310
Marc Slemkoe6889de2006-08-12 00:32:53 +0000311 printf("testMulti()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000312
Marc Slemkoe6889de2006-08-12 00:32:53 +0000313 hello.string_thing = "Hello2";
314 hello.byte_thing = arg0;
315 hello.i32_thing = arg1;
316 hello.i64_thing = (int64_t)arg2;
Marc Slemkoe6889de2006-08-12 00:32:53 +0000317 }
Marc Slemkobf4fd192006-08-15 21:29:39 +0000318
Sebastian Zenker042580f2019-01-29 15:48:12 +0100319 void testException(const std::string& arg) override {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000320 printf("testException(%s)\n", arg.c_str());
Mark Sleed3d733a2006-09-01 22:19:06 +0000321 if (arg.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000322 Xception e;
323 e.errorCode = 1001;
David Reiss9813cbe2009-01-31 21:39:11 +0000324 e.message = arg;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000325 throw e;
Roger Meier1f8b48f2012-05-02 22:56:47 +0000326 } else if (arg.compare("TException") == 0) {
T Jake Lucianib5e62212009-01-31 22:36:20 +0000327 apache::thrift::TException e;
David Reiss55ff70f2008-06-11 00:58:25 +0000328 throw e;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000329 } else {
330 Xtruct result;
331 result.string_thing = arg;
332 return;
333 }
334 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000335
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100336 void testMultiException(Xtruct& result,
337 const std::string& arg0,
Sebastian Zenker042580f2019-01-29 15:48:12 +0100338 const std::string& arg1) override {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000339
Marc Slemko71d4e472006-08-15 22:34:04 +0000340 printf("testMultiException(%s, %s)\n", arg0.c_str(), arg1.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000341
Mark Sleef5f2be42006-09-05 21:05:31 +0000342 if (arg0.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000343 Xception e;
344 e.errorCode = 1001;
345 e.message = "This is an Xception";
346 throw e;
Mark Sleef5f2be42006-09-05 21:05:31 +0000347 } else if (arg0.compare("Xception2") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000348 Xception2 e;
349 e.errorCode = 2002;
350 e.struct_thing.string_thing = "This is an Xception2";
351 throw e;
352 } else {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000353 result.string_thing = arg1;
Mark Slee1921d202007-01-24 19:43:06 +0000354 return;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000355 }
356 }
David Reiss2ab6fe82008-02-18 02:11:44 +0000357
Sebastian Zenker042580f2019-01-29 15:48:12 +0100358 void testOneway(const int32_t aNum) override {
James E. King, III58402ff2017-11-17 14:41:46 -0500359 printf("testOneway(%d): call received\n", aNum);
David Reiss2ab6fe82008-02-18 02:11:44 +0000360 }
Mark Sleee8540632006-05-30 09:24:40 +0000361};
362
James E. King, III58402ff2017-11-17 14:41:46 -0500363class SecondHandler : public SecondServiceIf
364{
365 public:
Sebastian Zenker042580f2019-01-29 15:48:12 +0100366 void secondtestString(std::string& result, const std::string& thing) override
James E. King, III58402ff2017-11-17 14:41:46 -0500367 { result = "testString(\"" + thing + "\")"; }
James E. King, III39eaae62017-11-19 20:17:33 -0500368};
James E. King, III58402ff2017-11-17 14:41:46 -0500369
David Reissd7192062010-10-06 17:09:33 +0000370class TestProcessorEventHandler : public TProcessorEventHandler {
Sebastian Zenker042580f2019-01-29 15:48:12 +0100371 void* getContext(const char* fn_name, void* serverContext) override {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100372 (void)serverContext;
David Reissd7192062010-10-06 17:09:33 +0000373 return new std::string(fn_name);
374 }
Sebastian Zenker042580f2019-01-29 15:48:12 +0100375 void freeContext(void* ctx, const char* fn_name) override {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100376 (void)fn_name;
David Reissd7192062010-10-06 17:09:33 +0000377 delete static_cast<std::string*>(ctx);
378 }
Sebastian Zenker042580f2019-01-29 15:48:12 +0100379 void preRead(void* ctx, const char* fn_name) override { communicate("preRead", ctx, fn_name); }
380 void postRead(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("postRead", ctx, fn_name);
383 }
Sebastian Zenker042580f2019-01-29 15:48:12 +0100384 void preWrite(void* ctx, const char* fn_name) override { communicate("preWrite", ctx, fn_name); }
385 void postWrite(void* ctx, const char* fn_name, uint32_t bytes) override {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100386 (void)bytes;
David Reissd7192062010-10-06 17:09:33 +0000387 communicate("postWrite", ctx, fn_name);
388 }
Sebastian Zenker042580f2019-01-29 15:48:12 +0100389 void asyncComplete(void* ctx, const char* fn_name) override {
David Reissd7192062010-10-06 17:09:33 +0000390 communicate("asyncComplete", ctx, fn_name);
391 }
Sebastian Zenker042580f2019-01-29 15:48:12 +0100392 void handlerError(void* ctx, const char* fn_name) override {
David Reissd7192062010-10-06 17:09:33 +0000393 communicate("handlerError", ctx, fn_name);
394 }
395
396 void communicate(const char* event, void* ctx, const char* fn_name) {
CJCombrink4a280d52024-03-14 19:57:41 +0100397 std::cout << event << ": " << *static_cast<std::string*>(ctx) << " = " << fn_name << '\n';
David Reissd7192062010-10-06 17:09:33 +0000398 }
399};
400
Roger Meier7e056e72011-07-17 07:28:28 +0000401class TestHandlerAsync : public ThriftTestCobSvIf {
402public:
cyy316723a2019-01-05 16:35:14 +0800403 TestHandlerAsync(std::shared_ptr<TestHandler>& handler) : _delegate(handler) {}
Sebastian Zenker042580f2019-01-29 15:48:12 +0100404 ~TestHandlerAsync() override = default;
Roger Meier7e056e72011-07-17 07:28:28 +0000405
Sebastian Zenker042580f2019-01-29 15:48:12 +0100406 void testVoid(std::function<void()> cob) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000407 _delegate->testVoid();
408 cob();
409 }
410
Sebastian Zenker042580f2019-01-29 15:48:12 +0100411 void testString(std::function<void(std::string const& _return)> cob,
412 const std::string& thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000413 std::string res;
414 _delegate->testString(res, thing);
415 cob(res);
416 }
417
Sebastian Zenker042580f2019-01-29 15:48:12 +0100418 void testBool(std::function<void(bool const& _return)> cob, const bool thing) override {
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900419 bool res = _delegate->testBool(thing);
420 cob(res);
421 }
422
Sebastian Zenker042580f2019-01-29 15:48:12 +0100423 void testByte(std::function<void(int8_t const& _return)> cob, const int8_t thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000424 int8_t res = _delegate->testByte(thing);
425 cob(res);
426 }
427
Sebastian Zenker042580f2019-01-29 15:48:12 +0100428 void testI32(std::function<void(int32_t const& _return)> cob, const int32_t thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000429 int32_t res = _delegate->testI32(thing);
430 cob(res);
431 }
432
Sebastian Zenker042580f2019-01-29 15:48:12 +0100433 void testI64(std::function<void(int64_t const& _return)> cob, const int64_t thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000434 int64_t res = _delegate->testI64(thing);
435 cob(res);
436 }
437
Sebastian Zenker042580f2019-01-29 15:48:12 +0100438 void testDouble(std::function<void(double const& _return)> cob, const double thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000439 double res = _delegate->testDouble(thing);
440 cob(res);
441 }
442
Sebastian Zenker042580f2019-01-29 15:48:12 +0100443 void testBinary(std::function<void(std::string const& _return)> cob,
444 const std::string& thing) override {
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100445 std::string res;
446 _delegate->testBinary(res, thing);
447 cob(res);
448 }
449
CJCombrink4b909092024-04-27 19:51:39 +0200450 void testUuid(::std::function<void(TUuid const& _return)> cob, const TUuid thing) override {
451 TUuid res = _delegate->testUuid(thing);
CJCombrink1d886ca2024-03-23 21:32:28 +0100452 cob(res);
453 }
454
Sebastian Zenker042580f2019-01-29 15:48:12 +0100455 void testStruct(std::function<void(Xtruct const& _return)> cob, const Xtruct& thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000456 Xtruct res;
457 _delegate->testStruct(res, thing);
458 cob(res);
459 }
460
Sebastian Zenker042580f2019-01-29 15:48:12 +0100461 void testNest(std::function<void(Xtruct2 const& _return)> cob, const Xtruct2& thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000462 Xtruct2 res;
463 _delegate->testNest(res, thing);
464 cob(res);
465 }
466
Sebastian Zenker042580f2019-01-29 15:48:12 +0100467 void testMap(std::function<void(std::map<int32_t, int32_t> const& _return)> cob,
468 const std::map<int32_t, int32_t>& thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000469 std::map<int32_t, int32_t> res;
470 _delegate->testMap(res, thing);
471 cob(res);
472 }
473
Sebastian Zenker042580f2019-01-29 15:48:12 +0100474 void testStringMap(
cyy316723a2019-01-05 16:35:14 +0800475 std::function<void(std::map<std::string, std::string> const& _return)> cob,
Sebastian Zenker042580f2019-01-29 15:48:12 +0100476 const std::map<std::string, std::string>& thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000477 std::map<std::string, std::string> res;
478 _delegate->testStringMap(res, thing);
479 cob(res);
480 }
481
Sebastian Zenker042580f2019-01-29 15:48:12 +0100482 void testSet(std::function<void(std::set<int32_t> const& _return)> cob,
483 const std::set<int32_t>& thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000484 std::set<int32_t> res;
485 _delegate->testSet(res, thing);
486 cob(res);
487 }
488
Sebastian Zenker042580f2019-01-29 15:48:12 +0100489 void testList(std::function<void(std::vector<int32_t> const& _return)> cob,
490 const std::vector<int32_t>& thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000491 std::vector<int32_t> res;
492 _delegate->testList(res, thing);
493 cob(res);
494 }
495
Sebastian Zenker042580f2019-01-29 15:48:12 +0100496 void testEnum(std::function<void(Numberz::type const& _return)> cob,
497 const Numberz::type thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000498 Numberz::type res = _delegate->testEnum(thing);
499 cob(res);
500 }
501
Sebastian Zenker042580f2019-01-29 15:48:12 +0100502 void testTypedef(std::function<void(UserId const& _return)> cob, const UserId thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000503 UserId res = _delegate->testTypedef(thing);
504 cob(res);
505 }
506
Sebastian Zenker042580f2019-01-29 15:48:12 +0100507 void testMapMap(
cyy316723a2019-01-05 16:35:14 +0800508 std::function<void(std::map<int32_t, std::map<int32_t, int32_t> > const& _return)> cob,
Sebastian Zenker042580f2019-01-29 15:48:12 +0100509 const int32_t hello) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000510 std::map<int32_t, std::map<int32_t, int32_t> > res;
511 _delegate->testMapMap(res, hello);
512 cob(res);
513 }
514
Sebastian Zenker042580f2019-01-29 15:48:12 +0100515 void testInsanity(
cyy316723a2019-01-05 16:35:14 +0800516 std::function<void(std::map<UserId, std::map<Numberz::type, Insanity> > const& _return)> cob,
Sebastian Zenker042580f2019-01-29 15:48:12 +0100517 const Insanity& argument) override {
Jake Farrell5d02b802014-01-07 21:42:01 -0500518 std::map<UserId, std::map<Numberz::type, Insanity> > res;
Roger Meier7e056e72011-07-17 07:28:28 +0000519 _delegate->testInsanity(res, argument);
520 cob(res);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100521 }
Roger Meier7e056e72011-07-17 07:28:28 +0000522
Sebastian Zenker042580f2019-01-29 15:48:12 +0100523 void testMulti(std::function<void(Xtruct const& _return)> cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100524 const int8_t arg0,
525 const int32_t arg1,
526 const int64_t arg2,
527 const std::map<int16_t, std::string>& arg3,
528 const Numberz::type arg4,
Sebastian Zenker042580f2019-01-29 15:48:12 +0100529 const UserId arg5) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000530 Xtruct res;
531 _delegate->testMulti(res, arg0, arg1, arg2, arg3, arg4, arg5);
532 cob(res);
533 }
534
Sebastian Zenker042580f2019-01-29 15:48:12 +0100535 void testException(
cyy316723a2019-01-05 16:35:14 +0800536 std::function<void()> cob,
537 std::function<void(::apache::thrift::TDelayedException* _throw)> exn_cob,
Sebastian Zenker042580f2019-01-29 15:48:12 +0100538 const std::string& arg) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000539 try {
540 _delegate->testException(arg);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100541 } catch (const apache::thrift::TException& e) {
Roger Meier7e056e72011-07-17 07:28:28 +0000542 exn_cob(apache::thrift::TDelayedException::delayException(e));
543 return;
544 }
545 cob();
546 }
547
Sebastian Zenker042580f2019-01-29 15:48:12 +0100548 void testMultiException(
cyy316723a2019-01-05 16:35:14 +0800549 std::function<void(Xtruct const& _return)> cob,
550 std::function<void(::apache::thrift::TDelayedException* _throw)> exn_cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100551 const std::string& arg0,
Sebastian Zenker042580f2019-01-29 15:48:12 +0100552 const std::string& arg1) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000553 Xtruct res;
554 try {
555 _delegate->testMultiException(res, arg0, arg1);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100556 } catch (const apache::thrift::TException& e) {
Roger Meier7e056e72011-07-17 07:28:28 +0000557 exn_cob(apache::thrift::TDelayedException::delayException(e));
558 return;
559 }
560 cob(res);
561 }
562
Sebastian Zenker042580f2019-01-29 15:48:12 +0100563 void testOneway(std::function<void()> cob, const int32_t secondsToSleep) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000564 _delegate->testOneway(secondsToSleep);
565 cob();
566 }
567
568protected:
cyy316723a2019-01-05 16:35:14 +0800569 std::shared_ptr<TestHandler> _delegate;
Roger Meier7e056e72011-07-17 07:28:28 +0000570};
571
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900572namespace po = boost::program_options;
573
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100574int main(int argc, char** argv) {
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +0530575
Nobuaki Sukegawac8744082016-03-09 19:55:56 +0900576 string testDir = boost::filesystem::system_complete(argv[0]).parent_path().parent_path().parent_path().string();
577 string certPath = testDir + "/keys/server.crt";
578 string keyPath = testDir + "/keys/server.key";
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +0530579
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100580#if _WIN32
Jake Farrell5d02b802014-01-07 21:42:01 -0500581 transport::TWinsockSingleton::create();
582#endif
Mark Sleee8540632006-05-30 09:24:40 +0000583 int port = 9090;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000584 bool ssl = false;
James E. King IIIb2b767e2018-09-15 20:32:04 +0000585 bool zlib = false;
Roger Meierca142b02011-06-07 17:59:07 +0000586 string transport_type = "buffered";
587 string protocol_type = "binary";
588 string server_type = "simple";
589 string domain_socket = "";
pavlodd08f6e2015-10-08 16:43:56 -0400590 bool abstract_namespace = false;
Roger Meierca142b02011-06-07 17:59:07 +0000591 size_t workers = 4;
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900592 int string_limit = 0;
593 int container_limit = 0;
Marc Slemko6be374b2006-08-04 03:16:25 +0000594
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900595 po::options_description desc("Allowed options");
596 desc.add_options()
597 ("help,h", "produce help message")
598 ("port", po::value<int>(&port)->default_value(port), "Port number to listen")
599 ("domain-socket", po::value<string>(&domain_socket) ->default_value(domain_socket), "Unix Domain Socket (e.g. /tmp/ThriftTest.thrift)")
600 ("abstract-namespace", "Create the domain socket in the Abstract Namespace (no connection with filesystem pathnames)")
601 ("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 -0700602 ("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 -0500603 ("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 +0900604 ("ssl", "Encrypted Transport using SSL")
James E. King IIIb2b767e2018-09-15 20:32:04 +0000605 ("zlib", "Wrapped Transport using Zlib")
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900606 ("processor-events", "processor-events")
607 ("workers,n", po::value<size_t>(&workers)->default_value(workers), "Number of thread pools workers. Only valid for thread-pool server type")
608 ("string-limit", po::value<int>(&string_limit))
609 ("container-limit", po::value<int>(&container_limit));
Marc Slemko6be374b2006-08-04 03:16:25 +0000610
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900611 po::variables_map vm;
612 po::store(po::parse_command_line(argc, argv, desc), vm);
613 po::notify(vm);
Marc Slemko6be374b2006-08-04 03:16:25 +0000614
Roger Meierca142b02011-06-07 17:59:07 +0000615 if (vm.count("help")) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100616 cout << desc << "\n";
617 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000618 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500619
Marc Slemko6be374b2006-08-04 03:16:25 +0000620 try {
Roger Meierca142b02011-06-07 17:59:07 +0000621 if (!server_type.empty()) {
622 if (server_type == "simple") {
623 } else if (server_type == "thread-pool") {
624 } else if (server_type == "threaded") {
625 } else if (server_type == "nonblocking") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000626 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100627 throw invalid_argument("Unknown server type " + server_type);
Roger Meierca142b02011-06-07 17:59:07 +0000628 }
629 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500630
Roger Meierca142b02011-06-07 17:59:07 +0000631 if (!protocol_type.empty()) {
632 if (protocol_type == "binary") {
Roger Meier284101c2014-03-11 21:20:35 +0100633 } else if (protocol_type == "compact") {
Roger Meierca142b02011-06-07 17:59:07 +0000634 } else if (protocol_type == "json") {
Dave Watson792db4e2015-01-16 11:22:01 -0800635 } else if (protocol_type == "header") {
James E. King, III39eaae62017-11-19 20:17:33 -0500636 } else if (protocol_type == "multi") { // multiplexed binary
637 } else if (protocol_type == "multic") { // multiplexed compact
638 } else if (protocol_type == "multih") { // multiplexed header
639 } else if (protocol_type == "multij") { // multiplexed json
Roger Meierca142b02011-06-07 17:59:07 +0000640 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100641 throw invalid_argument("Unknown protocol type " + protocol_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000642 }
643 }
644
Roger Meier284101c2014-03-11 21:20:35 +0100645 if (!transport_type.empty()) {
Roger Meierca142b02011-06-07 17:59:07 +0000646 if (transport_type == "buffered") {
647 } else if (transport_type == "framed") {
648 } else if (transport_type == "http") {
penenin1ab096c2020-05-18 12:27:31 -0700649 } else if (transport_type == "websocket") {
James E. King IIIb2b767e2018-09-15 20:32:04 +0000650 } else if (transport_type == "zlib") {
651 // crosstester will pass zlib as a flag and a transport right now...
Marc Slemko6be374b2006-08-04 03:16:25 +0000652 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100653 throw invalid_argument("Unknown transport type " + transport_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000654 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000655 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000656
Bryan Duxbury833ae492010-09-27 17:26:02 +0000657 } catch (std::exception& e) {
CJCombrink4a280d52024-03-14 19:57:41 +0100658 cerr << e.what() << '\n';
Roger Meierca142b02011-06-07 17:59:07 +0000659 cout << desc << "\n";
660 return 1;
Marc Slemko6be374b2006-08-04 03:16:25 +0000661 }
662
Roger Meierca142b02011-06-07 17:59:07 +0000663 if (vm.count("ssl")) {
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000664 ssl = true;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000665 }
666
James E. King IIIb2b767e2018-09-15 20:32:04 +0000667 if (vm.count("zlib")) {
668 zlib = true;
669 }
670
James E. King III9bea32f2018-03-16 16:07:42 -0400671#if defined(HAVE_SIGNAL_H) && defined(SIGPIPE)
672 if (ssl) {
673 signal(SIGPIPE, SIG_IGN); // for OpenSSL, otherwise we end abruptly
674 }
675#endif
676
pavlodd08f6e2015-10-08 16:43:56 -0400677 if (vm.count("abstract-namespace")) {
678 abstract_namespace = true;
679 }
680
Mark Sleee8540632006-05-30 09:24:40 +0000681 // Dispatcher
cyy316723a2019-01-05 16:35:14 +0800682 std::shared_ptr<TProtocolFactory> protocolFactory;
James E. King, III58402ff2017-11-17 14:41:46 -0500683 if (protocol_type == "json" || protocol_type == "multij") {
cyy316723a2019-01-05 16:35:14 +0800684 std::shared_ptr<TProtocolFactory> jsonProtocolFactory(new TJSONProtocolFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000685 protocolFactory = jsonProtocolFactory;
James E. King, III58402ff2017-11-17 14:41:46 -0500686 } else if (protocol_type == "compact" || protocol_type == "multic") {
Sebastian Zenker042580f2019-01-29 15:48:12 +0100687 auto *compactProtocolFactory = new TCompactProtocolFactoryT<TBufferBase>();
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900688 compactProtocolFactory->setContainerSizeLimit(container_limit);
689 compactProtocolFactory->setStringSizeLimit(string_limit);
690 protocolFactory.reset(compactProtocolFactory);
James E. King, III58402ff2017-11-17 14:41:46 -0500691 } else if (protocol_type == "header" || protocol_type == "multih") {
cyy316723a2019-01-05 16:35:14 +0800692 std::shared_ptr<TProtocolFactory> headerProtocolFactory(new THeaderProtocolFactory());
Dave Watson792db4e2015-01-16 11:22:01 -0800693 protocolFactory = headerProtocolFactory;
Roger Meierca142b02011-06-07 17:59:07 +0000694 } else {
Sebastian Zenker042580f2019-01-29 15:48:12 +0100695 auto* binaryProtocolFactory = new TBinaryProtocolFactoryT<TBufferBase>();
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900696 binaryProtocolFactory->setContainerSizeLimit(container_limit);
697 binaryProtocolFactory->setStringSizeLimit(string_limit);
698 protocolFactory.reset(binaryProtocolFactory);
Roger Meierca142b02011-06-07 17:59:07 +0000699 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000700
James E. King, III58402ff2017-11-17 14:41:46 -0500701 // Processors
cyy316723a2019-01-05 16:35:14 +0800702 std::shared_ptr<TestHandler> testHandler(new TestHandler());
703 std::shared_ptr<TProcessor> testProcessor(new ThriftTestProcessor(testHandler));
Jake Farrell5d02b802014-01-07 21:42:01 -0500704
Roger Meierca142b02011-06-07 17:59:07 +0000705 if (vm.count("processor-events")) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100706 testProcessor->setEventHandler(
cyy316723a2019-01-05 16:35:14 +0800707 std::shared_ptr<TProcessorEventHandler>(new TestProcessorEventHandler()));
David Reissd7192062010-10-06 17:09:33 +0000708 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500709
Mark Sleee8540632006-05-30 09:24:40 +0000710 // Transport
cyy316723a2019-01-05 16:35:14 +0800711 std::shared_ptr<TSSLSocketFactory> sslSocketFactory;
712 std::shared_ptr<TServerSocket> serverSocket;
Mark Sleee8540632006-05-30 09:24:40 +0000713
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000714 if (ssl) {
cyy316723a2019-01-05 16:35:14 +0800715 sslSocketFactory = std::shared_ptr<TSSLSocketFactory>(new TSSLSocketFactory());
Nobuaki Sukegawac8744082016-03-09 19:55:56 +0900716 sslSocketFactory->loadCertificate(certPath.c_str());
717 sslSocketFactory->loadPrivateKey(keyPath.c_str());
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000718 sslSocketFactory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
James E. King III70b33fb2018-03-11 10:57:10 -0400719 if (server_type != "nonblocking") {
cyy316723a2019-01-05 16:35:14 +0800720 serverSocket = std::shared_ptr<TServerSocket>(new TSSLServerSocket(port, sslSocketFactory));
James E. King III70b33fb2018-03-11 10:57:10 -0400721 }
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000722 } else {
Roger Meierc94b2932014-02-22 20:07:33 +0100723 if (domain_socket != "") {
pavlodd08f6e2015-10-08 16:43:56 -0400724 if (abstract_namespace) {
725 std::string abstract_socket("\0", 1);
726 abstract_socket += domain_socket;
cyy316723a2019-01-05 16:35:14 +0800727 serverSocket = std::shared_ptr<TServerSocket>(new TServerSocket(abstract_socket));
pavlodd08f6e2015-10-08 16:43:56 -0400728 } else {
729 unlink(domain_socket.c_str());
cyy316723a2019-01-05 16:35:14 +0800730 serverSocket = std::shared_ptr<TServerSocket>(new TServerSocket(domain_socket));
pavlodd08f6e2015-10-08 16:43:56 -0400731 }
Roger Meierc94b2932014-02-22 20:07:33 +0100732 port = 0;
733 } else {
cyy316723a2019-01-05 16:35:14 +0800734 serverSocket = std::shared_ptr<TServerSocket>(new TServerSocket(port));
Roger Meierc94b2932014-02-22 20:07:33 +0100735 }
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000736 }
Roger Meierca142b02011-06-07 17:59:07 +0000737
Mark Sleed788b2e2006-09-07 01:26:35 +0000738 // Factory
cyy316723a2019-01-05 16:35:14 +0800739 std::shared_ptr<TTransportFactory> transportFactory;
Jake Farrell5d02b802014-01-07 21:42:01 -0500740
Roger Meier7e056e72011-07-17 07:28:28 +0000741 if (transport_type == "http" && server_type != "nonblocking") {
cyy316723a2019-01-05 16:35:14 +0800742 transportFactory = std::make_shared<THttpServerTransportFactory>();
penenin1ab096c2020-05-18 12:27:31 -0700743 } else if (transport_type == "websocket" && server_type != "nonblocking") {
744 if (protocol_type == "json" || protocol_type == "multij") {
745 transportFactory = std::make_shared<TTextWebSocketServerTransportFactory>();
746 } else {
747 transportFactory = std::make_shared<TBinaryWebSocketServerTransportFactory>();
748 }
Roger Meierca142b02011-06-07 17:59:07 +0000749 } else if (transport_type == "framed") {
cyy316723a2019-01-05 16:35:14 +0800750 transportFactory = std::make_shared<TFramedTransportFactory>();
Roger Meierca142b02011-06-07 17:59:07 +0000751 } else {
cyy316723a2019-01-05 16:35:14 +0800752 transportFactory = std::make_shared<TBufferedTransportFactory>();
James E. King IIIb2b767e2018-09-15 20:32:04 +0000753 }
754
755 if (zlib) {
Mario Emmenlauer2d2df9e2020-04-24 18:02:18 +0200756 // currently TZlibTransportFactory is the only factory than can wrap another:
757 transportFactory = std::make_shared<TZlibTransportFactory>(transportFactory);
Roger Meierca142b02011-06-07 17:59:07 +0000758 }
Mark Sleed788b2e2006-09-07 01:26:35 +0000759
Roger Meierca142b02011-06-07 17:59:07 +0000760 // Server Info
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100761 cout << "Starting \"" << server_type << "\" server (" << transport_type << "/" << protocol_type
pavlodd08f6e2015-10-08 16:43:56 -0400762 << ") listen on: ";
763 if (abstract_namespace) {
764 cout << '@';
765 }
766 cout << domain_socket;
Roger Meierca142b02011-06-07 17:59:07 +0000767 if (port != 0) {
768 cout << port;
769 }
CJCombrink4a280d52024-03-14 19:57:41 +0100770 cout << '\n';
Mark Sleee8540632006-05-30 09:24:40 +0000771
James E. King, III58402ff2017-11-17 14:41:46 -0500772 // Multiplexed Processor if needed
773 if (boost::starts_with(protocol_type, "multi")) {
cyy316723a2019-01-05 16:35:14 +0800774 std::shared_ptr<SecondHandler> secondHandler(new SecondHandler());
775 std::shared_ptr<SecondServiceProcessor> secondProcessor(new SecondServiceProcessor(secondHandler));
James E. King, III58402ff2017-11-17 14:41:46 -0500776
cyy316723a2019-01-05 16:35:14 +0800777 std::shared_ptr<TMultiplexedProcessor> multiplexedProcessor(new TMultiplexedProcessor());
James E. King, III58402ff2017-11-17 14:41:46 -0500778 multiplexedProcessor->registerDefault(testProcessor); // non-multi clients go to the default processor (multi:binary, multic:compact, ...)
779 multiplexedProcessor->registerProcessor("ThriftTest", testProcessor);
780 multiplexedProcessor->registerProcessor("SecondService", secondProcessor);
cyy316723a2019-01-05 16:35:14 +0800781 testProcessor = std::dynamic_pointer_cast<TProcessor>(multiplexedProcessor);
James E. King, III58402ff2017-11-17 14:41:46 -0500782 }
783
Roger Meierca142b02011-06-07 17:59:07 +0000784 // Server
cyy316723a2019-01-05 16:35:14 +0800785 std::shared_ptr<apache::thrift::server::TServer> server;
Jake Farrell5d02b802014-01-07 21:42:01 -0500786
Roger Meierca142b02011-06-07 17:59:07 +0000787 if (server_type == "simple") {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100788 server.reset(new TSimpleServer(testProcessor, serverSocket, transportFactory, protocolFactory));
Roger Meierca142b02011-06-07 17:59:07 +0000789 } else if (server_type == "thread-pool") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000790
cyyca8af9b2019-01-11 22:13:12 +0800791 std::shared_ptr<ThreadFactory> threadFactory
792 = std::shared_ptr<ThreadFactory>(new ThreadFactory());
Marc Slemko6be374b2006-08-04 03:16:25 +0000793
cyy316723a2019-01-05 16:35:14 +0800794 std::shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workers);
Marc Slemko6be374b2006-08-04 03:16:25 +0000795 threadManager->threadFactory(threadFactory);
Marc Slemko6be374b2006-08-04 03:16:25 +0000796 threadManager->start();
797
Jake Farrell5d02b802014-01-07 21:42:01 -0500798 server.reset(new TThreadPoolServer(testProcessor,
Roger Meierca142b02011-06-07 17:59:07 +0000799 serverSocket,
Mark Sleed788b2e2006-09-07 01:26:35 +0000800 transportFactory,
Mark Sleea3302652006-10-25 19:03:32 +0000801 protocolFactory,
Jake Farrell5d02b802014-01-07 21:42:01 -0500802 threadManager));
Roger Meierca142b02011-06-07 17:59:07 +0000803 } else if (server_type == "threaded") {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100804 server.reset(
805 new TThreadedServer(testProcessor, serverSocket, transportFactory, protocolFactory));
Roger Meierca142b02011-06-07 17:59:07 +0000806 } else if (server_type == "nonblocking") {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100807 if (transport_type == "http") {
cyy316723a2019-01-05 16:35:14 +0800808 std::shared_ptr<TestHandlerAsync> testHandlerAsync(new TestHandlerAsync(testHandler));
809 std::shared_ptr<TAsyncProcessor> testProcessorAsync(
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100810 new ThriftTestAsyncProcessor(testHandlerAsync));
cyy316723a2019-01-05 16:35:14 +0800811 std::shared_ptr<TAsyncBufferProcessor> testBufferProcessor(
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100812 new TAsyncProtocolProcessor(testProcessorAsync, protocolFactory));
Jake Farrell5d02b802014-01-07 21:42:01 -0500813
814 // not loading nonblockingServer into "server" because
815 // TEvhttpServer doesn't inherit from TServer, and doesn't
816 // provide a stop method.
Roger Meier7e056e72011-07-17 07:28:28 +0000817 TEvhttpServer nonblockingServer(testBufferProcessor, port);
818 nonblockingServer.serve();
James E. King III70b33fb2018-03-11 10:57:10 -0400819 } else if (transport_type == "framed") {
cyy316723a2019-01-05 16:35:14 +0800820 std::shared_ptr<transport::TNonblockingServerTransport> nbSocket;
James E. King III9bea32f2018-03-16 16:07:42 -0400821 nbSocket.reset(
822 ssl ? new transport::TNonblockingSSLServerSocket(port, sslSocketFactory)
823 : new transport::TNonblockingServerSocket(port));
Divya Thaluru808d1432017-08-06 16:36:36 -0700824 server.reset(new TNonblockingServer(testProcessor, protocolFactory, nbSocket));
James E. King III70b33fb2018-03-11 10:57:10 -0400825 } else {
CJCombrink4a280d52024-03-14 19:57:41 +0100826 cerr << "server-type nonblocking requires transport of http or framed" << '\n';
James E. King III9bea32f2018-03-16 16:07:42 -0400827 exit(1);
Roger Meier7e056e72011-07-17 07:28:28 +0000828 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000829 }
830
Sebastian Zenker042580f2019-01-29 15:48:12 +0100831 if (server.get() != nullptr) {
Dave Watson792db4e2015-01-16 11:22:01 -0800832 if (protocol_type == "header") {
James E. King, IIIdf899132016-11-12 15:16:30 -0500833 // Tell the server to use the same protocol for input / output
Dave Watson792db4e2015-01-16 11:22:01 -0800834 // if using header
cyy316723a2019-01-05 16:35:14 +0800835 server->setOutputProtocolFactory(std::shared_ptr<TProtocolFactory>());
Dave Watson792db4e2015-01-16 11:22:01 -0800836 }
Mario Emmenlauer2d2df9e2020-04-24 18:02:18 +0200837
cyyca8af9b2019-01-11 22:13:12 +0800838 apache::thrift::concurrency::ThreadFactory factory;
Jake Farrell5d02b802014-01-07 21:42:01 -0500839 factory.setDetached(false);
cyy316723a2019-01-05 16:35:14 +0800840 std::shared_ptr<apache::thrift::concurrency::Runnable> serverThreadRunner(server);
841 std::shared_ptr<apache::thrift::concurrency::Thread> thread
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100842 = factory.newThread(serverThreadRunner);
Jake Farrell5d02b802014-01-07 21:42:01 -0500843
James E. King III9bea32f2018-03-16 16:07:42 -0400844#ifdef HAVE_SIGNAL_H
845 signal(SIGINT, signal_handler);
846#endif
847
848 thread->start();
849 gMonitor.waitForever(); // wait for a shutdown signal
Mario Emmenlauer2d2df9e2020-04-24 18:02:18 +0200850
James E. King III9bea32f2018-03-16 16:07:42 -0400851#ifdef HAVE_SIGNAL_H
852 signal(SIGINT, SIG_DFL);
853#endif
Jake Farrell5d02b802014-01-07 21:42:01 -0500854
855 server->stop();
856 thread->join();
857 server.reset();
858 }
859
CJCombrink4a280d52024-03-14 19:57:41 +0100860 cout << "done." << '\n';
Mark Sleee8540632006-05-30 09:24:40 +0000861 return 0;
862}