blob: b3c292aef0f444da719ca592d7c438b092a61567 [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 Meierd3b9dca2011-06-24 14:01:10 +000020#define __STDC_FORMAT_MACROS
21#include <inttypes.h>
22
Roger Meier49ff8b12012-04-13 09:12:31 +000023#include <thrift/concurrency/ThreadManager.h>
24#include <thrift/concurrency/PlatformThreadFactory.h>
25#include <thrift/protocol/TBinaryProtocol.h>
Roger Meier023192f2014-02-12 09:35:12 +010026#include <thrift/protocol/TCompactProtocol.h>
Dave Watson792db4e2015-01-16 11:22:01 -080027#include <thrift/protocol/THeaderProtocol.h>
Roger Meier49ff8b12012-04-13 09:12:31 +000028#include <thrift/protocol/TJSONProtocol.h>
29#include <thrift/server/TSimpleServer.h>
30#include <thrift/server/TThreadedServer.h>
31#include <thrift/server/TThreadPoolServer.h>
32#include <thrift/async/TEvhttpServer.h>
33#include <thrift/async/TAsyncBufferProcessor.h>
34#include <thrift/async/TAsyncProtocolProcessor.h>
35#include <thrift/server/TNonblockingServer.h>
36#include <thrift/transport/TServerSocket.h>
37#include <thrift/transport/TSSLServerSocket.h>
38#include <thrift/transport/TSSLSocket.h>
39#include <thrift/transport/THttpServer.h>
40#include <thrift/transport/THttpTransport.h>
41#include <thrift/transport/TTransportUtils.h>
Mark Slee95771002006-06-07 06:53:25 +000042#include "ThriftTest.h"
Marc Slemko6be374b2006-08-04 03:16:25 +000043
44#include <iostream>
45#include <stdexcept>
46#include <sstream>
47
Roger Meierca142b02011-06-07 17:59:07 +000048#include <boost/program_options.hpp>
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +053049#include <boost/filesystem.hpp>
Roger Meier0a7c69c2014-05-02 21:15:45 +020050#include <thrift/cxxfunctional.h>
Roger Meierca142b02011-06-07 17:59:07 +000051
Bryan Duxburycd9aea12011-02-22 18:12:06 +000052#include <signal.h>
Jake Farrell5d02b802014-01-07 21:42:01 -050053#if _WIN32
Konrad Grochowski16a23a62014-11-13 15:33:38 +010054#include <thrift/windows/TWinsockSingleton.h>
Jake Farrell5d02b802014-01-07 21:42:01 -050055#endif
David Reissbc3dddb2007-08-22 23:20:24 +000056
Mark Sleee8540632006-05-30 09:24:40 +000057using namespace std;
58
T Jake Lucianib5e62212009-01-31 22:36:20 +000059using namespace apache::thrift;
60using namespace apache::thrift::concurrency;
61using namespace apache::thrift::protocol;
62using namespace apache::thrift::transport;
63using namespace apache::thrift::server;
Roger Meier7e056e72011-07-17 07:28:28 +000064using namespace apache::thrift::async;
Marc Slemko6be374b2006-08-04 03:16:25 +000065
Marc Slemkobf4fd192006-08-15 21:29:39 +000066using namespace thrift::test;
67
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +053068// Length of argv[0] - Length of script dir
69#define EXECUTABLE_FILE_NAME_LENGTH 19
70
Mark Sleed2655522006-09-05 22:09:57 +000071class TestHandler : public ThriftTestIf {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010072public:
Mark Sleed2655522006-09-05 22:09:57 +000073 TestHandler() {}
Mark Sleee8540632006-05-30 09:24:40 +000074
Konrad Grochowski16a23a62014-11-13 15:33:38 +010075 void testVoid() { printf("testVoid()\n"); }
Mark Sleee8540632006-05-30 09:24:40 +000076
Konrad Grochowski16a23a62014-11-13 15:33:38 +010077 void testString(string& out, const string& thing) {
Mark Sleee8540632006-05-30 09:24:40 +000078 printf("testString(\"%s\")\n", thing.c_str());
Mark Slee1921d202007-01-24 19:43:06 +000079 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +000080 }
81
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +090082 bool testBool(const bool thing) {
83 printf("testBool(%s)\n", thing ? "true" : "false");
84 return thing;
85 }
86
Mark Slee1921d202007-01-24 19:43:06 +000087 int8_t testByte(const int8_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +000088 printf("testByte(%d)\n", (int)thing);
89 return thing;
90 }
91
Mark Slee1921d202007-01-24 19:43:06 +000092 int32_t testI32(const int32_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +000093 printf("testI32(%d)\n", thing);
94 return thing;
95 }
96
Mark Slee1921d202007-01-24 19:43:06 +000097 int64_t testI64(const int64_t thing) {
Roger Meier0e814802014-01-17 21:07:58 +010098 printf("testI64(%" PRId64 ")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +000099 return thing;
100 }
101
Mark Slee1921d202007-01-24 19:43:06 +0000102 double testDouble(const double thing) {
Roger Meiera8cef6e2011-07-17 18:55:59 +0000103 printf("testDouble(%f)\n", thing);
Mark Sleec98d0502006-09-06 02:42:25 +0000104 return thing;
105 }
106
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100107 void testBinary(std::string& _return, const std::string& thing) {
108 std::ostringstream hexstr;
109 hexstr << std::hex << thing;
110 printf("testBinary(%s)\n", hexstr.str().c_str());
111 _return = thing;
112 }
113
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100114 void testStruct(Xtruct& out, const Xtruct& thing) {
115 printf("testStruct({\"%s\", %d, %d, %" PRId64 "})\n",
116 thing.string_thing.c_str(),
117 (int)thing.byte_thing,
118 thing.i32_thing,
119 thing.i64_thing);
Mark Slee1921d202007-01-24 19:43:06 +0000120 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000121 }
122
Mark Slee1921d202007-01-24 19:43:06 +0000123 void testNest(Xtruct2& out, const Xtruct2& nest) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100124 const Xtruct& thing = nest.struct_thing;
125 printf("testNest({%d, {\"%s\", %d, %d, %" PRId64 "}, %d})\n",
126 (int)nest.byte_thing,
127 thing.string_thing.c_str(),
128 (int)thing.byte_thing,
129 thing.i32_thing,
130 thing.i64_thing,
131 nest.i32_thing);
Mark Slee1921d202007-01-24 19:43:06 +0000132 out = nest;
Mark Sleee8540632006-05-30 09:24:40 +0000133 }
134
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100135 void testMap(map<int32_t, int32_t>& out, const map<int32_t, int32_t>& thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000136 printf("testMap({");
137 map<int32_t, int32_t>::const_iterator m_iter;
138 bool first = true;
139 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
140 if (first) {
141 first = false;
142 } else {
143 printf(", ");
144 }
145 printf("%d => %d", m_iter->first, m_iter->second);
146 }
147 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000148 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000149 }
150
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100151 void testStringMap(map<std::string, std::string>& out,
152 const map<std::string, std::string>& thing) {
Roger Meierd3b9dca2011-06-24 14:01:10 +0000153 printf("testMap({");
154 map<std::string, std::string>::const_iterator m_iter;
155 bool first = true;
156 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
157 if (first) {
158 first = false;
159 } else {
160 printf(", ");
161 }
162 printf("%s => %s", (m_iter->first).c_str(), (m_iter->second).c_str());
163 }
164 printf("})\n");
165 out = thing;
166 }
167
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100168 void testSet(set<int32_t>& out, const set<int32_t>& thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000169 printf("testSet({");
170 set<int32_t>::const_iterator s_iter;
171 bool first = true;
172 for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) {
173 if (first) {
174 first = false;
175 } else {
176 printf(", ");
177 }
178 printf("%d", *s_iter);
179 }
180 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000181 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000182 }
183
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100184 void testList(vector<int32_t>& out, const vector<int32_t>& thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000185 printf("testList({");
Mark Sleeb9acf982006-10-10 01:57:32 +0000186 vector<int32_t>::const_iterator l_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000187 bool first = true;
188 for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) {
189 if (first) {
190 first = false;
191 } else {
192 printf(", ");
193 }
194 printf("%d", *l_iter);
195 }
196 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000197 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000198 }
199
Bryan Duxbury833ae492010-09-27 17:26:02 +0000200 Numberz::type testEnum(const Numberz::type thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000201 printf("testEnum(%d)\n", thing);
202 return thing;
203 }
204
Mark Slee1921d202007-01-24 19:43:06 +0000205 UserId testTypedef(const UserId thing) {
Roger Meier0e814802014-01-17 21:07:58 +0100206 printf("testTypedef(%" PRId64 ")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +0000207 return thing;
208 }
209
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100210 void testMapMap(map<int32_t, map<int32_t, int32_t> >& mapmap, const int32_t hello) {
Mark Sleee8540632006-05-30 09:24:40 +0000211 printf("testMapMap(%d)\n", hello);
Mark Sleee8540632006-05-30 09:24:40 +0000212
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100213 map<int32_t, int32_t> pos;
214 map<int32_t, int32_t> neg;
Mark Sleee8540632006-05-30 09:24:40 +0000215 for (int i = 1; i < 5; i++) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100216 pos.insert(make_pair(i, i));
217 neg.insert(make_pair(-i, -i));
Mark Sleee8540632006-05-30 09:24:40 +0000218 }
219
220 mapmap.insert(make_pair(4, pos));
221 mapmap.insert(make_pair(-4, neg));
Mark Sleee8540632006-05-30 09:24:40 +0000222 }
223
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100224 void testInsanity(map<UserId, map<Numberz::type, Insanity> >& insane, const Insanity& argument) {
Mark Sleee8540632006-05-30 09:24:40 +0000225 printf("testInsanity()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000226
Mark Sleee8540632006-05-30 09:24:40 +0000227 Insanity looney;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000228 map<Numberz::type, Insanity> first_map;
229 map<Numberz::type, Insanity> second_map;
Mark Sleee8540632006-05-30 09:24:40 +0000230
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900231 first_map.insert(make_pair(Numberz::TWO, argument));
232 first_map.insert(make_pair(Numberz::THREE, argument));
Mark Sleee8540632006-05-30 09:24:40 +0000233
Bryan Duxbury833ae492010-09-27 17:26:02 +0000234 second_map.insert(make_pair(Numberz::SIX, looney));
Mark Sleee8540632006-05-30 09:24:40 +0000235
Mark Sleee8540632006-05-30 09:24:40 +0000236 insane.insert(make_pair(1, first_map));
237 insane.insert(make_pair(2, second_map));
238
239 printf("return");
240 printf(" = {");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100241 map<UserId, map<Numberz::type, Insanity> >::const_iterator i_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000242 for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) {
Roger Meier0e814802014-01-17 21:07:58 +0100243 printf("%" PRId64 " => {", i_iter->first);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100244 map<Numberz::type, Insanity>::const_iterator i2_iter;
245 for (i2_iter = i_iter->second.begin(); i2_iter != i_iter->second.end(); ++i2_iter) {
Mark Sleee8540632006-05-30 09:24:40 +0000246 printf("%d => {", i2_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000247 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
248 map<Numberz::type, UserId>::const_iterator um;
Mark Sleee8540632006-05-30 09:24:40 +0000249 printf("{");
250 for (um = userMap.begin(); um != userMap.end(); ++um) {
Roger Meier0e814802014-01-17 21:07:58 +0100251 printf("%d => %" PRId64 ", ", um->first, um->second);
Mark Sleee8540632006-05-30 09:24:40 +0000252 }
253 printf("}, ");
254
Mark Sleeb9acf982006-10-10 01:57:32 +0000255 vector<Xtruct> xtructs = i2_iter->second.xtructs;
256 vector<Xtruct>::const_iterator x;
Mark Sleee8540632006-05-30 09:24:40 +0000257 printf("{");
258 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100259 printf("{\"%s\", %d, %d, %" PRId64 "}, ",
260 x->string_thing.c_str(),
261 (int)x->byte_thing,
262 x->i32_thing,
263 x->i64_thing);
Mark Sleee8540632006-05-30 09:24:40 +0000264 }
265 printf("}");
266
267 printf("}, ");
268 }
269 printf("}, ");
270 }
271 printf("}\n");
Mark Sleee8540632006-05-30 09:24:40 +0000272 }
Marc Slemkoe6889de2006-08-12 00:32:53 +0000273
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100274 void testMulti(Xtruct& hello,
275 const int8_t arg0,
276 const int32_t arg1,
277 const int64_t arg2,
278 const std::map<int16_t, std::string>& arg3,
279 const Numberz::type arg4,
280 const UserId arg5) {
281 (void)arg3;
282 (void)arg4;
283 (void)arg5;
Jake Farrell5d02b802014-01-07 21:42:01 -0500284
Marc Slemkoe6889de2006-08-12 00:32:53 +0000285 printf("testMulti()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000286
Marc Slemkoe6889de2006-08-12 00:32:53 +0000287 hello.string_thing = "Hello2";
288 hello.byte_thing = arg0;
289 hello.i32_thing = arg1;
290 hello.i64_thing = (int64_t)arg2;
Marc Slemkoe6889de2006-08-12 00:32:53 +0000291 }
Marc Slemkobf4fd192006-08-15 21:29:39 +0000292
ben-craigfae08e72015-07-15 11:34:47 -0500293 void testException(const std::string& arg) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000294 printf("testException(%s)\n", arg.c_str());
Mark Sleed3d733a2006-09-01 22:19:06 +0000295 if (arg.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000296 Xception e;
297 e.errorCode = 1001;
David Reiss9813cbe2009-01-31 21:39:11 +0000298 e.message = arg;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000299 throw e;
Roger Meier1f8b48f2012-05-02 22:56:47 +0000300 } else if (arg.compare("TException") == 0) {
T Jake Lucianib5e62212009-01-31 22:36:20 +0000301 apache::thrift::TException e;
David Reiss55ff70f2008-06-11 00:58:25 +0000302 throw e;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000303 } else {
304 Xtruct result;
305 result.string_thing = arg;
306 return;
307 }
308 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000309
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100310 void testMultiException(Xtruct& result,
311 const std::string& arg0,
ben-craigfae08e72015-07-15 11:34:47 -0500312 const std::string& arg1) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000313
Marc Slemko71d4e472006-08-15 22:34:04 +0000314 printf("testMultiException(%s, %s)\n", arg0.c_str(), arg1.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000315
Mark Sleef5f2be42006-09-05 21:05:31 +0000316 if (arg0.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000317 Xception e;
318 e.errorCode = 1001;
319 e.message = "This is an Xception";
320 throw e;
Mark Sleef5f2be42006-09-05 21:05:31 +0000321 } else if (arg0.compare("Xception2") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000322 Xception2 e;
323 e.errorCode = 2002;
324 e.struct_thing.string_thing = "This is an Xception2";
325 throw e;
326 } else {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000327 result.string_thing = arg1;
Mark Slee1921d202007-01-24 19:43:06 +0000328 return;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000329 }
330 }
David Reiss2ab6fe82008-02-18 02:11:44 +0000331
Jake Farrell5d02b802014-01-07 21:42:01 -0500332 void testOneway(const int32_t sleepFor) {
David Reiss6ce401d2009-03-24 20:01:58 +0000333 printf("testOneway(%d): Sleeping...\n", sleepFor);
Jake Farrell5d02b802014-01-07 21:42:01 -0500334 THRIFT_SLEEP_SEC(sleepFor);
David Reiss6ce401d2009-03-24 20:01:58 +0000335 printf("testOneway(%d): done sleeping!\n", sleepFor);
David Reiss2ab6fe82008-02-18 02:11:44 +0000336 }
Mark Sleee8540632006-05-30 09:24:40 +0000337};
338
David Reissd7192062010-10-06 17:09:33 +0000339class TestProcessorEventHandler : public TProcessorEventHandler {
David Reiss23248712010-10-06 17:10:08 +0000340 virtual void* getContext(const char* fn_name, void* serverContext) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100341 (void)serverContext;
David Reissd7192062010-10-06 17:09:33 +0000342 return new std::string(fn_name);
343 }
344 virtual void freeContext(void* ctx, const char* fn_name) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100345 (void)fn_name;
David Reissd7192062010-10-06 17:09:33 +0000346 delete static_cast<std::string*>(ctx);
347 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100348 virtual void preRead(void* ctx, const char* fn_name) { communicate("preRead", ctx, fn_name); }
David Reissef7200f2010-10-06 17:09:42 +0000349 virtual void postRead(void* ctx, const char* fn_name, uint32_t bytes) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100350 (void)bytes;
David Reissd7192062010-10-06 17:09:33 +0000351 communicate("postRead", ctx, fn_name);
352 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100353 virtual void preWrite(void* ctx, const char* fn_name) { communicate("preWrite", ctx, fn_name); }
David Reissef7200f2010-10-06 17:09:42 +0000354 virtual void postWrite(void* ctx, const char* fn_name, uint32_t bytes) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100355 (void)bytes;
David Reissd7192062010-10-06 17:09:33 +0000356 communicate("postWrite", ctx, fn_name);
357 }
358 virtual void asyncComplete(void* ctx, const char* fn_name) {
359 communicate("asyncComplete", ctx, fn_name);
360 }
361 virtual void handlerError(void* ctx, const char* fn_name) {
362 communicate("handlerError", ctx, fn_name);
363 }
364
365 void communicate(const char* event, void* ctx, const char* fn_name) {
366 std::cout << event << ": " << *static_cast<std::string*>(ctx) << " = " << fn_name << std::endl;
367 }
368};
369
Roger Meier7e056e72011-07-17 07:28:28 +0000370class TestHandlerAsync : public ThriftTestCobSvIf {
371public:
Roger Meier611f90c2011-12-11 22:08:51 +0000372 TestHandlerAsync(boost::shared_ptr<TestHandler>& handler) : _delegate(handler) {}
Roger Meier7e056e72011-07-17 07:28:28 +0000373 virtual ~TestHandlerAsync() {}
374
Roger Meier0a7c69c2014-05-02 21:15:45 +0200375 virtual void testVoid(tcxx::function<void()> cob) {
Roger Meier7e056e72011-07-17 07:28:28 +0000376 _delegate->testVoid();
377 cob();
378 }
379
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100380 virtual void testString(tcxx::function<void(std::string const& _return)> cob,
381 const std::string& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000382 std::string res;
383 _delegate->testString(res, thing);
384 cob(res);
385 }
386
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900387 virtual void testBool(tcxx::function<void(bool const& _return)> cob, const bool thing) {
388 bool res = _delegate->testBool(thing);
389 cob(res);
390 }
391
Roger Meier0a7c69c2014-05-02 21:15:45 +0200392 virtual void testByte(tcxx::function<void(int8_t const& _return)> cob, const int8_t thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000393 int8_t res = _delegate->testByte(thing);
394 cob(res);
395 }
396
Roger Meier0a7c69c2014-05-02 21:15:45 +0200397 virtual void testI32(tcxx::function<void(int32_t const& _return)> cob, const int32_t thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000398 int32_t res = _delegate->testI32(thing);
399 cob(res);
400 }
401
Roger Meier0a7c69c2014-05-02 21:15:45 +0200402 virtual void testI64(tcxx::function<void(int64_t const& _return)> cob, const int64_t thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000403 int64_t res = _delegate->testI64(thing);
404 cob(res);
405 }
406
Roger Meier0a7c69c2014-05-02 21:15:45 +0200407 virtual void testDouble(tcxx::function<void(double const& _return)> cob, const double thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000408 double res = _delegate->testDouble(thing);
409 cob(res);
410 }
411
Konrad Grochowski1f6e3802015-05-18 18:10:06 +0200412 virtual void testBinary(tcxx::function<void(std::string const& _return)> cob,
413 const std::string& thing) {
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100414 std::string res;
415 _delegate->testBinary(res, thing);
416 cob(res);
417 }
418
Roger Meier0a7c69c2014-05-02 21:15:45 +0200419 virtual void testStruct(tcxx::function<void(Xtruct const& _return)> cob, const Xtruct& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000420 Xtruct res;
421 _delegate->testStruct(res, thing);
422 cob(res);
423 }
424
Roger Meier0a7c69c2014-05-02 21:15:45 +0200425 virtual void testNest(tcxx::function<void(Xtruct2 const& _return)> cob, const Xtruct2& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000426 Xtruct2 res;
427 _delegate->testNest(res, thing);
428 cob(res);
429 }
430
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100431 virtual void testMap(tcxx::function<void(std::map<int32_t, int32_t> const& _return)> cob,
432 const std::map<int32_t, int32_t>& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000433 std::map<int32_t, int32_t> res;
434 _delegate->testMap(res, thing);
435 cob(res);
436 }
437
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100438 virtual void testStringMap(
439 tcxx::function<void(std::map<std::string, std::string> const& _return)> cob,
440 const std::map<std::string, std::string>& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000441 std::map<std::string, std::string> res;
442 _delegate->testStringMap(res, thing);
443 cob(res);
444 }
445
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100446 virtual void testSet(tcxx::function<void(std::set<int32_t> const& _return)> cob,
447 const std::set<int32_t>& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000448 std::set<int32_t> res;
449 _delegate->testSet(res, thing);
450 cob(res);
451 }
452
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100453 virtual void testList(tcxx::function<void(std::vector<int32_t> const& _return)> cob,
454 const std::vector<int32_t>& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000455 std::vector<int32_t> res;
456 _delegate->testList(res, thing);
457 cob(res);
458 }
459
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100460 virtual void testEnum(tcxx::function<void(Numberz::type const& _return)> cob,
461 const Numberz::type thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000462 Numberz::type res = _delegate->testEnum(thing);
463 cob(res);
464 }
465
Roger Meier0a7c69c2014-05-02 21:15:45 +0200466 virtual void testTypedef(tcxx::function<void(UserId const& _return)> cob, const UserId thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000467 UserId res = _delegate->testTypedef(thing);
468 cob(res);
469 }
470
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100471 virtual void testMapMap(
472 tcxx::function<void(std::map<int32_t, std::map<int32_t, int32_t> > const& _return)> cob,
473 const int32_t hello) {
Roger Meier7e056e72011-07-17 07:28:28 +0000474 std::map<int32_t, std::map<int32_t, int32_t> > res;
475 _delegate->testMapMap(res, hello);
476 cob(res);
477 }
478
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100479 virtual void testInsanity(
480 tcxx::function<void(std::map<UserId, std::map<Numberz::type, Insanity> > const& _return)> cob,
481 const Insanity& argument) {
Jake Farrell5d02b802014-01-07 21:42:01 -0500482 std::map<UserId, std::map<Numberz::type, Insanity> > res;
Roger Meier7e056e72011-07-17 07:28:28 +0000483 _delegate->testInsanity(res, argument);
484 cob(res);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100485 }
Roger Meier7e056e72011-07-17 07:28:28 +0000486
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100487 virtual void testMulti(tcxx::function<void(Xtruct const& _return)> cob,
488 const int8_t arg0,
489 const int32_t arg1,
490 const int64_t arg2,
491 const std::map<int16_t, std::string>& arg3,
492 const Numberz::type arg4,
493 const UserId arg5) {
Roger Meier7e056e72011-07-17 07:28:28 +0000494 Xtruct res;
495 _delegate->testMulti(res, arg0, arg1, arg2, arg3, arg4, arg5);
496 cob(res);
497 }
498
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100499 virtual void testException(
500 tcxx::function<void()> cob,
501 tcxx::function<void(::apache::thrift::TDelayedException* _throw)> exn_cob,
502 const std::string& arg) {
Roger Meier7e056e72011-07-17 07:28:28 +0000503 try {
504 _delegate->testException(arg);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100505 } catch (const apache::thrift::TException& e) {
Roger Meier7e056e72011-07-17 07:28:28 +0000506 exn_cob(apache::thrift::TDelayedException::delayException(e));
507 return;
508 }
509 cob();
510 }
511
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100512 virtual void testMultiException(
513 tcxx::function<void(Xtruct const& _return)> cob,
514 tcxx::function<void(::apache::thrift::TDelayedException* _throw)> exn_cob,
515 const std::string& arg0,
516 const std::string& arg1) {
Roger Meier7e056e72011-07-17 07:28:28 +0000517 Xtruct res;
518 try {
519 _delegate->testMultiException(res, arg0, arg1);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100520 } catch (const apache::thrift::TException& e) {
Roger Meier7e056e72011-07-17 07:28:28 +0000521 exn_cob(apache::thrift::TDelayedException::delayException(e));
522 return;
523 }
524 cob(res);
525 }
526
Roger Meier0a7c69c2014-05-02 21:15:45 +0200527 virtual void testOneway(tcxx::function<void()> cob, const int32_t secondsToSleep) {
Roger Meier7e056e72011-07-17 07:28:28 +0000528 _delegate->testOneway(secondsToSleep);
529 cob();
530 }
531
532protected:
Roger Meier611f90c2011-12-11 22:08:51 +0000533 boost::shared_ptr<TestHandler> _delegate;
Roger Meier7e056e72011-07-17 07:28:28 +0000534};
535
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900536namespace po = boost::program_options;
537
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100538int main(int argc, char** argv) {
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +0530539
540 string file_path = boost::filesystem::system_complete(argv[0]).string();
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100541 string dir_path = file_path.substr(0, file_path.size() - EXECUTABLE_FILE_NAME_LENGTH);
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +0530542
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100543#if _WIN32
Jake Farrell5d02b802014-01-07 21:42:01 -0500544 transport::TWinsockSingleton::create();
545#endif
Mark Sleee8540632006-05-30 09:24:40 +0000546 int port = 9090;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000547 bool ssl = false;
Roger Meierca142b02011-06-07 17:59:07 +0000548 string transport_type = "buffered";
549 string protocol_type = "binary";
550 string server_type = "simple";
551 string domain_socket = "";
pavlodd08f6e2015-10-08 16:43:56 -0400552 bool abstract_namespace = false;
Roger Meierca142b02011-06-07 17:59:07 +0000553 size_t workers = 4;
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900554 int string_limit = 0;
555 int container_limit = 0;
Marc Slemko6be374b2006-08-04 03:16:25 +0000556
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900557 po::options_description desc("Allowed options");
558 desc.add_options()
559 ("help,h", "produce help message")
560 ("port", po::value<int>(&port)->default_value(port), "Port number to listen")
561 ("domain-socket", po::value<string>(&domain_socket) ->default_value(domain_socket), "Unix Domain Socket (e.g. /tmp/ThriftTest.thrift)")
562 ("abstract-namespace", "Create the domain socket in the Abstract Namespace (no connection with filesystem pathnames)")
563 ("server-type", po::value<string>(&server_type)->default_value(server_type), "type of server, \"simple\", \"thread-pool\", \"threaded\", or \"nonblocking\"")
564 ("transport", po::value<string>(&transport_type)->default_value(transport_type), "transport: buffered, framed, http")
565 ("protocol", po::value<string>(&protocol_type)->default_value(protocol_type), "protocol: binary, compact, header, json")
566 ("ssl", "Encrypted Transport using SSL")
567 ("processor-events", "processor-events")
568 ("workers,n", po::value<size_t>(&workers)->default_value(workers), "Number of thread pools workers. Only valid for thread-pool server type")
569 ("string-limit", po::value<int>(&string_limit))
570 ("container-limit", po::value<int>(&container_limit));
Marc Slemko6be374b2006-08-04 03:16:25 +0000571
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900572 po::variables_map vm;
573 po::store(po::parse_command_line(argc, argv, desc), vm);
574 po::notify(vm);
Marc Slemko6be374b2006-08-04 03:16:25 +0000575
Roger Meierca142b02011-06-07 17:59:07 +0000576 if (vm.count("help")) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100577 cout << desc << "\n";
578 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000579 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500580
Marc Slemko6be374b2006-08-04 03:16:25 +0000581 try {
Roger Meierca142b02011-06-07 17:59:07 +0000582 if (!server_type.empty()) {
583 if (server_type == "simple") {
584 } else if (server_type == "thread-pool") {
585 } else if (server_type == "threaded") {
586 } else if (server_type == "nonblocking") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000587 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100588 throw invalid_argument("Unknown server type " + server_type);
Roger Meierca142b02011-06-07 17:59:07 +0000589 }
590 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500591
Roger Meierca142b02011-06-07 17:59:07 +0000592 if (!protocol_type.empty()) {
593 if (protocol_type == "binary") {
Roger Meier284101c2014-03-11 21:20:35 +0100594 } else if (protocol_type == "compact") {
Roger Meierca142b02011-06-07 17:59:07 +0000595 } else if (protocol_type == "json") {
Dave Watson792db4e2015-01-16 11:22:01 -0800596 } else if (protocol_type == "header") {
Roger Meierca142b02011-06-07 17:59:07 +0000597 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100598 throw invalid_argument("Unknown protocol type " + protocol_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000599 }
600 }
601
Roger Meier284101c2014-03-11 21:20:35 +0100602 if (!transport_type.empty()) {
Roger Meierca142b02011-06-07 17:59:07 +0000603 if (transport_type == "buffered") {
604 } else if (transport_type == "framed") {
605 } else if (transport_type == "http") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000606 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100607 throw invalid_argument("Unknown transport type " + transport_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000608 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000609 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000610
Bryan Duxbury833ae492010-09-27 17:26:02 +0000611 } catch (std::exception& e) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000612 cerr << e.what() << endl;
Roger Meierca142b02011-06-07 17:59:07 +0000613 cout << desc << "\n";
614 return 1;
Marc Slemko6be374b2006-08-04 03:16:25 +0000615 }
616
Roger Meierca142b02011-06-07 17:59:07 +0000617 if (vm.count("ssl")) {
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000618 ssl = true;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000619 }
620
pavlodd08f6e2015-10-08 16:43:56 -0400621 if (vm.count("abstract-namespace")) {
622 abstract_namespace = true;
623 }
624
Mark Sleee8540632006-05-30 09:24:40 +0000625 // Dispatcher
Roger Meier611f90c2011-12-11 22:08:51 +0000626 boost::shared_ptr<TProtocolFactory> protocolFactory;
Roger Meierca142b02011-06-07 17:59:07 +0000627 if (protocol_type == "json") {
Roger Meier611f90c2011-12-11 22:08:51 +0000628 boost::shared_ptr<TProtocolFactory> jsonProtocolFactory(new TJSONProtocolFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000629 protocolFactory = jsonProtocolFactory;
Roger Meier023192f2014-02-12 09:35:12 +0100630 } else if (protocol_type == "compact") {
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900631 TCompactProtocolFactoryT<TBufferBase> *compactProtocolFactory = new TCompactProtocolFactoryT<TBufferBase>();
632 compactProtocolFactory->setContainerSizeLimit(container_limit);
633 compactProtocolFactory->setStringSizeLimit(string_limit);
634 protocolFactory.reset(compactProtocolFactory);
Dave Watson792db4e2015-01-16 11:22:01 -0800635 } else if (protocol_type == "header") {
636 boost::shared_ptr<TProtocolFactory> headerProtocolFactory(new THeaderProtocolFactory());
637 protocolFactory = headerProtocolFactory;
Roger Meierca142b02011-06-07 17:59:07 +0000638 } else {
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900639 TBinaryProtocolFactoryT<TBufferBase>* binaryProtocolFactory = new TBinaryProtocolFactoryT<TBufferBase>();
640 binaryProtocolFactory->setContainerSizeLimit(container_limit);
641 binaryProtocolFactory->setStringSizeLimit(string_limit);
642 protocolFactory.reset(binaryProtocolFactory);
Roger Meierca142b02011-06-07 17:59:07 +0000643 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000644
Roger Meierca142b02011-06-07 17:59:07 +0000645 // Processor
Roger Meier611f90c2011-12-11 22:08:51 +0000646 boost::shared_ptr<TestHandler> testHandler(new TestHandler());
647 boost::shared_ptr<ThriftTestProcessor> testProcessor(new ThriftTestProcessor(testHandler));
Jake Farrell5d02b802014-01-07 21:42:01 -0500648
Roger Meierca142b02011-06-07 17:59:07 +0000649 if (vm.count("processor-events")) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100650 testProcessor->setEventHandler(
651 boost::shared_ptr<TProcessorEventHandler>(new TestProcessorEventHandler()));
David Reissd7192062010-10-06 17:09:33 +0000652 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500653
Mark Sleee8540632006-05-30 09:24:40 +0000654 // Transport
Roger Meier611f90c2011-12-11 22:08:51 +0000655 boost::shared_ptr<TSSLSocketFactory> sslSocketFactory;
656 boost::shared_ptr<TServerSocket> serverSocket;
Mark Sleee8540632006-05-30 09:24:40 +0000657
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000658 if (ssl) {
Roger Meier611f90c2011-12-11 22:08:51 +0000659 sslSocketFactory = boost::shared_ptr<TSSLSocketFactory>(new TSSLSocketFactory());
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +0530660 sslSocketFactory->loadCertificate((dir_path + "../keys/server.crt").c_str());
661 sslSocketFactory->loadPrivateKey((dir_path + "../keys/server.key").c_str());
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000662 sslSocketFactory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
Roger Meier611f90c2011-12-11 22:08:51 +0000663 serverSocket = boost::shared_ptr<TServerSocket>(new TSSLServerSocket(port, sslSocketFactory));
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000664 } else {
Roger Meierc94b2932014-02-22 20:07:33 +0100665 if (domain_socket != "") {
pavlodd08f6e2015-10-08 16:43:56 -0400666 if (abstract_namespace) {
667 std::string abstract_socket("\0", 1);
668 abstract_socket += domain_socket;
669 serverSocket = boost::shared_ptr<TServerSocket>(new TServerSocket(abstract_socket));
670 } else {
671 unlink(domain_socket.c_str());
672 serverSocket = boost::shared_ptr<TServerSocket>(new TServerSocket(domain_socket));
673 }
Roger Meierc94b2932014-02-22 20:07:33 +0100674 port = 0;
675 } else {
Roger Meier611f90c2011-12-11 22:08:51 +0000676 serverSocket = boost::shared_ptr<TServerSocket>(new TServerSocket(port));
Roger Meierc94b2932014-02-22 20:07:33 +0100677 }
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000678 }
Roger Meierca142b02011-06-07 17:59:07 +0000679
Mark Sleed788b2e2006-09-07 01:26:35 +0000680 // Factory
Roger Meier611f90c2011-12-11 22:08:51 +0000681 boost::shared_ptr<TTransportFactory> transportFactory;
Jake Farrell5d02b802014-01-07 21:42:01 -0500682
Roger Meier7e056e72011-07-17 07:28:28 +0000683 if (transport_type == "http" && server_type != "nonblocking") {
Jake Farrell5d02b802014-01-07 21:42:01 -0500684 boost::shared_ptr<TTransportFactory> httpTransportFactory(new THttpServerTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000685 transportFactory = httpTransportFactory;
686 } else if (transport_type == "framed") {
Jake Farrell5d02b802014-01-07 21:42:01 -0500687 boost::shared_ptr<TTransportFactory> framedTransportFactory(new TFramedTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000688 transportFactory = framedTransportFactory;
689 } else {
Jake Farrell5d02b802014-01-07 21:42:01 -0500690 boost::shared_ptr<TTransportFactory> bufferedTransportFactory(new TBufferedTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000691 transportFactory = bufferedTransportFactory;
692 }
Mark Sleed788b2e2006-09-07 01:26:35 +0000693
Roger Meierca142b02011-06-07 17:59:07 +0000694 // Server Info
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100695 cout << "Starting \"" << server_type << "\" server (" << transport_type << "/" << protocol_type
pavlodd08f6e2015-10-08 16:43:56 -0400696 << ") listen on: ";
697 if (abstract_namespace) {
698 cout << '@';
699 }
700 cout << domain_socket;
Roger Meierca142b02011-06-07 17:59:07 +0000701 if (port != 0) {
702 cout << port;
703 }
704 cout << endl;
Mark Sleee8540632006-05-30 09:24:40 +0000705
Roger Meierca142b02011-06-07 17:59:07 +0000706 // Server
Jake Farrell5d02b802014-01-07 21:42:01 -0500707 boost::shared_ptr<apache::thrift::server::TServer> server;
708
Roger Meierca142b02011-06-07 17:59:07 +0000709 if (server_type == "simple") {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100710 server.reset(new TSimpleServer(testProcessor, serverSocket, transportFactory, protocolFactory));
Roger Meierca142b02011-06-07 17:59:07 +0000711 } else if (server_type == "thread-pool") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000712
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100713 boost::shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workers);
Marc Slemko6be374b2006-08-04 03:16:25 +0000714
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100715 boost::shared_ptr<PlatformThreadFactory> threadFactory
716 = boost::shared_ptr<PlatformThreadFactory>(new PlatformThreadFactory());
Marc Slemko6be374b2006-08-04 03:16:25 +0000717
718 threadManager->threadFactory(threadFactory);
719
720 threadManager->start();
721
Jake Farrell5d02b802014-01-07 21:42:01 -0500722 server.reset(new TThreadPoolServer(testProcessor,
Roger Meierca142b02011-06-07 17:59:07 +0000723 serverSocket,
Mark Sleed788b2e2006-09-07 01:26:35 +0000724 transportFactory,
Mark Sleea3302652006-10-25 19:03:32 +0000725 protocolFactory,
Jake Farrell5d02b802014-01-07 21:42:01 -0500726 threadManager));
Roger Meierca142b02011-06-07 17:59:07 +0000727 } else if (server_type == "threaded") {
David Reiss0c90f6f2008-02-06 22:18:40 +0000728
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100729 server.reset(
730 new TThreadedServer(testProcessor, serverSocket, transportFactory, protocolFactory));
Roger Meierca142b02011-06-07 17:59:07 +0000731 } else if (server_type == "nonblocking") {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100732 if (transport_type == "http") {
Roger Meier611f90c2011-12-11 22:08:51 +0000733 boost::shared_ptr<TestHandlerAsync> testHandlerAsync(new TestHandlerAsync(testHandler));
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100734 boost::shared_ptr<TAsyncProcessor> testProcessorAsync(
735 new ThriftTestAsyncProcessor(testHandlerAsync));
736 boost::shared_ptr<TAsyncBufferProcessor> testBufferProcessor(
737 new TAsyncProtocolProcessor(testProcessorAsync, protocolFactory));
Jake Farrell5d02b802014-01-07 21:42:01 -0500738
739 // not loading nonblockingServer into "server" because
740 // TEvhttpServer doesn't inherit from TServer, and doesn't
741 // provide a stop method.
Roger Meier7e056e72011-07-17 07:28:28 +0000742 TEvhttpServer nonblockingServer(testBufferProcessor, port);
743 nonblockingServer.serve();
Jake Farrell5d02b802014-01-07 21:42:01 -0500744 } else {
Dave Watson792db4e2015-01-16 11:22:01 -0800745 server.reset(new TNonblockingServer(testProcessor, protocolFactory, port));
Roger Meier7e056e72011-07-17 07:28:28 +0000746 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000747 }
748
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100749 if (server.get() != NULL) {
Dave Watson792db4e2015-01-16 11:22:01 -0800750 if (protocol_type == "header") {
751 // Tell the server to use the same protocol for input / output
752 // if using header
753 server->setOutputProtocolFactory(boost::shared_ptr<TProtocolFactory>());
754 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500755 apache::thrift::concurrency::PlatformThreadFactory factory;
756 factory.setDetached(false);
757 boost::shared_ptr<apache::thrift::concurrency::Runnable> serverThreadRunner(server);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100758 boost::shared_ptr<apache::thrift::concurrency::Thread> thread
759 = factory.newThread(serverThreadRunner);
Jake Farrell5d02b802014-01-07 21:42:01 -0500760 thread->start();
761
Roger Meier284101c2014-03-11 21:20:35 +0100762 // HACK: cross language test suite is unable to handle cin properly
763 // that's why we stay in a endless loop here
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100764 while (1) {
765 }
Roger Meier284101c2014-03-11 21:20:35 +0100766 // FIXME: find another way to stop the server (e.g. a signal)
767 // cout<<"Press enter to stop the server."<<endl;
768 // cin.ignore(); //wait until a key is pressed
Jake Farrell5d02b802014-01-07 21:42:01 -0500769
770 server->stop();
771 thread->join();
772 server.reset();
773 }
774
Roger Meierca142b02011-06-07 17:59:07 +0000775 cout << "done." << endl;
Mark Sleee8540632006-05-30 09:24:40 +0000776 return 0;
777}