blob: b86b34c17fce36c221b04c865697d72b0d9f6cb0 [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>
36#include <thrift/transport/THttpServer.h>
37#include <thrift/transport/THttpTransport.h>
38#include <thrift/transport/TTransportUtils.h>
Mark Slee95771002006-06-07 06:53:25 +000039#include "ThriftTest.h"
Marc Slemko6be374b2006-08-04 03:16:25 +000040
James E. King, III7edc8fa2017-01-20 10:11:41 -050041#ifdef HAVE_STDINT_H
42#include <stdint.h>
43#endif
44#ifdef HAVE_INTTYPES_H
45#include <inttypes.h>
46#endif
47
Marc Slemko6be374b2006-08-04 03:16:25 +000048#include <iostream>
49#include <stdexcept>
50#include <sstream>
51
Roger Meierca142b02011-06-07 17:59:07 +000052#include <boost/program_options.hpp>
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +053053#include <boost/filesystem.hpp>
Roger Meier0a7c69c2014-05-02 21:15:45 +020054#include <thrift/cxxfunctional.h>
Roger Meierca142b02011-06-07 17:59:07 +000055
Bryan Duxburycd9aea12011-02-22 18:12:06 +000056#include <signal.h>
Jake Farrell5d02b802014-01-07 21:42:01 -050057#if _WIN32
Konrad Grochowski16a23a62014-11-13 15:33:38 +010058#include <thrift/windows/TWinsockSingleton.h>
Jake Farrell5d02b802014-01-07 21:42:01 -050059#endif
David Reissbc3dddb2007-08-22 23:20:24 +000060
Mark Sleee8540632006-05-30 09:24:40 +000061using namespace std;
62
T Jake Lucianib5e62212009-01-31 22:36:20 +000063using namespace apache::thrift;
64using namespace apache::thrift::concurrency;
65using namespace apache::thrift::protocol;
66using namespace apache::thrift::transport;
67using namespace apache::thrift::server;
Roger Meier7e056e72011-07-17 07:28:28 +000068using namespace apache::thrift::async;
Marc Slemko6be374b2006-08-04 03:16:25 +000069
Marc Slemkobf4fd192006-08-15 21:29:39 +000070using namespace thrift::test;
71
Mark Sleed2655522006-09-05 22:09:57 +000072class TestHandler : public ThriftTestIf {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010073public:
Mark Sleed2655522006-09-05 22:09:57 +000074 TestHandler() {}
Mark Sleee8540632006-05-30 09:24:40 +000075
Konrad Grochowski16a23a62014-11-13 15:33:38 +010076 void testVoid() { printf("testVoid()\n"); }
Mark Sleee8540632006-05-30 09:24:40 +000077
Konrad Grochowski16a23a62014-11-13 15:33:38 +010078 void testString(string& out, const string& thing) {
Mark Sleee8540632006-05-30 09:24:40 +000079 printf("testString(\"%s\")\n", thing.c_str());
Mark Slee1921d202007-01-24 19:43:06 +000080 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +000081 }
82
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +090083 bool testBool(const bool thing) {
84 printf("testBool(%s)\n", thing ? "true" : "false");
85 return thing;
86 }
87
Mark Slee1921d202007-01-24 19:43:06 +000088 int8_t testByte(const int8_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +000089 printf("testByte(%d)\n", (int)thing);
90 return thing;
91 }
92
Mark Slee1921d202007-01-24 19:43:06 +000093 int32_t testI32(const int32_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +000094 printf("testI32(%d)\n", thing);
95 return thing;
96 }
97
Mark Slee1921d202007-01-24 19:43:06 +000098 int64_t testI64(const int64_t thing) {
Roger Meier0e814802014-01-17 21:07:58 +010099 printf("testI64(%" PRId64 ")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +0000100 return thing;
101 }
102
Mark Slee1921d202007-01-24 19:43:06 +0000103 double testDouble(const double thing) {
Roger Meiera8cef6e2011-07-17 18:55:59 +0000104 printf("testDouble(%f)\n", thing);
Mark Sleec98d0502006-09-06 02:42:25 +0000105 return thing;
106 }
107
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100108 void testBinary(std::string& _return, const std::string& thing) {
109 std::ostringstream hexstr;
110 hexstr << std::hex << thing;
111 printf("testBinary(%s)\n", hexstr.str().c_str());
112 _return = thing;
113 }
114
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100115 void testStruct(Xtruct& out, const Xtruct& thing) {
116 printf("testStruct({\"%s\", %d, %d, %" PRId64 "})\n",
117 thing.string_thing.c_str(),
118 (int)thing.byte_thing,
119 thing.i32_thing,
120 thing.i64_thing);
Mark Slee1921d202007-01-24 19:43:06 +0000121 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000122 }
123
Mark Slee1921d202007-01-24 19:43:06 +0000124 void testNest(Xtruct2& out, const Xtruct2& nest) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100125 const Xtruct& thing = nest.struct_thing;
126 printf("testNest({%d, {\"%s\", %d, %d, %" PRId64 "}, %d})\n",
127 (int)nest.byte_thing,
128 thing.string_thing.c_str(),
129 (int)thing.byte_thing,
130 thing.i32_thing,
131 thing.i64_thing,
132 nest.i32_thing);
Mark Slee1921d202007-01-24 19:43:06 +0000133 out = nest;
Mark Sleee8540632006-05-30 09:24:40 +0000134 }
135
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100136 void testMap(map<int32_t, int32_t>& out, const map<int32_t, int32_t>& thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000137 printf("testMap({");
138 map<int32_t, int32_t>::const_iterator m_iter;
139 bool first = true;
140 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
141 if (first) {
142 first = false;
143 } else {
144 printf(", ");
145 }
146 printf("%d => %d", m_iter->first, m_iter->second);
147 }
148 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000149 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000150 }
151
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100152 void testStringMap(map<std::string, std::string>& out,
153 const map<std::string, std::string>& thing) {
Roger Meierd3b9dca2011-06-24 14:01:10 +0000154 printf("testMap({");
155 map<std::string, std::string>::const_iterator m_iter;
156 bool first = true;
157 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
158 if (first) {
159 first = false;
160 } else {
161 printf(", ");
162 }
163 printf("%s => %s", (m_iter->first).c_str(), (m_iter->second).c_str());
164 }
165 printf("})\n");
166 out = thing;
167 }
168
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100169 void testSet(set<int32_t>& out, const set<int32_t>& thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000170 printf("testSet({");
171 set<int32_t>::const_iterator s_iter;
172 bool first = true;
173 for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) {
174 if (first) {
175 first = false;
176 } else {
177 printf(", ");
178 }
179 printf("%d", *s_iter);
180 }
181 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000182 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000183 }
184
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100185 void testList(vector<int32_t>& out, const vector<int32_t>& thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000186 printf("testList({");
Mark Sleeb9acf982006-10-10 01:57:32 +0000187 vector<int32_t>::const_iterator l_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000188 bool first = true;
189 for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) {
190 if (first) {
191 first = false;
192 } else {
193 printf(", ");
194 }
195 printf("%d", *l_iter);
196 }
197 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000198 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000199 }
200
Bryan Duxbury833ae492010-09-27 17:26:02 +0000201 Numberz::type testEnum(const Numberz::type thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000202 printf("testEnum(%d)\n", thing);
203 return thing;
204 }
205
Mark Slee1921d202007-01-24 19:43:06 +0000206 UserId testTypedef(const UserId thing) {
Roger Meier0e814802014-01-17 21:07:58 +0100207 printf("testTypedef(%" PRId64 ")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +0000208 return thing;
209 }
210
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100211 void testMapMap(map<int32_t, map<int32_t, int32_t> >& mapmap, const int32_t hello) {
Mark Sleee8540632006-05-30 09:24:40 +0000212 printf("testMapMap(%d)\n", hello);
Mark Sleee8540632006-05-30 09:24:40 +0000213
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100214 map<int32_t, int32_t> pos;
215 map<int32_t, int32_t> neg;
Mark Sleee8540632006-05-30 09:24:40 +0000216 for (int i = 1; i < 5; i++) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100217 pos.insert(make_pair(i, i));
218 neg.insert(make_pair(-i, -i));
Mark Sleee8540632006-05-30 09:24:40 +0000219 }
220
221 mapmap.insert(make_pair(4, pos));
222 mapmap.insert(make_pair(-4, neg));
Mark Sleee8540632006-05-30 09:24:40 +0000223 }
224
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100225 void testInsanity(map<UserId, map<Numberz::type, Insanity> >& insane, const Insanity& argument) {
Mark Sleee8540632006-05-30 09:24:40 +0000226 printf("testInsanity()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000227
Mark Sleee8540632006-05-30 09:24:40 +0000228 Insanity looney;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000229 map<Numberz::type, Insanity> first_map;
230 map<Numberz::type, Insanity> second_map;
Mark Sleee8540632006-05-30 09:24:40 +0000231
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900232 first_map.insert(make_pair(Numberz::TWO, argument));
233 first_map.insert(make_pair(Numberz::THREE, argument));
Mark Sleee8540632006-05-30 09:24:40 +0000234
Bryan Duxbury833ae492010-09-27 17:26:02 +0000235 second_map.insert(make_pair(Numberz::SIX, looney));
Mark Sleee8540632006-05-30 09:24:40 +0000236
Mark Sleee8540632006-05-30 09:24:40 +0000237 insane.insert(make_pair(1, first_map));
238 insane.insert(make_pair(2, second_map));
239
240 printf("return");
241 printf(" = {");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100242 map<UserId, map<Numberz::type, Insanity> >::const_iterator i_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000243 for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) {
Roger Meier0e814802014-01-17 21:07:58 +0100244 printf("%" PRId64 " => {", i_iter->first);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100245 map<Numberz::type, Insanity>::const_iterator i2_iter;
246 for (i2_iter = i_iter->second.begin(); i2_iter != i_iter->second.end(); ++i2_iter) {
Mark Sleee8540632006-05-30 09:24:40 +0000247 printf("%d => {", i2_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000248 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
249 map<Numberz::type, UserId>::const_iterator um;
Mark Sleee8540632006-05-30 09:24:40 +0000250 printf("{");
251 for (um = userMap.begin(); um != userMap.end(); ++um) {
Roger Meier0e814802014-01-17 21:07:58 +0100252 printf("%d => %" PRId64 ", ", um->first, um->second);
Mark Sleee8540632006-05-30 09:24:40 +0000253 }
254 printf("}, ");
255
Mark Sleeb9acf982006-10-10 01:57:32 +0000256 vector<Xtruct> xtructs = i2_iter->second.xtructs;
257 vector<Xtruct>::const_iterator x;
Mark Sleee8540632006-05-30 09:24:40 +0000258 printf("{");
259 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100260 printf("{\"%s\", %d, %d, %" PRId64 "}, ",
261 x->string_thing.c_str(),
262 (int)x->byte_thing,
263 x->i32_thing,
264 x->i64_thing);
Mark Sleee8540632006-05-30 09:24:40 +0000265 }
266 printf("}");
267
268 printf("}, ");
269 }
270 printf("}, ");
271 }
272 printf("}\n");
Mark Sleee8540632006-05-30 09:24:40 +0000273 }
Marc Slemkoe6889de2006-08-12 00:32:53 +0000274
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100275 void testMulti(Xtruct& hello,
276 const int8_t arg0,
277 const int32_t arg1,
278 const int64_t arg2,
279 const std::map<int16_t, std::string>& arg3,
280 const Numberz::type arg4,
281 const UserId arg5) {
282 (void)arg3;
283 (void)arg4;
284 (void)arg5;
Jake Farrell5d02b802014-01-07 21:42:01 -0500285
Marc Slemkoe6889de2006-08-12 00:32:53 +0000286 printf("testMulti()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000287
Marc Slemkoe6889de2006-08-12 00:32:53 +0000288 hello.string_thing = "Hello2";
289 hello.byte_thing = arg0;
290 hello.i32_thing = arg1;
291 hello.i64_thing = (int64_t)arg2;
Marc Slemkoe6889de2006-08-12 00:32:53 +0000292 }
Marc Slemkobf4fd192006-08-15 21:29:39 +0000293
ben-craigfae08e72015-07-15 11:34:47 -0500294 void testException(const std::string& arg) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000295 printf("testException(%s)\n", arg.c_str());
Mark Sleed3d733a2006-09-01 22:19:06 +0000296 if (arg.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000297 Xception e;
298 e.errorCode = 1001;
David Reiss9813cbe2009-01-31 21:39:11 +0000299 e.message = arg;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000300 throw e;
Roger Meier1f8b48f2012-05-02 22:56:47 +0000301 } else if (arg.compare("TException") == 0) {
T Jake Lucianib5e62212009-01-31 22:36:20 +0000302 apache::thrift::TException e;
David Reiss55ff70f2008-06-11 00:58:25 +0000303 throw e;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000304 } else {
305 Xtruct result;
306 result.string_thing = arg;
307 return;
308 }
309 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000310
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100311 void testMultiException(Xtruct& result,
312 const std::string& arg0,
ben-craigfae08e72015-07-15 11:34:47 -0500313 const std::string& arg1) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000314
Marc Slemko71d4e472006-08-15 22:34:04 +0000315 printf("testMultiException(%s, %s)\n", arg0.c_str(), arg1.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000316
Mark Sleef5f2be42006-09-05 21:05:31 +0000317 if (arg0.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000318 Xception e;
319 e.errorCode = 1001;
320 e.message = "This is an Xception";
321 throw e;
Mark Sleef5f2be42006-09-05 21:05:31 +0000322 } else if (arg0.compare("Xception2") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000323 Xception2 e;
324 e.errorCode = 2002;
325 e.struct_thing.string_thing = "This is an Xception2";
326 throw e;
327 } else {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000328 result.string_thing = arg1;
Mark Slee1921d202007-01-24 19:43:06 +0000329 return;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000330 }
331 }
David Reiss2ab6fe82008-02-18 02:11:44 +0000332
Jake Farrell5d02b802014-01-07 21:42:01 -0500333 void testOneway(const int32_t sleepFor) {
David Reiss6ce401d2009-03-24 20:01:58 +0000334 printf("testOneway(%d): Sleeping...\n", sleepFor);
Jake Farrell5d02b802014-01-07 21:42:01 -0500335 THRIFT_SLEEP_SEC(sleepFor);
David Reiss6ce401d2009-03-24 20:01:58 +0000336 printf("testOneway(%d): done sleeping!\n", sleepFor);
David Reiss2ab6fe82008-02-18 02:11:44 +0000337 }
Mark Sleee8540632006-05-30 09:24:40 +0000338};
339
David Reissd7192062010-10-06 17:09:33 +0000340class TestProcessorEventHandler : public TProcessorEventHandler {
David Reiss23248712010-10-06 17:10:08 +0000341 virtual void* getContext(const char* fn_name, void* serverContext) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100342 (void)serverContext;
David Reissd7192062010-10-06 17:09:33 +0000343 return new std::string(fn_name);
344 }
345 virtual void freeContext(void* ctx, const char* fn_name) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100346 (void)fn_name;
David Reissd7192062010-10-06 17:09:33 +0000347 delete static_cast<std::string*>(ctx);
348 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100349 virtual void preRead(void* ctx, const char* fn_name) { communicate("preRead", ctx, fn_name); }
David Reissef7200f2010-10-06 17:09:42 +0000350 virtual void postRead(void* ctx, const char* fn_name, uint32_t bytes) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100351 (void)bytes;
David Reissd7192062010-10-06 17:09:33 +0000352 communicate("postRead", ctx, fn_name);
353 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100354 virtual void preWrite(void* ctx, const char* fn_name) { communicate("preWrite", ctx, fn_name); }
David Reissef7200f2010-10-06 17:09:42 +0000355 virtual void postWrite(void* ctx, const char* fn_name, uint32_t bytes) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100356 (void)bytes;
David Reissd7192062010-10-06 17:09:33 +0000357 communicate("postWrite", ctx, fn_name);
358 }
359 virtual void asyncComplete(void* ctx, const char* fn_name) {
360 communicate("asyncComplete", ctx, fn_name);
361 }
362 virtual void handlerError(void* ctx, const char* fn_name) {
363 communicate("handlerError", ctx, fn_name);
364 }
365
366 void communicate(const char* event, void* ctx, const char* fn_name) {
367 std::cout << event << ": " << *static_cast<std::string*>(ctx) << " = " << fn_name << std::endl;
368 }
369};
370
Roger Meier7e056e72011-07-17 07:28:28 +0000371class TestHandlerAsync : public ThriftTestCobSvIf {
372public:
Roger Meier611f90c2011-12-11 22:08:51 +0000373 TestHandlerAsync(boost::shared_ptr<TestHandler>& handler) : _delegate(handler) {}
Roger Meier7e056e72011-07-17 07:28:28 +0000374 virtual ~TestHandlerAsync() {}
375
Roger Meier0a7c69c2014-05-02 21:15:45 +0200376 virtual void testVoid(tcxx::function<void()> cob) {
Roger Meier7e056e72011-07-17 07:28:28 +0000377 _delegate->testVoid();
378 cob();
379 }
380
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100381 virtual void testString(tcxx::function<void(std::string const& _return)> cob,
382 const std::string& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000383 std::string res;
384 _delegate->testString(res, thing);
385 cob(res);
386 }
387
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900388 virtual void testBool(tcxx::function<void(bool const& _return)> cob, const bool thing) {
389 bool res = _delegate->testBool(thing);
390 cob(res);
391 }
392
Roger Meier0a7c69c2014-05-02 21:15:45 +0200393 virtual void testByte(tcxx::function<void(int8_t const& _return)> cob, const int8_t thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000394 int8_t res = _delegate->testByte(thing);
395 cob(res);
396 }
397
Roger Meier0a7c69c2014-05-02 21:15:45 +0200398 virtual void testI32(tcxx::function<void(int32_t const& _return)> cob, const int32_t thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000399 int32_t res = _delegate->testI32(thing);
400 cob(res);
401 }
402
Roger Meier0a7c69c2014-05-02 21:15:45 +0200403 virtual void testI64(tcxx::function<void(int64_t const& _return)> cob, const int64_t thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000404 int64_t res = _delegate->testI64(thing);
405 cob(res);
406 }
407
Roger Meier0a7c69c2014-05-02 21:15:45 +0200408 virtual void testDouble(tcxx::function<void(double const& _return)> cob, const double thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000409 double res = _delegate->testDouble(thing);
410 cob(res);
411 }
412
Konrad Grochowski1f6e3802015-05-18 18:10:06 +0200413 virtual void testBinary(tcxx::function<void(std::string const& _return)> cob,
414 const std::string& thing) {
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100415 std::string res;
416 _delegate->testBinary(res, thing);
417 cob(res);
418 }
419
Roger Meier0a7c69c2014-05-02 21:15:45 +0200420 virtual void testStruct(tcxx::function<void(Xtruct const& _return)> cob, const Xtruct& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000421 Xtruct res;
422 _delegate->testStruct(res, thing);
423 cob(res);
424 }
425
Roger Meier0a7c69c2014-05-02 21:15:45 +0200426 virtual void testNest(tcxx::function<void(Xtruct2 const& _return)> cob, const Xtruct2& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000427 Xtruct2 res;
428 _delegate->testNest(res, thing);
429 cob(res);
430 }
431
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100432 virtual void testMap(tcxx::function<void(std::map<int32_t, int32_t> const& _return)> cob,
433 const std::map<int32_t, int32_t>& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000434 std::map<int32_t, int32_t> res;
435 _delegate->testMap(res, thing);
436 cob(res);
437 }
438
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100439 virtual void testStringMap(
440 tcxx::function<void(std::map<std::string, std::string> const& _return)> cob,
441 const std::map<std::string, std::string>& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000442 std::map<std::string, std::string> res;
443 _delegate->testStringMap(res, thing);
444 cob(res);
445 }
446
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100447 virtual void testSet(tcxx::function<void(std::set<int32_t> const& _return)> cob,
448 const std::set<int32_t>& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000449 std::set<int32_t> res;
450 _delegate->testSet(res, thing);
451 cob(res);
452 }
453
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100454 virtual void testList(tcxx::function<void(std::vector<int32_t> const& _return)> cob,
455 const std::vector<int32_t>& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000456 std::vector<int32_t> res;
457 _delegate->testList(res, thing);
458 cob(res);
459 }
460
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100461 virtual void testEnum(tcxx::function<void(Numberz::type const& _return)> cob,
462 const Numberz::type thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000463 Numberz::type res = _delegate->testEnum(thing);
464 cob(res);
465 }
466
Roger Meier0a7c69c2014-05-02 21:15:45 +0200467 virtual void testTypedef(tcxx::function<void(UserId const& _return)> cob, const UserId thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000468 UserId res = _delegate->testTypedef(thing);
469 cob(res);
470 }
471
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100472 virtual void testMapMap(
473 tcxx::function<void(std::map<int32_t, std::map<int32_t, int32_t> > const& _return)> cob,
474 const int32_t hello) {
Roger Meier7e056e72011-07-17 07:28:28 +0000475 std::map<int32_t, std::map<int32_t, int32_t> > res;
476 _delegate->testMapMap(res, hello);
477 cob(res);
478 }
479
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100480 virtual void testInsanity(
481 tcxx::function<void(std::map<UserId, std::map<Numberz::type, Insanity> > const& _return)> cob,
482 const Insanity& argument) {
Jake Farrell5d02b802014-01-07 21:42:01 -0500483 std::map<UserId, std::map<Numberz::type, Insanity> > res;
Roger Meier7e056e72011-07-17 07:28:28 +0000484 _delegate->testInsanity(res, argument);
485 cob(res);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100486 }
Roger Meier7e056e72011-07-17 07:28:28 +0000487
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100488 virtual void testMulti(tcxx::function<void(Xtruct const& _return)> cob,
489 const int8_t arg0,
490 const int32_t arg1,
491 const int64_t arg2,
492 const std::map<int16_t, std::string>& arg3,
493 const Numberz::type arg4,
494 const UserId arg5) {
Roger Meier7e056e72011-07-17 07:28:28 +0000495 Xtruct res;
496 _delegate->testMulti(res, arg0, arg1, arg2, arg3, arg4, arg5);
497 cob(res);
498 }
499
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100500 virtual void testException(
501 tcxx::function<void()> cob,
502 tcxx::function<void(::apache::thrift::TDelayedException* _throw)> exn_cob,
503 const std::string& arg) {
Roger Meier7e056e72011-07-17 07:28:28 +0000504 try {
505 _delegate->testException(arg);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100506 } catch (const apache::thrift::TException& e) {
Roger Meier7e056e72011-07-17 07:28:28 +0000507 exn_cob(apache::thrift::TDelayedException::delayException(e));
508 return;
509 }
510 cob();
511 }
512
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100513 virtual void testMultiException(
514 tcxx::function<void(Xtruct const& _return)> cob,
515 tcxx::function<void(::apache::thrift::TDelayedException* _throw)> exn_cob,
516 const std::string& arg0,
517 const std::string& arg1) {
Roger Meier7e056e72011-07-17 07:28:28 +0000518 Xtruct res;
519 try {
520 _delegate->testMultiException(res, arg0, arg1);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100521 } catch (const apache::thrift::TException& e) {
Roger Meier7e056e72011-07-17 07:28:28 +0000522 exn_cob(apache::thrift::TDelayedException::delayException(e));
523 return;
524 }
525 cob(res);
526 }
527
Roger Meier0a7c69c2014-05-02 21:15:45 +0200528 virtual void testOneway(tcxx::function<void()> cob, const int32_t secondsToSleep) {
Roger Meier7e056e72011-07-17 07:28:28 +0000529 _delegate->testOneway(secondsToSleep);
530 cob();
531 }
532
533protected:
Roger Meier611f90c2011-12-11 22:08:51 +0000534 boost::shared_ptr<TestHandler> _delegate;
Roger Meier7e056e72011-07-17 07:28:28 +0000535};
536
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900537namespace po = boost::program_options;
538
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100539int main(int argc, char** argv) {
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +0530540
Nobuaki Sukegawac8744082016-03-09 19:55:56 +0900541 string testDir = boost::filesystem::system_complete(argv[0]).parent_path().parent_path().parent_path().string();
542 string certPath = testDir + "/keys/server.crt";
543 string keyPath = testDir + "/keys/server.key";
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +0530544
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100545#if _WIN32
Jake Farrell5d02b802014-01-07 21:42:01 -0500546 transport::TWinsockSingleton::create();
547#endif
Mark Sleee8540632006-05-30 09:24:40 +0000548 int port = 9090;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000549 bool ssl = false;
Roger Meierca142b02011-06-07 17:59:07 +0000550 string transport_type = "buffered";
551 string protocol_type = "binary";
552 string server_type = "simple";
553 string domain_socket = "";
pavlodd08f6e2015-10-08 16:43:56 -0400554 bool abstract_namespace = false;
Roger Meierca142b02011-06-07 17:59:07 +0000555 size_t workers = 4;
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900556 int string_limit = 0;
557 int container_limit = 0;
Marc Slemko6be374b2006-08-04 03:16:25 +0000558
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900559 po::options_description desc("Allowed options");
560 desc.add_options()
561 ("help,h", "produce help message")
562 ("port", po::value<int>(&port)->default_value(port), "Port number to listen")
563 ("domain-socket", po::value<string>(&domain_socket) ->default_value(domain_socket), "Unix Domain Socket (e.g. /tmp/ThriftTest.thrift)")
564 ("abstract-namespace", "Create the domain socket in the Abstract Namespace (no connection with filesystem pathnames)")
565 ("server-type", po::value<string>(&server_type)->default_value(server_type), "type of server, \"simple\", \"thread-pool\", \"threaded\", or \"nonblocking\"")
566 ("transport", po::value<string>(&transport_type)->default_value(transport_type), "transport: buffered, framed, http")
567 ("protocol", po::value<string>(&protocol_type)->default_value(protocol_type), "protocol: binary, compact, header, json")
568 ("ssl", "Encrypted Transport using SSL")
569 ("processor-events", "processor-events")
570 ("workers,n", po::value<size_t>(&workers)->default_value(workers), "Number of thread pools workers. Only valid for thread-pool server type")
571 ("string-limit", po::value<int>(&string_limit))
572 ("container-limit", po::value<int>(&container_limit));
Marc Slemko6be374b2006-08-04 03:16:25 +0000573
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900574 po::variables_map vm;
575 po::store(po::parse_command_line(argc, argv, desc), vm);
576 po::notify(vm);
Marc Slemko6be374b2006-08-04 03:16:25 +0000577
Roger Meierca142b02011-06-07 17:59:07 +0000578 if (vm.count("help")) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100579 cout << desc << "\n";
580 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000581 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500582
Marc Slemko6be374b2006-08-04 03:16:25 +0000583 try {
Roger Meierca142b02011-06-07 17:59:07 +0000584 if (!server_type.empty()) {
585 if (server_type == "simple") {
586 } else if (server_type == "thread-pool") {
587 } else if (server_type == "threaded") {
588 } else if (server_type == "nonblocking") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000589 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100590 throw invalid_argument("Unknown server type " + server_type);
Roger Meierca142b02011-06-07 17:59:07 +0000591 }
592 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500593
Roger Meierca142b02011-06-07 17:59:07 +0000594 if (!protocol_type.empty()) {
595 if (protocol_type == "binary") {
Roger Meier284101c2014-03-11 21:20:35 +0100596 } else if (protocol_type == "compact") {
Roger Meierca142b02011-06-07 17:59:07 +0000597 } else if (protocol_type == "json") {
Dave Watson792db4e2015-01-16 11:22:01 -0800598 } else if (protocol_type == "header") {
Roger Meierca142b02011-06-07 17:59:07 +0000599 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100600 throw invalid_argument("Unknown protocol type " + protocol_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000601 }
602 }
603
Roger Meier284101c2014-03-11 21:20:35 +0100604 if (!transport_type.empty()) {
Roger Meierca142b02011-06-07 17:59:07 +0000605 if (transport_type == "buffered") {
606 } else if (transport_type == "framed") {
607 } else if (transport_type == "http") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000608 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100609 throw invalid_argument("Unknown transport type " + transport_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000610 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000611 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000612
Bryan Duxbury833ae492010-09-27 17:26:02 +0000613 } catch (std::exception& e) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000614 cerr << e.what() << endl;
Roger Meierca142b02011-06-07 17:59:07 +0000615 cout << desc << "\n";
616 return 1;
Marc Slemko6be374b2006-08-04 03:16:25 +0000617 }
618
Roger Meierca142b02011-06-07 17:59:07 +0000619 if (vm.count("ssl")) {
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000620 ssl = true;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000621 }
622
pavlodd08f6e2015-10-08 16:43:56 -0400623 if (vm.count("abstract-namespace")) {
624 abstract_namespace = true;
625 }
626
Mark Sleee8540632006-05-30 09:24:40 +0000627 // Dispatcher
Roger Meier611f90c2011-12-11 22:08:51 +0000628 boost::shared_ptr<TProtocolFactory> protocolFactory;
Roger Meierca142b02011-06-07 17:59:07 +0000629 if (protocol_type == "json") {
Roger Meier611f90c2011-12-11 22:08:51 +0000630 boost::shared_ptr<TProtocolFactory> jsonProtocolFactory(new TJSONProtocolFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000631 protocolFactory = jsonProtocolFactory;
Roger Meier023192f2014-02-12 09:35:12 +0100632 } else if (protocol_type == "compact") {
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900633 TCompactProtocolFactoryT<TBufferBase> *compactProtocolFactory = new TCompactProtocolFactoryT<TBufferBase>();
634 compactProtocolFactory->setContainerSizeLimit(container_limit);
635 compactProtocolFactory->setStringSizeLimit(string_limit);
636 protocolFactory.reset(compactProtocolFactory);
Dave Watson792db4e2015-01-16 11:22:01 -0800637 } else if (protocol_type == "header") {
638 boost::shared_ptr<TProtocolFactory> headerProtocolFactory(new THeaderProtocolFactory());
639 protocolFactory = headerProtocolFactory;
Roger Meierca142b02011-06-07 17:59:07 +0000640 } else {
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900641 TBinaryProtocolFactoryT<TBufferBase>* binaryProtocolFactory = new TBinaryProtocolFactoryT<TBufferBase>();
642 binaryProtocolFactory->setContainerSizeLimit(container_limit);
643 binaryProtocolFactory->setStringSizeLimit(string_limit);
644 protocolFactory.reset(binaryProtocolFactory);
Roger Meierca142b02011-06-07 17:59:07 +0000645 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000646
Roger Meierca142b02011-06-07 17:59:07 +0000647 // Processor
Roger Meier611f90c2011-12-11 22:08:51 +0000648 boost::shared_ptr<TestHandler> testHandler(new TestHandler());
649 boost::shared_ptr<ThriftTestProcessor> testProcessor(new ThriftTestProcessor(testHandler));
Jake Farrell5d02b802014-01-07 21:42:01 -0500650
Roger Meierca142b02011-06-07 17:59:07 +0000651 if (vm.count("processor-events")) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100652 testProcessor->setEventHandler(
653 boost::shared_ptr<TProcessorEventHandler>(new TestProcessorEventHandler()));
David Reissd7192062010-10-06 17:09:33 +0000654 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500655
Mark Sleee8540632006-05-30 09:24:40 +0000656 // Transport
Roger Meier611f90c2011-12-11 22:08:51 +0000657 boost::shared_ptr<TSSLSocketFactory> sslSocketFactory;
658 boost::shared_ptr<TServerSocket> serverSocket;
Mark Sleee8540632006-05-30 09:24:40 +0000659
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000660 if (ssl) {
Roger Meier611f90c2011-12-11 22:08:51 +0000661 sslSocketFactory = boost::shared_ptr<TSSLSocketFactory>(new TSSLSocketFactory());
Nobuaki Sukegawac8744082016-03-09 19:55:56 +0900662 sslSocketFactory->loadCertificate(certPath.c_str());
663 sslSocketFactory->loadPrivateKey(keyPath.c_str());
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000664 sslSocketFactory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
Roger Meier611f90c2011-12-11 22:08:51 +0000665 serverSocket = boost::shared_ptr<TServerSocket>(new TSSLServerSocket(port, sslSocketFactory));
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000666 } else {
Roger Meierc94b2932014-02-22 20:07:33 +0100667 if (domain_socket != "") {
pavlodd08f6e2015-10-08 16:43:56 -0400668 if (abstract_namespace) {
669 std::string abstract_socket("\0", 1);
670 abstract_socket += domain_socket;
671 serverSocket = boost::shared_ptr<TServerSocket>(new TServerSocket(abstract_socket));
672 } else {
673 unlink(domain_socket.c_str());
674 serverSocket = boost::shared_ptr<TServerSocket>(new TServerSocket(domain_socket));
675 }
Roger Meierc94b2932014-02-22 20:07:33 +0100676 port = 0;
677 } else {
Roger Meier611f90c2011-12-11 22:08:51 +0000678 serverSocket = boost::shared_ptr<TServerSocket>(new TServerSocket(port));
Roger Meierc94b2932014-02-22 20:07:33 +0100679 }
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000680 }
Roger Meierca142b02011-06-07 17:59:07 +0000681
Mark Sleed788b2e2006-09-07 01:26:35 +0000682 // Factory
Roger Meier611f90c2011-12-11 22:08:51 +0000683 boost::shared_ptr<TTransportFactory> transportFactory;
Jake Farrell5d02b802014-01-07 21:42:01 -0500684
Roger Meier7e056e72011-07-17 07:28:28 +0000685 if (transport_type == "http" && server_type != "nonblocking") {
Jake Farrell5d02b802014-01-07 21:42:01 -0500686 boost::shared_ptr<TTransportFactory> httpTransportFactory(new THttpServerTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000687 transportFactory = httpTransportFactory;
688 } else if (transport_type == "framed") {
Jake Farrell5d02b802014-01-07 21:42:01 -0500689 boost::shared_ptr<TTransportFactory> framedTransportFactory(new TFramedTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000690 transportFactory = framedTransportFactory;
691 } else {
Jake Farrell5d02b802014-01-07 21:42:01 -0500692 boost::shared_ptr<TTransportFactory> bufferedTransportFactory(new TBufferedTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000693 transportFactory = bufferedTransportFactory;
694 }
Mark Sleed788b2e2006-09-07 01:26:35 +0000695
Roger Meierca142b02011-06-07 17:59:07 +0000696 // Server Info
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100697 cout << "Starting \"" << server_type << "\" server (" << transport_type << "/" << protocol_type
pavlodd08f6e2015-10-08 16:43:56 -0400698 << ") listen on: ";
699 if (abstract_namespace) {
700 cout << '@';
701 }
702 cout << domain_socket;
Roger Meierca142b02011-06-07 17:59:07 +0000703 if (port != 0) {
704 cout << port;
705 }
706 cout << endl;
Mark Sleee8540632006-05-30 09:24:40 +0000707
Roger Meierca142b02011-06-07 17:59:07 +0000708 // Server
Jake Farrell5d02b802014-01-07 21:42:01 -0500709 boost::shared_ptr<apache::thrift::server::TServer> server;
710
Roger Meierca142b02011-06-07 17:59:07 +0000711 if (server_type == "simple") {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100712 server.reset(new TSimpleServer(testProcessor, serverSocket, transportFactory, protocolFactory));
Roger Meierca142b02011-06-07 17:59:07 +0000713 } else if (server_type == "thread-pool") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000714
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100715 boost::shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workers);
Marc Slemko6be374b2006-08-04 03:16:25 +0000716
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100717 boost::shared_ptr<PlatformThreadFactory> threadFactory
718 = boost::shared_ptr<PlatformThreadFactory>(new PlatformThreadFactory());
Marc Slemko6be374b2006-08-04 03:16:25 +0000719
720 threadManager->threadFactory(threadFactory);
721
722 threadManager->start();
723
Jake Farrell5d02b802014-01-07 21:42:01 -0500724 server.reset(new TThreadPoolServer(testProcessor,
Roger Meierca142b02011-06-07 17:59:07 +0000725 serverSocket,
Mark Sleed788b2e2006-09-07 01:26:35 +0000726 transportFactory,
Mark Sleea3302652006-10-25 19:03:32 +0000727 protocolFactory,
Jake Farrell5d02b802014-01-07 21:42:01 -0500728 threadManager));
Roger Meierca142b02011-06-07 17:59:07 +0000729 } else if (server_type == "threaded") {
David Reiss0c90f6f2008-02-06 22:18:40 +0000730
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100731 server.reset(
732 new TThreadedServer(testProcessor, serverSocket, transportFactory, protocolFactory));
Roger Meierca142b02011-06-07 17:59:07 +0000733 } else if (server_type == "nonblocking") {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100734 if (transport_type == "http") {
Roger Meier611f90c2011-12-11 22:08:51 +0000735 boost::shared_ptr<TestHandlerAsync> testHandlerAsync(new TestHandlerAsync(testHandler));
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100736 boost::shared_ptr<TAsyncProcessor> testProcessorAsync(
737 new ThriftTestAsyncProcessor(testHandlerAsync));
738 boost::shared_ptr<TAsyncBufferProcessor> testBufferProcessor(
739 new TAsyncProtocolProcessor(testProcessorAsync, protocolFactory));
Jake Farrell5d02b802014-01-07 21:42:01 -0500740
741 // not loading nonblockingServer into "server" because
742 // TEvhttpServer doesn't inherit from TServer, and doesn't
743 // provide a stop method.
Roger Meier7e056e72011-07-17 07:28:28 +0000744 TEvhttpServer nonblockingServer(testBufferProcessor, port);
745 nonblockingServer.serve();
Jake Farrell5d02b802014-01-07 21:42:01 -0500746 } else {
Dave Watson792db4e2015-01-16 11:22:01 -0800747 server.reset(new TNonblockingServer(testProcessor, protocolFactory, port));
Roger Meier7e056e72011-07-17 07:28:28 +0000748 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000749 }
750
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100751 if (server.get() != NULL) {
Dave Watson792db4e2015-01-16 11:22:01 -0800752 if (protocol_type == "header") {
James E. King, IIIdf899132016-11-12 15:16:30 -0500753 // Tell the server to use the same protocol for input / output
Dave Watson792db4e2015-01-16 11:22:01 -0800754 // if using header
755 server->setOutputProtocolFactory(boost::shared_ptr<TProtocolFactory>());
756 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500757 apache::thrift::concurrency::PlatformThreadFactory factory;
758 factory.setDetached(false);
759 boost::shared_ptr<apache::thrift::concurrency::Runnable> serverThreadRunner(server);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100760 boost::shared_ptr<apache::thrift::concurrency::Thread> thread
761 = factory.newThread(serverThreadRunner);
Jake Farrell5d02b802014-01-07 21:42:01 -0500762 thread->start();
763
Roger Meier284101c2014-03-11 21:20:35 +0100764 // HACK: cross language test suite is unable to handle cin properly
765 // that's why we stay in a endless loop here
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100766 while (1) {
767 }
Roger Meier284101c2014-03-11 21:20:35 +0100768 // FIXME: find another way to stop the server (e.g. a signal)
769 // cout<<"Press enter to stop the server."<<endl;
770 // cin.ignore(); //wait until a key is pressed
Jake Farrell5d02b802014-01-07 21:42:01 -0500771
772 server->stop();
773 thread->join();
774 server.reset();
775 }
776
Roger Meierca142b02011-06-07 17:59:07 +0000777 cout << "done." << endl;
Mark Sleee8540632006-05-30 09:24:40 +0000778 return 0;
779}