blob: 78b0a742b4f1f46f922c0591487c7dcdd9ff4240 [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, III58402ff2017-11-17 14:41:46 -050036#include <thrift/transport/TNonblockingServerSocket.h>
37#include <thrift/transport/TSSLServerSocket.h>
38#include <thrift/transport/TSSLSocket.h>
39#include <thrift/transport/TServerSocket.h>
Roger Meier49ff8b12012-04-13 09:12:31 +000040#include <thrift/transport/TTransportUtils.h>
James E. King, III58402ff2017-11-17 14:41:46 -050041
42#include "SecondService.h"
Mark Slee95771002006-06-07 06:53:25 +000043#include "ThriftTest.h"
Marc Slemko6be374b2006-08-04 03:16:25 +000044
James E. King, III7edc8fa2017-01-20 10:11:41 -050045#ifdef HAVE_STDINT_H
46#include <stdint.h>
47#endif
48#ifdef HAVE_INTTYPES_H
49#include <inttypes.h>
50#endif
51
Marc Slemko6be374b2006-08-04 03:16:25 +000052#include <iostream>
53#include <stdexcept>
54#include <sstream>
55
James E. King, III58402ff2017-11-17 14:41:46 -050056#include <boost/algorithm/string.hpp>
Roger Meierca142b02011-06-07 17:59:07 +000057#include <boost/program_options.hpp>
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +053058#include <boost/filesystem.hpp>
James E. King, III82ae9572017-08-05 12:23:54 -040059#include <thrift/stdcxx.h>
Roger Meierca142b02011-06-07 17:59:07 +000060
Bryan Duxburycd9aea12011-02-22 18:12:06 +000061#include <signal.h>
Jake Farrell5d02b802014-01-07 21:42:01 -050062#if _WIN32
Konrad Grochowski16a23a62014-11-13 15:33:38 +010063#include <thrift/windows/TWinsockSingleton.h>
Jake Farrell5d02b802014-01-07 21:42:01 -050064#endif
David Reissbc3dddb2007-08-22 23:20:24 +000065
Mark Sleee8540632006-05-30 09:24:40 +000066using namespace std;
67
T Jake Lucianib5e62212009-01-31 22:36:20 +000068using namespace apache::thrift;
James E. King, III82ae9572017-08-05 12:23:54 -040069using namespace apache::thrift::async;
T Jake Lucianib5e62212009-01-31 22:36:20 +000070using namespace apache::thrift::concurrency;
71using namespace apache::thrift::protocol;
72using namespace apache::thrift::transport;
73using namespace apache::thrift::server;
Marc Slemko6be374b2006-08-04 03:16:25 +000074
Marc Slemkobf4fd192006-08-15 21:29:39 +000075using namespace thrift::test;
76
Mark Sleed2655522006-09-05 22:09:57 +000077class TestHandler : public ThriftTestIf {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010078public:
Mark Sleed2655522006-09-05 22:09:57 +000079 TestHandler() {}
Mark Sleee8540632006-05-30 09:24:40 +000080
Konrad Grochowski16a23a62014-11-13 15:33:38 +010081 void testVoid() { printf("testVoid()\n"); }
Mark Sleee8540632006-05-30 09:24:40 +000082
Konrad Grochowski16a23a62014-11-13 15:33:38 +010083 void testString(string& out, const string& thing) {
Mark Sleee8540632006-05-30 09:24:40 +000084 printf("testString(\"%s\")\n", thing.c_str());
Mark Slee1921d202007-01-24 19:43:06 +000085 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +000086 }
87
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +090088 bool testBool(const bool thing) {
89 printf("testBool(%s)\n", thing ? "true" : "false");
90 return thing;
91 }
92
Mark Slee1921d202007-01-24 19:43:06 +000093 int8_t testByte(const int8_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +000094 printf("testByte(%d)\n", (int)thing);
95 return thing;
96 }
97
Mark Slee1921d202007-01-24 19:43:06 +000098 int32_t testI32(const int32_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +000099 printf("testI32(%d)\n", thing);
100 return thing;
101 }
102
Mark Slee1921d202007-01-24 19:43:06 +0000103 int64_t testI64(const int64_t thing) {
Roger Meier0e814802014-01-17 21:07:58 +0100104 printf("testI64(%" PRId64 ")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +0000105 return thing;
106 }
107
Mark Slee1921d202007-01-24 19:43:06 +0000108 double testDouble(const double thing) {
Roger Meiera8cef6e2011-07-17 18:55:59 +0000109 printf("testDouble(%f)\n", thing);
Mark Sleec98d0502006-09-06 02:42:25 +0000110 return thing;
111 }
112
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100113 void testBinary(std::string& _return, const std::string& thing) {
114 std::ostringstream hexstr;
115 hexstr << std::hex << thing;
James E. King, III39eaae62017-11-19 20:17:33 -0500116 printf("testBinary(%lu: %s)\n", thing.size(), hexstr.str().c_str());
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100117 _return = thing;
118 }
119
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100120 void testStruct(Xtruct& out, const Xtruct& thing) {
121 printf("testStruct({\"%s\", %d, %d, %" PRId64 "})\n",
122 thing.string_thing.c_str(),
123 (int)thing.byte_thing,
124 thing.i32_thing,
125 thing.i64_thing);
Mark Slee1921d202007-01-24 19:43:06 +0000126 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000127 }
128
Mark Slee1921d202007-01-24 19:43:06 +0000129 void testNest(Xtruct2& out, const Xtruct2& nest) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100130 const Xtruct& thing = nest.struct_thing;
131 printf("testNest({%d, {\"%s\", %d, %d, %" PRId64 "}, %d})\n",
132 (int)nest.byte_thing,
133 thing.string_thing.c_str(),
134 (int)thing.byte_thing,
135 thing.i32_thing,
136 thing.i64_thing,
137 nest.i32_thing);
Mark Slee1921d202007-01-24 19:43:06 +0000138 out = nest;
Mark Sleee8540632006-05-30 09:24:40 +0000139 }
140
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100141 void testMap(map<int32_t, int32_t>& out, const map<int32_t, int32_t>& thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000142 printf("testMap({");
143 map<int32_t, int32_t>::const_iterator m_iter;
144 bool first = true;
145 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
146 if (first) {
147 first = false;
148 } else {
149 printf(", ");
150 }
151 printf("%d => %d", m_iter->first, m_iter->second);
152 }
153 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000154 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000155 }
156
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100157 void testStringMap(map<std::string, std::string>& out,
158 const map<std::string, std::string>& thing) {
Roger Meierd3b9dca2011-06-24 14:01:10 +0000159 printf("testMap({");
160 map<std::string, std::string>::const_iterator m_iter;
161 bool first = true;
162 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
163 if (first) {
164 first = false;
165 } else {
166 printf(", ");
167 }
168 printf("%s => %s", (m_iter->first).c_str(), (m_iter->second).c_str());
169 }
170 printf("})\n");
171 out = thing;
172 }
173
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100174 void testSet(set<int32_t>& out, const set<int32_t>& thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000175 printf("testSet({");
176 set<int32_t>::const_iterator s_iter;
177 bool first = true;
178 for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) {
179 if (first) {
180 first = false;
181 } else {
182 printf(", ");
183 }
184 printf("%d", *s_iter);
185 }
186 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000187 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000188 }
189
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100190 void testList(vector<int32_t>& out, const vector<int32_t>& thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000191 printf("testList({");
Mark Sleeb9acf982006-10-10 01:57:32 +0000192 vector<int32_t>::const_iterator l_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000193 bool first = true;
194 for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) {
195 if (first) {
196 first = false;
197 } else {
198 printf(", ");
199 }
200 printf("%d", *l_iter);
201 }
202 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000203 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000204 }
205
Bryan Duxbury833ae492010-09-27 17:26:02 +0000206 Numberz::type testEnum(const Numberz::type thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000207 printf("testEnum(%d)\n", thing);
208 return thing;
209 }
210
Mark Slee1921d202007-01-24 19:43:06 +0000211 UserId testTypedef(const UserId thing) {
Roger Meier0e814802014-01-17 21:07:58 +0100212 printf("testTypedef(%" PRId64 ")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +0000213 return thing;
214 }
215
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100216 void testMapMap(map<int32_t, map<int32_t, int32_t> >& mapmap, const int32_t hello) {
Mark Sleee8540632006-05-30 09:24:40 +0000217 printf("testMapMap(%d)\n", hello);
Mark Sleee8540632006-05-30 09:24:40 +0000218
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100219 map<int32_t, int32_t> pos;
220 map<int32_t, int32_t> neg;
Mark Sleee8540632006-05-30 09:24:40 +0000221 for (int i = 1; i < 5; i++) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100222 pos.insert(make_pair(i, i));
223 neg.insert(make_pair(-i, -i));
Mark Sleee8540632006-05-30 09:24:40 +0000224 }
225
226 mapmap.insert(make_pair(4, pos));
227 mapmap.insert(make_pair(-4, neg));
Mark Sleee8540632006-05-30 09:24:40 +0000228 }
229
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100230 void testInsanity(map<UserId, map<Numberz::type, Insanity> >& insane, const Insanity& argument) {
Mark Sleee8540632006-05-30 09:24:40 +0000231 printf("testInsanity()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000232
Mark Sleee8540632006-05-30 09:24:40 +0000233 Insanity looney;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000234 map<Numberz::type, Insanity> first_map;
235 map<Numberz::type, Insanity> second_map;
Mark Sleee8540632006-05-30 09:24:40 +0000236
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900237 first_map.insert(make_pair(Numberz::TWO, argument));
238 first_map.insert(make_pair(Numberz::THREE, argument));
Mark Sleee8540632006-05-30 09:24:40 +0000239
Bryan Duxbury833ae492010-09-27 17:26:02 +0000240 second_map.insert(make_pair(Numberz::SIX, looney));
Mark Sleee8540632006-05-30 09:24:40 +0000241
Mark Sleee8540632006-05-30 09:24:40 +0000242 insane.insert(make_pair(1, first_map));
243 insane.insert(make_pair(2, second_map));
244
245 printf("return");
246 printf(" = {");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100247 map<UserId, map<Numberz::type, Insanity> >::const_iterator i_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000248 for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) {
Roger Meier0e814802014-01-17 21:07:58 +0100249 printf("%" PRId64 " => {", i_iter->first);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100250 map<Numberz::type, Insanity>::const_iterator i2_iter;
251 for (i2_iter = i_iter->second.begin(); i2_iter != i_iter->second.end(); ++i2_iter) {
Mark Sleee8540632006-05-30 09:24:40 +0000252 printf("%d => {", i2_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000253 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
254 map<Numberz::type, UserId>::const_iterator um;
Mark Sleee8540632006-05-30 09:24:40 +0000255 printf("{");
256 for (um = userMap.begin(); um != userMap.end(); ++um) {
Roger Meier0e814802014-01-17 21:07:58 +0100257 printf("%d => %" PRId64 ", ", um->first, um->second);
Mark Sleee8540632006-05-30 09:24:40 +0000258 }
259 printf("}, ");
260
Mark Sleeb9acf982006-10-10 01:57:32 +0000261 vector<Xtruct> xtructs = i2_iter->second.xtructs;
262 vector<Xtruct>::const_iterator x;
Mark Sleee8540632006-05-30 09:24:40 +0000263 printf("{");
264 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100265 printf("{\"%s\", %d, %d, %" PRId64 "}, ",
266 x->string_thing.c_str(),
267 (int)x->byte_thing,
268 x->i32_thing,
269 x->i64_thing);
Mark Sleee8540632006-05-30 09:24:40 +0000270 }
271 printf("}");
272
273 printf("}, ");
274 }
275 printf("}, ");
276 }
277 printf("}\n");
Mark Sleee8540632006-05-30 09:24:40 +0000278 }
Marc Slemkoe6889de2006-08-12 00:32:53 +0000279
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100280 void testMulti(Xtruct& hello,
281 const int8_t arg0,
282 const int32_t arg1,
283 const int64_t arg2,
284 const std::map<int16_t, std::string>& arg3,
285 const Numberz::type arg4,
286 const UserId arg5) {
287 (void)arg3;
288 (void)arg4;
289 (void)arg5;
Jake Farrell5d02b802014-01-07 21:42:01 -0500290
Marc Slemkoe6889de2006-08-12 00:32:53 +0000291 printf("testMulti()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000292
Marc Slemkoe6889de2006-08-12 00:32:53 +0000293 hello.string_thing = "Hello2";
294 hello.byte_thing = arg0;
295 hello.i32_thing = arg1;
296 hello.i64_thing = (int64_t)arg2;
Marc Slemkoe6889de2006-08-12 00:32:53 +0000297 }
Marc Slemkobf4fd192006-08-15 21:29:39 +0000298
ben-craigfae08e72015-07-15 11:34:47 -0500299 void testException(const std::string& arg) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000300 printf("testException(%s)\n", arg.c_str());
Mark Sleed3d733a2006-09-01 22:19:06 +0000301 if (arg.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000302 Xception e;
303 e.errorCode = 1001;
David Reiss9813cbe2009-01-31 21:39:11 +0000304 e.message = arg;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000305 throw e;
Roger Meier1f8b48f2012-05-02 22:56:47 +0000306 } else if (arg.compare("TException") == 0) {
T Jake Lucianib5e62212009-01-31 22:36:20 +0000307 apache::thrift::TException e;
David Reiss55ff70f2008-06-11 00:58:25 +0000308 throw e;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000309 } else {
310 Xtruct result;
311 result.string_thing = arg;
312 return;
313 }
314 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000315
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100316 void testMultiException(Xtruct& result,
317 const std::string& arg0,
ben-craigfae08e72015-07-15 11:34:47 -0500318 const std::string& arg1) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000319
Marc Slemko71d4e472006-08-15 22:34:04 +0000320 printf("testMultiException(%s, %s)\n", arg0.c_str(), arg1.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000321
Mark Sleef5f2be42006-09-05 21:05:31 +0000322 if (arg0.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000323 Xception e;
324 e.errorCode = 1001;
325 e.message = "This is an Xception";
326 throw e;
Mark Sleef5f2be42006-09-05 21:05:31 +0000327 } else if (arg0.compare("Xception2") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000328 Xception2 e;
329 e.errorCode = 2002;
330 e.struct_thing.string_thing = "This is an Xception2";
331 throw e;
332 } else {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000333 result.string_thing = arg1;
Mark Slee1921d202007-01-24 19:43:06 +0000334 return;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000335 }
336 }
David Reiss2ab6fe82008-02-18 02:11:44 +0000337
James E. King, III58402ff2017-11-17 14:41:46 -0500338 void testOneway(const int32_t aNum) {
339 printf("testOneway(%d): call received\n", aNum);
David Reiss2ab6fe82008-02-18 02:11:44 +0000340 }
Mark Sleee8540632006-05-30 09:24:40 +0000341};
342
James E. King, III58402ff2017-11-17 14:41:46 -0500343class SecondHandler : public SecondServiceIf
344{
345 public:
346 void secondtestString(std::string& result, const std::string& thing)
347 { result = "testString(\"" + thing + "\")"; }
James E. King, III39eaae62017-11-19 20:17:33 -0500348};
James E. King, III58402ff2017-11-17 14:41:46 -0500349
David Reissd7192062010-10-06 17:09:33 +0000350class TestProcessorEventHandler : public TProcessorEventHandler {
David Reiss23248712010-10-06 17:10:08 +0000351 virtual void* getContext(const char* fn_name, void* serverContext) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100352 (void)serverContext;
David Reissd7192062010-10-06 17:09:33 +0000353 return new std::string(fn_name);
354 }
355 virtual void freeContext(void* ctx, const char* fn_name) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100356 (void)fn_name;
David Reissd7192062010-10-06 17:09:33 +0000357 delete static_cast<std::string*>(ctx);
358 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100359 virtual void preRead(void* ctx, const char* fn_name) { communicate("preRead", ctx, fn_name); }
David Reissef7200f2010-10-06 17:09:42 +0000360 virtual void postRead(void* ctx, const char* fn_name, uint32_t bytes) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100361 (void)bytes;
David Reissd7192062010-10-06 17:09:33 +0000362 communicate("postRead", ctx, fn_name);
363 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100364 virtual void preWrite(void* ctx, const char* fn_name) { communicate("preWrite", ctx, fn_name); }
David Reissef7200f2010-10-06 17:09:42 +0000365 virtual void postWrite(void* ctx, const char* fn_name, uint32_t bytes) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100366 (void)bytes;
David Reissd7192062010-10-06 17:09:33 +0000367 communicate("postWrite", ctx, fn_name);
368 }
369 virtual void asyncComplete(void* ctx, const char* fn_name) {
370 communicate("asyncComplete", ctx, fn_name);
371 }
372 virtual void handlerError(void* ctx, const char* fn_name) {
373 communicate("handlerError", ctx, fn_name);
374 }
375
376 void communicate(const char* event, void* ctx, const char* fn_name) {
377 std::cout << event << ": " << *static_cast<std::string*>(ctx) << " = " << fn_name << std::endl;
378 }
379};
380
Roger Meier7e056e72011-07-17 07:28:28 +0000381class TestHandlerAsync : public ThriftTestCobSvIf {
382public:
James E. King, III82ae9572017-08-05 12:23:54 -0400383 TestHandlerAsync(stdcxx::shared_ptr<TestHandler>& handler) : _delegate(handler) {}
Roger Meier7e056e72011-07-17 07:28:28 +0000384 virtual ~TestHandlerAsync() {}
385
James E. King, III82ae9572017-08-05 12:23:54 -0400386 virtual void testVoid(stdcxx::function<void()> cob) {
Roger Meier7e056e72011-07-17 07:28:28 +0000387 _delegate->testVoid();
388 cob();
389 }
390
James E. King, III82ae9572017-08-05 12:23:54 -0400391 virtual void testString(stdcxx::function<void(std::string const& _return)> cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100392 const std::string& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000393 std::string res;
394 _delegate->testString(res, thing);
395 cob(res);
396 }
397
James E. King, III82ae9572017-08-05 12:23:54 -0400398 virtual void testBool(stdcxx::function<void(bool const& _return)> cob, const bool thing) {
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900399 bool res = _delegate->testBool(thing);
400 cob(res);
401 }
402
James E. King, III82ae9572017-08-05 12:23:54 -0400403 virtual void testByte(stdcxx::function<void(int8_t const& _return)> cob, const int8_t thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000404 int8_t res = _delegate->testByte(thing);
405 cob(res);
406 }
407
James E. King, III82ae9572017-08-05 12:23:54 -0400408 virtual void testI32(stdcxx::function<void(int32_t const& _return)> cob, const int32_t thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000409 int32_t res = _delegate->testI32(thing);
410 cob(res);
411 }
412
James E. King, III82ae9572017-08-05 12:23:54 -0400413 virtual void testI64(stdcxx::function<void(int64_t const& _return)> cob, const int64_t thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000414 int64_t res = _delegate->testI64(thing);
415 cob(res);
416 }
417
James E. King, III82ae9572017-08-05 12:23:54 -0400418 virtual void testDouble(stdcxx::function<void(double const& _return)> cob, const double thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000419 double res = _delegate->testDouble(thing);
420 cob(res);
421 }
422
James E. King, III82ae9572017-08-05 12:23:54 -0400423 virtual void testBinary(stdcxx::function<void(std::string const& _return)> cob,
Konrad Grochowski1f6e3802015-05-18 18:10:06 +0200424 const std::string& thing) {
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100425 std::string res;
426 _delegate->testBinary(res, thing);
427 cob(res);
428 }
429
James E. King, III82ae9572017-08-05 12:23:54 -0400430 virtual void testStruct(stdcxx::function<void(Xtruct const& _return)> cob, const Xtruct& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000431 Xtruct res;
432 _delegate->testStruct(res, thing);
433 cob(res);
434 }
435
James E. King, III82ae9572017-08-05 12:23:54 -0400436 virtual void testNest(stdcxx::function<void(Xtruct2 const& _return)> cob, const Xtruct2& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000437 Xtruct2 res;
438 _delegate->testNest(res, thing);
439 cob(res);
440 }
441
James E. King, III82ae9572017-08-05 12:23:54 -0400442 virtual void testMap(stdcxx::function<void(std::map<int32_t, int32_t> const& _return)> cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100443 const std::map<int32_t, int32_t>& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000444 std::map<int32_t, int32_t> res;
445 _delegate->testMap(res, thing);
446 cob(res);
447 }
448
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100449 virtual void testStringMap(
James E. King, III82ae9572017-08-05 12:23:54 -0400450 stdcxx::function<void(std::map<std::string, std::string> const& _return)> cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100451 const std::map<std::string, std::string>& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000452 std::map<std::string, std::string> res;
453 _delegate->testStringMap(res, thing);
454 cob(res);
455 }
456
James E. King, III82ae9572017-08-05 12:23:54 -0400457 virtual void testSet(stdcxx::function<void(std::set<int32_t> const& _return)> cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100458 const std::set<int32_t>& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000459 std::set<int32_t> res;
460 _delegate->testSet(res, thing);
461 cob(res);
462 }
463
James E. King, III82ae9572017-08-05 12:23:54 -0400464 virtual void testList(stdcxx::function<void(std::vector<int32_t> const& _return)> cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100465 const std::vector<int32_t>& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000466 std::vector<int32_t> res;
467 _delegate->testList(res, thing);
468 cob(res);
469 }
470
James E. King, III82ae9572017-08-05 12:23:54 -0400471 virtual void testEnum(stdcxx::function<void(Numberz::type const& _return)> cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100472 const Numberz::type thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000473 Numberz::type res = _delegate->testEnum(thing);
474 cob(res);
475 }
476
James E. King, III82ae9572017-08-05 12:23:54 -0400477 virtual void testTypedef(stdcxx::function<void(UserId const& _return)> cob, const UserId thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000478 UserId res = _delegate->testTypedef(thing);
479 cob(res);
480 }
481
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100482 virtual void testMapMap(
James E. King, III82ae9572017-08-05 12:23:54 -0400483 stdcxx::function<void(std::map<int32_t, std::map<int32_t, int32_t> > const& _return)> cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100484 const int32_t hello) {
Roger Meier7e056e72011-07-17 07:28:28 +0000485 std::map<int32_t, std::map<int32_t, int32_t> > res;
486 _delegate->testMapMap(res, hello);
487 cob(res);
488 }
489
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100490 virtual void testInsanity(
James E. King, III82ae9572017-08-05 12:23:54 -0400491 stdcxx::function<void(std::map<UserId, std::map<Numberz::type, Insanity> > const& _return)> cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100492 const Insanity& argument) {
Jake Farrell5d02b802014-01-07 21:42:01 -0500493 std::map<UserId, std::map<Numberz::type, Insanity> > res;
Roger Meier7e056e72011-07-17 07:28:28 +0000494 _delegate->testInsanity(res, argument);
495 cob(res);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100496 }
Roger Meier7e056e72011-07-17 07:28:28 +0000497
James E. King, III82ae9572017-08-05 12:23:54 -0400498 virtual void testMulti(stdcxx::function<void(Xtruct const& _return)> cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100499 const int8_t arg0,
500 const int32_t arg1,
501 const int64_t arg2,
502 const std::map<int16_t, std::string>& arg3,
503 const Numberz::type arg4,
504 const UserId arg5) {
Roger Meier7e056e72011-07-17 07:28:28 +0000505 Xtruct res;
506 _delegate->testMulti(res, arg0, arg1, arg2, arg3, arg4, arg5);
507 cob(res);
508 }
509
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100510 virtual void testException(
James E. King, III82ae9572017-08-05 12:23:54 -0400511 stdcxx::function<void()> cob,
512 stdcxx::function<void(::apache::thrift::TDelayedException* _throw)> exn_cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100513 const std::string& arg) {
Roger Meier7e056e72011-07-17 07:28:28 +0000514 try {
515 _delegate->testException(arg);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100516 } catch (const apache::thrift::TException& e) {
Roger Meier7e056e72011-07-17 07:28:28 +0000517 exn_cob(apache::thrift::TDelayedException::delayException(e));
518 return;
519 }
520 cob();
521 }
522
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100523 virtual void testMultiException(
James E. King, III82ae9572017-08-05 12:23:54 -0400524 stdcxx::function<void(Xtruct const& _return)> cob,
525 stdcxx::function<void(::apache::thrift::TDelayedException* _throw)> exn_cob,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100526 const std::string& arg0,
527 const std::string& arg1) {
Roger Meier7e056e72011-07-17 07:28:28 +0000528 Xtruct res;
529 try {
530 _delegate->testMultiException(res, arg0, arg1);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100531 } catch (const apache::thrift::TException& e) {
Roger Meier7e056e72011-07-17 07:28:28 +0000532 exn_cob(apache::thrift::TDelayedException::delayException(e));
533 return;
534 }
535 cob(res);
536 }
537
James E. King, III82ae9572017-08-05 12:23:54 -0400538 virtual void testOneway(stdcxx::function<void()> cob, const int32_t secondsToSleep) {
Roger Meier7e056e72011-07-17 07:28:28 +0000539 _delegate->testOneway(secondsToSleep);
540 cob();
541 }
542
543protected:
James E. King, III82ae9572017-08-05 12:23:54 -0400544 stdcxx::shared_ptr<TestHandler> _delegate;
Roger Meier7e056e72011-07-17 07:28:28 +0000545};
546
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900547namespace po = boost::program_options;
548
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100549int main(int argc, char** argv) {
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +0530550
Nobuaki Sukegawac8744082016-03-09 19:55:56 +0900551 string testDir = boost::filesystem::system_complete(argv[0]).parent_path().parent_path().parent_path().string();
552 string certPath = testDir + "/keys/server.crt";
553 string keyPath = testDir + "/keys/server.key";
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +0530554
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100555#if _WIN32
Jake Farrell5d02b802014-01-07 21:42:01 -0500556 transport::TWinsockSingleton::create();
557#endif
Mark Sleee8540632006-05-30 09:24:40 +0000558 int port = 9090;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000559 bool ssl = false;
Roger Meierca142b02011-06-07 17:59:07 +0000560 string transport_type = "buffered";
561 string protocol_type = "binary";
562 string server_type = "simple";
563 string domain_socket = "";
pavlodd08f6e2015-10-08 16:43:56 -0400564 bool abstract_namespace = false;
Roger Meierca142b02011-06-07 17:59:07 +0000565 size_t workers = 4;
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900566 int string_limit = 0;
567 int container_limit = 0;
Marc Slemko6be374b2006-08-04 03:16:25 +0000568
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900569 po::options_description desc("Allowed options");
570 desc.add_options()
571 ("help,h", "produce help message")
572 ("port", po::value<int>(&port)->default_value(port), "Port number to listen")
573 ("domain-socket", po::value<string>(&domain_socket) ->default_value(domain_socket), "Unix Domain Socket (e.g. /tmp/ThriftTest.thrift)")
574 ("abstract-namespace", "Create the domain socket in the Abstract Namespace (no connection with filesystem pathnames)")
575 ("server-type", po::value<string>(&server_type)->default_value(server_type), "type of server, \"simple\", \"thread-pool\", \"threaded\", or \"nonblocking\"")
576 ("transport", po::value<string>(&transport_type)->default_value(transport_type), "transport: buffered, framed, http")
James E. King, III58402ff2017-11-17 14:41:46 -0500577 ("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 +0900578 ("ssl", "Encrypted Transport using SSL")
579 ("processor-events", "processor-events")
580 ("workers,n", po::value<size_t>(&workers)->default_value(workers), "Number of thread pools workers. Only valid for thread-pool server type")
581 ("string-limit", po::value<int>(&string_limit))
582 ("container-limit", po::value<int>(&container_limit));
Marc Slemko6be374b2006-08-04 03:16:25 +0000583
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900584 po::variables_map vm;
585 po::store(po::parse_command_line(argc, argv, desc), vm);
586 po::notify(vm);
Marc Slemko6be374b2006-08-04 03:16:25 +0000587
Roger Meierca142b02011-06-07 17:59:07 +0000588 if (vm.count("help")) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100589 cout << desc << "\n";
590 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000591 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500592
Marc Slemko6be374b2006-08-04 03:16:25 +0000593 try {
Roger Meierca142b02011-06-07 17:59:07 +0000594 if (!server_type.empty()) {
595 if (server_type == "simple") {
596 } else if (server_type == "thread-pool") {
597 } else if (server_type == "threaded") {
598 } else if (server_type == "nonblocking") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000599 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100600 throw invalid_argument("Unknown server type " + server_type);
Roger Meierca142b02011-06-07 17:59:07 +0000601 }
602 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500603
Roger Meierca142b02011-06-07 17:59:07 +0000604 if (!protocol_type.empty()) {
605 if (protocol_type == "binary") {
Roger Meier284101c2014-03-11 21:20:35 +0100606 } else if (protocol_type == "compact") {
Roger Meierca142b02011-06-07 17:59:07 +0000607 } else if (protocol_type == "json") {
Dave Watson792db4e2015-01-16 11:22:01 -0800608 } else if (protocol_type == "header") {
James E. King, III39eaae62017-11-19 20:17:33 -0500609 } else if (protocol_type == "multi") { // multiplexed binary
610 } else if (protocol_type == "multic") { // multiplexed compact
611 } else if (protocol_type == "multih") { // multiplexed header
612 } else if (protocol_type == "multij") { // multiplexed json
Roger Meierca142b02011-06-07 17:59:07 +0000613 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100614 throw invalid_argument("Unknown protocol type " + protocol_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000615 }
616 }
617
Roger Meier284101c2014-03-11 21:20:35 +0100618 if (!transport_type.empty()) {
Roger Meierca142b02011-06-07 17:59:07 +0000619 if (transport_type == "buffered") {
620 } else if (transport_type == "framed") {
621 } else if (transport_type == "http") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000622 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100623 throw invalid_argument("Unknown transport type " + transport_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000624 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000625 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000626
Bryan Duxbury833ae492010-09-27 17:26:02 +0000627 } catch (std::exception& e) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000628 cerr << e.what() << endl;
Roger Meierca142b02011-06-07 17:59:07 +0000629 cout << desc << "\n";
630 return 1;
Marc Slemko6be374b2006-08-04 03:16:25 +0000631 }
632
Roger Meierca142b02011-06-07 17:59:07 +0000633 if (vm.count("ssl")) {
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000634 ssl = true;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000635 }
636
pavlodd08f6e2015-10-08 16:43:56 -0400637 if (vm.count("abstract-namespace")) {
638 abstract_namespace = true;
639 }
640
Mark Sleee8540632006-05-30 09:24:40 +0000641 // Dispatcher
James E. King, III82ae9572017-08-05 12:23:54 -0400642 stdcxx::shared_ptr<TProtocolFactory> protocolFactory;
James E. King, III58402ff2017-11-17 14:41:46 -0500643 if (protocol_type == "json" || protocol_type == "multij") {
James E. King, III82ae9572017-08-05 12:23:54 -0400644 stdcxx::shared_ptr<TProtocolFactory> jsonProtocolFactory(new TJSONProtocolFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000645 protocolFactory = jsonProtocolFactory;
James E. King, III58402ff2017-11-17 14:41:46 -0500646 } else if (protocol_type == "compact" || protocol_type == "multic") {
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900647 TCompactProtocolFactoryT<TBufferBase> *compactProtocolFactory = new TCompactProtocolFactoryT<TBufferBase>();
648 compactProtocolFactory->setContainerSizeLimit(container_limit);
649 compactProtocolFactory->setStringSizeLimit(string_limit);
650 protocolFactory.reset(compactProtocolFactory);
James E. King, III58402ff2017-11-17 14:41:46 -0500651 } else if (protocol_type == "header" || protocol_type == "multih") {
James E. King, III82ae9572017-08-05 12:23:54 -0400652 stdcxx::shared_ptr<TProtocolFactory> headerProtocolFactory(new THeaderProtocolFactory());
Dave Watson792db4e2015-01-16 11:22:01 -0800653 protocolFactory = headerProtocolFactory;
Roger Meierca142b02011-06-07 17:59:07 +0000654 } else {
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900655 TBinaryProtocolFactoryT<TBufferBase>* binaryProtocolFactory = new TBinaryProtocolFactoryT<TBufferBase>();
656 binaryProtocolFactory->setContainerSizeLimit(container_limit);
657 binaryProtocolFactory->setStringSizeLimit(string_limit);
658 protocolFactory.reset(binaryProtocolFactory);
Roger Meierca142b02011-06-07 17:59:07 +0000659 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000660
James E. King, III58402ff2017-11-17 14:41:46 -0500661 // Processors
James E. King, III82ae9572017-08-05 12:23:54 -0400662 stdcxx::shared_ptr<TestHandler> testHandler(new TestHandler());
James E. King, III58402ff2017-11-17 14:41:46 -0500663 stdcxx::shared_ptr<TProcessor> testProcessor(new ThriftTestProcessor(testHandler));
Jake Farrell5d02b802014-01-07 21:42:01 -0500664
Roger Meierca142b02011-06-07 17:59:07 +0000665 if (vm.count("processor-events")) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100666 testProcessor->setEventHandler(
James E. King, III82ae9572017-08-05 12:23:54 -0400667 stdcxx::shared_ptr<TProcessorEventHandler>(new TestProcessorEventHandler()));
David Reissd7192062010-10-06 17:09:33 +0000668 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500669
Mark Sleee8540632006-05-30 09:24:40 +0000670 // Transport
James E. King, III82ae9572017-08-05 12:23:54 -0400671 stdcxx::shared_ptr<TSSLSocketFactory> sslSocketFactory;
672 stdcxx::shared_ptr<TServerSocket> serverSocket;
Mark Sleee8540632006-05-30 09:24:40 +0000673
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000674 if (ssl) {
James E. King, III82ae9572017-08-05 12:23:54 -0400675 sslSocketFactory = stdcxx::shared_ptr<TSSLSocketFactory>(new TSSLSocketFactory());
Nobuaki Sukegawac8744082016-03-09 19:55:56 +0900676 sslSocketFactory->loadCertificate(certPath.c_str());
677 sslSocketFactory->loadPrivateKey(keyPath.c_str());
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000678 sslSocketFactory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
James E. King, III82ae9572017-08-05 12:23:54 -0400679 serverSocket = stdcxx::shared_ptr<TServerSocket>(new TSSLServerSocket(port, sslSocketFactory));
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000680 } else {
Roger Meierc94b2932014-02-22 20:07:33 +0100681 if (domain_socket != "") {
pavlodd08f6e2015-10-08 16:43:56 -0400682 if (abstract_namespace) {
683 std::string abstract_socket("\0", 1);
684 abstract_socket += domain_socket;
James E. King, III82ae9572017-08-05 12:23:54 -0400685 serverSocket = stdcxx::shared_ptr<TServerSocket>(new TServerSocket(abstract_socket));
pavlodd08f6e2015-10-08 16:43:56 -0400686 } else {
687 unlink(domain_socket.c_str());
James E. King, III82ae9572017-08-05 12:23:54 -0400688 serverSocket = stdcxx::shared_ptr<TServerSocket>(new TServerSocket(domain_socket));
pavlodd08f6e2015-10-08 16:43:56 -0400689 }
Roger Meierc94b2932014-02-22 20:07:33 +0100690 port = 0;
691 } else {
James E. King, III82ae9572017-08-05 12:23:54 -0400692 serverSocket = stdcxx::shared_ptr<TServerSocket>(new TServerSocket(port));
Roger Meierc94b2932014-02-22 20:07:33 +0100693 }
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000694 }
Roger Meierca142b02011-06-07 17:59:07 +0000695
Mark Sleed788b2e2006-09-07 01:26:35 +0000696 // Factory
James E. King, III82ae9572017-08-05 12:23:54 -0400697 stdcxx::shared_ptr<TTransportFactory> transportFactory;
Jake Farrell5d02b802014-01-07 21:42:01 -0500698
Roger Meier7e056e72011-07-17 07:28:28 +0000699 if (transport_type == "http" && server_type != "nonblocking") {
James E. King, III82ae9572017-08-05 12:23:54 -0400700 stdcxx::shared_ptr<TTransportFactory> httpTransportFactory(new THttpServerTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000701 transportFactory = httpTransportFactory;
702 } else if (transport_type == "framed") {
James E. King, III82ae9572017-08-05 12:23:54 -0400703 stdcxx::shared_ptr<TTransportFactory> framedTransportFactory(new TFramedTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000704 transportFactory = framedTransportFactory;
705 } else {
James E. King, III82ae9572017-08-05 12:23:54 -0400706 stdcxx::shared_ptr<TTransportFactory> bufferedTransportFactory(new TBufferedTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000707 transportFactory = bufferedTransportFactory;
708 }
Mark Sleed788b2e2006-09-07 01:26:35 +0000709
Roger Meierca142b02011-06-07 17:59:07 +0000710 // Server Info
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100711 cout << "Starting \"" << server_type << "\" server (" << transport_type << "/" << protocol_type
pavlodd08f6e2015-10-08 16:43:56 -0400712 << ") listen on: ";
713 if (abstract_namespace) {
714 cout << '@';
715 }
716 cout << domain_socket;
Roger Meierca142b02011-06-07 17:59:07 +0000717 if (port != 0) {
718 cout << port;
719 }
720 cout << endl;
Mark Sleee8540632006-05-30 09:24:40 +0000721
James E. King, III58402ff2017-11-17 14:41:46 -0500722 // Multiplexed Processor if needed
723 if (boost::starts_with(protocol_type, "multi")) {
724 stdcxx::shared_ptr<SecondHandler> secondHandler(new SecondHandler());
725 stdcxx::shared_ptr<SecondServiceProcessor> secondProcessor(new SecondServiceProcessor(secondHandler));
726
727 stdcxx::shared_ptr<TMultiplexedProcessor> multiplexedProcessor(new TMultiplexedProcessor());
728 multiplexedProcessor->registerDefault(testProcessor); // non-multi clients go to the default processor (multi:binary, multic:compact, ...)
729 multiplexedProcessor->registerProcessor("ThriftTest", testProcessor);
730 multiplexedProcessor->registerProcessor("SecondService", secondProcessor);
731 testProcessor = stdcxx::dynamic_pointer_cast<TProcessor>(multiplexedProcessor);
732 }
733
Roger Meierca142b02011-06-07 17:59:07 +0000734 // Server
James E. King, III82ae9572017-08-05 12:23:54 -0400735 stdcxx::shared_ptr<apache::thrift::server::TServer> server;
Jake Farrell5d02b802014-01-07 21:42:01 -0500736
Roger Meierca142b02011-06-07 17:59:07 +0000737 if (server_type == "simple") {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100738 server.reset(new TSimpleServer(testProcessor, serverSocket, transportFactory, protocolFactory));
Roger Meierca142b02011-06-07 17:59:07 +0000739 } else if (server_type == "thread-pool") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000740
James E. King, III82ae9572017-08-05 12:23:54 -0400741 stdcxx::shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workers);
Marc Slemko6be374b2006-08-04 03:16:25 +0000742
James E. King, III82ae9572017-08-05 12:23:54 -0400743 stdcxx::shared_ptr<PlatformThreadFactory> threadFactory
744 = stdcxx::shared_ptr<PlatformThreadFactory>(new PlatformThreadFactory());
Marc Slemko6be374b2006-08-04 03:16:25 +0000745
746 threadManager->threadFactory(threadFactory);
747
748 threadManager->start();
749
Jake Farrell5d02b802014-01-07 21:42:01 -0500750 server.reset(new TThreadPoolServer(testProcessor,
Roger Meierca142b02011-06-07 17:59:07 +0000751 serverSocket,
Mark Sleed788b2e2006-09-07 01:26:35 +0000752 transportFactory,
Mark Sleea3302652006-10-25 19:03:32 +0000753 protocolFactory,
Jake Farrell5d02b802014-01-07 21:42:01 -0500754 threadManager));
Roger Meierca142b02011-06-07 17:59:07 +0000755 } else if (server_type == "threaded") {
David Reiss0c90f6f2008-02-06 22:18:40 +0000756
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();
Jake Farrell5d02b802014-01-07 21:42:01 -0500772 } else {
James E. King, III82ae9572017-08-05 12:23:54 -0400773 stdcxx::shared_ptr<transport::TNonblockingServerSocket> nbSocket;
Divya Thaluru808d1432017-08-06 16:36:36 -0700774 nbSocket.reset(new transport::TNonblockingServerSocket(port));
775 server.reset(new TNonblockingServer(testProcessor, protocolFactory, nbSocket));
Roger Meier7e056e72011-07-17 07:28:28 +0000776 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000777 }
778
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100779 if (server.get() != NULL) {
Dave Watson792db4e2015-01-16 11:22:01 -0800780 if (protocol_type == "header") {
James E. King, IIIdf899132016-11-12 15:16:30 -0500781 // Tell the server to use the same protocol for input / output
Dave Watson792db4e2015-01-16 11:22:01 -0800782 // if using header
James E. King, III82ae9572017-08-05 12:23:54 -0400783 server->setOutputProtocolFactory(stdcxx::shared_ptr<TProtocolFactory>());
Dave Watson792db4e2015-01-16 11:22:01 -0800784 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500785 apache::thrift::concurrency::PlatformThreadFactory factory;
786 factory.setDetached(false);
James E. King, III82ae9572017-08-05 12:23:54 -0400787 stdcxx::shared_ptr<apache::thrift::concurrency::Runnable> serverThreadRunner(server);
788 stdcxx::shared_ptr<apache::thrift::concurrency::Thread> thread
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100789 = factory.newThread(serverThreadRunner);
Jake Farrell5d02b802014-01-07 21:42:01 -0500790 thread->start();
791
Roger Meier284101c2014-03-11 21:20:35 +0100792 // HACK: cross language test suite is unable to handle cin properly
793 // that's why we stay in a endless loop here
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100794 while (1) {
795 }
Roger Meier284101c2014-03-11 21:20:35 +0100796 // FIXME: find another way to stop the server (e.g. a signal)
797 // cout<<"Press enter to stop the server."<<endl;
798 // cin.ignore(); //wait until a key is pressed
Jake Farrell5d02b802014-01-07 21:42:01 -0500799
800 server->stop();
801 thread->join();
802 server.reset();
803 }
804
Roger Meierca142b02011-06-07 17:59:07 +0000805 cout << "done." << endl;
Mark Sleee8540632006-05-30 09:24:40 +0000806 return 0;
807}