blob: 2b58f8ace5bfdf5f684d154bf07331caab1e9b6d [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
Roger Meier49ff8b12012-04-13 09:12:31 +000020#include <thrift/concurrency/ThreadManager.h>
21#include <thrift/concurrency/PlatformThreadFactory.h>
22#include <thrift/protocol/TBinaryProtocol.h>
Roger Meier023192f2014-02-12 09:35:12 +010023#include <thrift/protocol/TCompactProtocol.h>
Dave Watson792db4e2015-01-16 11:22:01 -080024#include <thrift/protocol/THeaderProtocol.h>
Roger Meier49ff8b12012-04-13 09:12:31 +000025#include <thrift/protocol/TJSONProtocol.h>
26#include <thrift/server/TSimpleServer.h>
27#include <thrift/server/TThreadedServer.h>
28#include <thrift/server/TThreadPoolServer.h>
29#include <thrift/async/TEvhttpServer.h>
30#include <thrift/async/TAsyncBufferProcessor.h>
31#include <thrift/async/TAsyncProtocolProcessor.h>
32#include <thrift/server/TNonblockingServer.h>
33#include <thrift/transport/TServerSocket.h>
34#include <thrift/transport/TSSLServerSocket.h>
35#include <thrift/transport/TSSLSocket.h>
Divya Thaluru808d1432017-08-06 16:36:36 -070036#include <thrift/transport/TNonblockingServerSocket.h>
Roger Meier49ff8b12012-04-13 09:12:31 +000037#include <thrift/transport/THttpServer.h>
38#include <thrift/transport/THttpTransport.h>
39#include <thrift/transport/TTransportUtils.h>
Mark Slee95771002006-06-07 06:53:25 +000040#include "ThriftTest.h"
Marc Slemko6be374b2006-08-04 03:16:25 +000041
James E. King, III7edc8fa2017-01-20 10:11:41 -050042#ifdef HAVE_STDINT_H
43#include <stdint.h>
44#endif
45#ifdef HAVE_INTTYPES_H
46#include <inttypes.h>
47#endif
48
Marc Slemko6be374b2006-08-04 03:16:25 +000049#include <iostream>
50#include <stdexcept>
51#include <sstream>
52
Roger Meierca142b02011-06-07 17:59:07 +000053#include <boost/program_options.hpp>
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +053054#include <boost/filesystem.hpp>
Roger Meier0a7c69c2014-05-02 21:15:45 +020055#include <thrift/cxxfunctional.h>
Roger Meierca142b02011-06-07 17:59:07 +000056
Bryan Duxburycd9aea12011-02-22 18:12:06 +000057#include <signal.h>
Jake Farrell5d02b802014-01-07 21:42:01 -050058#if _WIN32
Konrad Grochowski16a23a62014-11-13 15:33:38 +010059#include <thrift/windows/TWinsockSingleton.h>
Jake Farrell5d02b802014-01-07 21:42:01 -050060#endif
David Reissbc3dddb2007-08-22 23:20:24 +000061
Mark Sleee8540632006-05-30 09:24:40 +000062using namespace std;
63
T Jake Lucianib5e62212009-01-31 22:36:20 +000064using namespace apache::thrift;
65using namespace apache::thrift::concurrency;
66using namespace apache::thrift::protocol;
67using namespace apache::thrift::transport;
68using namespace apache::thrift::server;
Roger Meier7e056e72011-07-17 07:28:28 +000069using namespace apache::thrift::async;
Marc Slemko6be374b2006-08-04 03:16:25 +000070
Marc Slemkobf4fd192006-08-15 21:29:39 +000071using namespace thrift::test;
72
Mark Sleed2655522006-09-05 22:09:57 +000073class TestHandler : public ThriftTestIf {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010074public:
Mark Sleed2655522006-09-05 22:09:57 +000075 TestHandler() {}
Mark Sleee8540632006-05-30 09:24:40 +000076
Konrad Grochowski16a23a62014-11-13 15:33:38 +010077 void testVoid() { printf("testVoid()\n"); }
Mark Sleee8540632006-05-30 09:24:40 +000078
Konrad Grochowski16a23a62014-11-13 15:33:38 +010079 void testString(string& out, const string& thing) {
Mark Sleee8540632006-05-30 09:24:40 +000080 printf("testString(\"%s\")\n", thing.c_str());
Mark Slee1921d202007-01-24 19:43:06 +000081 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +000082 }
83
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +090084 bool testBool(const bool thing) {
85 printf("testBool(%s)\n", thing ? "true" : "false");
86 return thing;
87 }
88
Mark Slee1921d202007-01-24 19:43:06 +000089 int8_t testByte(const int8_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +000090 printf("testByte(%d)\n", (int)thing);
91 return thing;
92 }
93
Mark Slee1921d202007-01-24 19:43:06 +000094 int32_t testI32(const int32_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +000095 printf("testI32(%d)\n", thing);
96 return thing;
97 }
98
Mark Slee1921d202007-01-24 19:43:06 +000099 int64_t testI64(const int64_t thing) {
Roger Meier0e814802014-01-17 21:07:58 +0100100 printf("testI64(%" PRId64 ")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +0000101 return thing;
102 }
103
Mark Slee1921d202007-01-24 19:43:06 +0000104 double testDouble(const double thing) {
Roger Meiera8cef6e2011-07-17 18:55:59 +0000105 printf("testDouble(%f)\n", thing);
Mark Sleec98d0502006-09-06 02:42:25 +0000106 return thing;
107 }
108
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100109 void testBinary(std::string& _return, const std::string& thing) {
110 std::ostringstream hexstr;
111 hexstr << std::hex << thing;
112 printf("testBinary(%s)\n", hexstr.str().c_str());
113 _return = thing;
114 }
115
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100116 void testStruct(Xtruct& out, const Xtruct& thing) {
117 printf("testStruct({\"%s\", %d, %d, %" PRId64 "})\n",
118 thing.string_thing.c_str(),
119 (int)thing.byte_thing,
120 thing.i32_thing,
121 thing.i64_thing);
Mark Slee1921d202007-01-24 19:43:06 +0000122 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000123 }
124
Mark Slee1921d202007-01-24 19:43:06 +0000125 void testNest(Xtruct2& out, const Xtruct2& nest) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100126 const Xtruct& thing = nest.struct_thing;
127 printf("testNest({%d, {\"%s\", %d, %d, %" PRId64 "}, %d})\n",
128 (int)nest.byte_thing,
129 thing.string_thing.c_str(),
130 (int)thing.byte_thing,
131 thing.i32_thing,
132 thing.i64_thing,
133 nest.i32_thing);
Mark Slee1921d202007-01-24 19:43:06 +0000134 out = nest;
Mark Sleee8540632006-05-30 09:24:40 +0000135 }
136
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100137 void testMap(map<int32_t, int32_t>& out, const map<int32_t, int32_t>& thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000138 printf("testMap({");
139 map<int32_t, int32_t>::const_iterator m_iter;
140 bool first = true;
141 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
142 if (first) {
143 first = false;
144 } else {
145 printf(", ");
146 }
147 printf("%d => %d", m_iter->first, m_iter->second);
148 }
149 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000150 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000151 }
152
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100153 void testStringMap(map<std::string, std::string>& out,
154 const map<std::string, std::string>& thing) {
Roger Meierd3b9dca2011-06-24 14:01:10 +0000155 printf("testMap({");
156 map<std::string, std::string>::const_iterator m_iter;
157 bool first = true;
158 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
159 if (first) {
160 first = false;
161 } else {
162 printf(", ");
163 }
164 printf("%s => %s", (m_iter->first).c_str(), (m_iter->second).c_str());
165 }
166 printf("})\n");
167 out = thing;
168 }
169
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100170 void testSet(set<int32_t>& out, const set<int32_t>& thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000171 printf("testSet({");
172 set<int32_t>::const_iterator s_iter;
173 bool first = true;
174 for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) {
175 if (first) {
176 first = false;
177 } else {
178 printf(", ");
179 }
180 printf("%d", *s_iter);
181 }
182 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000183 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000184 }
185
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100186 void testList(vector<int32_t>& out, const vector<int32_t>& thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000187 printf("testList({");
Mark Sleeb9acf982006-10-10 01:57:32 +0000188 vector<int32_t>::const_iterator l_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000189 bool first = true;
190 for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) {
191 if (first) {
192 first = false;
193 } else {
194 printf(", ");
195 }
196 printf("%d", *l_iter);
197 }
198 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000199 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000200 }
201
Bryan Duxbury833ae492010-09-27 17:26:02 +0000202 Numberz::type testEnum(const Numberz::type thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000203 printf("testEnum(%d)\n", thing);
204 return thing;
205 }
206
Mark Slee1921d202007-01-24 19:43:06 +0000207 UserId testTypedef(const UserId thing) {
Roger Meier0e814802014-01-17 21:07:58 +0100208 printf("testTypedef(%" PRId64 ")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +0000209 return thing;
210 }
211
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100212 void testMapMap(map<int32_t, map<int32_t, int32_t> >& mapmap, const int32_t hello) {
Mark Sleee8540632006-05-30 09:24:40 +0000213 printf("testMapMap(%d)\n", hello);
Mark Sleee8540632006-05-30 09:24:40 +0000214
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100215 map<int32_t, int32_t> pos;
216 map<int32_t, int32_t> neg;
Mark Sleee8540632006-05-30 09:24:40 +0000217 for (int i = 1; i < 5; i++) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100218 pos.insert(make_pair(i, i));
219 neg.insert(make_pair(-i, -i));
Mark Sleee8540632006-05-30 09:24:40 +0000220 }
221
222 mapmap.insert(make_pair(4, pos));
223 mapmap.insert(make_pair(-4, neg));
Mark Sleee8540632006-05-30 09:24:40 +0000224 }
225
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100226 void testInsanity(map<UserId, map<Numberz::type, Insanity> >& insane, const Insanity& argument) {
Mark Sleee8540632006-05-30 09:24:40 +0000227 printf("testInsanity()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000228
Mark Sleee8540632006-05-30 09:24:40 +0000229 Insanity looney;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000230 map<Numberz::type, Insanity> first_map;
231 map<Numberz::type, Insanity> second_map;
Mark Sleee8540632006-05-30 09:24:40 +0000232
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900233 first_map.insert(make_pair(Numberz::TWO, argument));
234 first_map.insert(make_pair(Numberz::THREE, argument));
Mark Sleee8540632006-05-30 09:24:40 +0000235
Bryan Duxbury833ae492010-09-27 17:26:02 +0000236 second_map.insert(make_pair(Numberz::SIX, looney));
Mark Sleee8540632006-05-30 09:24:40 +0000237
Mark Sleee8540632006-05-30 09:24:40 +0000238 insane.insert(make_pair(1, first_map));
239 insane.insert(make_pair(2, second_map));
240
241 printf("return");
242 printf(" = {");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100243 map<UserId, map<Numberz::type, Insanity> >::const_iterator i_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000244 for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) {
Roger Meier0e814802014-01-17 21:07:58 +0100245 printf("%" PRId64 " => {", i_iter->first);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100246 map<Numberz::type, Insanity>::const_iterator i2_iter;
247 for (i2_iter = i_iter->second.begin(); i2_iter != i_iter->second.end(); ++i2_iter) {
Mark Sleee8540632006-05-30 09:24:40 +0000248 printf("%d => {", i2_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000249 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
250 map<Numberz::type, UserId>::const_iterator um;
Mark Sleee8540632006-05-30 09:24:40 +0000251 printf("{");
252 for (um = userMap.begin(); um != userMap.end(); ++um) {
Roger Meier0e814802014-01-17 21:07:58 +0100253 printf("%d => %" PRId64 ", ", um->first, um->second);
Mark Sleee8540632006-05-30 09:24:40 +0000254 }
255 printf("}, ");
256
Mark Sleeb9acf982006-10-10 01:57:32 +0000257 vector<Xtruct> xtructs = i2_iter->second.xtructs;
258 vector<Xtruct>::const_iterator x;
Mark Sleee8540632006-05-30 09:24:40 +0000259 printf("{");
260 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100261 printf("{\"%s\", %d, %d, %" PRId64 "}, ",
262 x->string_thing.c_str(),
263 (int)x->byte_thing,
264 x->i32_thing,
265 x->i64_thing);
Mark Sleee8540632006-05-30 09:24:40 +0000266 }
267 printf("}");
268
269 printf("}, ");
270 }
271 printf("}, ");
272 }
273 printf("}\n");
Mark Sleee8540632006-05-30 09:24:40 +0000274 }
Marc Slemkoe6889de2006-08-12 00:32:53 +0000275
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100276 void testMulti(Xtruct& hello,
277 const int8_t arg0,
278 const int32_t arg1,
279 const int64_t arg2,
280 const std::map<int16_t, std::string>& arg3,
281 const Numberz::type arg4,
282 const UserId arg5) {
283 (void)arg3;
284 (void)arg4;
285 (void)arg5;
Jake Farrell5d02b802014-01-07 21:42:01 -0500286
Marc Slemkoe6889de2006-08-12 00:32:53 +0000287 printf("testMulti()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000288
Marc Slemkoe6889de2006-08-12 00:32:53 +0000289 hello.string_thing = "Hello2";
290 hello.byte_thing = arg0;
291 hello.i32_thing = arg1;
292 hello.i64_thing = (int64_t)arg2;
Marc Slemkoe6889de2006-08-12 00:32:53 +0000293 }
Marc Slemkobf4fd192006-08-15 21:29:39 +0000294
ben-craigfae08e72015-07-15 11:34:47 -0500295 void testException(const std::string& arg) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000296 printf("testException(%s)\n", arg.c_str());
Mark Sleed3d733a2006-09-01 22:19:06 +0000297 if (arg.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000298 Xception e;
299 e.errorCode = 1001;
David Reiss9813cbe2009-01-31 21:39:11 +0000300 e.message = arg;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000301 throw e;
Roger Meier1f8b48f2012-05-02 22:56:47 +0000302 } else if (arg.compare("TException") == 0) {
T Jake Lucianib5e62212009-01-31 22:36:20 +0000303 apache::thrift::TException e;
David Reiss55ff70f2008-06-11 00:58:25 +0000304 throw e;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000305 } else {
306 Xtruct result;
307 result.string_thing = arg;
308 return;
309 }
310 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000311
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100312 void testMultiException(Xtruct& result,
313 const std::string& arg0,
ben-craigfae08e72015-07-15 11:34:47 -0500314 const std::string& arg1) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000315
Marc Slemko71d4e472006-08-15 22:34:04 +0000316 printf("testMultiException(%s, %s)\n", arg0.c_str(), arg1.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000317
Mark Sleef5f2be42006-09-05 21:05:31 +0000318 if (arg0.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000319 Xception e;
320 e.errorCode = 1001;
321 e.message = "This is an Xception";
322 throw e;
Mark Sleef5f2be42006-09-05 21:05:31 +0000323 } else if (arg0.compare("Xception2") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000324 Xception2 e;
325 e.errorCode = 2002;
326 e.struct_thing.string_thing = "This is an Xception2";
327 throw e;
328 } else {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000329 result.string_thing = arg1;
Mark Slee1921d202007-01-24 19:43:06 +0000330 return;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000331 }
332 }
David Reiss2ab6fe82008-02-18 02:11:44 +0000333
Jake Farrell5d02b802014-01-07 21:42:01 -0500334 void testOneway(const int32_t sleepFor) {
David Reiss6ce401d2009-03-24 20:01:58 +0000335 printf("testOneway(%d): Sleeping...\n", sleepFor);
Jake Farrell5d02b802014-01-07 21:42:01 -0500336 THRIFT_SLEEP_SEC(sleepFor);
David Reiss6ce401d2009-03-24 20:01:58 +0000337 printf("testOneway(%d): done sleeping!\n", sleepFor);
David Reiss2ab6fe82008-02-18 02:11:44 +0000338 }
Mark Sleee8540632006-05-30 09:24:40 +0000339};
340
David Reissd7192062010-10-06 17:09:33 +0000341class TestProcessorEventHandler : public TProcessorEventHandler {
David Reiss23248712010-10-06 17:10:08 +0000342 virtual void* getContext(const char* fn_name, void* serverContext) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100343 (void)serverContext;
David Reissd7192062010-10-06 17:09:33 +0000344 return new std::string(fn_name);
345 }
346 virtual void freeContext(void* ctx, const char* fn_name) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100347 (void)fn_name;
David Reissd7192062010-10-06 17:09:33 +0000348 delete static_cast<std::string*>(ctx);
349 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100350 virtual void preRead(void* ctx, const char* fn_name) { communicate("preRead", ctx, fn_name); }
David Reissef7200f2010-10-06 17:09:42 +0000351 virtual void postRead(void* ctx, const char* fn_name, uint32_t bytes) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100352 (void)bytes;
David Reissd7192062010-10-06 17:09:33 +0000353 communicate("postRead", ctx, fn_name);
354 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100355 virtual void preWrite(void* ctx, const char* fn_name) { communicate("preWrite", ctx, fn_name); }
David Reissef7200f2010-10-06 17:09:42 +0000356 virtual void postWrite(void* ctx, const char* fn_name, uint32_t bytes) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100357 (void)bytes;
David Reissd7192062010-10-06 17:09:33 +0000358 communicate("postWrite", ctx, fn_name);
359 }
360 virtual void asyncComplete(void* ctx, const char* fn_name) {
361 communicate("asyncComplete", ctx, fn_name);
362 }
363 virtual void handlerError(void* ctx, const char* fn_name) {
364 communicate("handlerError", ctx, fn_name);
365 }
366
367 void communicate(const char* event, void* ctx, const char* fn_name) {
368 std::cout << event << ": " << *static_cast<std::string*>(ctx) << " = " << fn_name << std::endl;
369 }
370};
371
Roger Meier7e056e72011-07-17 07:28:28 +0000372class TestHandlerAsync : public ThriftTestCobSvIf {
373public:
Roger Meier611f90c2011-12-11 22:08:51 +0000374 TestHandlerAsync(boost::shared_ptr<TestHandler>& handler) : _delegate(handler) {}
Roger Meier7e056e72011-07-17 07:28:28 +0000375 virtual ~TestHandlerAsync() {}
376
Roger Meier0a7c69c2014-05-02 21:15:45 +0200377 virtual void testVoid(tcxx::function<void()> cob) {
Roger Meier7e056e72011-07-17 07:28:28 +0000378 _delegate->testVoid();
379 cob();
380 }
381
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100382 virtual void testString(tcxx::function<void(std::string const& _return)> cob,
383 const std::string& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000384 std::string res;
385 _delegate->testString(res, thing);
386 cob(res);
387 }
388
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900389 virtual void testBool(tcxx::function<void(bool const& _return)> cob, const bool thing) {
390 bool res = _delegate->testBool(thing);
391 cob(res);
392 }
393
Roger Meier0a7c69c2014-05-02 21:15:45 +0200394 virtual void testByte(tcxx::function<void(int8_t const& _return)> cob, const int8_t thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000395 int8_t res = _delegate->testByte(thing);
396 cob(res);
397 }
398
Roger Meier0a7c69c2014-05-02 21:15:45 +0200399 virtual void testI32(tcxx::function<void(int32_t const& _return)> cob, const int32_t thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000400 int32_t res = _delegate->testI32(thing);
401 cob(res);
402 }
403
Roger Meier0a7c69c2014-05-02 21:15:45 +0200404 virtual void testI64(tcxx::function<void(int64_t const& _return)> cob, const int64_t thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000405 int64_t res = _delegate->testI64(thing);
406 cob(res);
407 }
408
Roger Meier0a7c69c2014-05-02 21:15:45 +0200409 virtual void testDouble(tcxx::function<void(double const& _return)> cob, const double thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000410 double res = _delegate->testDouble(thing);
411 cob(res);
412 }
413
Konrad Grochowski1f6e3802015-05-18 18:10:06 +0200414 virtual void testBinary(tcxx::function<void(std::string const& _return)> cob,
415 const std::string& thing) {
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100416 std::string res;
417 _delegate->testBinary(res, thing);
418 cob(res);
419 }
420
Roger Meier0a7c69c2014-05-02 21:15:45 +0200421 virtual void testStruct(tcxx::function<void(Xtruct const& _return)> cob, const Xtruct& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000422 Xtruct res;
423 _delegate->testStruct(res, thing);
424 cob(res);
425 }
426
Roger Meier0a7c69c2014-05-02 21:15:45 +0200427 virtual void testNest(tcxx::function<void(Xtruct2 const& _return)> cob, const Xtruct2& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000428 Xtruct2 res;
429 _delegate->testNest(res, thing);
430 cob(res);
431 }
432
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100433 virtual void testMap(tcxx::function<void(std::map<int32_t, int32_t> const& _return)> cob,
434 const std::map<int32_t, int32_t>& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000435 std::map<int32_t, int32_t> res;
436 _delegate->testMap(res, thing);
437 cob(res);
438 }
439
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100440 virtual void testStringMap(
441 tcxx::function<void(std::map<std::string, std::string> const& _return)> cob,
442 const std::map<std::string, std::string>& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000443 std::map<std::string, std::string> res;
444 _delegate->testStringMap(res, thing);
445 cob(res);
446 }
447
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100448 virtual void testSet(tcxx::function<void(std::set<int32_t> const& _return)> cob,
449 const std::set<int32_t>& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000450 std::set<int32_t> res;
451 _delegate->testSet(res, thing);
452 cob(res);
453 }
454
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100455 virtual void testList(tcxx::function<void(std::vector<int32_t> const& _return)> cob,
456 const std::vector<int32_t>& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000457 std::vector<int32_t> res;
458 _delegate->testList(res, thing);
459 cob(res);
460 }
461
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100462 virtual void testEnum(tcxx::function<void(Numberz::type const& _return)> cob,
463 const Numberz::type thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000464 Numberz::type res = _delegate->testEnum(thing);
465 cob(res);
466 }
467
Roger Meier0a7c69c2014-05-02 21:15:45 +0200468 virtual void testTypedef(tcxx::function<void(UserId const& _return)> cob, const UserId thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000469 UserId res = _delegate->testTypedef(thing);
470 cob(res);
471 }
472
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100473 virtual void testMapMap(
474 tcxx::function<void(std::map<int32_t, std::map<int32_t, int32_t> > const& _return)> cob,
475 const int32_t hello) {
Roger Meier7e056e72011-07-17 07:28:28 +0000476 std::map<int32_t, std::map<int32_t, int32_t> > res;
477 _delegate->testMapMap(res, hello);
478 cob(res);
479 }
480
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100481 virtual void testInsanity(
482 tcxx::function<void(std::map<UserId, std::map<Numberz::type, Insanity> > const& _return)> cob,
483 const Insanity& argument) {
Jake Farrell5d02b802014-01-07 21:42:01 -0500484 std::map<UserId, std::map<Numberz::type, Insanity> > res;
Roger Meier7e056e72011-07-17 07:28:28 +0000485 _delegate->testInsanity(res, argument);
486 cob(res);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100487 }
Roger Meier7e056e72011-07-17 07:28:28 +0000488
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100489 virtual void testMulti(tcxx::function<void(Xtruct const& _return)> cob,
490 const int8_t arg0,
491 const int32_t arg1,
492 const int64_t arg2,
493 const std::map<int16_t, std::string>& arg3,
494 const Numberz::type arg4,
495 const UserId arg5) {
Roger Meier7e056e72011-07-17 07:28:28 +0000496 Xtruct res;
497 _delegate->testMulti(res, arg0, arg1, arg2, arg3, arg4, arg5);
498 cob(res);
499 }
500
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100501 virtual void testException(
502 tcxx::function<void()> cob,
503 tcxx::function<void(::apache::thrift::TDelayedException* _throw)> exn_cob,
504 const std::string& arg) {
Roger Meier7e056e72011-07-17 07:28:28 +0000505 try {
506 _delegate->testException(arg);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100507 } catch (const apache::thrift::TException& e) {
Roger Meier7e056e72011-07-17 07:28:28 +0000508 exn_cob(apache::thrift::TDelayedException::delayException(e));
509 return;
510 }
511 cob();
512 }
513
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100514 virtual void testMultiException(
515 tcxx::function<void(Xtruct const& _return)> cob,
516 tcxx::function<void(::apache::thrift::TDelayedException* _throw)> exn_cob,
517 const std::string& arg0,
518 const std::string& arg1) {
Roger Meier7e056e72011-07-17 07:28:28 +0000519 Xtruct res;
520 try {
521 _delegate->testMultiException(res, arg0, arg1);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100522 } catch (const apache::thrift::TException& e) {
Roger Meier7e056e72011-07-17 07:28:28 +0000523 exn_cob(apache::thrift::TDelayedException::delayException(e));
524 return;
525 }
526 cob(res);
527 }
528
Roger Meier0a7c69c2014-05-02 21:15:45 +0200529 virtual void testOneway(tcxx::function<void()> cob, const int32_t secondsToSleep) {
Roger Meier7e056e72011-07-17 07:28:28 +0000530 _delegate->testOneway(secondsToSleep);
531 cob();
532 }
533
534protected:
Roger Meier611f90c2011-12-11 22:08:51 +0000535 boost::shared_ptr<TestHandler> _delegate;
Roger Meier7e056e72011-07-17 07:28:28 +0000536};
537
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900538namespace po = boost::program_options;
539
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100540int main(int argc, char** argv) {
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +0530541
Nobuaki Sukegawac8744082016-03-09 19:55:56 +0900542 string testDir = boost::filesystem::system_complete(argv[0]).parent_path().parent_path().parent_path().string();
543 string certPath = testDir + "/keys/server.crt";
544 string keyPath = testDir + "/keys/server.key";
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +0530545
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100546#if _WIN32
Jake Farrell5d02b802014-01-07 21:42:01 -0500547 transport::TWinsockSingleton::create();
548#endif
Mark Sleee8540632006-05-30 09:24:40 +0000549 int port = 9090;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000550 bool ssl = false;
Roger Meierca142b02011-06-07 17:59:07 +0000551 string transport_type = "buffered";
552 string protocol_type = "binary";
553 string server_type = "simple";
554 string domain_socket = "";
pavlodd08f6e2015-10-08 16:43:56 -0400555 bool abstract_namespace = false;
Roger Meierca142b02011-06-07 17:59:07 +0000556 size_t workers = 4;
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900557 int string_limit = 0;
558 int container_limit = 0;
Marc Slemko6be374b2006-08-04 03:16:25 +0000559
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900560 po::options_description desc("Allowed options");
561 desc.add_options()
562 ("help,h", "produce help message")
563 ("port", po::value<int>(&port)->default_value(port), "Port number to listen")
564 ("domain-socket", po::value<string>(&domain_socket) ->default_value(domain_socket), "Unix Domain Socket (e.g. /tmp/ThriftTest.thrift)")
565 ("abstract-namespace", "Create the domain socket in the Abstract Namespace (no connection with filesystem pathnames)")
566 ("server-type", po::value<string>(&server_type)->default_value(server_type), "type of server, \"simple\", \"thread-pool\", \"threaded\", or \"nonblocking\"")
567 ("transport", po::value<string>(&transport_type)->default_value(transport_type), "transport: buffered, framed, http")
568 ("protocol", po::value<string>(&protocol_type)->default_value(protocol_type), "protocol: binary, compact, header, json")
569 ("ssl", "Encrypted Transport using SSL")
570 ("processor-events", "processor-events")
571 ("workers,n", po::value<size_t>(&workers)->default_value(workers), "Number of thread pools workers. Only valid for thread-pool server type")
572 ("string-limit", po::value<int>(&string_limit))
573 ("container-limit", po::value<int>(&container_limit));
Marc Slemko6be374b2006-08-04 03:16:25 +0000574
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900575 po::variables_map vm;
576 po::store(po::parse_command_line(argc, argv, desc), vm);
577 po::notify(vm);
Marc Slemko6be374b2006-08-04 03:16:25 +0000578
Roger Meierca142b02011-06-07 17:59:07 +0000579 if (vm.count("help")) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100580 cout << desc << "\n";
581 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000582 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500583
Marc Slemko6be374b2006-08-04 03:16:25 +0000584 try {
Roger Meierca142b02011-06-07 17:59:07 +0000585 if (!server_type.empty()) {
586 if (server_type == "simple") {
587 } else if (server_type == "thread-pool") {
588 } else if (server_type == "threaded") {
589 } else if (server_type == "nonblocking") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000590 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100591 throw invalid_argument("Unknown server type " + server_type);
Roger Meierca142b02011-06-07 17:59:07 +0000592 }
593 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500594
Roger Meierca142b02011-06-07 17:59:07 +0000595 if (!protocol_type.empty()) {
596 if (protocol_type == "binary") {
Roger Meier284101c2014-03-11 21:20:35 +0100597 } else if (protocol_type == "compact") {
Roger Meierca142b02011-06-07 17:59:07 +0000598 } else if (protocol_type == "json") {
Dave Watson792db4e2015-01-16 11:22:01 -0800599 } else if (protocol_type == "header") {
Roger Meierca142b02011-06-07 17:59:07 +0000600 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100601 throw invalid_argument("Unknown protocol type " + protocol_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000602 }
603 }
604
Roger Meier284101c2014-03-11 21:20:35 +0100605 if (!transport_type.empty()) {
Roger Meierca142b02011-06-07 17:59:07 +0000606 if (transport_type == "buffered") {
607 } else if (transport_type == "framed") {
608 } else if (transport_type == "http") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000609 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100610 throw invalid_argument("Unknown transport type " + transport_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000611 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000612 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000613
Bryan Duxbury833ae492010-09-27 17:26:02 +0000614 } catch (std::exception& e) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000615 cerr << e.what() << endl;
Roger Meierca142b02011-06-07 17:59:07 +0000616 cout << desc << "\n";
617 return 1;
Marc Slemko6be374b2006-08-04 03:16:25 +0000618 }
619
Roger Meierca142b02011-06-07 17:59:07 +0000620 if (vm.count("ssl")) {
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000621 ssl = true;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000622 }
623
pavlodd08f6e2015-10-08 16:43:56 -0400624 if (vm.count("abstract-namespace")) {
625 abstract_namespace = true;
626 }
627
Mark Sleee8540632006-05-30 09:24:40 +0000628 // Dispatcher
Roger Meier611f90c2011-12-11 22:08:51 +0000629 boost::shared_ptr<TProtocolFactory> protocolFactory;
Roger Meierca142b02011-06-07 17:59:07 +0000630 if (protocol_type == "json") {
Roger Meier611f90c2011-12-11 22:08:51 +0000631 boost::shared_ptr<TProtocolFactory> jsonProtocolFactory(new TJSONProtocolFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000632 protocolFactory = jsonProtocolFactory;
Roger Meier023192f2014-02-12 09:35:12 +0100633 } else if (protocol_type == "compact") {
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900634 TCompactProtocolFactoryT<TBufferBase> *compactProtocolFactory = new TCompactProtocolFactoryT<TBufferBase>();
635 compactProtocolFactory->setContainerSizeLimit(container_limit);
636 compactProtocolFactory->setStringSizeLimit(string_limit);
637 protocolFactory.reset(compactProtocolFactory);
Dave Watson792db4e2015-01-16 11:22:01 -0800638 } else if (protocol_type == "header") {
639 boost::shared_ptr<TProtocolFactory> headerProtocolFactory(new THeaderProtocolFactory());
640 protocolFactory = headerProtocolFactory;
Roger Meierca142b02011-06-07 17:59:07 +0000641 } else {
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900642 TBinaryProtocolFactoryT<TBufferBase>* binaryProtocolFactory = new TBinaryProtocolFactoryT<TBufferBase>();
643 binaryProtocolFactory->setContainerSizeLimit(container_limit);
644 binaryProtocolFactory->setStringSizeLimit(string_limit);
645 protocolFactory.reset(binaryProtocolFactory);
Roger Meierca142b02011-06-07 17:59:07 +0000646 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000647
Roger Meierca142b02011-06-07 17:59:07 +0000648 // Processor
Roger Meier611f90c2011-12-11 22:08:51 +0000649 boost::shared_ptr<TestHandler> testHandler(new TestHandler());
650 boost::shared_ptr<ThriftTestProcessor> testProcessor(new ThriftTestProcessor(testHandler));
Jake Farrell5d02b802014-01-07 21:42:01 -0500651
Roger Meierca142b02011-06-07 17:59:07 +0000652 if (vm.count("processor-events")) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100653 testProcessor->setEventHandler(
654 boost::shared_ptr<TProcessorEventHandler>(new TestProcessorEventHandler()));
David Reissd7192062010-10-06 17:09:33 +0000655 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500656
Mark Sleee8540632006-05-30 09:24:40 +0000657 // Transport
Roger Meier611f90c2011-12-11 22:08:51 +0000658 boost::shared_ptr<TSSLSocketFactory> sslSocketFactory;
659 boost::shared_ptr<TServerSocket> serverSocket;
Mark Sleee8540632006-05-30 09:24:40 +0000660
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000661 if (ssl) {
Roger Meier611f90c2011-12-11 22:08:51 +0000662 sslSocketFactory = boost::shared_ptr<TSSLSocketFactory>(new TSSLSocketFactory());
Nobuaki Sukegawac8744082016-03-09 19:55:56 +0900663 sslSocketFactory->loadCertificate(certPath.c_str());
664 sslSocketFactory->loadPrivateKey(keyPath.c_str());
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000665 sslSocketFactory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
Roger Meier611f90c2011-12-11 22:08:51 +0000666 serverSocket = boost::shared_ptr<TServerSocket>(new TSSLServerSocket(port, sslSocketFactory));
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000667 } else {
Roger Meierc94b2932014-02-22 20:07:33 +0100668 if (domain_socket != "") {
pavlodd08f6e2015-10-08 16:43:56 -0400669 if (abstract_namespace) {
670 std::string abstract_socket("\0", 1);
671 abstract_socket += domain_socket;
672 serverSocket = boost::shared_ptr<TServerSocket>(new TServerSocket(abstract_socket));
673 } else {
674 unlink(domain_socket.c_str());
675 serverSocket = boost::shared_ptr<TServerSocket>(new TServerSocket(domain_socket));
676 }
Roger Meierc94b2932014-02-22 20:07:33 +0100677 port = 0;
678 } else {
Roger Meier611f90c2011-12-11 22:08:51 +0000679 serverSocket = boost::shared_ptr<TServerSocket>(new TServerSocket(port));
Roger Meierc94b2932014-02-22 20:07:33 +0100680 }
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000681 }
Roger Meierca142b02011-06-07 17:59:07 +0000682
Mark Sleed788b2e2006-09-07 01:26:35 +0000683 // Factory
Roger Meier611f90c2011-12-11 22:08:51 +0000684 boost::shared_ptr<TTransportFactory> transportFactory;
Jake Farrell5d02b802014-01-07 21:42:01 -0500685
Roger Meier7e056e72011-07-17 07:28:28 +0000686 if (transport_type == "http" && server_type != "nonblocking") {
Jake Farrell5d02b802014-01-07 21:42:01 -0500687 boost::shared_ptr<TTransportFactory> httpTransportFactory(new THttpServerTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000688 transportFactory = httpTransportFactory;
689 } else if (transport_type == "framed") {
Jake Farrell5d02b802014-01-07 21:42:01 -0500690 boost::shared_ptr<TTransportFactory> framedTransportFactory(new TFramedTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000691 transportFactory = framedTransportFactory;
692 } else {
Jake Farrell5d02b802014-01-07 21:42:01 -0500693 boost::shared_ptr<TTransportFactory> bufferedTransportFactory(new TBufferedTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000694 transportFactory = bufferedTransportFactory;
695 }
Mark Sleed788b2e2006-09-07 01:26:35 +0000696
Roger Meierca142b02011-06-07 17:59:07 +0000697 // Server Info
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100698 cout << "Starting \"" << server_type << "\" server (" << transport_type << "/" << protocol_type
pavlodd08f6e2015-10-08 16:43:56 -0400699 << ") listen on: ";
700 if (abstract_namespace) {
701 cout << '@';
702 }
703 cout << domain_socket;
Roger Meierca142b02011-06-07 17:59:07 +0000704 if (port != 0) {
705 cout << port;
706 }
707 cout << endl;
Mark Sleee8540632006-05-30 09:24:40 +0000708
Roger Meierca142b02011-06-07 17:59:07 +0000709 // Server
Jake Farrell5d02b802014-01-07 21:42:01 -0500710 boost::shared_ptr<apache::thrift::server::TServer> server;
711
Roger Meierca142b02011-06-07 17:59:07 +0000712 if (server_type == "simple") {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100713 server.reset(new TSimpleServer(testProcessor, serverSocket, transportFactory, protocolFactory));
Roger Meierca142b02011-06-07 17:59:07 +0000714 } else if (server_type == "thread-pool") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000715
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100716 boost::shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workers);
Marc Slemko6be374b2006-08-04 03:16:25 +0000717
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100718 boost::shared_ptr<PlatformThreadFactory> threadFactory
719 = boost::shared_ptr<PlatformThreadFactory>(new PlatformThreadFactory());
Marc Slemko6be374b2006-08-04 03:16:25 +0000720
721 threadManager->threadFactory(threadFactory);
722
723 threadManager->start();
724
Jake Farrell5d02b802014-01-07 21:42:01 -0500725 server.reset(new TThreadPoolServer(testProcessor,
Roger Meierca142b02011-06-07 17:59:07 +0000726 serverSocket,
Mark Sleed788b2e2006-09-07 01:26:35 +0000727 transportFactory,
Mark Sleea3302652006-10-25 19:03:32 +0000728 protocolFactory,
Jake Farrell5d02b802014-01-07 21:42:01 -0500729 threadManager));
Roger Meierca142b02011-06-07 17:59:07 +0000730 } else if (server_type == "threaded") {
David Reiss0c90f6f2008-02-06 22:18:40 +0000731
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100732 server.reset(
733 new TThreadedServer(testProcessor, serverSocket, transportFactory, protocolFactory));
Roger Meierca142b02011-06-07 17:59:07 +0000734 } else if (server_type == "nonblocking") {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100735 if (transport_type == "http") {
Roger Meier611f90c2011-12-11 22:08:51 +0000736 boost::shared_ptr<TestHandlerAsync> testHandlerAsync(new TestHandlerAsync(testHandler));
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100737 boost::shared_ptr<TAsyncProcessor> testProcessorAsync(
738 new ThriftTestAsyncProcessor(testHandlerAsync));
739 boost::shared_ptr<TAsyncBufferProcessor> testBufferProcessor(
740 new TAsyncProtocolProcessor(testProcessorAsync, protocolFactory));
Jake Farrell5d02b802014-01-07 21:42:01 -0500741
742 // not loading nonblockingServer into "server" because
743 // TEvhttpServer doesn't inherit from TServer, and doesn't
744 // provide a stop method.
Roger Meier7e056e72011-07-17 07:28:28 +0000745 TEvhttpServer nonblockingServer(testBufferProcessor, port);
746 nonblockingServer.serve();
Jake Farrell5d02b802014-01-07 21:42:01 -0500747 } else {
Divya Thaluru808d1432017-08-06 16:36:36 -0700748 boost::shared_ptr<transport::TNonblockingServerSocket> nbSocket;
749 nbSocket.reset(new transport::TNonblockingServerSocket(port));
750 server.reset(new TNonblockingServer(testProcessor, protocolFactory, nbSocket));
Roger Meier7e056e72011-07-17 07:28:28 +0000751 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000752 }
753
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100754 if (server.get() != NULL) {
Dave Watson792db4e2015-01-16 11:22:01 -0800755 if (protocol_type == "header") {
James E. King, IIIdf899132016-11-12 15:16:30 -0500756 // Tell the server to use the same protocol for input / output
Dave Watson792db4e2015-01-16 11:22:01 -0800757 // if using header
758 server->setOutputProtocolFactory(boost::shared_ptr<TProtocolFactory>());
759 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500760 apache::thrift::concurrency::PlatformThreadFactory factory;
761 factory.setDetached(false);
762 boost::shared_ptr<apache::thrift::concurrency::Runnable> serverThreadRunner(server);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100763 boost::shared_ptr<apache::thrift::concurrency::Thread> thread
764 = factory.newThread(serverThreadRunner);
Jake Farrell5d02b802014-01-07 21:42:01 -0500765 thread->start();
766
Roger Meier284101c2014-03-11 21:20:35 +0100767 // HACK: cross language test suite is unable to handle cin properly
768 // that's why we stay in a endless loop here
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100769 while (1) {
770 }
Roger Meier284101c2014-03-11 21:20:35 +0100771 // FIXME: find another way to stop the server (e.g. a signal)
772 // cout<<"Press enter to stop the server."<<endl;
773 // cin.ignore(); //wait until a key is pressed
Jake Farrell5d02b802014-01-07 21:42:01 -0500774
775 server->stop();
776 thread->join();
777 server.reset();
778 }
779
Roger Meierca142b02011-06-07 17:59:07 +0000780 cout << "done." << endl;
Mark Sleee8540632006-05-30 09:24:40 +0000781 return 0;
782}