blob: 6d2a260f0665eeb04654c802a7f6768061f51641 [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>
Roger Meier49ff8b12012-04-13 09:12:31 +000027#include <thrift/protocol/TJSONProtocol.h>
28#include <thrift/server/TSimpleServer.h>
29#include <thrift/server/TThreadedServer.h>
30#include <thrift/server/TThreadPoolServer.h>
31#include <thrift/async/TEvhttpServer.h>
32#include <thrift/async/TAsyncBufferProcessor.h>
33#include <thrift/async/TAsyncProtocolProcessor.h>
34#include <thrift/server/TNonblockingServer.h>
35#include <thrift/transport/TServerSocket.h>
36#include <thrift/transport/TSSLServerSocket.h>
37#include <thrift/transport/TSSLSocket.h>
38#include <thrift/transport/THttpServer.h>
39#include <thrift/transport/THttpTransport.h>
40#include <thrift/transport/TTransportUtils.h>
Mark Slee95771002006-06-07 06:53:25 +000041#include "ThriftTest.h"
Marc Slemko6be374b2006-08-04 03:16:25 +000042
43#include <iostream>
44#include <stdexcept>
45#include <sstream>
46
Roger Meierca142b02011-06-07 17:59:07 +000047#include <boost/program_options.hpp>
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +053048#include <boost/filesystem.hpp>
Roger Meier0a7c69c2014-05-02 21:15:45 +020049#include <thrift/cxxfunctional.h>
Roger Meierca142b02011-06-07 17:59:07 +000050
Bryan Duxburycd9aea12011-02-22 18:12:06 +000051#include <signal.h>
Jake Farrell5d02b802014-01-07 21:42:01 -050052#if _WIN32
Konrad Grochowski16a23a62014-11-13 15:33:38 +010053#include <thrift/windows/TWinsockSingleton.h>
Jake Farrell5d02b802014-01-07 21:42:01 -050054#endif
David Reissbc3dddb2007-08-22 23:20:24 +000055
Mark Sleee8540632006-05-30 09:24:40 +000056using namespace std;
57
T Jake Lucianib5e62212009-01-31 22:36:20 +000058using namespace apache::thrift;
59using namespace apache::thrift::concurrency;
60using namespace apache::thrift::protocol;
61using namespace apache::thrift::transport;
62using namespace apache::thrift::server;
Roger Meier7e056e72011-07-17 07:28:28 +000063using namespace apache::thrift::async;
Marc Slemko6be374b2006-08-04 03:16:25 +000064
Marc Slemkobf4fd192006-08-15 21:29:39 +000065using namespace thrift::test;
66
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +053067// Length of argv[0] - Length of script dir
68#define EXECUTABLE_FILE_NAME_LENGTH 19
69
Mark Sleed2655522006-09-05 22:09:57 +000070class TestHandler : public ThriftTestIf {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010071public:
Mark Sleed2655522006-09-05 22:09:57 +000072 TestHandler() {}
Mark Sleee8540632006-05-30 09:24:40 +000073
Konrad Grochowski16a23a62014-11-13 15:33:38 +010074 void testVoid() { printf("testVoid()\n"); }
Mark Sleee8540632006-05-30 09:24:40 +000075
Konrad Grochowski16a23a62014-11-13 15:33:38 +010076 void testString(string& out, const string& thing) {
Mark Sleee8540632006-05-30 09:24:40 +000077 printf("testString(\"%s\")\n", thing.c_str());
Mark Slee1921d202007-01-24 19:43:06 +000078 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +000079 }
80
Mark Slee1921d202007-01-24 19:43:06 +000081 int8_t testByte(const int8_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +000082 printf("testByte(%d)\n", (int)thing);
83 return thing;
84 }
85
Mark Slee1921d202007-01-24 19:43:06 +000086 int32_t testI32(const int32_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +000087 printf("testI32(%d)\n", thing);
88 return thing;
89 }
90
Mark Slee1921d202007-01-24 19:43:06 +000091 int64_t testI64(const int64_t thing) {
Roger Meier0e814802014-01-17 21:07:58 +010092 printf("testI64(%" PRId64 ")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +000093 return thing;
94 }
95
Mark Slee1921d202007-01-24 19:43:06 +000096 double testDouble(const double thing) {
Roger Meiera8cef6e2011-07-17 18:55:59 +000097 printf("testDouble(%f)\n", thing);
Mark Sleec98d0502006-09-06 02:42:25 +000098 return thing;
99 }
100
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100101 void testStruct(Xtruct& out, const Xtruct& thing) {
102 printf("testStruct({\"%s\", %d, %d, %" PRId64 "})\n",
103 thing.string_thing.c_str(),
104 (int)thing.byte_thing,
105 thing.i32_thing,
106 thing.i64_thing);
Mark Slee1921d202007-01-24 19:43:06 +0000107 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000108 }
109
Mark Slee1921d202007-01-24 19:43:06 +0000110 void testNest(Xtruct2& out, const Xtruct2& nest) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100111 const Xtruct& thing = nest.struct_thing;
112 printf("testNest({%d, {\"%s\", %d, %d, %" PRId64 "}, %d})\n",
113 (int)nest.byte_thing,
114 thing.string_thing.c_str(),
115 (int)thing.byte_thing,
116 thing.i32_thing,
117 thing.i64_thing,
118 nest.i32_thing);
Mark Slee1921d202007-01-24 19:43:06 +0000119 out = nest;
Mark Sleee8540632006-05-30 09:24:40 +0000120 }
121
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100122 void testMap(map<int32_t, int32_t>& out, const map<int32_t, int32_t>& thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000123 printf("testMap({");
124 map<int32_t, int32_t>::const_iterator m_iter;
125 bool first = true;
126 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
127 if (first) {
128 first = false;
129 } else {
130 printf(", ");
131 }
132 printf("%d => %d", m_iter->first, m_iter->second);
133 }
134 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000135 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000136 }
137
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100138 void testStringMap(map<std::string, std::string>& out,
139 const map<std::string, std::string>& thing) {
Roger Meierd3b9dca2011-06-24 14:01:10 +0000140 printf("testMap({");
141 map<std::string, std::string>::const_iterator m_iter;
142 bool first = true;
143 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
144 if (first) {
145 first = false;
146 } else {
147 printf(", ");
148 }
149 printf("%s => %s", (m_iter->first).c_str(), (m_iter->second).c_str());
150 }
151 printf("})\n");
152 out = thing;
153 }
154
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100155 void testSet(set<int32_t>& out, const set<int32_t>& thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000156 printf("testSet({");
157 set<int32_t>::const_iterator s_iter;
158 bool first = true;
159 for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) {
160 if (first) {
161 first = false;
162 } else {
163 printf(", ");
164 }
165 printf("%d", *s_iter);
166 }
167 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000168 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000169 }
170
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100171 void testList(vector<int32_t>& out, const vector<int32_t>& thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000172 printf("testList({");
Mark Sleeb9acf982006-10-10 01:57:32 +0000173 vector<int32_t>::const_iterator l_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000174 bool first = true;
175 for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) {
176 if (first) {
177 first = false;
178 } else {
179 printf(", ");
180 }
181 printf("%d", *l_iter);
182 }
183 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000184 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000185 }
186
Bryan Duxbury833ae492010-09-27 17:26:02 +0000187 Numberz::type testEnum(const Numberz::type thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000188 printf("testEnum(%d)\n", thing);
189 return thing;
190 }
191
Mark Slee1921d202007-01-24 19:43:06 +0000192 UserId testTypedef(const UserId thing) {
Roger Meier0e814802014-01-17 21:07:58 +0100193 printf("testTypedef(%" PRId64 ")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +0000194 return thing;
195 }
196
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100197 void testMapMap(map<int32_t, map<int32_t, int32_t> >& mapmap, const int32_t hello) {
Mark Sleee8540632006-05-30 09:24:40 +0000198 printf("testMapMap(%d)\n", hello);
Mark Sleee8540632006-05-30 09:24:40 +0000199
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100200 map<int32_t, int32_t> pos;
201 map<int32_t, int32_t> neg;
Mark Sleee8540632006-05-30 09:24:40 +0000202 for (int i = 1; i < 5; i++) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100203 pos.insert(make_pair(i, i));
204 neg.insert(make_pair(-i, -i));
Mark Sleee8540632006-05-30 09:24:40 +0000205 }
206
207 mapmap.insert(make_pair(4, pos));
208 mapmap.insert(make_pair(-4, neg));
Mark Sleee8540632006-05-30 09:24:40 +0000209 }
210
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100211 void testInsanity(map<UserId, map<Numberz::type, Insanity> >& insane, const Insanity& argument) {
212 (void)argument;
Mark Sleee8540632006-05-30 09:24:40 +0000213 printf("testInsanity()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000214
Mark Sleee8540632006-05-30 09:24:40 +0000215 Xtruct hello;
216 hello.string_thing = "Hello2";
217 hello.byte_thing = 2;
Mark Sleee8540632006-05-30 09:24:40 +0000218 hello.i32_thing = 2;
Mark Sleee8540632006-05-30 09:24:40 +0000219 hello.i64_thing = 2;
220
221 Xtruct goodbye;
222 goodbye.string_thing = "Goodbye4";
223 goodbye.byte_thing = 4;
Mark Sleee8540632006-05-30 09:24:40 +0000224 goodbye.i32_thing = 4;
Mark Sleee8540632006-05-30 09:24:40 +0000225 goodbye.i64_thing = 4;
226
227 Insanity crazy;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000228 crazy.userMap.insert(make_pair(Numberz::EIGHT, 8));
Mark Sleee8540632006-05-30 09:24:40 +0000229 crazy.xtructs.push_back(goodbye);
230
231 Insanity looney;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000232 crazy.userMap.insert(make_pair(Numberz::FIVE, 5));
Mark Sleee8540632006-05-30 09:24:40 +0000233 crazy.xtructs.push_back(hello);
234
Bryan Duxbury833ae492010-09-27 17:26:02 +0000235 map<Numberz::type, Insanity> first_map;
236 map<Numberz::type, Insanity> second_map;
Mark Sleee8540632006-05-30 09:24:40 +0000237
Bryan Duxbury833ae492010-09-27 17:26:02 +0000238 first_map.insert(make_pair(Numberz::TWO, crazy));
239 first_map.insert(make_pair(Numberz::THREE, crazy));
Mark Sleee8540632006-05-30 09:24:40 +0000240
Bryan Duxbury833ae492010-09-27 17:26:02 +0000241 second_map.insert(make_pair(Numberz::SIX, looney));
Mark Sleee8540632006-05-30 09:24:40 +0000242
Mark Sleee8540632006-05-30 09:24:40 +0000243 insane.insert(make_pair(1, first_map));
244 insane.insert(make_pair(2, second_map));
245
246 printf("return");
247 printf(" = {");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100248 map<UserId, map<Numberz::type, Insanity> >::const_iterator i_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000249 for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) {
Roger Meier0e814802014-01-17 21:07:58 +0100250 printf("%" PRId64 " => {", i_iter->first);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100251 map<Numberz::type, Insanity>::const_iterator i2_iter;
252 for (i2_iter = i_iter->second.begin(); i2_iter != i_iter->second.end(); ++i2_iter) {
Mark Sleee8540632006-05-30 09:24:40 +0000253 printf("%d => {", i2_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000254 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
255 map<Numberz::type, UserId>::const_iterator um;
Mark Sleee8540632006-05-30 09:24:40 +0000256 printf("{");
257 for (um = userMap.begin(); um != userMap.end(); ++um) {
Roger Meier0e814802014-01-17 21:07:58 +0100258 printf("%d => %" PRId64 ", ", um->first, um->second);
Mark Sleee8540632006-05-30 09:24:40 +0000259 }
260 printf("}, ");
261
Mark Sleeb9acf982006-10-10 01:57:32 +0000262 vector<Xtruct> xtructs = i2_iter->second.xtructs;
263 vector<Xtruct>::const_iterator x;
Mark Sleee8540632006-05-30 09:24:40 +0000264 printf("{");
265 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100266 printf("{\"%s\", %d, %d, %" PRId64 "}, ",
267 x->string_thing.c_str(),
268 (int)x->byte_thing,
269 x->i32_thing,
270 x->i64_thing);
Mark Sleee8540632006-05-30 09:24:40 +0000271 }
272 printf("}");
273
274 printf("}, ");
275 }
276 printf("}, ");
277 }
278 printf("}\n");
Mark Sleee8540632006-05-30 09:24:40 +0000279 }
Marc Slemkoe6889de2006-08-12 00:32:53 +0000280
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100281 void testMulti(Xtruct& hello,
282 const int8_t arg0,
283 const int32_t arg1,
284 const int64_t arg2,
285 const std::map<int16_t, std::string>& arg3,
286 const Numberz::type arg4,
287 const UserId arg5) {
288 (void)arg3;
289 (void)arg4;
290 (void)arg5;
Jake Farrell5d02b802014-01-07 21:42:01 -0500291
Marc Slemkoe6889de2006-08-12 00:32:53 +0000292 printf("testMulti()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000293
Marc Slemkoe6889de2006-08-12 00:32:53 +0000294 hello.string_thing = "Hello2";
295 hello.byte_thing = arg0;
296 hello.i32_thing = arg1;
297 hello.i64_thing = (int64_t)arg2;
Marc Slemkoe6889de2006-08-12 00:32:53 +0000298 }
Marc Slemkobf4fd192006-08-15 21:29:39 +0000299
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100300 void testException(const std::string& arg) throw(Xception, apache::thrift::TException) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000301 printf("testException(%s)\n", arg.c_str());
Mark Sleed3d733a2006-09-01 22:19:06 +0000302 if (arg.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000303 Xception e;
304 e.errorCode = 1001;
David Reiss9813cbe2009-01-31 21:39:11 +0000305 e.message = arg;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000306 throw e;
Roger Meier1f8b48f2012-05-02 22:56:47 +0000307 } else if (arg.compare("TException") == 0) {
T Jake Lucianib5e62212009-01-31 22:36:20 +0000308 apache::thrift::TException e;
David Reiss55ff70f2008-06-11 00:58:25 +0000309 throw e;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000310 } else {
311 Xtruct result;
312 result.string_thing = arg;
313 return;
314 }
315 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000316
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100317 void testMultiException(Xtruct& result,
318 const std::string& arg0,
319 const std::string& arg1) throw(Xception, Xception2) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000320
Marc Slemko71d4e472006-08-15 22:34:04 +0000321 printf("testMultiException(%s, %s)\n", arg0.c_str(), arg1.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000322
Mark Sleef5f2be42006-09-05 21:05:31 +0000323 if (arg0.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000324 Xception e;
325 e.errorCode = 1001;
326 e.message = "This is an Xception";
327 throw e;
Mark Sleef5f2be42006-09-05 21:05:31 +0000328 } else if (arg0.compare("Xception2") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000329 Xception2 e;
330 e.errorCode = 2002;
331 e.struct_thing.string_thing = "This is an Xception2";
332 throw e;
333 } else {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000334 result.string_thing = arg1;
Mark Slee1921d202007-01-24 19:43:06 +0000335 return;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000336 }
337 }
David Reiss2ab6fe82008-02-18 02:11:44 +0000338
Jake Farrell5d02b802014-01-07 21:42:01 -0500339 void testOneway(const int32_t sleepFor) {
David Reiss6ce401d2009-03-24 20:01:58 +0000340 printf("testOneway(%d): Sleeping...\n", sleepFor);
Jake Farrell5d02b802014-01-07 21:42:01 -0500341 THRIFT_SLEEP_SEC(sleepFor);
David Reiss6ce401d2009-03-24 20:01:58 +0000342 printf("testOneway(%d): done sleeping!\n", sleepFor);
David Reiss2ab6fe82008-02-18 02:11:44 +0000343 }
Mark Sleee8540632006-05-30 09:24:40 +0000344};
345
David Reissd7192062010-10-06 17:09:33 +0000346class TestProcessorEventHandler : public TProcessorEventHandler {
David Reiss23248712010-10-06 17:10:08 +0000347 virtual void* getContext(const char* fn_name, void* serverContext) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100348 (void)serverContext;
David Reissd7192062010-10-06 17:09:33 +0000349 return new std::string(fn_name);
350 }
351 virtual void freeContext(void* ctx, const char* fn_name) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100352 (void)fn_name;
David Reissd7192062010-10-06 17:09:33 +0000353 delete static_cast<std::string*>(ctx);
354 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100355 virtual void preRead(void* ctx, const char* fn_name) { communicate("preRead", ctx, fn_name); }
David Reissef7200f2010-10-06 17:09:42 +0000356 virtual void postRead(void* ctx, const char* fn_name, uint32_t bytes) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100357 (void)bytes;
David Reissd7192062010-10-06 17:09:33 +0000358 communicate("postRead", ctx, fn_name);
359 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100360 virtual void preWrite(void* ctx, const char* fn_name) { communicate("preWrite", ctx, fn_name); }
David Reissef7200f2010-10-06 17:09:42 +0000361 virtual void postWrite(void* ctx, const char* fn_name, uint32_t bytes) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100362 (void)bytes;
David Reissd7192062010-10-06 17:09:33 +0000363 communicate("postWrite", ctx, fn_name);
364 }
365 virtual void asyncComplete(void* ctx, const char* fn_name) {
366 communicate("asyncComplete", ctx, fn_name);
367 }
368 virtual void handlerError(void* ctx, const char* fn_name) {
369 communicate("handlerError", ctx, fn_name);
370 }
371
372 void communicate(const char* event, void* ctx, const char* fn_name) {
373 std::cout << event << ": " << *static_cast<std::string*>(ctx) << " = " << fn_name << std::endl;
374 }
375};
376
Roger Meier7e056e72011-07-17 07:28:28 +0000377class TestHandlerAsync : public ThriftTestCobSvIf {
378public:
Roger Meier611f90c2011-12-11 22:08:51 +0000379 TestHandlerAsync(boost::shared_ptr<TestHandler>& handler) : _delegate(handler) {}
Roger Meier7e056e72011-07-17 07:28:28 +0000380 virtual ~TestHandlerAsync() {}
381
Roger Meier0a7c69c2014-05-02 21:15:45 +0200382 virtual void testVoid(tcxx::function<void()> cob) {
Roger Meier7e056e72011-07-17 07:28:28 +0000383 _delegate->testVoid();
384 cob();
385 }
386
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100387 virtual void testString(tcxx::function<void(std::string const& _return)> cob,
388 const std::string& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000389 std::string res;
390 _delegate->testString(res, thing);
391 cob(res);
392 }
393
Roger Meier0a7c69c2014-05-02 21:15:45 +0200394 virtual void testByte(tcxx::function<void(int8_t const& _return)> cob, const int8_t thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000395 int8_t res = _delegate->testByte(thing);
396 cob(res);
397 }
398
Roger Meier0a7c69c2014-05-02 21:15:45 +0200399 virtual void testI32(tcxx::function<void(int32_t const& _return)> cob, const int32_t thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000400 int32_t res = _delegate->testI32(thing);
401 cob(res);
402 }
403
Roger Meier0a7c69c2014-05-02 21:15:45 +0200404 virtual void testI64(tcxx::function<void(int64_t const& _return)> cob, const int64_t thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000405 int64_t res = _delegate->testI64(thing);
406 cob(res);
407 }
408
Roger Meier0a7c69c2014-05-02 21:15:45 +0200409 virtual void testDouble(tcxx::function<void(double const& _return)> cob, const double thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000410 double res = _delegate->testDouble(thing);
411 cob(res);
412 }
413
Roger Meier0a7c69c2014-05-02 21:15:45 +0200414 virtual void testStruct(tcxx::function<void(Xtruct const& _return)> cob, const Xtruct& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000415 Xtruct res;
416 _delegate->testStruct(res, thing);
417 cob(res);
418 }
419
Roger Meier0a7c69c2014-05-02 21:15:45 +0200420 virtual void testNest(tcxx::function<void(Xtruct2 const& _return)> cob, const Xtruct2& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000421 Xtruct2 res;
422 _delegate->testNest(res, thing);
423 cob(res);
424 }
425
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100426 virtual void testMap(tcxx::function<void(std::map<int32_t, int32_t> const& _return)> cob,
427 const std::map<int32_t, int32_t>& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000428 std::map<int32_t, int32_t> res;
429 _delegate->testMap(res, thing);
430 cob(res);
431 }
432
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100433 virtual void testStringMap(
434 tcxx::function<void(std::map<std::string, std::string> const& _return)> cob,
435 const std::map<std::string, std::string>& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000436 std::map<std::string, std::string> res;
437 _delegate->testStringMap(res, thing);
438 cob(res);
439 }
440
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100441 virtual void testSet(tcxx::function<void(std::set<int32_t> const& _return)> cob,
442 const std::set<int32_t>& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000443 std::set<int32_t> res;
444 _delegate->testSet(res, thing);
445 cob(res);
446 }
447
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100448 virtual void testList(tcxx::function<void(std::vector<int32_t> const& _return)> cob,
449 const std::vector<int32_t>& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000450 std::vector<int32_t> res;
451 _delegate->testList(res, thing);
452 cob(res);
453 }
454
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100455 virtual void testEnum(tcxx::function<void(Numberz::type const& _return)> cob,
456 const Numberz::type thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000457 Numberz::type res = _delegate->testEnum(thing);
458 cob(res);
459 }
460
Roger Meier0a7c69c2014-05-02 21:15:45 +0200461 virtual void testTypedef(tcxx::function<void(UserId const& _return)> cob, const UserId thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000462 UserId res = _delegate->testTypedef(thing);
463 cob(res);
464 }
465
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100466 virtual void testMapMap(
467 tcxx::function<void(std::map<int32_t, std::map<int32_t, int32_t> > const& _return)> cob,
468 const int32_t hello) {
Roger Meier7e056e72011-07-17 07:28:28 +0000469 std::map<int32_t, std::map<int32_t, int32_t> > res;
470 _delegate->testMapMap(res, hello);
471 cob(res);
472 }
473
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100474 virtual void testInsanity(
475 tcxx::function<void(std::map<UserId, std::map<Numberz::type, Insanity> > const& _return)> cob,
476 const Insanity& argument) {
Jake Farrell5d02b802014-01-07 21:42:01 -0500477 std::map<UserId, std::map<Numberz::type, Insanity> > res;
Roger Meier7e056e72011-07-17 07:28:28 +0000478 _delegate->testInsanity(res, argument);
479 cob(res);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100480 }
Roger Meier7e056e72011-07-17 07:28:28 +0000481
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100482 virtual void testMulti(tcxx::function<void(Xtruct const& _return)> cob,
483 const int8_t arg0,
484 const int32_t arg1,
485 const int64_t arg2,
486 const std::map<int16_t, std::string>& arg3,
487 const Numberz::type arg4,
488 const UserId arg5) {
Roger Meier7e056e72011-07-17 07:28:28 +0000489 Xtruct res;
490 _delegate->testMulti(res, arg0, arg1, arg2, arg3, arg4, arg5);
491 cob(res);
492 }
493
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100494 virtual void testException(
495 tcxx::function<void()> cob,
496 tcxx::function<void(::apache::thrift::TDelayedException* _throw)> exn_cob,
497 const std::string& arg) {
Roger Meier7e056e72011-07-17 07:28:28 +0000498 try {
499 _delegate->testException(arg);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100500 } catch (const apache::thrift::TException& e) {
Roger Meier7e056e72011-07-17 07:28:28 +0000501 exn_cob(apache::thrift::TDelayedException::delayException(e));
502 return;
503 }
504 cob();
505 }
506
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100507 virtual void testMultiException(
508 tcxx::function<void(Xtruct const& _return)> cob,
509 tcxx::function<void(::apache::thrift::TDelayedException* _throw)> exn_cob,
510 const std::string& arg0,
511 const std::string& arg1) {
Roger Meier7e056e72011-07-17 07:28:28 +0000512 Xtruct res;
513 try {
514 _delegate->testMultiException(res, arg0, arg1);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100515 } catch (const apache::thrift::TException& e) {
Roger Meier7e056e72011-07-17 07:28:28 +0000516 exn_cob(apache::thrift::TDelayedException::delayException(e));
517 return;
518 }
519 cob(res);
520 }
521
Roger Meier0a7c69c2014-05-02 21:15:45 +0200522 virtual void testOneway(tcxx::function<void()> cob, const int32_t secondsToSleep) {
Roger Meier7e056e72011-07-17 07:28:28 +0000523 _delegate->testOneway(secondsToSleep);
524 cob();
525 }
526
527protected:
Roger Meier611f90c2011-12-11 22:08:51 +0000528 boost::shared_ptr<TestHandler> _delegate;
Roger Meier7e056e72011-07-17 07:28:28 +0000529};
530
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100531int main(int argc, char** argv) {
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +0530532
533 string file_path = boost::filesystem::system_complete(argv[0]).string();
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100534 string dir_path = file_path.substr(0, file_path.size() - EXECUTABLE_FILE_NAME_LENGTH);
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +0530535
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100536#if _WIN32
Jake Farrell5d02b802014-01-07 21:42:01 -0500537 transport::TWinsockSingleton::create();
538#endif
Mark Sleee8540632006-05-30 09:24:40 +0000539 int port = 9090;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000540 bool ssl = false;
Roger Meierca142b02011-06-07 17:59:07 +0000541 string transport_type = "buffered";
542 string protocol_type = "binary";
543 string server_type = "simple";
544 string domain_socket = "";
545 size_t workers = 4;
Marc Slemko6be374b2006-08-04 03:16:25 +0000546
Jake Farrell5d02b802014-01-07 21:42:01 -0500547 boost::program_options::options_description desc("Allowed options");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100548 desc.add_options()("help,h", "produce help message")(
549 "port",
550 boost::program_options::value<int>(&port)->default_value(port),
551 "Port number to listen")("domain-socket",
552 boost::program_options::value<string>(&domain_socket)
553 ->default_value(domain_socket),
554 "Unix Domain Socket (e.g. /tmp/ThriftTest.thrift)")(
555 "server-type",
556 boost::program_options::value<string>(&server_type)->default_value(server_type),
557 "type of server, \"simple\", \"thread-pool\", \"threaded\", or \"nonblocking\"")(
558 "transport",
559 boost::program_options::value<string>(&transport_type)->default_value(transport_type),
560 "transport: buffered, framed, http")(
561 "protocol",
562 boost::program_options::value<string>(&protocol_type)->default_value(protocol_type),
563 "protocol: binary, compact, json")("ssl", "Encrypted Transport using SSL")(
564 "processor-events",
565 "processor-events")("workers,n",
566 boost::program_options::value<size_t>(&workers)->default_value(workers),
567 "Number of thread pools workers. Only valid for thread-pool server type");
Marc Slemko6be374b2006-08-04 03:16:25 +0000568
Jake Farrell5d02b802014-01-07 21:42:01 -0500569 boost::program_options::variables_map vm;
570 boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
571 boost::program_options::notify(vm);
Marc Slemko6be374b2006-08-04 03:16:25 +0000572
Roger Meierca142b02011-06-07 17:59:07 +0000573 if (vm.count("help")) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100574 cout << desc << "\n";
575 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000576 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500577
Marc Slemko6be374b2006-08-04 03:16:25 +0000578 try {
Roger Meierca142b02011-06-07 17:59:07 +0000579 if (!server_type.empty()) {
580 if (server_type == "simple") {
581 } else if (server_type == "thread-pool") {
582 } else if (server_type == "threaded") {
583 } else if (server_type == "nonblocking") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000584 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100585 throw invalid_argument("Unknown server type " + server_type);
Roger Meierca142b02011-06-07 17:59:07 +0000586 }
587 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500588
Roger Meierca142b02011-06-07 17:59:07 +0000589 if (!protocol_type.empty()) {
590 if (protocol_type == "binary") {
Roger Meier284101c2014-03-11 21:20:35 +0100591 } else if (protocol_type == "compact") {
Roger Meierca142b02011-06-07 17:59:07 +0000592 } else if (protocol_type == "json") {
593 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100594 throw invalid_argument("Unknown protocol type " + protocol_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000595 }
596 }
597
Roger Meier284101c2014-03-11 21:20:35 +0100598 if (!transport_type.empty()) {
Roger Meierca142b02011-06-07 17:59:07 +0000599 if (transport_type == "buffered") {
600 } else if (transport_type == "framed") {
601 } else if (transport_type == "http") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000602 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100603 throw invalid_argument("Unknown transport type " + transport_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000604 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000605 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000606
Bryan Duxbury833ae492010-09-27 17:26:02 +0000607 } catch (std::exception& e) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000608 cerr << e.what() << endl;
Roger Meierca142b02011-06-07 17:59:07 +0000609 cout << desc << "\n";
610 return 1;
Marc Slemko6be374b2006-08-04 03:16:25 +0000611 }
612
Roger Meierca142b02011-06-07 17:59:07 +0000613 if (vm.count("ssl")) {
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000614 ssl = true;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000615 }
616
Mark Sleee8540632006-05-30 09:24:40 +0000617 // Dispatcher
Roger Meier611f90c2011-12-11 22:08:51 +0000618 boost::shared_ptr<TProtocolFactory> protocolFactory;
Roger Meierca142b02011-06-07 17:59:07 +0000619 if (protocol_type == "json") {
Roger Meier611f90c2011-12-11 22:08:51 +0000620 boost::shared_ptr<TProtocolFactory> jsonProtocolFactory(new TJSONProtocolFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000621 protocolFactory = jsonProtocolFactory;
Roger Meier023192f2014-02-12 09:35:12 +0100622 } else if (protocol_type == "compact") {
623 boost::shared_ptr<TProtocolFactory> compactProtocolFactory(new TCompactProtocolFactory());
624 protocolFactory = compactProtocolFactory;
Roger Meierca142b02011-06-07 17:59:07 +0000625 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100626 boost::shared_ptr<TProtocolFactory> binaryProtocolFactory(
627 new TBinaryProtocolFactoryT<TBufferBase>());
Roger Meierca142b02011-06-07 17:59:07 +0000628 protocolFactory = binaryProtocolFactory;
629 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000630
Roger Meierca142b02011-06-07 17:59:07 +0000631 // Processor
Roger Meier611f90c2011-12-11 22:08:51 +0000632 boost::shared_ptr<TestHandler> testHandler(new TestHandler());
633 boost::shared_ptr<ThriftTestProcessor> testProcessor(new ThriftTestProcessor(testHandler));
Jake Farrell5d02b802014-01-07 21:42:01 -0500634
Roger Meierca142b02011-06-07 17:59:07 +0000635 if (vm.count("processor-events")) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100636 testProcessor->setEventHandler(
637 boost::shared_ptr<TProcessorEventHandler>(new TestProcessorEventHandler()));
David Reissd7192062010-10-06 17:09:33 +0000638 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500639
Mark Sleee8540632006-05-30 09:24:40 +0000640 // Transport
Roger Meier611f90c2011-12-11 22:08:51 +0000641 boost::shared_ptr<TSSLSocketFactory> sslSocketFactory;
642 boost::shared_ptr<TServerSocket> serverSocket;
Mark Sleee8540632006-05-30 09:24:40 +0000643
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000644 if (ssl) {
Roger Meier611f90c2011-12-11 22:08:51 +0000645 sslSocketFactory = boost::shared_ptr<TSSLSocketFactory>(new TSSLSocketFactory());
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +0530646 sslSocketFactory->loadCertificate((dir_path + "../keys/server.crt").c_str());
647 sslSocketFactory->loadPrivateKey((dir_path + "../keys/server.key").c_str());
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000648 sslSocketFactory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
Roger Meier611f90c2011-12-11 22:08:51 +0000649 serverSocket = boost::shared_ptr<TServerSocket>(new TSSLServerSocket(port, sslSocketFactory));
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000650 } else {
Roger Meierc94b2932014-02-22 20:07:33 +0100651 if (domain_socket != "") {
652 unlink(domain_socket.c_str());
653 serverSocket = boost::shared_ptr<TServerSocket>(new TServerSocket(domain_socket));
654 port = 0;
655 } else {
Roger Meier611f90c2011-12-11 22:08:51 +0000656 serverSocket = boost::shared_ptr<TServerSocket>(new TServerSocket(port));
Roger Meierc94b2932014-02-22 20:07:33 +0100657 }
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000658 }
Roger Meierca142b02011-06-07 17:59:07 +0000659
Mark Sleed788b2e2006-09-07 01:26:35 +0000660 // Factory
Roger Meier611f90c2011-12-11 22:08:51 +0000661 boost::shared_ptr<TTransportFactory> transportFactory;
Jake Farrell5d02b802014-01-07 21:42:01 -0500662
Roger Meier7e056e72011-07-17 07:28:28 +0000663 if (transport_type == "http" && server_type != "nonblocking") {
Jake Farrell5d02b802014-01-07 21:42:01 -0500664 boost::shared_ptr<TTransportFactory> httpTransportFactory(new THttpServerTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000665 transportFactory = httpTransportFactory;
666 } else if (transport_type == "framed") {
Jake Farrell5d02b802014-01-07 21:42:01 -0500667 boost::shared_ptr<TTransportFactory> framedTransportFactory(new TFramedTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000668 transportFactory = framedTransportFactory;
669 } else {
Jake Farrell5d02b802014-01-07 21:42:01 -0500670 boost::shared_ptr<TTransportFactory> bufferedTransportFactory(new TBufferedTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000671 transportFactory = bufferedTransportFactory;
672 }
Mark Sleed788b2e2006-09-07 01:26:35 +0000673
Roger Meierca142b02011-06-07 17:59:07 +0000674 // Server Info
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100675 cout << "Starting \"" << server_type << "\" server (" << transport_type << "/" << protocol_type
676 << ") listen on: " << domain_socket;
Roger Meierca142b02011-06-07 17:59:07 +0000677 if (port != 0) {
678 cout << port;
679 }
680 cout << endl;
Mark Sleee8540632006-05-30 09:24:40 +0000681
Roger Meierca142b02011-06-07 17:59:07 +0000682 // Server
Jake Farrell5d02b802014-01-07 21:42:01 -0500683 boost::shared_ptr<apache::thrift::server::TServer> server;
684
Roger Meierca142b02011-06-07 17:59:07 +0000685 if (server_type == "simple") {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100686 server.reset(new TSimpleServer(testProcessor, serverSocket, transportFactory, protocolFactory));
Roger Meierca142b02011-06-07 17:59:07 +0000687 } else if (server_type == "thread-pool") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000688
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100689 boost::shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workers);
Marc Slemko6be374b2006-08-04 03:16:25 +0000690
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100691 boost::shared_ptr<PlatformThreadFactory> threadFactory
692 = boost::shared_ptr<PlatformThreadFactory>(new PlatformThreadFactory());
Marc Slemko6be374b2006-08-04 03:16:25 +0000693
694 threadManager->threadFactory(threadFactory);
695
696 threadManager->start();
697
Jake Farrell5d02b802014-01-07 21:42:01 -0500698 server.reset(new TThreadPoolServer(testProcessor,
Roger Meierca142b02011-06-07 17:59:07 +0000699 serverSocket,
Mark Sleed788b2e2006-09-07 01:26:35 +0000700 transportFactory,
Mark Sleea3302652006-10-25 19:03:32 +0000701 protocolFactory,
Jake Farrell5d02b802014-01-07 21:42:01 -0500702 threadManager));
Roger Meierca142b02011-06-07 17:59:07 +0000703 } else if (server_type == "threaded") {
David Reiss0c90f6f2008-02-06 22:18:40 +0000704
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100705 server.reset(
706 new TThreadedServer(testProcessor, serverSocket, transportFactory, protocolFactory));
Roger Meierca142b02011-06-07 17:59:07 +0000707 } else if (server_type == "nonblocking") {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100708 if (transport_type == "http") {
Roger Meier611f90c2011-12-11 22:08:51 +0000709 boost::shared_ptr<TestHandlerAsync> testHandlerAsync(new TestHandlerAsync(testHandler));
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100710 boost::shared_ptr<TAsyncProcessor> testProcessorAsync(
711 new ThriftTestAsyncProcessor(testHandlerAsync));
712 boost::shared_ptr<TAsyncBufferProcessor> testBufferProcessor(
713 new TAsyncProtocolProcessor(testProcessorAsync, protocolFactory));
Jake Farrell5d02b802014-01-07 21:42:01 -0500714
715 // not loading nonblockingServer into "server" because
716 // TEvhttpServer doesn't inherit from TServer, and doesn't
717 // provide a stop method.
Roger Meier7e056e72011-07-17 07:28:28 +0000718 TEvhttpServer nonblockingServer(testBufferProcessor, port);
719 nonblockingServer.serve();
Jake Farrell5d02b802014-01-07 21:42:01 -0500720 } else {
721 server.reset(new TNonblockingServer(testProcessor, port));
Roger Meier7e056e72011-07-17 07:28:28 +0000722 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000723 }
724
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100725 if (server.get() != NULL) {
Jake Farrell5d02b802014-01-07 21:42:01 -0500726 apache::thrift::concurrency::PlatformThreadFactory factory;
727 factory.setDetached(false);
728 boost::shared_ptr<apache::thrift::concurrency::Runnable> serverThreadRunner(server);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100729 boost::shared_ptr<apache::thrift::concurrency::Thread> thread
730 = factory.newThread(serverThreadRunner);
Jake Farrell5d02b802014-01-07 21:42:01 -0500731 thread->start();
732
Roger Meier284101c2014-03-11 21:20:35 +0100733 // HACK: cross language test suite is unable to handle cin properly
734 // that's why we stay in a endless loop here
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100735 while (1) {
736 }
Roger Meier284101c2014-03-11 21:20:35 +0100737 // FIXME: find another way to stop the server (e.g. a signal)
738 // cout<<"Press enter to stop the server."<<endl;
739 // cin.ignore(); //wait until a key is pressed
Jake Farrell5d02b802014-01-07 21:42:01 -0500740
741 server->stop();
742 thread->join();
743 server.reset();
744 }
745
Roger Meierca142b02011-06-07 17:59:07 +0000746 cout << "done." << endl;
Mark Sleee8540632006-05-30 09:24:40 +0000747 return 0;
748}