blob: 4808d89f4f1aad326db9a8e828779f6a88fb25d7 [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
Mark Sleed2655522006-09-05 22:09:57 +000068class TestHandler : public ThriftTestIf {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010069public:
Mark Sleed2655522006-09-05 22:09:57 +000070 TestHandler() {}
Mark Sleee8540632006-05-30 09:24:40 +000071
Konrad Grochowski16a23a62014-11-13 15:33:38 +010072 void testVoid() { printf("testVoid()\n"); }
Mark Sleee8540632006-05-30 09:24:40 +000073
Konrad Grochowski16a23a62014-11-13 15:33:38 +010074 void testString(string& out, const string& thing) {
Mark Sleee8540632006-05-30 09:24:40 +000075 printf("testString(\"%s\")\n", thing.c_str());
Mark Slee1921d202007-01-24 19:43:06 +000076 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +000077 }
78
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +090079 bool testBool(const bool thing) {
80 printf("testBool(%s)\n", thing ? "true" : "false");
81 return thing;
82 }
83
Mark Slee1921d202007-01-24 19:43:06 +000084 int8_t testByte(const int8_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +000085 printf("testByte(%d)\n", (int)thing);
86 return thing;
87 }
88
Mark Slee1921d202007-01-24 19:43:06 +000089 int32_t testI32(const int32_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +000090 printf("testI32(%d)\n", thing);
91 return thing;
92 }
93
Mark Slee1921d202007-01-24 19:43:06 +000094 int64_t testI64(const int64_t thing) {
Roger Meier0e814802014-01-17 21:07:58 +010095 printf("testI64(%" PRId64 ")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +000096 return thing;
97 }
98
Mark Slee1921d202007-01-24 19:43:06 +000099 double testDouble(const double thing) {
Roger Meiera8cef6e2011-07-17 18:55:59 +0000100 printf("testDouble(%f)\n", thing);
Mark Sleec98d0502006-09-06 02:42:25 +0000101 return thing;
102 }
103
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100104 void testBinary(std::string& _return, const std::string& thing) {
105 std::ostringstream hexstr;
106 hexstr << std::hex << thing;
107 printf("testBinary(%s)\n", hexstr.str().c_str());
108 _return = thing;
109 }
110
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100111 void testStruct(Xtruct& out, const Xtruct& thing) {
112 printf("testStruct({\"%s\", %d, %d, %" PRId64 "})\n",
113 thing.string_thing.c_str(),
114 (int)thing.byte_thing,
115 thing.i32_thing,
116 thing.i64_thing);
Mark Slee1921d202007-01-24 19:43:06 +0000117 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000118 }
119
Mark Slee1921d202007-01-24 19:43:06 +0000120 void testNest(Xtruct2& out, const Xtruct2& nest) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100121 const Xtruct& thing = nest.struct_thing;
122 printf("testNest({%d, {\"%s\", %d, %d, %" PRId64 "}, %d})\n",
123 (int)nest.byte_thing,
124 thing.string_thing.c_str(),
125 (int)thing.byte_thing,
126 thing.i32_thing,
127 thing.i64_thing,
128 nest.i32_thing);
Mark Slee1921d202007-01-24 19:43:06 +0000129 out = nest;
Mark Sleee8540632006-05-30 09:24:40 +0000130 }
131
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100132 void testMap(map<int32_t, int32_t>& out, const map<int32_t, int32_t>& thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000133 printf("testMap({");
134 map<int32_t, int32_t>::const_iterator m_iter;
135 bool first = true;
136 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
137 if (first) {
138 first = false;
139 } else {
140 printf(", ");
141 }
142 printf("%d => %d", m_iter->first, m_iter->second);
143 }
144 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000145 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000146 }
147
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100148 void testStringMap(map<std::string, std::string>& out,
149 const map<std::string, std::string>& thing) {
Roger Meierd3b9dca2011-06-24 14:01:10 +0000150 printf("testMap({");
151 map<std::string, std::string>::const_iterator m_iter;
152 bool first = true;
153 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
154 if (first) {
155 first = false;
156 } else {
157 printf(", ");
158 }
159 printf("%s => %s", (m_iter->first).c_str(), (m_iter->second).c_str());
160 }
161 printf("})\n");
162 out = thing;
163 }
164
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100165 void testSet(set<int32_t>& out, const set<int32_t>& thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000166 printf("testSet({");
167 set<int32_t>::const_iterator s_iter;
168 bool first = true;
169 for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) {
170 if (first) {
171 first = false;
172 } else {
173 printf(", ");
174 }
175 printf("%d", *s_iter);
176 }
177 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000178 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000179 }
180
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100181 void testList(vector<int32_t>& out, const vector<int32_t>& thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000182 printf("testList({");
Mark Sleeb9acf982006-10-10 01:57:32 +0000183 vector<int32_t>::const_iterator l_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000184 bool first = true;
185 for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) {
186 if (first) {
187 first = false;
188 } else {
189 printf(", ");
190 }
191 printf("%d", *l_iter);
192 }
193 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000194 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000195 }
196
Bryan Duxbury833ae492010-09-27 17:26:02 +0000197 Numberz::type testEnum(const Numberz::type thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000198 printf("testEnum(%d)\n", thing);
199 return thing;
200 }
201
Mark Slee1921d202007-01-24 19:43:06 +0000202 UserId testTypedef(const UserId thing) {
Roger Meier0e814802014-01-17 21:07:58 +0100203 printf("testTypedef(%" PRId64 ")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +0000204 return thing;
205 }
206
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100207 void testMapMap(map<int32_t, map<int32_t, int32_t> >& mapmap, const int32_t hello) {
Mark Sleee8540632006-05-30 09:24:40 +0000208 printf("testMapMap(%d)\n", hello);
Mark Sleee8540632006-05-30 09:24:40 +0000209
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100210 map<int32_t, int32_t> pos;
211 map<int32_t, int32_t> neg;
Mark Sleee8540632006-05-30 09:24:40 +0000212 for (int i = 1; i < 5; i++) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100213 pos.insert(make_pair(i, i));
214 neg.insert(make_pair(-i, -i));
Mark Sleee8540632006-05-30 09:24:40 +0000215 }
216
217 mapmap.insert(make_pair(4, pos));
218 mapmap.insert(make_pair(-4, neg));
Mark Sleee8540632006-05-30 09:24:40 +0000219 }
220
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100221 void testInsanity(map<UserId, map<Numberz::type, Insanity> >& insane, const Insanity& argument) {
Mark Sleee8540632006-05-30 09:24:40 +0000222 printf("testInsanity()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000223
Mark Sleee8540632006-05-30 09:24:40 +0000224 Insanity looney;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000225 map<Numberz::type, Insanity> first_map;
226 map<Numberz::type, Insanity> second_map;
Mark Sleee8540632006-05-30 09:24:40 +0000227
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900228 first_map.insert(make_pair(Numberz::TWO, argument));
229 first_map.insert(make_pair(Numberz::THREE, argument));
Mark Sleee8540632006-05-30 09:24:40 +0000230
Bryan Duxbury833ae492010-09-27 17:26:02 +0000231 second_map.insert(make_pair(Numberz::SIX, looney));
Mark Sleee8540632006-05-30 09:24:40 +0000232
Mark Sleee8540632006-05-30 09:24:40 +0000233 insane.insert(make_pair(1, first_map));
234 insane.insert(make_pair(2, second_map));
235
236 printf("return");
237 printf(" = {");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100238 map<UserId, map<Numberz::type, Insanity> >::const_iterator i_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000239 for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) {
Roger Meier0e814802014-01-17 21:07:58 +0100240 printf("%" PRId64 " => {", i_iter->first);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100241 map<Numberz::type, Insanity>::const_iterator i2_iter;
242 for (i2_iter = i_iter->second.begin(); i2_iter != i_iter->second.end(); ++i2_iter) {
Mark Sleee8540632006-05-30 09:24:40 +0000243 printf("%d => {", i2_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000244 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
245 map<Numberz::type, UserId>::const_iterator um;
Mark Sleee8540632006-05-30 09:24:40 +0000246 printf("{");
247 for (um = userMap.begin(); um != userMap.end(); ++um) {
Roger Meier0e814802014-01-17 21:07:58 +0100248 printf("%d => %" PRId64 ", ", um->first, um->second);
Mark Sleee8540632006-05-30 09:24:40 +0000249 }
250 printf("}, ");
251
Mark Sleeb9acf982006-10-10 01:57:32 +0000252 vector<Xtruct> xtructs = i2_iter->second.xtructs;
253 vector<Xtruct>::const_iterator x;
Mark Sleee8540632006-05-30 09:24:40 +0000254 printf("{");
255 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100256 printf("{\"%s\", %d, %d, %" PRId64 "}, ",
257 x->string_thing.c_str(),
258 (int)x->byte_thing,
259 x->i32_thing,
260 x->i64_thing);
Mark Sleee8540632006-05-30 09:24:40 +0000261 }
262 printf("}");
263
264 printf("}, ");
265 }
266 printf("}, ");
267 }
268 printf("}\n");
Mark Sleee8540632006-05-30 09:24:40 +0000269 }
Marc Slemkoe6889de2006-08-12 00:32:53 +0000270
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100271 void testMulti(Xtruct& hello,
272 const int8_t arg0,
273 const int32_t arg1,
274 const int64_t arg2,
275 const std::map<int16_t, std::string>& arg3,
276 const Numberz::type arg4,
277 const UserId arg5) {
278 (void)arg3;
279 (void)arg4;
280 (void)arg5;
Jake Farrell5d02b802014-01-07 21:42:01 -0500281
Marc Slemkoe6889de2006-08-12 00:32:53 +0000282 printf("testMulti()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000283
Marc Slemkoe6889de2006-08-12 00:32:53 +0000284 hello.string_thing = "Hello2";
285 hello.byte_thing = arg0;
286 hello.i32_thing = arg1;
287 hello.i64_thing = (int64_t)arg2;
Marc Slemkoe6889de2006-08-12 00:32:53 +0000288 }
Marc Slemkobf4fd192006-08-15 21:29:39 +0000289
ben-craigfae08e72015-07-15 11:34:47 -0500290 void testException(const std::string& arg) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000291 printf("testException(%s)\n", arg.c_str());
Mark Sleed3d733a2006-09-01 22:19:06 +0000292 if (arg.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000293 Xception e;
294 e.errorCode = 1001;
David Reiss9813cbe2009-01-31 21:39:11 +0000295 e.message = arg;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000296 throw e;
Roger Meier1f8b48f2012-05-02 22:56:47 +0000297 } else if (arg.compare("TException") == 0) {
T Jake Lucianib5e62212009-01-31 22:36:20 +0000298 apache::thrift::TException e;
David Reiss55ff70f2008-06-11 00:58:25 +0000299 throw e;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000300 } else {
301 Xtruct result;
302 result.string_thing = arg;
303 return;
304 }
305 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000306
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100307 void testMultiException(Xtruct& result,
308 const std::string& arg0,
ben-craigfae08e72015-07-15 11:34:47 -0500309 const std::string& arg1) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000310
Marc Slemko71d4e472006-08-15 22:34:04 +0000311 printf("testMultiException(%s, %s)\n", arg0.c_str(), arg1.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000312
Mark Sleef5f2be42006-09-05 21:05:31 +0000313 if (arg0.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000314 Xception e;
315 e.errorCode = 1001;
316 e.message = "This is an Xception";
317 throw e;
Mark Sleef5f2be42006-09-05 21:05:31 +0000318 } else if (arg0.compare("Xception2") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000319 Xception2 e;
320 e.errorCode = 2002;
321 e.struct_thing.string_thing = "This is an Xception2";
322 throw e;
323 } else {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000324 result.string_thing = arg1;
Mark Slee1921d202007-01-24 19:43:06 +0000325 return;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000326 }
327 }
David Reiss2ab6fe82008-02-18 02:11:44 +0000328
Jake Farrell5d02b802014-01-07 21:42:01 -0500329 void testOneway(const int32_t sleepFor) {
David Reiss6ce401d2009-03-24 20:01:58 +0000330 printf("testOneway(%d): Sleeping...\n", sleepFor);
Jake Farrell5d02b802014-01-07 21:42:01 -0500331 THRIFT_SLEEP_SEC(sleepFor);
David Reiss6ce401d2009-03-24 20:01:58 +0000332 printf("testOneway(%d): done sleeping!\n", sleepFor);
David Reiss2ab6fe82008-02-18 02:11:44 +0000333 }
Mark Sleee8540632006-05-30 09:24:40 +0000334};
335
David Reissd7192062010-10-06 17:09:33 +0000336class TestProcessorEventHandler : public TProcessorEventHandler {
David Reiss23248712010-10-06 17:10:08 +0000337 virtual void* getContext(const char* fn_name, void* serverContext) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100338 (void)serverContext;
David Reissd7192062010-10-06 17:09:33 +0000339 return new std::string(fn_name);
340 }
341 virtual void freeContext(void* ctx, const char* fn_name) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100342 (void)fn_name;
David Reissd7192062010-10-06 17:09:33 +0000343 delete static_cast<std::string*>(ctx);
344 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100345 virtual void preRead(void* ctx, const char* fn_name) { communicate("preRead", ctx, fn_name); }
David Reissef7200f2010-10-06 17:09:42 +0000346 virtual void postRead(void* ctx, const char* fn_name, uint32_t bytes) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100347 (void)bytes;
David Reissd7192062010-10-06 17:09:33 +0000348 communicate("postRead", ctx, fn_name);
349 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100350 virtual void preWrite(void* ctx, const char* fn_name) { communicate("preWrite", ctx, fn_name); }
David Reissef7200f2010-10-06 17:09:42 +0000351 virtual void postWrite(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("postWrite", ctx, fn_name);
354 }
355 virtual void asyncComplete(void* ctx, const char* fn_name) {
356 communicate("asyncComplete", ctx, fn_name);
357 }
358 virtual void handlerError(void* ctx, const char* fn_name) {
359 communicate("handlerError", ctx, fn_name);
360 }
361
362 void communicate(const char* event, void* ctx, const char* fn_name) {
363 std::cout << event << ": " << *static_cast<std::string*>(ctx) << " = " << fn_name << std::endl;
364 }
365};
366
Roger Meier7e056e72011-07-17 07:28:28 +0000367class TestHandlerAsync : public ThriftTestCobSvIf {
368public:
Roger Meier611f90c2011-12-11 22:08:51 +0000369 TestHandlerAsync(boost::shared_ptr<TestHandler>& handler) : _delegate(handler) {}
Roger Meier7e056e72011-07-17 07:28:28 +0000370 virtual ~TestHandlerAsync() {}
371
Roger Meier0a7c69c2014-05-02 21:15:45 +0200372 virtual void testVoid(tcxx::function<void()> cob) {
Roger Meier7e056e72011-07-17 07:28:28 +0000373 _delegate->testVoid();
374 cob();
375 }
376
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100377 virtual void testString(tcxx::function<void(std::string const& _return)> cob,
378 const std::string& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000379 std::string res;
380 _delegate->testString(res, thing);
381 cob(res);
382 }
383
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900384 virtual void testBool(tcxx::function<void(bool const& _return)> cob, const bool thing) {
385 bool res = _delegate->testBool(thing);
386 cob(res);
387 }
388
Roger Meier0a7c69c2014-05-02 21:15:45 +0200389 virtual void testByte(tcxx::function<void(int8_t const& _return)> cob, const int8_t thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000390 int8_t res = _delegate->testByte(thing);
391 cob(res);
392 }
393
Roger Meier0a7c69c2014-05-02 21:15:45 +0200394 virtual void testI32(tcxx::function<void(int32_t const& _return)> cob, const int32_t thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000395 int32_t res = _delegate->testI32(thing);
396 cob(res);
397 }
398
Roger Meier0a7c69c2014-05-02 21:15:45 +0200399 virtual void testI64(tcxx::function<void(int64_t const& _return)> cob, const int64_t thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000400 int64_t res = _delegate->testI64(thing);
401 cob(res);
402 }
403
Roger Meier0a7c69c2014-05-02 21:15:45 +0200404 virtual void testDouble(tcxx::function<void(double const& _return)> cob, const double thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000405 double res = _delegate->testDouble(thing);
406 cob(res);
407 }
408
Konrad Grochowski1f6e3802015-05-18 18:10:06 +0200409 virtual void testBinary(tcxx::function<void(std::string const& _return)> cob,
410 const std::string& thing) {
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100411 std::string res;
412 _delegate->testBinary(res, thing);
413 cob(res);
414 }
415
Roger Meier0a7c69c2014-05-02 21:15:45 +0200416 virtual void testStruct(tcxx::function<void(Xtruct const& _return)> cob, const Xtruct& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000417 Xtruct res;
418 _delegate->testStruct(res, thing);
419 cob(res);
420 }
421
Roger Meier0a7c69c2014-05-02 21:15:45 +0200422 virtual void testNest(tcxx::function<void(Xtruct2 const& _return)> cob, const Xtruct2& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000423 Xtruct2 res;
424 _delegate->testNest(res, thing);
425 cob(res);
426 }
427
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100428 virtual void testMap(tcxx::function<void(std::map<int32_t, int32_t> const& _return)> cob,
429 const std::map<int32_t, int32_t>& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000430 std::map<int32_t, int32_t> res;
431 _delegate->testMap(res, thing);
432 cob(res);
433 }
434
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100435 virtual void testStringMap(
436 tcxx::function<void(std::map<std::string, std::string> const& _return)> cob,
437 const std::map<std::string, std::string>& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000438 std::map<std::string, std::string> res;
439 _delegate->testStringMap(res, thing);
440 cob(res);
441 }
442
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100443 virtual void testSet(tcxx::function<void(std::set<int32_t> const& _return)> cob,
444 const std::set<int32_t>& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000445 std::set<int32_t> res;
446 _delegate->testSet(res, thing);
447 cob(res);
448 }
449
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100450 virtual void testList(tcxx::function<void(std::vector<int32_t> const& _return)> cob,
451 const std::vector<int32_t>& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000452 std::vector<int32_t> res;
453 _delegate->testList(res, thing);
454 cob(res);
455 }
456
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100457 virtual void testEnum(tcxx::function<void(Numberz::type const& _return)> cob,
458 const Numberz::type thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000459 Numberz::type res = _delegate->testEnum(thing);
460 cob(res);
461 }
462
Roger Meier0a7c69c2014-05-02 21:15:45 +0200463 virtual void testTypedef(tcxx::function<void(UserId const& _return)> cob, const UserId thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000464 UserId res = _delegate->testTypedef(thing);
465 cob(res);
466 }
467
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100468 virtual void testMapMap(
469 tcxx::function<void(std::map<int32_t, std::map<int32_t, int32_t> > const& _return)> cob,
470 const int32_t hello) {
Roger Meier7e056e72011-07-17 07:28:28 +0000471 std::map<int32_t, std::map<int32_t, int32_t> > res;
472 _delegate->testMapMap(res, hello);
473 cob(res);
474 }
475
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100476 virtual void testInsanity(
477 tcxx::function<void(std::map<UserId, std::map<Numberz::type, Insanity> > const& _return)> cob,
478 const Insanity& argument) {
Jake Farrell5d02b802014-01-07 21:42:01 -0500479 std::map<UserId, std::map<Numberz::type, Insanity> > res;
Roger Meier7e056e72011-07-17 07:28:28 +0000480 _delegate->testInsanity(res, argument);
481 cob(res);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100482 }
Roger Meier7e056e72011-07-17 07:28:28 +0000483
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100484 virtual void testMulti(tcxx::function<void(Xtruct const& _return)> cob,
485 const int8_t arg0,
486 const int32_t arg1,
487 const int64_t arg2,
488 const std::map<int16_t, std::string>& arg3,
489 const Numberz::type arg4,
490 const UserId arg5) {
Roger Meier7e056e72011-07-17 07:28:28 +0000491 Xtruct res;
492 _delegate->testMulti(res, arg0, arg1, arg2, arg3, arg4, arg5);
493 cob(res);
494 }
495
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100496 virtual void testException(
497 tcxx::function<void()> cob,
498 tcxx::function<void(::apache::thrift::TDelayedException* _throw)> exn_cob,
499 const std::string& arg) {
Roger Meier7e056e72011-07-17 07:28:28 +0000500 try {
501 _delegate->testException(arg);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100502 } catch (const apache::thrift::TException& e) {
Roger Meier7e056e72011-07-17 07:28:28 +0000503 exn_cob(apache::thrift::TDelayedException::delayException(e));
504 return;
505 }
506 cob();
507 }
508
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100509 virtual void testMultiException(
510 tcxx::function<void(Xtruct const& _return)> cob,
511 tcxx::function<void(::apache::thrift::TDelayedException* _throw)> exn_cob,
512 const std::string& arg0,
513 const std::string& arg1) {
Roger Meier7e056e72011-07-17 07:28:28 +0000514 Xtruct res;
515 try {
516 _delegate->testMultiException(res, arg0, arg1);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100517 } catch (const apache::thrift::TException& e) {
Roger Meier7e056e72011-07-17 07:28:28 +0000518 exn_cob(apache::thrift::TDelayedException::delayException(e));
519 return;
520 }
521 cob(res);
522 }
523
Roger Meier0a7c69c2014-05-02 21:15:45 +0200524 virtual void testOneway(tcxx::function<void()> cob, const int32_t secondsToSleep) {
Roger Meier7e056e72011-07-17 07:28:28 +0000525 _delegate->testOneway(secondsToSleep);
526 cob();
527 }
528
529protected:
Roger Meier611f90c2011-12-11 22:08:51 +0000530 boost::shared_ptr<TestHandler> _delegate;
Roger Meier7e056e72011-07-17 07:28:28 +0000531};
532
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900533namespace po = boost::program_options;
534
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100535int main(int argc, char** argv) {
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +0530536
Nobuaki Sukegawac8744082016-03-09 19:55:56 +0900537 string testDir = boost::filesystem::system_complete(argv[0]).parent_path().parent_path().parent_path().string();
538 string certPath = testDir + "/keys/server.crt";
539 string keyPath = testDir + "/keys/server.key";
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +0530540
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100541#if _WIN32
Jake Farrell5d02b802014-01-07 21:42:01 -0500542 transport::TWinsockSingleton::create();
543#endif
Mark Sleee8540632006-05-30 09:24:40 +0000544 int port = 9090;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000545 bool ssl = false;
Roger Meierca142b02011-06-07 17:59:07 +0000546 string transport_type = "buffered";
547 string protocol_type = "binary";
548 string server_type = "simple";
549 string domain_socket = "";
pavlodd08f6e2015-10-08 16:43:56 -0400550 bool abstract_namespace = false;
Roger Meierca142b02011-06-07 17:59:07 +0000551 size_t workers = 4;
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900552 int string_limit = 0;
553 int container_limit = 0;
Marc Slemko6be374b2006-08-04 03:16:25 +0000554
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900555 po::options_description desc("Allowed options");
556 desc.add_options()
557 ("help,h", "produce help message")
558 ("port", po::value<int>(&port)->default_value(port), "Port number to listen")
559 ("domain-socket", po::value<string>(&domain_socket) ->default_value(domain_socket), "Unix Domain Socket (e.g. /tmp/ThriftTest.thrift)")
560 ("abstract-namespace", "Create the domain socket in the Abstract Namespace (no connection with filesystem pathnames)")
561 ("server-type", po::value<string>(&server_type)->default_value(server_type), "type of server, \"simple\", \"thread-pool\", \"threaded\", or \"nonblocking\"")
562 ("transport", po::value<string>(&transport_type)->default_value(transport_type), "transport: buffered, framed, http")
563 ("protocol", po::value<string>(&protocol_type)->default_value(protocol_type), "protocol: binary, compact, header, json")
564 ("ssl", "Encrypted Transport using SSL")
565 ("processor-events", "processor-events")
566 ("workers,n", po::value<size_t>(&workers)->default_value(workers), "Number of thread pools workers. Only valid for thread-pool server type")
567 ("string-limit", po::value<int>(&string_limit))
568 ("container-limit", po::value<int>(&container_limit));
Marc Slemko6be374b2006-08-04 03:16:25 +0000569
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900570 po::variables_map vm;
571 po::store(po::parse_command_line(argc, argv, desc), vm);
572 po::notify(vm);
Marc Slemko6be374b2006-08-04 03:16:25 +0000573
Roger Meierca142b02011-06-07 17:59:07 +0000574 if (vm.count("help")) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100575 cout << desc << "\n";
576 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000577 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500578
Marc Slemko6be374b2006-08-04 03:16:25 +0000579 try {
Roger Meierca142b02011-06-07 17:59:07 +0000580 if (!server_type.empty()) {
581 if (server_type == "simple") {
582 } else if (server_type == "thread-pool") {
583 } else if (server_type == "threaded") {
584 } else if (server_type == "nonblocking") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000585 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100586 throw invalid_argument("Unknown server type " + server_type);
Roger Meierca142b02011-06-07 17:59:07 +0000587 }
588 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500589
Roger Meierca142b02011-06-07 17:59:07 +0000590 if (!protocol_type.empty()) {
591 if (protocol_type == "binary") {
Roger Meier284101c2014-03-11 21:20:35 +0100592 } else if (protocol_type == "compact") {
Roger Meierca142b02011-06-07 17:59:07 +0000593 } else if (protocol_type == "json") {
Dave Watson792db4e2015-01-16 11:22:01 -0800594 } else if (protocol_type == "header") {
Roger Meierca142b02011-06-07 17:59:07 +0000595 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100596 throw invalid_argument("Unknown protocol type " + protocol_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000597 }
598 }
599
Roger Meier284101c2014-03-11 21:20:35 +0100600 if (!transport_type.empty()) {
Roger Meierca142b02011-06-07 17:59:07 +0000601 if (transport_type == "buffered") {
602 } else if (transport_type == "framed") {
603 } else if (transport_type == "http") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000604 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100605 throw invalid_argument("Unknown transport type " + transport_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000606 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000607 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000608
Bryan Duxbury833ae492010-09-27 17:26:02 +0000609 } catch (std::exception& e) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000610 cerr << e.what() << endl;
Roger Meierca142b02011-06-07 17:59:07 +0000611 cout << desc << "\n";
612 return 1;
Marc Slemko6be374b2006-08-04 03:16:25 +0000613 }
614
Roger Meierca142b02011-06-07 17:59:07 +0000615 if (vm.count("ssl")) {
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000616 ssl = true;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000617 }
618
pavlodd08f6e2015-10-08 16:43:56 -0400619 if (vm.count("abstract-namespace")) {
620 abstract_namespace = true;
621 }
622
Mark Sleee8540632006-05-30 09:24:40 +0000623 // Dispatcher
Roger Meier611f90c2011-12-11 22:08:51 +0000624 boost::shared_ptr<TProtocolFactory> protocolFactory;
Roger Meierca142b02011-06-07 17:59:07 +0000625 if (protocol_type == "json") {
Roger Meier611f90c2011-12-11 22:08:51 +0000626 boost::shared_ptr<TProtocolFactory> jsonProtocolFactory(new TJSONProtocolFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000627 protocolFactory = jsonProtocolFactory;
Roger Meier023192f2014-02-12 09:35:12 +0100628 } else if (protocol_type == "compact") {
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900629 TCompactProtocolFactoryT<TBufferBase> *compactProtocolFactory = new TCompactProtocolFactoryT<TBufferBase>();
630 compactProtocolFactory->setContainerSizeLimit(container_limit);
631 compactProtocolFactory->setStringSizeLimit(string_limit);
632 protocolFactory.reset(compactProtocolFactory);
Dave Watson792db4e2015-01-16 11:22:01 -0800633 } else if (protocol_type == "header") {
634 boost::shared_ptr<TProtocolFactory> headerProtocolFactory(new THeaderProtocolFactory());
635 protocolFactory = headerProtocolFactory;
Roger Meierca142b02011-06-07 17:59:07 +0000636 } else {
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900637 TBinaryProtocolFactoryT<TBufferBase>* binaryProtocolFactory = new TBinaryProtocolFactoryT<TBufferBase>();
638 binaryProtocolFactory->setContainerSizeLimit(container_limit);
639 binaryProtocolFactory->setStringSizeLimit(string_limit);
640 protocolFactory.reset(binaryProtocolFactory);
Roger Meierca142b02011-06-07 17:59:07 +0000641 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000642
Roger Meierca142b02011-06-07 17:59:07 +0000643 // Processor
Roger Meier611f90c2011-12-11 22:08:51 +0000644 boost::shared_ptr<TestHandler> testHandler(new TestHandler());
645 boost::shared_ptr<ThriftTestProcessor> testProcessor(new ThriftTestProcessor(testHandler));
Jake Farrell5d02b802014-01-07 21:42:01 -0500646
Roger Meierca142b02011-06-07 17:59:07 +0000647 if (vm.count("processor-events")) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100648 testProcessor->setEventHandler(
649 boost::shared_ptr<TProcessorEventHandler>(new TestProcessorEventHandler()));
David Reissd7192062010-10-06 17:09:33 +0000650 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500651
Mark Sleee8540632006-05-30 09:24:40 +0000652 // Transport
Roger Meier611f90c2011-12-11 22:08:51 +0000653 boost::shared_ptr<TSSLSocketFactory> sslSocketFactory;
654 boost::shared_ptr<TServerSocket> serverSocket;
Mark Sleee8540632006-05-30 09:24:40 +0000655
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000656 if (ssl) {
Roger Meier611f90c2011-12-11 22:08:51 +0000657 sslSocketFactory = boost::shared_ptr<TSSLSocketFactory>(new TSSLSocketFactory());
Nobuaki Sukegawac8744082016-03-09 19:55:56 +0900658 sslSocketFactory->loadCertificate(certPath.c_str());
659 sslSocketFactory->loadPrivateKey(keyPath.c_str());
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000660 sslSocketFactory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
Roger Meier611f90c2011-12-11 22:08:51 +0000661 serverSocket = boost::shared_ptr<TServerSocket>(new TSSLServerSocket(port, sslSocketFactory));
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000662 } else {
Roger Meierc94b2932014-02-22 20:07:33 +0100663 if (domain_socket != "") {
pavlodd08f6e2015-10-08 16:43:56 -0400664 if (abstract_namespace) {
665 std::string abstract_socket("\0", 1);
666 abstract_socket += domain_socket;
667 serverSocket = boost::shared_ptr<TServerSocket>(new TServerSocket(abstract_socket));
668 } else {
669 unlink(domain_socket.c_str());
670 serverSocket = boost::shared_ptr<TServerSocket>(new TServerSocket(domain_socket));
671 }
Roger Meierc94b2932014-02-22 20:07:33 +0100672 port = 0;
673 } else {
Roger Meier611f90c2011-12-11 22:08:51 +0000674 serverSocket = boost::shared_ptr<TServerSocket>(new TServerSocket(port));
Roger Meierc94b2932014-02-22 20:07:33 +0100675 }
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000676 }
Roger Meierca142b02011-06-07 17:59:07 +0000677
Mark Sleed788b2e2006-09-07 01:26:35 +0000678 // Factory
Roger Meier611f90c2011-12-11 22:08:51 +0000679 boost::shared_ptr<TTransportFactory> transportFactory;
Jake Farrell5d02b802014-01-07 21:42:01 -0500680
Roger Meier7e056e72011-07-17 07:28:28 +0000681 if (transport_type == "http" && server_type != "nonblocking") {
Jake Farrell5d02b802014-01-07 21:42:01 -0500682 boost::shared_ptr<TTransportFactory> httpTransportFactory(new THttpServerTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000683 transportFactory = httpTransportFactory;
684 } else if (transport_type == "framed") {
Jake Farrell5d02b802014-01-07 21:42:01 -0500685 boost::shared_ptr<TTransportFactory> framedTransportFactory(new TFramedTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000686 transportFactory = framedTransportFactory;
687 } else {
Jake Farrell5d02b802014-01-07 21:42:01 -0500688 boost::shared_ptr<TTransportFactory> bufferedTransportFactory(new TBufferedTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000689 transportFactory = bufferedTransportFactory;
690 }
Mark Sleed788b2e2006-09-07 01:26:35 +0000691
Roger Meierca142b02011-06-07 17:59:07 +0000692 // Server Info
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100693 cout << "Starting \"" << server_type << "\" server (" << transport_type << "/" << protocol_type
pavlodd08f6e2015-10-08 16:43:56 -0400694 << ") listen on: ";
695 if (abstract_namespace) {
696 cout << '@';
697 }
698 cout << domain_socket;
Roger Meierca142b02011-06-07 17:59:07 +0000699 if (port != 0) {
700 cout << port;
701 }
702 cout << endl;
Mark Sleee8540632006-05-30 09:24:40 +0000703
Roger Meierca142b02011-06-07 17:59:07 +0000704 // Server
Jake Farrell5d02b802014-01-07 21:42:01 -0500705 boost::shared_ptr<apache::thrift::server::TServer> server;
706
Roger Meierca142b02011-06-07 17:59:07 +0000707 if (server_type == "simple") {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100708 server.reset(new TSimpleServer(testProcessor, serverSocket, transportFactory, protocolFactory));
Roger Meierca142b02011-06-07 17:59:07 +0000709 } else if (server_type == "thread-pool") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000710
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100711 boost::shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workers);
Marc Slemko6be374b2006-08-04 03:16:25 +0000712
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100713 boost::shared_ptr<PlatformThreadFactory> threadFactory
714 = boost::shared_ptr<PlatformThreadFactory>(new PlatformThreadFactory());
Marc Slemko6be374b2006-08-04 03:16:25 +0000715
716 threadManager->threadFactory(threadFactory);
717
718 threadManager->start();
719
Jake Farrell5d02b802014-01-07 21:42:01 -0500720 server.reset(new TThreadPoolServer(testProcessor,
Roger Meierca142b02011-06-07 17:59:07 +0000721 serverSocket,
Mark Sleed788b2e2006-09-07 01:26:35 +0000722 transportFactory,
Mark Sleea3302652006-10-25 19:03:32 +0000723 protocolFactory,
Jake Farrell5d02b802014-01-07 21:42:01 -0500724 threadManager));
Roger Meierca142b02011-06-07 17:59:07 +0000725 } else if (server_type == "threaded") {
David Reiss0c90f6f2008-02-06 22:18:40 +0000726
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100727 server.reset(
728 new TThreadedServer(testProcessor, serverSocket, transportFactory, protocolFactory));
Roger Meierca142b02011-06-07 17:59:07 +0000729 } else if (server_type == "nonblocking") {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100730 if (transport_type == "http") {
Roger Meier611f90c2011-12-11 22:08:51 +0000731 boost::shared_ptr<TestHandlerAsync> testHandlerAsync(new TestHandlerAsync(testHandler));
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100732 boost::shared_ptr<TAsyncProcessor> testProcessorAsync(
733 new ThriftTestAsyncProcessor(testHandlerAsync));
734 boost::shared_ptr<TAsyncBufferProcessor> testBufferProcessor(
735 new TAsyncProtocolProcessor(testProcessorAsync, protocolFactory));
Jake Farrell5d02b802014-01-07 21:42:01 -0500736
737 // not loading nonblockingServer into "server" because
738 // TEvhttpServer doesn't inherit from TServer, and doesn't
739 // provide a stop method.
Roger Meier7e056e72011-07-17 07:28:28 +0000740 TEvhttpServer nonblockingServer(testBufferProcessor, port);
741 nonblockingServer.serve();
Jake Farrell5d02b802014-01-07 21:42:01 -0500742 } else {
Dave Watson792db4e2015-01-16 11:22:01 -0800743 server.reset(new TNonblockingServer(testProcessor, protocolFactory, port));
Roger Meier7e056e72011-07-17 07:28:28 +0000744 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000745 }
746
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100747 if (server.get() != NULL) {
Dave Watson792db4e2015-01-16 11:22:01 -0800748 if (protocol_type == "header") {
749 // Tell the server to use the same protocol for input / output
750 // if using header
751 server->setOutputProtocolFactory(boost::shared_ptr<TProtocolFactory>());
752 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500753 apache::thrift::concurrency::PlatformThreadFactory factory;
754 factory.setDetached(false);
755 boost::shared_ptr<apache::thrift::concurrency::Runnable> serverThreadRunner(server);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100756 boost::shared_ptr<apache::thrift::concurrency::Thread> thread
757 = factory.newThread(serverThreadRunner);
Jake Farrell5d02b802014-01-07 21:42:01 -0500758 thread->start();
759
Roger Meier284101c2014-03-11 21:20:35 +0100760 // HACK: cross language test suite is unable to handle cin properly
761 // that's why we stay in a endless loop here
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100762 while (1) {
763 }
Roger Meier284101c2014-03-11 21:20:35 +0100764 // FIXME: find another way to stop the server (e.g. a signal)
765 // cout<<"Press enter to stop the server."<<endl;
766 // cin.ignore(); //wait until a key is pressed
Jake Farrell5d02b802014-01-07 21:42:01 -0500767
768 server->stop();
769 thread->join();
770 server.reset();
771 }
772
Roger Meierca142b02011-06-07 17:59:07 +0000773 cout << "done." << endl;
Mark Sleee8540632006-05-30 09:24:40 +0000774 return 0;
775}