blob: dc95af6ba1539f3789d58ad289f886e1097a1b87 [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>
Federico Giovanardi3b21bc92025-08-22 15:29:24 +020034#include <thrift/transport/PlatformSocket.h>
Roger Meier49ff8b12012-04-13 09:12:31 +000035#include <thrift/transport/THttpServer.h>
36#include <thrift/transport/THttpTransport.h>
James E. King III70b33fb2018-03-11 10:57:10 -040037#include <thrift/transport/TNonblockingSSLServerSocket.h>
James E. King, III58402ff2017-11-17 14:41:46 -050038#include <thrift/transport/TNonblockingServerSocket.h>
39#include <thrift/transport/TSSLServerSocket.h>
40#include <thrift/transport/TSSLSocket.h>
41#include <thrift/transport/TServerSocket.h>
Roger Meier49ff8b12012-04-13 09:12:31 +000042#include <thrift/transport/TTransportUtils.h>
penenin1ab096c2020-05-18 12:27:31 -070043#include <thrift/transport/TWebSocketServer.h>
James E. King IIIb2b767e2018-09-15 20:32:04 +000044#include <thrift/transport/TZlibTransport.h>
James E. King, III58402ff2017-11-17 14:41:46 -050045
46#include "SecondService.h"
Mark Slee95771002006-06-07 06:53:25 +000047#include "ThriftTest.h"
Marc Slemko6be374b2006-08-04 03:16:25 +000048
James E. King, III7edc8fa2017-01-20 10:11:41 -050049#ifdef HAVE_STDINT_H
50#include <stdint.h>
51#endif
52#ifdef HAVE_INTTYPES_H
53#include <inttypes.h>
54#endif
James E. King III9bea32f2018-03-16 16:07:42 -040055#ifdef HAVE_SIGNAL_H
56#include <signal.h>
57#endif
Federico Giovanardi3b21bc92025-08-22 15:29:24 +020058#ifdef HAVE_SYS_SOCKET_H
59#include <sys/socket.h>
60#endif
61#ifdef HAVE_SYS_UN_H
62#include <sys/un.h>
63#endif
James E. King, III7edc8fa2017-01-20 10:11:41 -050064
Marc Slemko6be374b2006-08-04 03:16:25 +000065#include <iostream>
Federico Giovanardi3b21bc92025-08-22 15:29:24 +020066#include <memory>
Marc Slemko6be374b2006-08-04 03:16:25 +000067#include <sstream>
Federico Giovanardi3b21bc92025-08-22 15:29:24 +020068#include <stdexcept>
Marc Slemko6be374b2006-08-04 03:16:25 +000069
James E. King, III58402ff2017-11-17 14:41:46 -050070#include <boost/algorithm/string.hpp>
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +053071#include <boost/filesystem.hpp>
Federico Giovanardi3b21bc92025-08-22 15:29:24 +020072#include <boost/program_options.hpp>
Roger Meierca142b02011-06-07 17:59:07 +000073
Jake Farrell5d02b802014-01-07 21:42:01 -050074#if _WIN32
Konrad Grochowski16a23a62014-11-13 15:33:38 +010075#include <thrift/windows/TWinsockSingleton.h>
Jake Farrell5d02b802014-01-07 21:42:01 -050076#endif
David Reissbc3dddb2007-08-22 23:20:24 +000077
Mark Sleee8540632006-05-30 09:24:40 +000078using namespace std;
79
T Jake Lucianib5e62212009-01-31 22:36:20 +000080using namespace apache::thrift;
James E. King, III82ae9572017-08-05 12:23:54 -040081using namespace apache::thrift::async;
T Jake Lucianib5e62212009-01-31 22:36:20 +000082using namespace apache::thrift::concurrency;
83using namespace apache::thrift::protocol;
84using namespace apache::thrift::transport;
85using namespace apache::thrift::server;
Marc Slemko6be374b2006-08-04 03:16:25 +000086
Marc Slemkobf4fd192006-08-15 21:29:39 +000087using namespace thrift::test;
88
James E. King III9bea32f2018-03-16 16:07:42 -040089// to handle a controlled shutdown, signal handling is mandatory
90#ifdef HAVE_SIGNAL_H
91apache::thrift::concurrency::Monitor gMonitor;
92void signal_handler(int signum)
93{
94 if (signum == SIGINT) {
95 gMonitor.notifyAll();
96 }
97}
98#endif
99
Mark Sleed2655522006-09-05 22:09:57 +0000100class TestHandler : public ThriftTestIf {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100101public:
Sebastian Zenker042580f2019-01-29 15:48:12 +0100102 TestHandler() = default;
Mark Sleee8540632006-05-30 09:24:40 +0000103
Sebastian Zenker042580f2019-01-29 15:48:12 +0100104 void testVoid() override { printf("testVoid()\n"); }
Mark Sleee8540632006-05-30 09:24:40 +0000105
Sebastian Zenker042580f2019-01-29 15:48:12 +0100106 void testString(string& out, const string& thing) override {
Mark Sleee8540632006-05-30 09:24:40 +0000107 printf("testString(\"%s\")\n", thing.c_str());
Mark Slee1921d202007-01-24 19:43:06 +0000108 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000109 }
110
Sebastian Zenker042580f2019-01-29 15:48:12 +0100111 bool testBool(const bool thing) override {
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900112 printf("testBool(%s)\n", thing ? "true" : "false");
113 return thing;
114 }
115
Sebastian Zenker042580f2019-01-29 15:48:12 +0100116 int8_t testByte(const int8_t thing) override {
Mark Sleee8540632006-05-30 09:24:40 +0000117 printf("testByte(%d)\n", (int)thing);
118 return thing;
119 }
120
Sebastian Zenker042580f2019-01-29 15:48:12 +0100121 int32_t testI32(const int32_t thing) override {
Mark Sleee8540632006-05-30 09:24:40 +0000122 printf("testI32(%d)\n", thing);
123 return thing;
124 }
125
Sebastian Zenker042580f2019-01-29 15:48:12 +0100126 int64_t testI64(const int64_t thing) override {
Roger Meier0e814802014-01-17 21:07:58 +0100127 printf("testI64(%" PRId64 ")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +0000128 return thing;
129 }
130
Sebastian Zenker042580f2019-01-29 15:48:12 +0100131 double testDouble(const double thing) override {
Roger Meiera8cef6e2011-07-17 18:55:59 +0000132 printf("testDouble(%f)\n", thing);
Mark Sleec98d0502006-09-06 02:42:25 +0000133 return thing;
134 }
135
Sebastian Zenker042580f2019-01-29 15:48:12 +0100136 void testBinary(std::string& _return, const std::string& thing) override {
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100137 std::ostringstream hexstr;
138 hexstr << std::hex << thing;
James E. King III3ae30422018-03-12 07:33:22 -0400139 printf("testBinary(%lu: %s)\n", safe_numeric_cast<unsigned long>(thing.size()), hexstr.str().c_str());
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100140 _return = thing;
141 }
142
Carel Combrinkaee3f632024-09-05 18:26:42 +0000143 void testUuid(apache::thrift::TUuid& _return, const apache::thrift::TUuid& thing) override {
CJCombrink4b909092024-04-27 19:51:39 +0200144 printf("testUuid(\"{%s}\")\n", to_string(thing).c_str());
Carel Combrinkaee3f632024-09-05 18:26:42 +0000145 _return = thing;
CJCombrink1d886ca2024-03-23 21:32:28 +0100146 }
147
Sebastian Zenker042580f2019-01-29 15:48:12 +0100148 void testStruct(Xtruct& out, const Xtruct& thing) override {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100149 printf("testStruct({\"%s\", %d, %d, %" PRId64 "})\n",
150 thing.string_thing.c_str(),
151 (int)thing.byte_thing,
152 thing.i32_thing,
153 thing.i64_thing);
Mark Slee1921d202007-01-24 19:43:06 +0000154 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000155 }
156
Sebastian Zenker042580f2019-01-29 15:48:12 +0100157 void testNest(Xtruct2& out, const Xtruct2& nest) override {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100158 const Xtruct& thing = nest.struct_thing;
159 printf("testNest({%d, {\"%s\", %d, %d, %" PRId64 "}, %d})\n",
160 (int)nest.byte_thing,
161 thing.string_thing.c_str(),
162 (int)thing.byte_thing,
163 thing.i32_thing,
164 thing.i64_thing,
165 nest.i32_thing);
Mark Slee1921d202007-01-24 19:43:06 +0000166 out = nest;
Mark Sleee8540632006-05-30 09:24:40 +0000167 }
168
Sebastian Zenker042580f2019-01-29 15:48:12 +0100169 void testMap(map<int32_t, int32_t>& out, const map<int32_t, int32_t>& thing) override {
Mark Sleee8540632006-05-30 09:24:40 +0000170 printf("testMap({");
171 map<int32_t, int32_t>::const_iterator m_iter;
172 bool first = true;
173 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
174 if (first) {
175 first = false;
176 } else {
177 printf(", ");
178 }
179 printf("%d => %d", m_iter->first, m_iter->second);
180 }
181 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000182 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000183 }
184
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100185 void testStringMap(map<std::string, std::string>& out,
Sebastian Zenker042580f2019-01-29 15:48:12 +0100186 const map<std::string, std::string>& thing) override {
Roger Meierd3b9dca2011-06-24 14:01:10 +0000187 printf("testMap({");
188 map<std::string, std::string>::const_iterator m_iter;
189 bool first = true;
190 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
191 if (first) {
192 first = false;
193 } else {
194 printf(", ");
195 }
196 printf("%s => %s", (m_iter->first).c_str(), (m_iter->second).c_str());
197 }
198 printf("})\n");
199 out = thing;
200 }
201
Sebastian Zenker042580f2019-01-29 15:48:12 +0100202 void testSet(set<int32_t>& out, const set<int32_t>& thing) override {
Mark Sleee8540632006-05-30 09:24:40 +0000203 printf("testSet({");
204 set<int32_t>::const_iterator s_iter;
205 bool first = true;
206 for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) {
207 if (first) {
208 first = false;
209 } else {
210 printf(", ");
211 }
212 printf("%d", *s_iter);
213 }
214 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000215 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000216 }
217
Sebastian Zenker042580f2019-01-29 15:48:12 +0100218 void testList(vector<int32_t>& out, const vector<int32_t>& thing) override {
Mark Sleee8540632006-05-30 09:24:40 +0000219 printf("testList({");
Mark Sleeb9acf982006-10-10 01:57:32 +0000220 vector<int32_t>::const_iterator l_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000221 bool first = true;
222 for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) {
223 if (first) {
224 first = false;
225 } else {
226 printf(", ");
227 }
228 printf("%d", *l_iter);
229 }
230 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000231 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000232 }
233
Sebastian Zenker042580f2019-01-29 15:48:12 +0100234 Numberz::type testEnum(const Numberz::type thing) override {
Mark Sleee8540632006-05-30 09:24:40 +0000235 printf("testEnum(%d)\n", thing);
236 return thing;
237 }
238
Sebastian Zenker042580f2019-01-29 15:48:12 +0100239 UserId testTypedef(const UserId thing) override {
Roger Meier0e814802014-01-17 21:07:58 +0100240 printf("testTypedef(%" PRId64 ")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +0000241 return thing;
242 }
243
Sebastian Zenker042580f2019-01-29 15:48:12 +0100244 void testMapMap(map<int32_t, map<int32_t, int32_t> >& mapmap, const int32_t hello) override {
Mark Sleee8540632006-05-30 09:24:40 +0000245 printf("testMapMap(%d)\n", hello);
Mark Sleee8540632006-05-30 09:24:40 +0000246
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100247 map<int32_t, int32_t> pos;
248 map<int32_t, int32_t> neg;
Mark Sleee8540632006-05-30 09:24:40 +0000249 for (int i = 1; i < 5; i++) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100250 pos.insert(make_pair(i, i));
251 neg.insert(make_pair(-i, -i));
Mark Sleee8540632006-05-30 09:24:40 +0000252 }
253
254 mapmap.insert(make_pair(4, pos));
255 mapmap.insert(make_pair(-4, neg));
Mark Sleee8540632006-05-30 09:24:40 +0000256 }
257
Sebastian Zenker042580f2019-01-29 15:48:12 +0100258 void testInsanity(map<UserId, map<Numberz::type, Insanity> >& insane, const Insanity& argument) override {
Mark Sleee8540632006-05-30 09:24:40 +0000259 printf("testInsanity()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000260
Mark Sleee8540632006-05-30 09:24:40 +0000261 Insanity looney;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000262 map<Numberz::type, Insanity> first_map;
263 map<Numberz::type, Insanity> second_map;
Mark Sleee8540632006-05-30 09:24:40 +0000264
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900265 first_map.insert(make_pair(Numberz::TWO, argument));
266 first_map.insert(make_pair(Numberz::THREE, argument));
Mark Sleee8540632006-05-30 09:24:40 +0000267
Bryan Duxbury833ae492010-09-27 17:26:02 +0000268 second_map.insert(make_pair(Numberz::SIX, looney));
Mark Sleee8540632006-05-30 09:24:40 +0000269
Mark Sleee8540632006-05-30 09:24:40 +0000270 insane.insert(make_pair(1, first_map));
271 insane.insert(make_pair(2, second_map));
272
273 printf("return");
274 printf(" = {");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100275 map<UserId, map<Numberz::type, Insanity> >::const_iterator i_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000276 for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) {
Roger Meier0e814802014-01-17 21:07:58 +0100277 printf("%" PRId64 " => {", i_iter->first);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100278 map<Numberz::type, Insanity>::const_iterator i2_iter;
279 for (i2_iter = i_iter->second.begin(); i2_iter != i_iter->second.end(); ++i2_iter) {
Mark Sleee8540632006-05-30 09:24:40 +0000280 printf("%d => {", i2_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000281 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
282 map<Numberz::type, UserId>::const_iterator um;
Mark Sleee8540632006-05-30 09:24:40 +0000283 printf("{");
284 for (um = userMap.begin(); um != userMap.end(); ++um) {
Roger Meier0e814802014-01-17 21:07:58 +0100285 printf("%d => %" PRId64 ", ", um->first, um->second);
Mark Sleee8540632006-05-30 09:24:40 +0000286 }
287 printf("}, ");
288
Mark Sleeb9acf982006-10-10 01:57:32 +0000289 vector<Xtruct> xtructs = i2_iter->second.xtructs;
290 vector<Xtruct>::const_iterator x;
Mark Sleee8540632006-05-30 09:24:40 +0000291 printf("{");
292 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100293 printf("{\"%s\", %d, %d, %" PRId64 "}, ",
294 x->string_thing.c_str(),
295 (int)x->byte_thing,
296 x->i32_thing,
297 x->i64_thing);
Mark Sleee8540632006-05-30 09:24:40 +0000298 }
299 printf("}");
300
301 printf("}, ");
302 }
303 printf("}, ");
304 }
305 printf("}\n");
Mark Sleee8540632006-05-30 09:24:40 +0000306 }
Marc Slemkoe6889de2006-08-12 00:32:53 +0000307
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100308 void testMulti(Xtruct& hello,
309 const int8_t arg0,
310 const int32_t arg1,
311 const int64_t arg2,
312 const std::map<int16_t, std::string>& arg3,
313 const Numberz::type arg4,
Sebastian Zenker042580f2019-01-29 15:48:12 +0100314 const UserId arg5) override {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100315 (void)arg3;
316 (void)arg4;
317 (void)arg5;
Jake Farrell5d02b802014-01-07 21:42:01 -0500318
Marc Slemkoe6889de2006-08-12 00:32:53 +0000319 printf("testMulti()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000320
Marc Slemkoe6889de2006-08-12 00:32:53 +0000321 hello.string_thing = "Hello2";
322 hello.byte_thing = arg0;
323 hello.i32_thing = arg1;
324 hello.i64_thing = (int64_t)arg2;
Marc Slemkoe6889de2006-08-12 00:32:53 +0000325 }
Marc Slemkobf4fd192006-08-15 21:29:39 +0000326
Sebastian Zenker042580f2019-01-29 15:48:12 +0100327 void testException(const std::string& arg) override {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000328 printf("testException(%s)\n", arg.c_str());
Mark Sleed3d733a2006-09-01 22:19:06 +0000329 if (arg.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000330 Xception e;
331 e.errorCode = 1001;
David Reiss9813cbe2009-01-31 21:39:11 +0000332 e.message = arg;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000333 throw e;
Roger Meier1f8b48f2012-05-02 22:56:47 +0000334 } else if (arg.compare("TException") == 0) {
T Jake Lucianib5e62212009-01-31 22:36:20 +0000335 apache::thrift::TException e;
David Reiss55ff70f2008-06-11 00:58:25 +0000336 throw e;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000337 } else {
338 Xtruct result;
339 result.string_thing = arg;
340 return;
341 }
342 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000343
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100344 void testMultiException(Xtruct& result,
345 const std::string& arg0,
Sebastian Zenker042580f2019-01-29 15:48:12 +0100346 const std::string& arg1) override {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000347
Marc Slemko71d4e472006-08-15 22:34:04 +0000348 printf("testMultiException(%s, %s)\n", arg0.c_str(), arg1.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000349
Mark Sleef5f2be42006-09-05 21:05:31 +0000350 if (arg0.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000351 Xception e;
352 e.errorCode = 1001;
353 e.message = "This is an Xception";
354 throw e;
Mark Sleef5f2be42006-09-05 21:05:31 +0000355 } else if (arg0.compare("Xception2") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000356 Xception2 e;
357 e.errorCode = 2002;
358 e.struct_thing.string_thing = "This is an Xception2";
359 throw e;
360 } else {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000361 result.string_thing = arg1;
Mark Slee1921d202007-01-24 19:43:06 +0000362 return;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000363 }
364 }
David Reiss2ab6fe82008-02-18 02:11:44 +0000365
Sebastian Zenker042580f2019-01-29 15:48:12 +0100366 void testOneway(const int32_t aNum) override {
James E. King, III58402ff2017-11-17 14:41:46 -0500367 printf("testOneway(%d): call received\n", aNum);
David Reiss2ab6fe82008-02-18 02:11:44 +0000368 }
Mark Sleee8540632006-05-30 09:24:40 +0000369};
370
James E. King, III58402ff2017-11-17 14:41:46 -0500371class SecondHandler : public SecondServiceIf
372{
373 public:
Sebastian Zenker042580f2019-01-29 15:48:12 +0100374 void secondtestString(std::string& result, const std::string& thing) override
James E. King, III58402ff2017-11-17 14:41:46 -0500375 { result = "testString(\"" + thing + "\")"; }
James E. King, III39eaae62017-11-19 20:17:33 -0500376};
James E. King, III58402ff2017-11-17 14:41:46 -0500377
David Reissd7192062010-10-06 17:09:33 +0000378class TestProcessorEventHandler : public TProcessorEventHandler {
Sebastian Zenker042580f2019-01-29 15:48:12 +0100379 void* getContext(const char* fn_name, void* serverContext) override {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100380 (void)serverContext;
David Reissd7192062010-10-06 17:09:33 +0000381 return new std::string(fn_name);
382 }
Sebastian Zenker042580f2019-01-29 15:48:12 +0100383 void freeContext(void* ctx, const char* fn_name) override {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100384 (void)fn_name;
David Reissd7192062010-10-06 17:09:33 +0000385 delete static_cast<std::string*>(ctx);
386 }
Sebastian Zenker042580f2019-01-29 15:48:12 +0100387 void preRead(void* ctx, const char* fn_name) override { communicate("preRead", ctx, fn_name); }
388 void postRead(void* ctx, const char* fn_name, uint32_t bytes) override {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100389 (void)bytes;
David Reissd7192062010-10-06 17:09:33 +0000390 communicate("postRead", ctx, fn_name);
391 }
Sebastian Zenker042580f2019-01-29 15:48:12 +0100392 void preWrite(void* ctx, const char* fn_name) override { communicate("preWrite", ctx, fn_name); }
393 void postWrite(void* ctx, const char* fn_name, uint32_t bytes) override {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100394 (void)bytes;
David Reissd7192062010-10-06 17:09:33 +0000395 communicate("postWrite", ctx, fn_name);
396 }
Sebastian Zenker042580f2019-01-29 15:48:12 +0100397 void asyncComplete(void* ctx, const char* fn_name) override {
David Reissd7192062010-10-06 17:09:33 +0000398 communicate("asyncComplete", ctx, fn_name);
399 }
Sebastian Zenker042580f2019-01-29 15:48:12 +0100400 void handlerError(void* ctx, const char* fn_name) override {
David Reissd7192062010-10-06 17:09:33 +0000401 communicate("handlerError", ctx, fn_name);
402 }
403
404 void communicate(const char* event, void* ctx, const char* fn_name) {
CJCombrink4a280d52024-03-14 19:57:41 +0100405 std::cout << event << ": " << *static_cast<std::string*>(ctx) << " = " << fn_name << '\n';
David Reissd7192062010-10-06 17:09:33 +0000406 }
407};
408
Roger Meier7e056e72011-07-17 07:28:28 +0000409class TestHandlerAsync : public ThriftTestCobSvIf {
410public:
cyy316723a2019-01-05 16:35:14 +0800411 TestHandlerAsync(std::shared_ptr<TestHandler>& handler) : _delegate(handler) {}
Sebastian Zenker042580f2019-01-29 15:48:12 +0100412 ~TestHandlerAsync() override = default;
Roger Meier7e056e72011-07-17 07:28:28 +0000413
Sebastian Zenker042580f2019-01-29 15:48:12 +0100414 void testVoid(std::function<void()> cob) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000415 _delegate->testVoid();
416 cob();
417 }
418
Sebastian Zenker042580f2019-01-29 15:48:12 +0100419 void testString(std::function<void(std::string const& _return)> cob,
420 const std::string& thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000421 std::string res;
422 _delegate->testString(res, thing);
423 cob(res);
424 }
425
Sebastian Zenker042580f2019-01-29 15:48:12 +0100426 void testBool(std::function<void(bool const& _return)> cob, const bool thing) override {
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900427 bool res = _delegate->testBool(thing);
428 cob(res);
429 }
430
Sebastian Zenker042580f2019-01-29 15:48:12 +0100431 void testByte(std::function<void(int8_t const& _return)> cob, const int8_t thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000432 int8_t res = _delegate->testByte(thing);
433 cob(res);
434 }
435
Sebastian Zenker042580f2019-01-29 15:48:12 +0100436 void testI32(std::function<void(int32_t const& _return)> cob, const int32_t thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000437 int32_t res = _delegate->testI32(thing);
438 cob(res);
439 }
440
Sebastian Zenker042580f2019-01-29 15:48:12 +0100441 void testI64(std::function<void(int64_t const& _return)> cob, const int64_t thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000442 int64_t res = _delegate->testI64(thing);
443 cob(res);
444 }
445
Sebastian Zenker042580f2019-01-29 15:48:12 +0100446 void testDouble(std::function<void(double const& _return)> cob, const double thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000447 double res = _delegate->testDouble(thing);
448 cob(res);
449 }
450
Sebastian Zenker042580f2019-01-29 15:48:12 +0100451 void testBinary(std::function<void(std::string const& _return)> cob,
452 const std::string& thing) override {
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100453 std::string res;
454 _delegate->testBinary(res, thing);
455 cob(res);
456 }
457
Carel Combrinkaee3f632024-09-05 18:26:42 +0000458 void testUuid(::std::function<void(apache::thrift::TUuid const& _return)> cob, const apache::thrift::TUuid& thing) override {
459 TUuid res;
460 _delegate->testUuid(res, thing);
CJCombrink1d886ca2024-03-23 21:32:28 +0100461 cob(res);
462 }
463
Sebastian Zenker042580f2019-01-29 15:48:12 +0100464 void testStruct(std::function<void(Xtruct const& _return)> cob, const Xtruct& thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000465 Xtruct res;
466 _delegate->testStruct(res, thing);
467 cob(res);
468 }
469
Sebastian Zenker042580f2019-01-29 15:48:12 +0100470 void testNest(std::function<void(Xtruct2 const& _return)> cob, const Xtruct2& thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000471 Xtruct2 res;
472 _delegate->testNest(res, thing);
473 cob(res);
474 }
475
Sebastian Zenker042580f2019-01-29 15:48:12 +0100476 void testMap(std::function<void(std::map<int32_t, int32_t> const& _return)> cob,
477 const std::map<int32_t, int32_t>& thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000478 std::map<int32_t, int32_t> res;
479 _delegate->testMap(res, thing);
480 cob(res);
481 }
482
Sebastian Zenker042580f2019-01-29 15:48:12 +0100483 void testStringMap(
cyy316723a2019-01-05 16:35:14 +0800484 std::function<void(std::map<std::string, std::string> const& _return)> cob,
Sebastian Zenker042580f2019-01-29 15:48:12 +0100485 const std::map<std::string, std::string>& thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000486 std::map<std::string, std::string> res;
487 _delegate->testStringMap(res, thing);
488 cob(res);
489 }
490
Sebastian Zenker042580f2019-01-29 15:48:12 +0100491 void testSet(std::function<void(std::set<int32_t> const& _return)> cob,
492 const std::set<int32_t>& thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000493 std::set<int32_t> res;
494 _delegate->testSet(res, thing);
495 cob(res);
496 }
497
Sebastian Zenker042580f2019-01-29 15:48:12 +0100498 void testList(std::function<void(std::vector<int32_t> const& _return)> cob,
499 const std::vector<int32_t>& thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000500 std::vector<int32_t> res;
501 _delegate->testList(res, thing);
502 cob(res);
503 }
504
Sebastian Zenker042580f2019-01-29 15:48:12 +0100505 void testEnum(std::function<void(Numberz::type const& _return)> cob,
506 const Numberz::type thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000507 Numberz::type res = _delegate->testEnum(thing);
508 cob(res);
509 }
510
Sebastian Zenker042580f2019-01-29 15:48:12 +0100511 void testTypedef(std::function<void(UserId const& _return)> cob, const UserId thing) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000512 UserId res = _delegate->testTypedef(thing);
513 cob(res);
514 }
515
Sebastian Zenker042580f2019-01-29 15:48:12 +0100516 void testMapMap(
cyy316723a2019-01-05 16:35:14 +0800517 std::function<void(std::map<int32_t, std::map<int32_t, int32_t> > const& _return)> cob,
Sebastian Zenker042580f2019-01-29 15:48:12 +0100518 const int32_t hello) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000519 std::map<int32_t, std::map<int32_t, int32_t> > res;
520 _delegate->testMapMap(res, hello);
521 cob(res);
522 }
523
Sebastian Zenker042580f2019-01-29 15:48:12 +0100524 void testInsanity(
cyy316723a2019-01-05 16:35:14 +0800525 std::function<void(std::map<UserId, std::map<Numberz::type, Insanity> > const& _return)> cob,
Sebastian Zenker042580f2019-01-29 15:48:12 +0100526 const Insanity& argument) override {
Jake Farrell5d02b802014-01-07 21:42:01 -0500527 std::map<UserId, std::map<Numberz::type, Insanity> > res;
Roger Meier7e056e72011-07-17 07:28:28 +0000528 _delegate->testInsanity(res, argument);
529 cob(res);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100530 }
Roger Meier7e056e72011-07-17 07:28:28 +0000531
Sebastian Zenker042580f2019-01-29 15:48:12 +0100532 void testMulti(std::function<void(Xtruct const& _return)> cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100533 const int8_t arg0,
534 const int32_t arg1,
535 const int64_t arg2,
536 const std::map<int16_t, std::string>& arg3,
537 const Numberz::type arg4,
Sebastian Zenker042580f2019-01-29 15:48:12 +0100538 const UserId arg5) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000539 Xtruct res;
540 _delegate->testMulti(res, arg0, arg1, arg2, arg3, arg4, arg5);
541 cob(res);
542 }
543
Sebastian Zenker042580f2019-01-29 15:48:12 +0100544 void testException(
cyy316723a2019-01-05 16:35:14 +0800545 std::function<void()> cob,
546 std::function<void(::apache::thrift::TDelayedException* _throw)> exn_cob,
Sebastian Zenker042580f2019-01-29 15:48:12 +0100547 const std::string& arg) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000548 try {
549 _delegate->testException(arg);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100550 } catch (const apache::thrift::TException& e) {
Roger Meier7e056e72011-07-17 07:28:28 +0000551 exn_cob(apache::thrift::TDelayedException::delayException(e));
552 return;
553 }
554 cob();
555 }
556
Sebastian Zenker042580f2019-01-29 15:48:12 +0100557 void testMultiException(
cyy316723a2019-01-05 16:35:14 +0800558 std::function<void(Xtruct const& _return)> cob,
559 std::function<void(::apache::thrift::TDelayedException* _throw)> exn_cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100560 const std::string& arg0,
Sebastian Zenker042580f2019-01-29 15:48:12 +0100561 const std::string& arg1) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000562 Xtruct res;
563 try {
564 _delegate->testMultiException(res, arg0, arg1);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100565 } catch (const apache::thrift::TException& e) {
Roger Meier7e056e72011-07-17 07:28:28 +0000566 exn_cob(apache::thrift::TDelayedException::delayException(e));
567 return;
568 }
569 cob(res);
570 }
571
Sebastian Zenker042580f2019-01-29 15:48:12 +0100572 void testOneway(std::function<void()> cob, const int32_t secondsToSleep) override {
Roger Meier7e056e72011-07-17 07:28:28 +0000573 _delegate->testOneway(secondsToSleep);
574 cob();
575 }
576
577protected:
cyy316723a2019-01-05 16:35:14 +0800578 std::shared_ptr<TestHandler> _delegate;
Roger Meier7e056e72011-07-17 07:28:28 +0000579};
580
Federico Giovanardi3b21bc92025-08-22 15:29:24 +0200581struct DomainSocketFd {
582 THRIFT_SOCKET socket_fd;
583 std::string path;
584 DomainSocketFd(const std::string& path) : path(path) {
585#ifdef HAVE_SYS_UN_H
586 unlink(path.c_str());
587 socket_fd = socket(AF_UNIX, SOCK_STREAM, IPPROTO_IP);
588 if (socket_fd == -1) {
589 std::ostringstream os;
590 os << "Cannot create domain socket: " << strerror(errno);
591 throw std::runtime_error(os.str());
592 }
593 if (path.size() > sizeof(sockaddr_un::sun_path) - 1)
594 throw std::runtime_error("Path size on domain socket too big");
595 struct sockaddr_un sa;
596 memset(&sa, 0, sizeof(sa));
597 sa.sun_family = AF_UNIX;
598 strcpy(sa.sun_path, path.c_str());
599 int rv = bind(socket_fd, (struct sockaddr*)&sa, sizeof(sa));
600 if (rv == -1) {
601 std::ostringstream os;
602 os << "Cannot bind domain socket: " << strerror(errno);
603 throw std::runtime_error(os.str());
604 }
605
606 rv = ::listen(socket_fd, 16);
607 if (rv == -1) {
608 std::ostringstream os;
609 os << "Cannot listen on domain socket: " << strerror(errno);
610 throw std::runtime_error(os.str());
611 }
612#else
613 throw std::runtime_error("Cannot create a domain socket without AF_UNIX");
614#endif
615 }
616 ~DomainSocketFd() {
617 ::THRIFT_CLOSESOCKET(socket_fd);
618 unlink(path.c_str());
619 }
620};
621
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900622namespace po = boost::program_options;
623
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100624int main(int argc, char** argv) {
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +0530625
Nobuaki Sukegawac8744082016-03-09 19:55:56 +0900626 string testDir = boost::filesystem::system_complete(argv[0]).parent_path().parent_path().parent_path().string();
627 string certPath = testDir + "/keys/server.crt";
628 string keyPath = testDir + "/keys/server.key";
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +0530629
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100630#if _WIN32
Jake Farrell5d02b802014-01-07 21:42:01 -0500631 transport::TWinsockSingleton::create();
632#endif
Mark Sleee8540632006-05-30 09:24:40 +0000633 int port = 9090;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000634 bool ssl = false;
James E. King IIIb2b767e2018-09-15 20:32:04 +0000635 bool zlib = false;
Roger Meierca142b02011-06-07 17:59:07 +0000636 string transport_type = "buffered";
637 string protocol_type = "binary";
638 string server_type = "simple";
639 string domain_socket = "";
pavlodd08f6e2015-10-08 16:43:56 -0400640 bool abstract_namespace = false;
Federico Giovanardi3b21bc92025-08-22 15:29:24 +0200641 bool emulate_socketactivation = false;
642 std::unique_ptr<DomainSocketFd> domain_socket_fd;
Roger Meierca142b02011-06-07 17:59:07 +0000643 size_t workers = 4;
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900644 int string_limit = 0;
645 int container_limit = 0;
Marc Slemko6be374b2006-08-04 03:16:25 +0000646
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900647 po::options_description desc("Allowed options");
648 desc.add_options()
649 ("help,h", "produce help message")
650 ("port", po::value<int>(&port)->default_value(port), "Port number to listen")
651 ("domain-socket", po::value<string>(&domain_socket) ->default_value(domain_socket), "Unix Domain Socket (e.g. /tmp/ThriftTest.thrift)")
652 ("abstract-namespace", "Create the domain socket in the Abstract Namespace (no connection with filesystem pathnames)")
Federico Giovanardi3b21bc92025-08-22 15:29:24 +0200653 ("emulate-socketactivation","Open the socket from the tester program and pass the library an already open fd")
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900654 ("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 -0700655 ("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 -0500656 ("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 +0900657 ("ssl", "Encrypted Transport using SSL")
James E. King IIIb2b767e2018-09-15 20:32:04 +0000658 ("zlib", "Wrapped Transport using Zlib")
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900659 ("processor-events", "processor-events")
660 ("workers,n", po::value<size_t>(&workers)->default_value(workers), "Number of thread pools workers. Only valid for thread-pool server type")
661 ("string-limit", po::value<int>(&string_limit))
662 ("container-limit", po::value<int>(&container_limit));
Marc Slemko6be374b2006-08-04 03:16:25 +0000663
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900664 po::variables_map vm;
665 po::store(po::parse_command_line(argc, argv, desc), vm);
666 po::notify(vm);
Marc Slemko6be374b2006-08-04 03:16:25 +0000667
Roger Meierca142b02011-06-07 17:59:07 +0000668 if (vm.count("help")) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100669 cout << desc << "\n";
670 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000671 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500672
Marc Slemko6be374b2006-08-04 03:16:25 +0000673 try {
Roger Meierca142b02011-06-07 17:59:07 +0000674 if (!server_type.empty()) {
675 if (server_type == "simple") {
676 } else if (server_type == "thread-pool") {
677 } else if (server_type == "threaded") {
678 } else if (server_type == "nonblocking") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000679 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100680 throw invalid_argument("Unknown server type " + server_type);
Roger Meierca142b02011-06-07 17:59:07 +0000681 }
682 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500683
Roger Meierca142b02011-06-07 17:59:07 +0000684 if (!protocol_type.empty()) {
685 if (protocol_type == "binary") {
Roger Meier284101c2014-03-11 21:20:35 +0100686 } else if (protocol_type == "compact") {
Roger Meierca142b02011-06-07 17:59:07 +0000687 } else if (protocol_type == "json") {
Dave Watson792db4e2015-01-16 11:22:01 -0800688 } else if (protocol_type == "header") {
James E. King, III39eaae62017-11-19 20:17:33 -0500689 } else if (protocol_type == "multi") { // multiplexed binary
690 } else if (protocol_type == "multic") { // multiplexed compact
691 } else if (protocol_type == "multih") { // multiplexed header
692 } else if (protocol_type == "multij") { // multiplexed json
Roger Meierca142b02011-06-07 17:59:07 +0000693 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100694 throw invalid_argument("Unknown protocol type " + protocol_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000695 }
696 }
697
Roger Meier284101c2014-03-11 21:20:35 +0100698 if (!transport_type.empty()) {
Roger Meierca142b02011-06-07 17:59:07 +0000699 if (transport_type == "buffered") {
700 } else if (transport_type == "framed") {
701 } else if (transport_type == "http") {
penenin1ab096c2020-05-18 12:27:31 -0700702 } else if (transport_type == "websocket") {
James E. King IIIb2b767e2018-09-15 20:32:04 +0000703 } else if (transport_type == "zlib") {
704 // crosstester will pass zlib as a flag and a transport right now...
Marc Slemko6be374b2006-08-04 03:16:25 +0000705 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100706 throw invalid_argument("Unknown transport type " + transport_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000707 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000708 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000709
Bryan Duxbury833ae492010-09-27 17:26:02 +0000710 } catch (std::exception& e) {
CJCombrink4a280d52024-03-14 19:57:41 +0100711 cerr << e.what() << '\n';
Roger Meierca142b02011-06-07 17:59:07 +0000712 cout << desc << "\n";
713 return 1;
Marc Slemko6be374b2006-08-04 03:16:25 +0000714 }
715
Roger Meierca142b02011-06-07 17:59:07 +0000716 if (vm.count("ssl")) {
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000717 ssl = true;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000718 }
719
James E. King IIIb2b767e2018-09-15 20:32:04 +0000720 if (vm.count("zlib")) {
721 zlib = true;
722 }
723
James E. King III9bea32f2018-03-16 16:07:42 -0400724#if defined(HAVE_SIGNAL_H) && defined(SIGPIPE)
725 if (ssl) {
726 signal(SIGPIPE, SIG_IGN); // for OpenSSL, otherwise we end abruptly
727 }
728#endif
729
pavlodd08f6e2015-10-08 16:43:56 -0400730 if (vm.count("abstract-namespace")) {
731 abstract_namespace = true;
732 }
Federico Giovanardi3b21bc92025-08-22 15:29:24 +0200733 if (vm.count("emulate-socketactivation")) {
734 emulate_socketactivation = true;
735 }
pavlodd08f6e2015-10-08 16:43:56 -0400736
Mark Sleee8540632006-05-30 09:24:40 +0000737 // Dispatcher
cyy316723a2019-01-05 16:35:14 +0800738 std::shared_ptr<TProtocolFactory> protocolFactory;
James E. King, III58402ff2017-11-17 14:41:46 -0500739 if (protocol_type == "json" || protocol_type == "multij") {
cyy316723a2019-01-05 16:35:14 +0800740 std::shared_ptr<TProtocolFactory> jsonProtocolFactory(new TJSONProtocolFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000741 protocolFactory = jsonProtocolFactory;
James E. King, III58402ff2017-11-17 14:41:46 -0500742 } else if (protocol_type == "compact" || protocol_type == "multic") {
Sebastian Zenker042580f2019-01-29 15:48:12 +0100743 auto *compactProtocolFactory = new TCompactProtocolFactoryT<TBufferBase>();
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900744 compactProtocolFactory->setContainerSizeLimit(container_limit);
745 compactProtocolFactory->setStringSizeLimit(string_limit);
746 protocolFactory.reset(compactProtocolFactory);
James E. King, III58402ff2017-11-17 14:41:46 -0500747 } else if (protocol_type == "header" || protocol_type == "multih") {
cyy316723a2019-01-05 16:35:14 +0800748 std::shared_ptr<TProtocolFactory> headerProtocolFactory(new THeaderProtocolFactory());
Dave Watson792db4e2015-01-16 11:22:01 -0800749 protocolFactory = headerProtocolFactory;
Roger Meierca142b02011-06-07 17:59:07 +0000750 } else {
Sebastian Zenker042580f2019-01-29 15:48:12 +0100751 auto* binaryProtocolFactory = new TBinaryProtocolFactoryT<TBufferBase>();
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900752 binaryProtocolFactory->setContainerSizeLimit(container_limit);
753 binaryProtocolFactory->setStringSizeLimit(string_limit);
754 protocolFactory.reset(binaryProtocolFactory);
Roger Meierca142b02011-06-07 17:59:07 +0000755 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000756
James E. King, III58402ff2017-11-17 14:41:46 -0500757 // Processors
cyy316723a2019-01-05 16:35:14 +0800758 std::shared_ptr<TestHandler> testHandler(new TestHandler());
759 std::shared_ptr<TProcessor> testProcessor(new ThriftTestProcessor(testHandler));
Jake Farrell5d02b802014-01-07 21:42:01 -0500760
Roger Meierca142b02011-06-07 17:59:07 +0000761 if (vm.count("processor-events")) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100762 testProcessor->setEventHandler(
cyy316723a2019-01-05 16:35:14 +0800763 std::shared_ptr<TProcessorEventHandler>(new TestProcessorEventHandler()));
David Reissd7192062010-10-06 17:09:33 +0000764 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500765
Mark Sleee8540632006-05-30 09:24:40 +0000766 // Transport
cyy316723a2019-01-05 16:35:14 +0800767 std::shared_ptr<TSSLSocketFactory> sslSocketFactory;
768 std::shared_ptr<TServerSocket> serverSocket;
Mark Sleee8540632006-05-30 09:24:40 +0000769
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000770 if (ssl) {
cyy316723a2019-01-05 16:35:14 +0800771 sslSocketFactory = std::shared_ptr<TSSLSocketFactory>(new TSSLSocketFactory());
Nobuaki Sukegawac8744082016-03-09 19:55:56 +0900772 sslSocketFactory->loadCertificate(certPath.c_str());
773 sslSocketFactory->loadPrivateKey(keyPath.c_str());
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000774 sslSocketFactory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
James E. King III70b33fb2018-03-11 10:57:10 -0400775 if (server_type != "nonblocking") {
cyy316723a2019-01-05 16:35:14 +0800776 serverSocket = std::shared_ptr<TServerSocket>(new TSSLServerSocket(port, sslSocketFactory));
James E. King III70b33fb2018-03-11 10:57:10 -0400777 }
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000778 } else {
Roger Meierc94b2932014-02-22 20:07:33 +0100779 if (domain_socket != "") {
pavlodd08f6e2015-10-08 16:43:56 -0400780 if (abstract_namespace) {
781 std::string abstract_socket("\0", 1);
782 abstract_socket += domain_socket;
cyy316723a2019-01-05 16:35:14 +0800783 serverSocket = std::shared_ptr<TServerSocket>(new TServerSocket(abstract_socket));
pavlodd08f6e2015-10-08 16:43:56 -0400784 } else {
Federico Giovanardi3b21bc92025-08-22 15:29:24 +0200785 if (emulate_socketactivation) {
786 unlink(domain_socket.c_str());
787 // open and bind the socket
788 domain_socket_fd.reset(new DomainSocketFd(domain_socket));
789 serverSocket = std::shared_ptr<TServerSocket>(
790 new TServerSocket(domain_socket_fd->socket_fd, SocketType::UNIX));
791 } else {
792 unlink(domain_socket.c_str());
793 serverSocket = std::shared_ptr<TServerSocket>(new TServerSocket(domain_socket));
794 }
pavlodd08f6e2015-10-08 16:43:56 -0400795 }
Roger Meierc94b2932014-02-22 20:07:33 +0100796 port = 0;
797 } else {
cyy316723a2019-01-05 16:35:14 +0800798 serverSocket = std::shared_ptr<TServerSocket>(new TServerSocket(port));
Roger Meierc94b2932014-02-22 20:07:33 +0100799 }
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000800 }
Roger Meierca142b02011-06-07 17:59:07 +0000801
Mark Sleed788b2e2006-09-07 01:26:35 +0000802 // Factory
cyy316723a2019-01-05 16:35:14 +0800803 std::shared_ptr<TTransportFactory> transportFactory;
Jake Farrell5d02b802014-01-07 21:42:01 -0500804
Roger Meier7e056e72011-07-17 07:28:28 +0000805 if (transport_type == "http" && server_type != "nonblocking") {
cyy316723a2019-01-05 16:35:14 +0800806 transportFactory = std::make_shared<THttpServerTransportFactory>();
penenin1ab096c2020-05-18 12:27:31 -0700807 } else if (transport_type == "websocket" && server_type != "nonblocking") {
808 if (protocol_type == "json" || protocol_type == "multij") {
809 transportFactory = std::make_shared<TTextWebSocketServerTransportFactory>();
810 } else {
811 transportFactory = std::make_shared<TBinaryWebSocketServerTransportFactory>();
812 }
Roger Meierca142b02011-06-07 17:59:07 +0000813 } else if (transport_type == "framed") {
cyy316723a2019-01-05 16:35:14 +0800814 transportFactory = std::make_shared<TFramedTransportFactory>();
Roger Meierca142b02011-06-07 17:59:07 +0000815 } else {
cyy316723a2019-01-05 16:35:14 +0800816 transportFactory = std::make_shared<TBufferedTransportFactory>();
James E. King IIIb2b767e2018-09-15 20:32:04 +0000817 }
818
819 if (zlib) {
Mario Emmenlauer2d2df9e2020-04-24 18:02:18 +0200820 // currently TZlibTransportFactory is the only factory than can wrap another:
821 transportFactory = std::make_shared<TZlibTransportFactory>(transportFactory);
Roger Meierca142b02011-06-07 17:59:07 +0000822 }
Mark Sleed788b2e2006-09-07 01:26:35 +0000823
Roger Meierca142b02011-06-07 17:59:07 +0000824 // Server Info
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100825 cout << "Starting \"" << server_type << "\" server (" << transport_type << "/" << protocol_type
pavlodd08f6e2015-10-08 16:43:56 -0400826 << ") listen on: ";
827 if (abstract_namespace) {
828 cout << '@';
829 }
830 cout << domain_socket;
Roger Meierca142b02011-06-07 17:59:07 +0000831 if (port != 0) {
832 cout << port;
833 }
CJCombrink4a280d52024-03-14 19:57:41 +0100834 cout << '\n';
Mark Sleee8540632006-05-30 09:24:40 +0000835
James E. King, III58402ff2017-11-17 14:41:46 -0500836 // Multiplexed Processor if needed
837 if (boost::starts_with(protocol_type, "multi")) {
cyy316723a2019-01-05 16:35:14 +0800838 std::shared_ptr<SecondHandler> secondHandler(new SecondHandler());
839 std::shared_ptr<SecondServiceProcessor> secondProcessor(new SecondServiceProcessor(secondHandler));
James E. King, III58402ff2017-11-17 14:41:46 -0500840
cyy316723a2019-01-05 16:35:14 +0800841 std::shared_ptr<TMultiplexedProcessor> multiplexedProcessor(new TMultiplexedProcessor());
James E. King, III58402ff2017-11-17 14:41:46 -0500842 multiplexedProcessor->registerDefault(testProcessor); // non-multi clients go to the default processor (multi:binary, multic:compact, ...)
843 multiplexedProcessor->registerProcessor("ThriftTest", testProcessor);
844 multiplexedProcessor->registerProcessor("SecondService", secondProcessor);
cyy316723a2019-01-05 16:35:14 +0800845 testProcessor = std::dynamic_pointer_cast<TProcessor>(multiplexedProcessor);
James E. King, III58402ff2017-11-17 14:41:46 -0500846 }
847
Roger Meierca142b02011-06-07 17:59:07 +0000848 // Server
cyy316723a2019-01-05 16:35:14 +0800849 std::shared_ptr<apache::thrift::server::TServer> server;
Jake Farrell5d02b802014-01-07 21:42:01 -0500850
Roger Meierca142b02011-06-07 17:59:07 +0000851 if (server_type == "simple") {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100852 server.reset(new TSimpleServer(testProcessor, serverSocket, transportFactory, protocolFactory));
Roger Meierca142b02011-06-07 17:59:07 +0000853 } else if (server_type == "thread-pool") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000854
cyyca8af9b2019-01-11 22:13:12 +0800855 std::shared_ptr<ThreadFactory> threadFactory
856 = std::shared_ptr<ThreadFactory>(new ThreadFactory());
Marc Slemko6be374b2006-08-04 03:16:25 +0000857
cyy316723a2019-01-05 16:35:14 +0800858 std::shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workers);
Marc Slemko6be374b2006-08-04 03:16:25 +0000859 threadManager->threadFactory(threadFactory);
Marc Slemko6be374b2006-08-04 03:16:25 +0000860 threadManager->start();
861
Jake Farrell5d02b802014-01-07 21:42:01 -0500862 server.reset(new TThreadPoolServer(testProcessor,
Roger Meierca142b02011-06-07 17:59:07 +0000863 serverSocket,
Mark Sleed788b2e2006-09-07 01:26:35 +0000864 transportFactory,
Mark Sleea3302652006-10-25 19:03:32 +0000865 protocolFactory,
Jake Farrell5d02b802014-01-07 21:42:01 -0500866 threadManager));
Roger Meierca142b02011-06-07 17:59:07 +0000867 } else if (server_type == "threaded") {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100868 server.reset(
869 new TThreadedServer(testProcessor, serverSocket, transportFactory, protocolFactory));
Roger Meierca142b02011-06-07 17:59:07 +0000870 } else if (server_type == "nonblocking") {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100871 if (transport_type == "http") {
cyy316723a2019-01-05 16:35:14 +0800872 std::shared_ptr<TestHandlerAsync> testHandlerAsync(new TestHandlerAsync(testHandler));
873 std::shared_ptr<TAsyncProcessor> testProcessorAsync(
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100874 new ThriftTestAsyncProcessor(testHandlerAsync));
cyy316723a2019-01-05 16:35:14 +0800875 std::shared_ptr<TAsyncBufferProcessor> testBufferProcessor(
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100876 new TAsyncProtocolProcessor(testProcessorAsync, protocolFactory));
Jake Farrell5d02b802014-01-07 21:42:01 -0500877
878 // not loading nonblockingServer into "server" because
879 // TEvhttpServer doesn't inherit from TServer, and doesn't
880 // provide a stop method.
Roger Meier7e056e72011-07-17 07:28:28 +0000881 TEvhttpServer nonblockingServer(testBufferProcessor, port);
882 nonblockingServer.serve();
James E. King III70b33fb2018-03-11 10:57:10 -0400883 } else if (transport_type == "framed") {
cyy316723a2019-01-05 16:35:14 +0800884 std::shared_ptr<transport::TNonblockingServerTransport> nbSocket;
James E. King III9bea32f2018-03-16 16:07:42 -0400885 nbSocket.reset(
886 ssl ? new transport::TNonblockingSSLServerSocket(port, sslSocketFactory)
887 : new transport::TNonblockingServerSocket(port));
Divya Thaluru808d1432017-08-06 16:36:36 -0700888 server.reset(new TNonblockingServer(testProcessor, protocolFactory, nbSocket));
James E. King III70b33fb2018-03-11 10:57:10 -0400889 } else {
CJCombrink4a280d52024-03-14 19:57:41 +0100890 cerr << "server-type nonblocking requires transport of http or framed" << '\n';
James E. King III9bea32f2018-03-16 16:07:42 -0400891 exit(1);
Roger Meier7e056e72011-07-17 07:28:28 +0000892 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000893 }
894
Sebastian Zenker042580f2019-01-29 15:48:12 +0100895 if (server.get() != nullptr) {
Dave Watson792db4e2015-01-16 11:22:01 -0800896 if (protocol_type == "header") {
James E. King, IIIdf899132016-11-12 15:16:30 -0500897 // Tell the server to use the same protocol for input / output
Dave Watson792db4e2015-01-16 11:22:01 -0800898 // if using header
cyy316723a2019-01-05 16:35:14 +0800899 server->setOutputProtocolFactory(std::shared_ptr<TProtocolFactory>());
Dave Watson792db4e2015-01-16 11:22:01 -0800900 }
Mario Emmenlauer2d2df9e2020-04-24 18:02:18 +0200901
cyyca8af9b2019-01-11 22:13:12 +0800902 apache::thrift::concurrency::ThreadFactory factory;
Jake Farrell5d02b802014-01-07 21:42:01 -0500903 factory.setDetached(false);
cyy316723a2019-01-05 16:35:14 +0800904 std::shared_ptr<apache::thrift::concurrency::Runnable> serverThreadRunner(server);
905 std::shared_ptr<apache::thrift::concurrency::Thread> thread
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100906 = factory.newThread(serverThreadRunner);
Jake Farrell5d02b802014-01-07 21:42:01 -0500907
James E. King III9bea32f2018-03-16 16:07:42 -0400908#ifdef HAVE_SIGNAL_H
909 signal(SIGINT, signal_handler);
910#endif
911
912 thread->start();
913 gMonitor.waitForever(); // wait for a shutdown signal
Mario Emmenlauer2d2df9e2020-04-24 18:02:18 +0200914
James E. King III9bea32f2018-03-16 16:07:42 -0400915#ifdef HAVE_SIGNAL_H
916 signal(SIGINT, SIG_DFL);
917#endif
Jake Farrell5d02b802014-01-07 21:42:01 -0500918
919 server->stop();
920 thread->join();
921 server.reset();
922 }
923
CJCombrink4a280d52024-03-14 19:57:41 +0100924 cout << "done." << '\n';
Mark Sleee8540632006-05-30 09:24:40 +0000925 return 0;
926}