blob: 6b4df7d7b1777396158de0904628fc896a34c214 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
James E. King, III58402ff2017-11-17 14:41:46 -050020#include <thrift/async/TAsyncBufferProcessor.h>
21#include <thrift/async/TAsyncProtocolProcessor.h>
22#include <thrift/async/TEvhttpServer.h>
Roger Meier49ff8b12012-04-13 09:12:31 +000023#include <thrift/concurrency/PlatformThreadFactory.h>
James E. King, III58402ff2017-11-17 14:41:46 -050024#include <thrift/concurrency/ThreadManager.h>
25#include <thrift/processor/TMultiplexedProcessor.h>
Roger Meier49ff8b12012-04-13 09:12:31 +000026#include <thrift/protocol/TBinaryProtocol.h>
Roger Meier023192f2014-02-12 09:35:12 +010027#include <thrift/protocol/TCompactProtocol.h>
Dave Watson792db4e2015-01-16 11:22:01 -080028#include <thrift/protocol/THeaderProtocol.h>
Roger Meier49ff8b12012-04-13 09:12:31 +000029#include <thrift/protocol/TJSONProtocol.h>
Roger Meier49ff8b12012-04-13 09:12:31 +000030#include <thrift/server/TNonblockingServer.h>
James E. King, III58402ff2017-11-17 14:41:46 -050031#include <thrift/server/TSimpleServer.h>
32#include <thrift/server/TThreadPoolServer.h>
33#include <thrift/server/TThreadedServer.h>
Roger Meier49ff8b12012-04-13 09:12:31 +000034#include <thrift/transport/THttpServer.h>
35#include <thrift/transport/THttpTransport.h>
James E. King III70b33fb2018-03-11 10:57:10 -040036#include <thrift/transport/TNonblockingSSLServerSocket.h>
James E. King, III58402ff2017-11-17 14:41:46 -050037#include <thrift/transport/TNonblockingServerSocket.h>
38#include <thrift/transport/TSSLServerSocket.h>
39#include <thrift/transport/TSSLSocket.h>
40#include <thrift/transport/TServerSocket.h>
Roger Meier49ff8b12012-04-13 09:12:31 +000041#include <thrift/transport/TTransportUtils.h>
James E. King, III58402ff2017-11-17 14:41:46 -050042
43#include "SecondService.h"
Mark Slee95771002006-06-07 06:53:25 +000044#include "ThriftTest.h"
Marc Slemko6be374b2006-08-04 03:16:25 +000045
James E. King, III7edc8fa2017-01-20 10:11:41 -050046#ifdef HAVE_STDINT_H
47#include <stdint.h>
48#endif
49#ifdef HAVE_INTTYPES_H
50#include <inttypes.h>
51#endif
52
Marc Slemko6be374b2006-08-04 03:16:25 +000053#include <iostream>
54#include <stdexcept>
55#include <sstream>
56
James E. King, III58402ff2017-11-17 14:41:46 -050057#include <boost/algorithm/string.hpp>
Roger Meierca142b02011-06-07 17:59:07 +000058#include <boost/program_options.hpp>
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +053059#include <boost/filesystem.hpp>
James E. King, III82ae9572017-08-05 12:23:54 -040060#include <thrift/stdcxx.h>
Roger Meierca142b02011-06-07 17:59:07 +000061
Bryan Duxburycd9aea12011-02-22 18:12:06 +000062#include <signal.h>
Jake Farrell5d02b802014-01-07 21:42:01 -050063#if _WIN32
Konrad Grochowski16a23a62014-11-13 15:33:38 +010064#include <thrift/windows/TWinsockSingleton.h>
Jake Farrell5d02b802014-01-07 21:42:01 -050065#endif
David Reissbc3dddb2007-08-22 23:20:24 +000066
Mark Sleee8540632006-05-30 09:24:40 +000067using namespace std;
68
T Jake Lucianib5e62212009-01-31 22:36:20 +000069using namespace apache::thrift;
James E. King, III82ae9572017-08-05 12:23:54 -040070using namespace apache::thrift::async;
T Jake Lucianib5e62212009-01-31 22:36:20 +000071using namespace apache::thrift::concurrency;
72using namespace apache::thrift::protocol;
73using namespace apache::thrift::transport;
74using namespace apache::thrift::server;
Marc Slemko6be374b2006-08-04 03:16:25 +000075
Marc Slemkobf4fd192006-08-15 21:29:39 +000076using namespace thrift::test;
77
Mark Sleed2655522006-09-05 22:09:57 +000078class TestHandler : public ThriftTestIf {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010079public:
Mark Sleed2655522006-09-05 22:09:57 +000080 TestHandler() {}
Mark Sleee8540632006-05-30 09:24:40 +000081
Konrad Grochowski16a23a62014-11-13 15:33:38 +010082 void testVoid() { printf("testVoid()\n"); }
Mark Sleee8540632006-05-30 09:24:40 +000083
Konrad Grochowski16a23a62014-11-13 15:33:38 +010084 void testString(string& out, const string& thing) {
Mark Sleee8540632006-05-30 09:24:40 +000085 printf("testString(\"%s\")\n", thing.c_str());
Mark Slee1921d202007-01-24 19:43:06 +000086 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +000087 }
88
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +090089 bool testBool(const bool thing) {
90 printf("testBool(%s)\n", thing ? "true" : "false");
91 return thing;
92 }
93
Mark Slee1921d202007-01-24 19:43:06 +000094 int8_t testByte(const int8_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +000095 printf("testByte(%d)\n", (int)thing);
96 return thing;
97 }
98
Mark Slee1921d202007-01-24 19:43:06 +000099 int32_t testI32(const int32_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000100 printf("testI32(%d)\n", thing);
101 return thing;
102 }
103
Mark Slee1921d202007-01-24 19:43:06 +0000104 int64_t testI64(const int64_t thing) {
Roger Meier0e814802014-01-17 21:07:58 +0100105 printf("testI64(%" PRId64 ")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +0000106 return thing;
107 }
108
Mark Slee1921d202007-01-24 19:43:06 +0000109 double testDouble(const double thing) {
Roger Meiera8cef6e2011-07-17 18:55:59 +0000110 printf("testDouble(%f)\n", thing);
Mark Sleec98d0502006-09-06 02:42:25 +0000111 return thing;
112 }
113
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100114 void testBinary(std::string& _return, const std::string& thing) {
115 std::ostringstream hexstr;
116 hexstr << std::hex << thing;
James E. King, III39eaae62017-11-19 20:17:33 -0500117 printf("testBinary(%lu: %s)\n", thing.size(), hexstr.str().c_str());
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100118 _return = thing;
119 }
120
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100121 void testStruct(Xtruct& out, const Xtruct& thing) {
122 printf("testStruct({\"%s\", %d, %d, %" PRId64 "})\n",
123 thing.string_thing.c_str(),
124 (int)thing.byte_thing,
125 thing.i32_thing,
126 thing.i64_thing);
Mark Slee1921d202007-01-24 19:43:06 +0000127 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000128 }
129
Mark Slee1921d202007-01-24 19:43:06 +0000130 void testNest(Xtruct2& out, const Xtruct2& nest) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100131 const Xtruct& thing = nest.struct_thing;
132 printf("testNest({%d, {\"%s\", %d, %d, %" PRId64 "}, %d})\n",
133 (int)nest.byte_thing,
134 thing.string_thing.c_str(),
135 (int)thing.byte_thing,
136 thing.i32_thing,
137 thing.i64_thing,
138 nest.i32_thing);
Mark Slee1921d202007-01-24 19:43:06 +0000139 out = nest;
Mark Sleee8540632006-05-30 09:24:40 +0000140 }
141
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100142 void testMap(map<int32_t, int32_t>& out, const map<int32_t, int32_t>& thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000143 printf("testMap({");
144 map<int32_t, int32_t>::const_iterator m_iter;
145 bool first = true;
146 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
147 if (first) {
148 first = false;
149 } else {
150 printf(", ");
151 }
152 printf("%d => %d", m_iter->first, m_iter->second);
153 }
154 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000155 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000156 }
157
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100158 void testStringMap(map<std::string, std::string>& out,
159 const map<std::string, std::string>& thing) {
Roger Meierd3b9dca2011-06-24 14:01:10 +0000160 printf("testMap({");
161 map<std::string, std::string>::const_iterator m_iter;
162 bool first = true;
163 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
164 if (first) {
165 first = false;
166 } else {
167 printf(", ");
168 }
169 printf("%s => %s", (m_iter->first).c_str(), (m_iter->second).c_str());
170 }
171 printf("})\n");
172 out = thing;
173 }
174
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100175 void testSet(set<int32_t>& out, const set<int32_t>& thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000176 printf("testSet({");
177 set<int32_t>::const_iterator s_iter;
178 bool first = true;
179 for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) {
180 if (first) {
181 first = false;
182 } else {
183 printf(", ");
184 }
185 printf("%d", *s_iter);
186 }
187 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000188 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000189 }
190
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100191 void testList(vector<int32_t>& out, const vector<int32_t>& thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000192 printf("testList({");
Mark Sleeb9acf982006-10-10 01:57:32 +0000193 vector<int32_t>::const_iterator l_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000194 bool first = true;
195 for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) {
196 if (first) {
197 first = false;
198 } else {
199 printf(", ");
200 }
201 printf("%d", *l_iter);
202 }
203 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000204 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000205 }
206
Bryan Duxbury833ae492010-09-27 17:26:02 +0000207 Numberz::type testEnum(const Numberz::type thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000208 printf("testEnum(%d)\n", thing);
209 return thing;
210 }
211
Mark Slee1921d202007-01-24 19:43:06 +0000212 UserId testTypedef(const UserId thing) {
Roger Meier0e814802014-01-17 21:07:58 +0100213 printf("testTypedef(%" PRId64 ")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +0000214 return thing;
215 }
216
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100217 void testMapMap(map<int32_t, map<int32_t, int32_t> >& mapmap, const int32_t hello) {
Mark Sleee8540632006-05-30 09:24:40 +0000218 printf("testMapMap(%d)\n", hello);
Mark Sleee8540632006-05-30 09:24:40 +0000219
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100220 map<int32_t, int32_t> pos;
221 map<int32_t, int32_t> neg;
Mark Sleee8540632006-05-30 09:24:40 +0000222 for (int i = 1; i < 5; i++) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100223 pos.insert(make_pair(i, i));
224 neg.insert(make_pair(-i, -i));
Mark Sleee8540632006-05-30 09:24:40 +0000225 }
226
227 mapmap.insert(make_pair(4, pos));
228 mapmap.insert(make_pair(-4, neg));
Mark Sleee8540632006-05-30 09:24:40 +0000229 }
230
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100231 void testInsanity(map<UserId, map<Numberz::type, Insanity> >& insane, const Insanity& argument) {
Mark Sleee8540632006-05-30 09:24:40 +0000232 printf("testInsanity()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000233
Mark Sleee8540632006-05-30 09:24:40 +0000234 Insanity looney;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000235 map<Numberz::type, Insanity> first_map;
236 map<Numberz::type, Insanity> second_map;
Mark Sleee8540632006-05-30 09:24:40 +0000237
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900238 first_map.insert(make_pair(Numberz::TWO, argument));
239 first_map.insert(make_pair(Numberz::THREE, argument));
Mark Sleee8540632006-05-30 09:24:40 +0000240
Bryan Duxbury833ae492010-09-27 17:26:02 +0000241 second_map.insert(make_pair(Numberz::SIX, looney));
Mark Sleee8540632006-05-30 09:24:40 +0000242
Mark Sleee8540632006-05-30 09:24:40 +0000243 insane.insert(make_pair(1, first_map));
244 insane.insert(make_pair(2, second_map));
245
246 printf("return");
247 printf(" = {");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100248 map<UserId, map<Numberz::type, Insanity> >::const_iterator i_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000249 for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) {
Roger Meier0e814802014-01-17 21:07:58 +0100250 printf("%" PRId64 " => {", i_iter->first);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100251 map<Numberz::type, Insanity>::const_iterator i2_iter;
252 for (i2_iter = i_iter->second.begin(); i2_iter != i_iter->second.end(); ++i2_iter) {
Mark Sleee8540632006-05-30 09:24:40 +0000253 printf("%d => {", i2_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000254 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
255 map<Numberz::type, UserId>::const_iterator um;
Mark Sleee8540632006-05-30 09:24:40 +0000256 printf("{");
257 for (um = userMap.begin(); um != userMap.end(); ++um) {
Roger Meier0e814802014-01-17 21:07:58 +0100258 printf("%d => %" PRId64 ", ", um->first, um->second);
Mark Sleee8540632006-05-30 09:24:40 +0000259 }
260 printf("}, ");
261
Mark Sleeb9acf982006-10-10 01:57:32 +0000262 vector<Xtruct> xtructs = i2_iter->second.xtructs;
263 vector<Xtruct>::const_iterator x;
Mark Sleee8540632006-05-30 09:24:40 +0000264 printf("{");
265 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100266 printf("{\"%s\", %d, %d, %" PRId64 "}, ",
267 x->string_thing.c_str(),
268 (int)x->byte_thing,
269 x->i32_thing,
270 x->i64_thing);
Mark Sleee8540632006-05-30 09:24:40 +0000271 }
272 printf("}");
273
274 printf("}, ");
275 }
276 printf("}, ");
277 }
278 printf("}\n");
Mark Sleee8540632006-05-30 09:24:40 +0000279 }
Marc Slemkoe6889de2006-08-12 00:32:53 +0000280
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100281 void testMulti(Xtruct& hello,
282 const int8_t arg0,
283 const int32_t arg1,
284 const int64_t arg2,
285 const std::map<int16_t, std::string>& arg3,
286 const Numberz::type arg4,
287 const UserId arg5) {
288 (void)arg3;
289 (void)arg4;
290 (void)arg5;
Jake Farrell5d02b802014-01-07 21:42:01 -0500291
Marc Slemkoe6889de2006-08-12 00:32:53 +0000292 printf("testMulti()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000293
Marc Slemkoe6889de2006-08-12 00:32:53 +0000294 hello.string_thing = "Hello2";
295 hello.byte_thing = arg0;
296 hello.i32_thing = arg1;
297 hello.i64_thing = (int64_t)arg2;
Marc Slemkoe6889de2006-08-12 00:32:53 +0000298 }
Marc Slemkobf4fd192006-08-15 21:29:39 +0000299
ben-craigfae08e72015-07-15 11:34:47 -0500300 void testException(const std::string& arg) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000301 printf("testException(%s)\n", arg.c_str());
Mark Sleed3d733a2006-09-01 22:19:06 +0000302 if (arg.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000303 Xception e;
304 e.errorCode = 1001;
David Reiss9813cbe2009-01-31 21:39:11 +0000305 e.message = arg;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000306 throw e;
Roger Meier1f8b48f2012-05-02 22:56:47 +0000307 } else if (arg.compare("TException") == 0) {
T Jake Lucianib5e62212009-01-31 22:36:20 +0000308 apache::thrift::TException e;
David Reiss55ff70f2008-06-11 00:58:25 +0000309 throw e;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000310 } else {
311 Xtruct result;
312 result.string_thing = arg;
313 return;
314 }
315 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000316
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100317 void testMultiException(Xtruct& result,
318 const std::string& arg0,
ben-craigfae08e72015-07-15 11:34:47 -0500319 const std::string& arg1) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000320
Marc Slemko71d4e472006-08-15 22:34:04 +0000321 printf("testMultiException(%s, %s)\n", arg0.c_str(), arg1.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000322
Mark Sleef5f2be42006-09-05 21:05:31 +0000323 if (arg0.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000324 Xception e;
325 e.errorCode = 1001;
326 e.message = "This is an Xception";
327 throw e;
Mark Sleef5f2be42006-09-05 21:05:31 +0000328 } else if (arg0.compare("Xception2") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000329 Xception2 e;
330 e.errorCode = 2002;
331 e.struct_thing.string_thing = "This is an Xception2";
332 throw e;
333 } else {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000334 result.string_thing = arg1;
Mark Slee1921d202007-01-24 19:43:06 +0000335 return;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000336 }
337 }
David Reiss2ab6fe82008-02-18 02:11:44 +0000338
James E. King, III58402ff2017-11-17 14:41:46 -0500339 void testOneway(const int32_t aNum) {
340 printf("testOneway(%d): call received\n", aNum);
David Reiss2ab6fe82008-02-18 02:11:44 +0000341 }
Mark Sleee8540632006-05-30 09:24:40 +0000342};
343
James E. King, III58402ff2017-11-17 14:41:46 -0500344class SecondHandler : public SecondServiceIf
345{
346 public:
347 void secondtestString(std::string& result, const std::string& thing)
348 { result = "testString(\"" + thing + "\")"; }
James E. King, III39eaae62017-11-19 20:17:33 -0500349};
James E. King, III58402ff2017-11-17 14:41:46 -0500350
David Reissd7192062010-10-06 17:09:33 +0000351class TestProcessorEventHandler : public TProcessorEventHandler {
David Reiss23248712010-10-06 17:10:08 +0000352 virtual void* getContext(const char* fn_name, void* serverContext) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100353 (void)serverContext;
David Reissd7192062010-10-06 17:09:33 +0000354 return new std::string(fn_name);
355 }
356 virtual void freeContext(void* ctx, const char* fn_name) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100357 (void)fn_name;
David Reissd7192062010-10-06 17:09:33 +0000358 delete static_cast<std::string*>(ctx);
359 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100360 virtual void preRead(void* ctx, const char* fn_name) { communicate("preRead", ctx, fn_name); }
David Reissef7200f2010-10-06 17:09:42 +0000361 virtual void postRead(void* ctx, const char* fn_name, uint32_t bytes) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100362 (void)bytes;
David Reissd7192062010-10-06 17:09:33 +0000363 communicate("postRead", ctx, fn_name);
364 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100365 virtual void preWrite(void* ctx, const char* fn_name) { communicate("preWrite", ctx, fn_name); }
David Reissef7200f2010-10-06 17:09:42 +0000366 virtual void postWrite(void* ctx, const char* fn_name, uint32_t bytes) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100367 (void)bytes;
David Reissd7192062010-10-06 17:09:33 +0000368 communicate("postWrite", ctx, fn_name);
369 }
370 virtual void asyncComplete(void* ctx, const char* fn_name) {
371 communicate("asyncComplete", ctx, fn_name);
372 }
373 virtual void handlerError(void* ctx, const char* fn_name) {
374 communicate("handlerError", ctx, fn_name);
375 }
376
377 void communicate(const char* event, void* ctx, const char* fn_name) {
378 std::cout << event << ": " << *static_cast<std::string*>(ctx) << " = " << fn_name << std::endl;
379 }
380};
381
Roger Meier7e056e72011-07-17 07:28:28 +0000382class TestHandlerAsync : public ThriftTestCobSvIf {
383public:
James E. King, III82ae9572017-08-05 12:23:54 -0400384 TestHandlerAsync(stdcxx::shared_ptr<TestHandler>& handler) : _delegate(handler) {}
Roger Meier7e056e72011-07-17 07:28:28 +0000385 virtual ~TestHandlerAsync() {}
386
James E. King, III82ae9572017-08-05 12:23:54 -0400387 virtual void testVoid(stdcxx::function<void()> cob) {
Roger Meier7e056e72011-07-17 07:28:28 +0000388 _delegate->testVoid();
389 cob();
390 }
391
James E. King, III82ae9572017-08-05 12:23:54 -0400392 virtual void testString(stdcxx::function<void(std::string const& _return)> cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100393 const std::string& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000394 std::string res;
395 _delegate->testString(res, thing);
396 cob(res);
397 }
398
James E. King, III82ae9572017-08-05 12:23:54 -0400399 virtual void testBool(stdcxx::function<void(bool const& _return)> cob, const bool thing) {
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900400 bool res = _delegate->testBool(thing);
401 cob(res);
402 }
403
James E. King, III82ae9572017-08-05 12:23:54 -0400404 virtual void testByte(stdcxx::function<void(int8_t const& _return)> cob, const int8_t thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000405 int8_t res = _delegate->testByte(thing);
406 cob(res);
407 }
408
James E. King, III82ae9572017-08-05 12:23:54 -0400409 virtual void testI32(stdcxx::function<void(int32_t const& _return)> cob, const int32_t thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000410 int32_t res = _delegate->testI32(thing);
411 cob(res);
412 }
413
James E. King, III82ae9572017-08-05 12:23:54 -0400414 virtual void testI64(stdcxx::function<void(int64_t const& _return)> cob, const int64_t thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000415 int64_t res = _delegate->testI64(thing);
416 cob(res);
417 }
418
James E. King, III82ae9572017-08-05 12:23:54 -0400419 virtual void testDouble(stdcxx::function<void(double const& _return)> cob, const double thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000420 double res = _delegate->testDouble(thing);
421 cob(res);
422 }
423
James E. King, III82ae9572017-08-05 12:23:54 -0400424 virtual void testBinary(stdcxx::function<void(std::string const& _return)> cob,
Konrad Grochowski1f6e3802015-05-18 18:10:06 +0200425 const std::string& thing) {
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100426 std::string res;
427 _delegate->testBinary(res, thing);
428 cob(res);
429 }
430
James E. King, III82ae9572017-08-05 12:23:54 -0400431 virtual void testStruct(stdcxx::function<void(Xtruct const& _return)> cob, const Xtruct& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000432 Xtruct res;
433 _delegate->testStruct(res, thing);
434 cob(res);
435 }
436
James E. King, III82ae9572017-08-05 12:23:54 -0400437 virtual void testNest(stdcxx::function<void(Xtruct2 const& _return)> cob, const Xtruct2& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000438 Xtruct2 res;
439 _delegate->testNest(res, thing);
440 cob(res);
441 }
442
James E. King, III82ae9572017-08-05 12:23:54 -0400443 virtual void testMap(stdcxx::function<void(std::map<int32_t, int32_t> const& _return)> cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100444 const std::map<int32_t, int32_t>& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000445 std::map<int32_t, int32_t> res;
446 _delegate->testMap(res, thing);
447 cob(res);
448 }
449
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100450 virtual void testStringMap(
James E. King, III82ae9572017-08-05 12:23:54 -0400451 stdcxx::function<void(std::map<std::string, std::string> const& _return)> cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100452 const std::map<std::string, std::string>& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000453 std::map<std::string, std::string> res;
454 _delegate->testStringMap(res, thing);
455 cob(res);
456 }
457
James E. King, III82ae9572017-08-05 12:23:54 -0400458 virtual void testSet(stdcxx::function<void(std::set<int32_t> const& _return)> cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100459 const std::set<int32_t>& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000460 std::set<int32_t> res;
461 _delegate->testSet(res, thing);
462 cob(res);
463 }
464
James E. King, III82ae9572017-08-05 12:23:54 -0400465 virtual void testList(stdcxx::function<void(std::vector<int32_t> const& _return)> cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100466 const std::vector<int32_t>& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000467 std::vector<int32_t> res;
468 _delegate->testList(res, thing);
469 cob(res);
470 }
471
James E. King, III82ae9572017-08-05 12:23:54 -0400472 virtual void testEnum(stdcxx::function<void(Numberz::type const& _return)> cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100473 const Numberz::type thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000474 Numberz::type res = _delegate->testEnum(thing);
475 cob(res);
476 }
477
James E. King, III82ae9572017-08-05 12:23:54 -0400478 virtual void testTypedef(stdcxx::function<void(UserId const& _return)> cob, const UserId thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000479 UserId res = _delegate->testTypedef(thing);
480 cob(res);
481 }
482
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100483 virtual void testMapMap(
James E. King, III82ae9572017-08-05 12:23:54 -0400484 stdcxx::function<void(std::map<int32_t, std::map<int32_t, int32_t> > const& _return)> cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100485 const int32_t hello) {
Roger Meier7e056e72011-07-17 07:28:28 +0000486 std::map<int32_t, std::map<int32_t, int32_t> > res;
487 _delegate->testMapMap(res, hello);
488 cob(res);
489 }
490
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100491 virtual void testInsanity(
James E. King, III82ae9572017-08-05 12:23:54 -0400492 stdcxx::function<void(std::map<UserId, std::map<Numberz::type, Insanity> > const& _return)> cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100493 const Insanity& argument) {
Jake Farrell5d02b802014-01-07 21:42:01 -0500494 std::map<UserId, std::map<Numberz::type, Insanity> > res;
Roger Meier7e056e72011-07-17 07:28:28 +0000495 _delegate->testInsanity(res, argument);
496 cob(res);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100497 }
Roger Meier7e056e72011-07-17 07:28:28 +0000498
James E. King, III82ae9572017-08-05 12:23:54 -0400499 virtual void testMulti(stdcxx::function<void(Xtruct const& _return)> cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100500 const int8_t arg0,
501 const int32_t arg1,
502 const int64_t arg2,
503 const std::map<int16_t, std::string>& arg3,
504 const Numberz::type arg4,
505 const UserId arg5) {
Roger Meier7e056e72011-07-17 07:28:28 +0000506 Xtruct res;
507 _delegate->testMulti(res, arg0, arg1, arg2, arg3, arg4, arg5);
508 cob(res);
509 }
510
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100511 virtual void testException(
James E. King, III82ae9572017-08-05 12:23:54 -0400512 stdcxx::function<void()> cob,
513 stdcxx::function<void(::apache::thrift::TDelayedException* _throw)> exn_cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100514 const std::string& arg) {
Roger Meier7e056e72011-07-17 07:28:28 +0000515 try {
516 _delegate->testException(arg);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100517 } catch (const apache::thrift::TException& e) {
Roger Meier7e056e72011-07-17 07:28:28 +0000518 exn_cob(apache::thrift::TDelayedException::delayException(e));
519 return;
520 }
521 cob();
522 }
523
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100524 virtual void testMultiException(
James E. King, III82ae9572017-08-05 12:23:54 -0400525 stdcxx::function<void(Xtruct const& _return)> cob,
526 stdcxx::function<void(::apache::thrift::TDelayedException* _throw)> exn_cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100527 const std::string& arg0,
528 const std::string& arg1) {
Roger Meier7e056e72011-07-17 07:28:28 +0000529 Xtruct res;
530 try {
531 _delegate->testMultiException(res, arg0, arg1);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100532 } catch (const apache::thrift::TException& e) {
Roger Meier7e056e72011-07-17 07:28:28 +0000533 exn_cob(apache::thrift::TDelayedException::delayException(e));
534 return;
535 }
536 cob(res);
537 }
538
James E. King, III82ae9572017-08-05 12:23:54 -0400539 virtual void testOneway(stdcxx::function<void()> cob, const int32_t secondsToSleep) {
Roger Meier7e056e72011-07-17 07:28:28 +0000540 _delegate->testOneway(secondsToSleep);
541 cob();
542 }
543
544protected:
James E. King, III82ae9572017-08-05 12:23:54 -0400545 stdcxx::shared_ptr<TestHandler> _delegate;
Roger Meier7e056e72011-07-17 07:28:28 +0000546};
547
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900548namespace po = boost::program_options;
549
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100550int main(int argc, char** argv) {
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +0530551
Nobuaki Sukegawac8744082016-03-09 19:55:56 +0900552 string testDir = boost::filesystem::system_complete(argv[0]).parent_path().parent_path().parent_path().string();
553 string certPath = testDir + "/keys/server.crt";
554 string keyPath = testDir + "/keys/server.key";
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +0530555
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100556#if _WIN32
Jake Farrell5d02b802014-01-07 21:42:01 -0500557 transport::TWinsockSingleton::create();
558#endif
Mark Sleee8540632006-05-30 09:24:40 +0000559 int port = 9090;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000560 bool ssl = false;
Roger Meierca142b02011-06-07 17:59:07 +0000561 string transport_type = "buffered";
562 string protocol_type = "binary";
563 string server_type = "simple";
564 string domain_socket = "";
pavlodd08f6e2015-10-08 16:43:56 -0400565 bool abstract_namespace = false;
Roger Meierca142b02011-06-07 17:59:07 +0000566 size_t workers = 4;
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900567 int string_limit = 0;
568 int container_limit = 0;
Marc Slemko6be374b2006-08-04 03:16:25 +0000569
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900570 po::options_description desc("Allowed options");
571 desc.add_options()
572 ("help,h", "produce help message")
573 ("port", po::value<int>(&port)->default_value(port), "Port number to listen")
574 ("domain-socket", po::value<string>(&domain_socket) ->default_value(domain_socket), "Unix Domain Socket (e.g. /tmp/ThriftTest.thrift)")
575 ("abstract-namespace", "Create the domain socket in the Abstract Namespace (no connection with filesystem pathnames)")
576 ("server-type", po::value<string>(&server_type)->default_value(server_type), "type of server, \"simple\", \"thread-pool\", \"threaded\", or \"nonblocking\"")
577 ("transport", po::value<string>(&transport_type)->default_value(transport_type), "transport: buffered, framed, http")
James E. King, III58402ff2017-11-17 14:41:46 -0500578 ("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 +0900579 ("ssl", "Encrypted Transport using SSL")
580 ("processor-events", "processor-events")
581 ("workers,n", po::value<size_t>(&workers)->default_value(workers), "Number of thread pools workers. Only valid for thread-pool server type")
582 ("string-limit", po::value<int>(&string_limit))
583 ("container-limit", po::value<int>(&container_limit));
Marc Slemko6be374b2006-08-04 03:16:25 +0000584
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900585 po::variables_map vm;
586 po::store(po::parse_command_line(argc, argv, desc), vm);
587 po::notify(vm);
Marc Slemko6be374b2006-08-04 03:16:25 +0000588
Roger Meierca142b02011-06-07 17:59:07 +0000589 if (vm.count("help")) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100590 cout << desc << "\n";
591 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000592 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500593
Marc Slemko6be374b2006-08-04 03:16:25 +0000594 try {
Roger Meierca142b02011-06-07 17:59:07 +0000595 if (!server_type.empty()) {
596 if (server_type == "simple") {
597 } else if (server_type == "thread-pool") {
598 } else if (server_type == "threaded") {
599 } else if (server_type == "nonblocking") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000600 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100601 throw invalid_argument("Unknown server type " + server_type);
Roger Meierca142b02011-06-07 17:59:07 +0000602 }
603 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500604
Roger Meierca142b02011-06-07 17:59:07 +0000605 if (!protocol_type.empty()) {
606 if (protocol_type == "binary") {
Roger Meier284101c2014-03-11 21:20:35 +0100607 } else if (protocol_type == "compact") {
Roger Meierca142b02011-06-07 17:59:07 +0000608 } else if (protocol_type == "json") {
Dave Watson792db4e2015-01-16 11:22:01 -0800609 } else if (protocol_type == "header") {
James E. King, III39eaae62017-11-19 20:17:33 -0500610 } else if (protocol_type == "multi") { // multiplexed binary
611 } else if (protocol_type == "multic") { // multiplexed compact
612 } else if (protocol_type == "multih") { // multiplexed header
613 } else if (protocol_type == "multij") { // multiplexed json
Roger Meierca142b02011-06-07 17:59:07 +0000614 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100615 throw invalid_argument("Unknown protocol type " + protocol_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000616 }
617 }
618
Roger Meier284101c2014-03-11 21:20:35 +0100619 if (!transport_type.empty()) {
Roger Meierca142b02011-06-07 17:59:07 +0000620 if (transport_type == "buffered") {
621 } else if (transport_type == "framed") {
622 } else if (transport_type == "http") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000623 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100624 throw invalid_argument("Unknown transport type " + transport_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000625 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000626 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000627
Bryan Duxbury833ae492010-09-27 17:26:02 +0000628 } catch (std::exception& e) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000629 cerr << e.what() << endl;
Roger Meierca142b02011-06-07 17:59:07 +0000630 cout << desc << "\n";
631 return 1;
Marc Slemko6be374b2006-08-04 03:16:25 +0000632 }
633
Roger Meierca142b02011-06-07 17:59:07 +0000634 if (vm.count("ssl")) {
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000635 ssl = true;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000636 }
637
pavlodd08f6e2015-10-08 16:43:56 -0400638 if (vm.count("abstract-namespace")) {
639 abstract_namespace = true;
640 }
641
Mark Sleee8540632006-05-30 09:24:40 +0000642 // Dispatcher
James E. King, III82ae9572017-08-05 12:23:54 -0400643 stdcxx::shared_ptr<TProtocolFactory> protocolFactory;
James E. King, III58402ff2017-11-17 14:41:46 -0500644 if (protocol_type == "json" || protocol_type == "multij") {
James E. King, III82ae9572017-08-05 12:23:54 -0400645 stdcxx::shared_ptr<TProtocolFactory> jsonProtocolFactory(new TJSONProtocolFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000646 protocolFactory = jsonProtocolFactory;
James E. King, III58402ff2017-11-17 14:41:46 -0500647 } else if (protocol_type == "compact" || protocol_type == "multic") {
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900648 TCompactProtocolFactoryT<TBufferBase> *compactProtocolFactory = new TCompactProtocolFactoryT<TBufferBase>();
649 compactProtocolFactory->setContainerSizeLimit(container_limit);
650 compactProtocolFactory->setStringSizeLimit(string_limit);
651 protocolFactory.reset(compactProtocolFactory);
James E. King, III58402ff2017-11-17 14:41:46 -0500652 } else if (protocol_type == "header" || protocol_type == "multih") {
James E. King, III82ae9572017-08-05 12:23:54 -0400653 stdcxx::shared_ptr<TProtocolFactory> headerProtocolFactory(new THeaderProtocolFactory());
Dave Watson792db4e2015-01-16 11:22:01 -0800654 protocolFactory = headerProtocolFactory;
Roger Meierca142b02011-06-07 17:59:07 +0000655 } else {
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900656 TBinaryProtocolFactoryT<TBufferBase>* binaryProtocolFactory = new TBinaryProtocolFactoryT<TBufferBase>();
657 binaryProtocolFactory->setContainerSizeLimit(container_limit);
658 binaryProtocolFactory->setStringSizeLimit(string_limit);
659 protocolFactory.reset(binaryProtocolFactory);
Roger Meierca142b02011-06-07 17:59:07 +0000660 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000661
James E. King, III58402ff2017-11-17 14:41:46 -0500662 // Processors
James E. King, III82ae9572017-08-05 12:23:54 -0400663 stdcxx::shared_ptr<TestHandler> testHandler(new TestHandler());
James E. King, III58402ff2017-11-17 14:41:46 -0500664 stdcxx::shared_ptr<TProcessor> testProcessor(new ThriftTestProcessor(testHandler));
Jake Farrell5d02b802014-01-07 21:42:01 -0500665
Roger Meierca142b02011-06-07 17:59:07 +0000666 if (vm.count("processor-events")) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100667 testProcessor->setEventHandler(
James E. King, III82ae9572017-08-05 12:23:54 -0400668 stdcxx::shared_ptr<TProcessorEventHandler>(new TestProcessorEventHandler()));
David Reissd7192062010-10-06 17:09:33 +0000669 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500670
Mark Sleee8540632006-05-30 09:24:40 +0000671 // Transport
James E. King, III82ae9572017-08-05 12:23:54 -0400672 stdcxx::shared_ptr<TSSLSocketFactory> sslSocketFactory;
673 stdcxx::shared_ptr<TServerSocket> serverSocket;
Mark Sleee8540632006-05-30 09:24:40 +0000674
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000675 if (ssl) {
James E. King, III82ae9572017-08-05 12:23:54 -0400676 sslSocketFactory = stdcxx::shared_ptr<TSSLSocketFactory>(new TSSLSocketFactory());
Nobuaki Sukegawac8744082016-03-09 19:55:56 +0900677 sslSocketFactory->loadCertificate(certPath.c_str());
678 sslSocketFactory->loadPrivateKey(keyPath.c_str());
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000679 sslSocketFactory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
James E. King III70b33fb2018-03-11 10:57:10 -0400680 if (server_type != "nonblocking") {
681 serverSocket = stdcxx::shared_ptr<TServerSocket>(new TSSLServerSocket(port, sslSocketFactory));
682 }
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000683 } else {
Roger Meierc94b2932014-02-22 20:07:33 +0100684 if (domain_socket != "") {
pavlodd08f6e2015-10-08 16:43:56 -0400685 if (abstract_namespace) {
686 std::string abstract_socket("\0", 1);
687 abstract_socket += domain_socket;
James E. King, III82ae9572017-08-05 12:23:54 -0400688 serverSocket = stdcxx::shared_ptr<TServerSocket>(new TServerSocket(abstract_socket));
pavlodd08f6e2015-10-08 16:43:56 -0400689 } else {
690 unlink(domain_socket.c_str());
James E. King, III82ae9572017-08-05 12:23:54 -0400691 serverSocket = stdcxx::shared_ptr<TServerSocket>(new TServerSocket(domain_socket));
pavlodd08f6e2015-10-08 16:43:56 -0400692 }
Roger Meierc94b2932014-02-22 20:07:33 +0100693 port = 0;
694 } else {
James E. King, III82ae9572017-08-05 12:23:54 -0400695 serverSocket = stdcxx::shared_ptr<TServerSocket>(new TServerSocket(port));
Roger Meierc94b2932014-02-22 20:07:33 +0100696 }
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000697 }
Roger Meierca142b02011-06-07 17:59:07 +0000698
Mark Sleed788b2e2006-09-07 01:26:35 +0000699 // Factory
James E. King, III82ae9572017-08-05 12:23:54 -0400700 stdcxx::shared_ptr<TTransportFactory> transportFactory;
Jake Farrell5d02b802014-01-07 21:42:01 -0500701
Roger Meier7e056e72011-07-17 07:28:28 +0000702 if (transport_type == "http" && server_type != "nonblocking") {
James E. King, III82ae9572017-08-05 12:23:54 -0400703 stdcxx::shared_ptr<TTransportFactory> httpTransportFactory(new THttpServerTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000704 transportFactory = httpTransportFactory;
705 } else if (transport_type == "framed") {
James E. King, III82ae9572017-08-05 12:23:54 -0400706 stdcxx::shared_ptr<TTransportFactory> framedTransportFactory(new TFramedTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000707 transportFactory = framedTransportFactory;
708 } else {
James E. King, III82ae9572017-08-05 12:23:54 -0400709 stdcxx::shared_ptr<TTransportFactory> bufferedTransportFactory(new TBufferedTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000710 transportFactory = bufferedTransportFactory;
711 }
Mark Sleed788b2e2006-09-07 01:26:35 +0000712
Roger Meierca142b02011-06-07 17:59:07 +0000713 // Server Info
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100714 cout << "Starting \"" << server_type << "\" server (" << transport_type << "/" << protocol_type
pavlodd08f6e2015-10-08 16:43:56 -0400715 << ") listen on: ";
716 if (abstract_namespace) {
717 cout << '@';
718 }
719 cout << domain_socket;
Roger Meierca142b02011-06-07 17:59:07 +0000720 if (port != 0) {
721 cout << port;
722 }
723 cout << endl;
Mark Sleee8540632006-05-30 09:24:40 +0000724
James E. King, III58402ff2017-11-17 14:41:46 -0500725 // Multiplexed Processor if needed
726 if (boost::starts_with(protocol_type, "multi")) {
727 stdcxx::shared_ptr<SecondHandler> secondHandler(new SecondHandler());
728 stdcxx::shared_ptr<SecondServiceProcessor> secondProcessor(new SecondServiceProcessor(secondHandler));
729
730 stdcxx::shared_ptr<TMultiplexedProcessor> multiplexedProcessor(new TMultiplexedProcessor());
731 multiplexedProcessor->registerDefault(testProcessor); // non-multi clients go to the default processor (multi:binary, multic:compact, ...)
732 multiplexedProcessor->registerProcessor("ThriftTest", testProcessor);
733 multiplexedProcessor->registerProcessor("SecondService", secondProcessor);
734 testProcessor = stdcxx::dynamic_pointer_cast<TProcessor>(multiplexedProcessor);
735 }
736
Roger Meierca142b02011-06-07 17:59:07 +0000737 // Server
James E. King, III82ae9572017-08-05 12:23:54 -0400738 stdcxx::shared_ptr<apache::thrift::server::TServer> server;
Jake Farrell5d02b802014-01-07 21:42:01 -0500739
Roger Meierca142b02011-06-07 17:59:07 +0000740 if (server_type == "simple") {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100741 server.reset(new TSimpleServer(testProcessor, serverSocket, transportFactory, protocolFactory));
Roger Meierca142b02011-06-07 17:59:07 +0000742 } else if (server_type == "thread-pool") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000743
James E. King, III82ae9572017-08-05 12:23:54 -0400744 stdcxx::shared_ptr<PlatformThreadFactory> threadFactory
745 = stdcxx::shared_ptr<PlatformThreadFactory>(new PlatformThreadFactory());
Marc Slemko6be374b2006-08-04 03:16:25 +0000746
James E. King III70b33fb2018-03-11 10:57:10 -0400747 stdcxx::shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workers);
Marc Slemko6be374b2006-08-04 03:16:25 +0000748 threadManager->threadFactory(threadFactory);
Marc Slemko6be374b2006-08-04 03:16:25 +0000749 threadManager->start();
750
Jake Farrell5d02b802014-01-07 21:42:01 -0500751 server.reset(new TThreadPoolServer(testProcessor,
Roger Meierca142b02011-06-07 17:59:07 +0000752 serverSocket,
Mark Sleed788b2e2006-09-07 01:26:35 +0000753 transportFactory,
Mark Sleea3302652006-10-25 19:03:32 +0000754 protocolFactory,
Jake Farrell5d02b802014-01-07 21:42:01 -0500755 threadManager));
Roger Meierca142b02011-06-07 17:59:07 +0000756 } else if (server_type == "threaded") {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100757 server.reset(
758 new TThreadedServer(testProcessor, serverSocket, transportFactory, protocolFactory));
Roger Meierca142b02011-06-07 17:59:07 +0000759 } else if (server_type == "nonblocking") {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100760 if (transport_type == "http") {
James E. King, III82ae9572017-08-05 12:23:54 -0400761 stdcxx::shared_ptr<TestHandlerAsync> testHandlerAsync(new TestHandlerAsync(testHandler));
762 stdcxx::shared_ptr<TAsyncProcessor> testProcessorAsync(
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100763 new ThriftTestAsyncProcessor(testHandlerAsync));
James E. King, III82ae9572017-08-05 12:23:54 -0400764 stdcxx::shared_ptr<TAsyncBufferProcessor> testBufferProcessor(
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100765 new TAsyncProtocolProcessor(testProcessorAsync, protocolFactory));
Jake Farrell5d02b802014-01-07 21:42:01 -0500766
767 // not loading nonblockingServer into "server" because
768 // TEvhttpServer doesn't inherit from TServer, and doesn't
769 // provide a stop method.
Roger Meier7e056e72011-07-17 07:28:28 +0000770 TEvhttpServer nonblockingServer(testBufferProcessor, port);
771 nonblockingServer.serve();
James E. King III70b33fb2018-03-11 10:57:10 -0400772 } else if (transport_type == "framed") {
773 stdcxx::shared_ptr<transport::TNonblockingServerTransport> nbSocket;
774 nbSocket.reset(
775 ssl ? new transport::TNonblockingSSLServerSocket(port, sslSocketFactory)
776 : new transport::TNonblockingServerSocket(port));
Divya Thaluru808d1432017-08-06 16:36:36 -0700777 server.reset(new TNonblockingServer(testProcessor, protocolFactory, nbSocket));
James E. King III70b33fb2018-03-11 10:57:10 -0400778 } else {
779 cerr << "server-type nonblocking requires transport of http or framed" << endl;
780 exit(1);
Roger Meier7e056e72011-07-17 07:28:28 +0000781 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000782 }
783
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100784 if (server.get() != NULL) {
Dave Watson792db4e2015-01-16 11:22:01 -0800785 if (protocol_type == "header") {
James E. King, IIIdf899132016-11-12 15:16:30 -0500786 // Tell the server to use the same protocol for input / output
Dave Watson792db4e2015-01-16 11:22:01 -0800787 // if using header
James E. King, III82ae9572017-08-05 12:23:54 -0400788 server->setOutputProtocolFactory(stdcxx::shared_ptr<TProtocolFactory>());
Dave Watson792db4e2015-01-16 11:22:01 -0800789 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500790 apache::thrift::concurrency::PlatformThreadFactory factory;
791 factory.setDetached(false);
James E. King, III82ae9572017-08-05 12:23:54 -0400792 stdcxx::shared_ptr<apache::thrift::concurrency::Runnable> serverThreadRunner(server);
793 stdcxx::shared_ptr<apache::thrift::concurrency::Thread> thread
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100794 = factory.newThread(serverThreadRunner);
Jake Farrell5d02b802014-01-07 21:42:01 -0500795 thread->start();
796
James E. King III70b33fb2018-03-11 10:57:10 -0400797 // THRIFT-4515: this needs to be improved
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100798 while (1) {
James E. King III70b33fb2018-03-11 10:57:10 -0400799 sleep(1); // do something other than chew up CPU like crazy
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100800 }
James E. King III70b33fb2018-03-11 10:57:10 -0400801 // NOTREACHED
Jake Farrell5d02b802014-01-07 21:42:01 -0500802
803 server->stop();
804 thread->join();
805 server.reset();
806 }
807
Roger Meierca142b02011-06-07 17:59:07 +0000808 cout << "done." << endl;
Mark Sleee8540632006-05-30 09:24:40 +0000809 return 0;
810}