blob: 66d3bb2f6031ee67d6f1ff5b4b4705a9d8717f43 [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
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +090081 bool testBool(const bool thing) {
82 printf("testBool(%s)\n", thing ? "true" : "false");
83 return thing;
84 }
85
Mark Slee1921d202007-01-24 19:43:06 +000086 int8_t testByte(const int8_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +000087 printf("testByte(%d)\n", (int)thing);
88 return thing;
89 }
90
Mark Slee1921d202007-01-24 19:43:06 +000091 int32_t testI32(const int32_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +000092 printf("testI32(%d)\n", thing);
93 return thing;
94 }
95
Mark Slee1921d202007-01-24 19:43:06 +000096 int64_t testI64(const int64_t thing) {
Roger Meier0e814802014-01-17 21:07:58 +010097 printf("testI64(%" PRId64 ")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +000098 return thing;
99 }
100
Mark Slee1921d202007-01-24 19:43:06 +0000101 double testDouble(const double thing) {
Roger Meiera8cef6e2011-07-17 18:55:59 +0000102 printf("testDouble(%f)\n", thing);
Mark Sleec98d0502006-09-06 02:42:25 +0000103 return thing;
104 }
105
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100106 void testBinary(std::string& _return, const std::string& thing) {
107 std::ostringstream hexstr;
108 hexstr << std::hex << thing;
109 printf("testBinary(%s)\n", hexstr.str().c_str());
110 _return = thing;
111 }
112
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100113 void testStruct(Xtruct& out, const Xtruct& thing) {
114 printf("testStruct({\"%s\", %d, %d, %" PRId64 "})\n",
115 thing.string_thing.c_str(),
116 (int)thing.byte_thing,
117 thing.i32_thing,
118 thing.i64_thing);
Mark Slee1921d202007-01-24 19:43:06 +0000119 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000120 }
121
Mark Slee1921d202007-01-24 19:43:06 +0000122 void testNest(Xtruct2& out, const Xtruct2& nest) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100123 const Xtruct& thing = nest.struct_thing;
124 printf("testNest({%d, {\"%s\", %d, %d, %" PRId64 "}, %d})\n",
125 (int)nest.byte_thing,
126 thing.string_thing.c_str(),
127 (int)thing.byte_thing,
128 thing.i32_thing,
129 thing.i64_thing,
130 nest.i32_thing);
Mark Slee1921d202007-01-24 19:43:06 +0000131 out = nest;
Mark Sleee8540632006-05-30 09:24:40 +0000132 }
133
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100134 void testMap(map<int32_t, int32_t>& out, const map<int32_t, int32_t>& thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000135 printf("testMap({");
136 map<int32_t, int32_t>::const_iterator m_iter;
137 bool first = true;
138 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
139 if (first) {
140 first = false;
141 } else {
142 printf(", ");
143 }
144 printf("%d => %d", m_iter->first, m_iter->second);
145 }
146 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000147 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000148 }
149
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100150 void testStringMap(map<std::string, std::string>& out,
151 const map<std::string, std::string>& thing) {
Roger Meierd3b9dca2011-06-24 14:01:10 +0000152 printf("testMap({");
153 map<std::string, std::string>::const_iterator m_iter;
154 bool first = true;
155 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
156 if (first) {
157 first = false;
158 } else {
159 printf(", ");
160 }
161 printf("%s => %s", (m_iter->first).c_str(), (m_iter->second).c_str());
162 }
163 printf("})\n");
164 out = thing;
165 }
166
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100167 void testSet(set<int32_t>& out, const set<int32_t>& thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000168 printf("testSet({");
169 set<int32_t>::const_iterator s_iter;
170 bool first = true;
171 for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) {
172 if (first) {
173 first = false;
174 } else {
175 printf(", ");
176 }
177 printf("%d", *s_iter);
178 }
179 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000180 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000181 }
182
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100183 void testList(vector<int32_t>& out, const vector<int32_t>& thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000184 printf("testList({");
Mark Sleeb9acf982006-10-10 01:57:32 +0000185 vector<int32_t>::const_iterator l_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000186 bool first = true;
187 for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) {
188 if (first) {
189 first = false;
190 } else {
191 printf(", ");
192 }
193 printf("%d", *l_iter);
194 }
195 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000196 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000197 }
198
Bryan Duxbury833ae492010-09-27 17:26:02 +0000199 Numberz::type testEnum(const Numberz::type thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000200 printf("testEnum(%d)\n", thing);
201 return thing;
202 }
203
Mark Slee1921d202007-01-24 19:43:06 +0000204 UserId testTypedef(const UserId thing) {
Roger Meier0e814802014-01-17 21:07:58 +0100205 printf("testTypedef(%" PRId64 ")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +0000206 return thing;
207 }
208
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100209 void testMapMap(map<int32_t, map<int32_t, int32_t> >& mapmap, const int32_t hello) {
Mark Sleee8540632006-05-30 09:24:40 +0000210 printf("testMapMap(%d)\n", hello);
Mark Sleee8540632006-05-30 09:24:40 +0000211
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100212 map<int32_t, int32_t> pos;
213 map<int32_t, int32_t> neg;
Mark Sleee8540632006-05-30 09:24:40 +0000214 for (int i = 1; i < 5; i++) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100215 pos.insert(make_pair(i, i));
216 neg.insert(make_pair(-i, -i));
Mark Sleee8540632006-05-30 09:24:40 +0000217 }
218
219 mapmap.insert(make_pair(4, pos));
220 mapmap.insert(make_pair(-4, neg));
Mark Sleee8540632006-05-30 09:24:40 +0000221 }
222
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100223 void testInsanity(map<UserId, map<Numberz::type, Insanity> >& insane, const Insanity& argument) {
Mark Sleee8540632006-05-30 09:24:40 +0000224 printf("testInsanity()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000225
Mark Sleee8540632006-05-30 09:24:40 +0000226 Insanity looney;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000227 map<Numberz::type, Insanity> first_map;
228 map<Numberz::type, Insanity> second_map;
Mark Sleee8540632006-05-30 09:24:40 +0000229
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900230 first_map.insert(make_pair(Numberz::TWO, argument));
231 first_map.insert(make_pair(Numberz::THREE, argument));
Mark Sleee8540632006-05-30 09:24:40 +0000232
Bryan Duxbury833ae492010-09-27 17:26:02 +0000233 second_map.insert(make_pair(Numberz::SIX, looney));
Mark Sleee8540632006-05-30 09:24:40 +0000234
Mark Sleee8540632006-05-30 09:24:40 +0000235 insane.insert(make_pair(1, first_map));
236 insane.insert(make_pair(2, second_map));
237
238 printf("return");
239 printf(" = {");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100240 map<UserId, map<Numberz::type, Insanity> >::const_iterator i_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000241 for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) {
Roger Meier0e814802014-01-17 21:07:58 +0100242 printf("%" PRId64 " => {", i_iter->first);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100243 map<Numberz::type, Insanity>::const_iterator i2_iter;
244 for (i2_iter = i_iter->second.begin(); i2_iter != i_iter->second.end(); ++i2_iter) {
Mark Sleee8540632006-05-30 09:24:40 +0000245 printf("%d => {", i2_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000246 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
247 map<Numberz::type, UserId>::const_iterator um;
Mark Sleee8540632006-05-30 09:24:40 +0000248 printf("{");
249 for (um = userMap.begin(); um != userMap.end(); ++um) {
Roger Meier0e814802014-01-17 21:07:58 +0100250 printf("%d => %" PRId64 ", ", um->first, um->second);
Mark Sleee8540632006-05-30 09:24:40 +0000251 }
252 printf("}, ");
253
Mark Sleeb9acf982006-10-10 01:57:32 +0000254 vector<Xtruct> xtructs = i2_iter->second.xtructs;
255 vector<Xtruct>::const_iterator x;
Mark Sleee8540632006-05-30 09:24:40 +0000256 printf("{");
257 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100258 printf("{\"%s\", %d, %d, %" PRId64 "}, ",
259 x->string_thing.c_str(),
260 (int)x->byte_thing,
261 x->i32_thing,
262 x->i64_thing);
Mark Sleee8540632006-05-30 09:24:40 +0000263 }
264 printf("}");
265
266 printf("}, ");
267 }
268 printf("}, ");
269 }
270 printf("}\n");
Mark Sleee8540632006-05-30 09:24:40 +0000271 }
Marc Slemkoe6889de2006-08-12 00:32:53 +0000272
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100273 void testMulti(Xtruct& hello,
274 const int8_t arg0,
275 const int32_t arg1,
276 const int64_t arg2,
277 const std::map<int16_t, std::string>& arg3,
278 const Numberz::type arg4,
279 const UserId arg5) {
280 (void)arg3;
281 (void)arg4;
282 (void)arg5;
Jake Farrell5d02b802014-01-07 21:42:01 -0500283
Marc Slemkoe6889de2006-08-12 00:32:53 +0000284 printf("testMulti()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000285
Marc Slemkoe6889de2006-08-12 00:32:53 +0000286 hello.string_thing = "Hello2";
287 hello.byte_thing = arg0;
288 hello.i32_thing = arg1;
289 hello.i64_thing = (int64_t)arg2;
Marc Slemkoe6889de2006-08-12 00:32:53 +0000290 }
Marc Slemkobf4fd192006-08-15 21:29:39 +0000291
ben-craigfae08e72015-07-15 11:34:47 -0500292 void testException(const std::string& arg) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000293 printf("testException(%s)\n", arg.c_str());
Mark Sleed3d733a2006-09-01 22:19:06 +0000294 if (arg.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000295 Xception e;
296 e.errorCode = 1001;
David Reiss9813cbe2009-01-31 21:39:11 +0000297 e.message = arg;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000298 throw e;
Roger Meier1f8b48f2012-05-02 22:56:47 +0000299 } else if (arg.compare("TException") == 0) {
T Jake Lucianib5e62212009-01-31 22:36:20 +0000300 apache::thrift::TException e;
David Reiss55ff70f2008-06-11 00:58:25 +0000301 throw e;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000302 } else {
303 Xtruct result;
304 result.string_thing = arg;
305 return;
306 }
307 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000308
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100309 void testMultiException(Xtruct& result,
310 const std::string& arg0,
ben-craigfae08e72015-07-15 11:34:47 -0500311 const std::string& arg1) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000312
Marc Slemko71d4e472006-08-15 22:34:04 +0000313 printf("testMultiException(%s, %s)\n", arg0.c_str(), arg1.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000314
Mark Sleef5f2be42006-09-05 21:05:31 +0000315 if (arg0.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000316 Xception e;
317 e.errorCode = 1001;
318 e.message = "This is an Xception";
319 throw e;
Mark Sleef5f2be42006-09-05 21:05:31 +0000320 } else if (arg0.compare("Xception2") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000321 Xception2 e;
322 e.errorCode = 2002;
323 e.struct_thing.string_thing = "This is an Xception2";
324 throw e;
325 } else {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000326 result.string_thing = arg1;
Mark Slee1921d202007-01-24 19:43:06 +0000327 return;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000328 }
329 }
David Reiss2ab6fe82008-02-18 02:11:44 +0000330
Jake Farrell5d02b802014-01-07 21:42:01 -0500331 void testOneway(const int32_t sleepFor) {
David Reiss6ce401d2009-03-24 20:01:58 +0000332 printf("testOneway(%d): Sleeping...\n", sleepFor);
Jake Farrell5d02b802014-01-07 21:42:01 -0500333 THRIFT_SLEEP_SEC(sleepFor);
David Reiss6ce401d2009-03-24 20:01:58 +0000334 printf("testOneway(%d): done sleeping!\n", sleepFor);
David Reiss2ab6fe82008-02-18 02:11:44 +0000335 }
Mark Sleee8540632006-05-30 09:24:40 +0000336};
337
David Reissd7192062010-10-06 17:09:33 +0000338class TestProcessorEventHandler : public TProcessorEventHandler {
David Reiss23248712010-10-06 17:10:08 +0000339 virtual void* getContext(const char* fn_name, void* serverContext) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100340 (void)serverContext;
David Reissd7192062010-10-06 17:09:33 +0000341 return new std::string(fn_name);
342 }
343 virtual void freeContext(void* ctx, const char* fn_name) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100344 (void)fn_name;
David Reissd7192062010-10-06 17:09:33 +0000345 delete static_cast<std::string*>(ctx);
346 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100347 virtual void preRead(void* ctx, const char* fn_name) { communicate("preRead", ctx, fn_name); }
David Reissef7200f2010-10-06 17:09:42 +0000348 virtual void postRead(void* ctx, const char* fn_name, uint32_t bytes) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100349 (void)bytes;
David Reissd7192062010-10-06 17:09:33 +0000350 communicate("postRead", ctx, fn_name);
351 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100352 virtual void preWrite(void* ctx, const char* fn_name) { communicate("preWrite", ctx, fn_name); }
David Reissef7200f2010-10-06 17:09:42 +0000353 virtual void postWrite(void* ctx, const char* fn_name, uint32_t bytes) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100354 (void)bytes;
David Reissd7192062010-10-06 17:09:33 +0000355 communicate("postWrite", ctx, fn_name);
356 }
357 virtual void asyncComplete(void* ctx, const char* fn_name) {
358 communicate("asyncComplete", ctx, fn_name);
359 }
360 virtual void handlerError(void* ctx, const char* fn_name) {
361 communicate("handlerError", ctx, fn_name);
362 }
363
364 void communicate(const char* event, void* ctx, const char* fn_name) {
365 std::cout << event << ": " << *static_cast<std::string*>(ctx) << " = " << fn_name << std::endl;
366 }
367};
368
Roger Meier7e056e72011-07-17 07:28:28 +0000369class TestHandlerAsync : public ThriftTestCobSvIf {
370public:
Roger Meier611f90c2011-12-11 22:08:51 +0000371 TestHandlerAsync(boost::shared_ptr<TestHandler>& handler) : _delegate(handler) {}
Roger Meier7e056e72011-07-17 07:28:28 +0000372 virtual ~TestHandlerAsync() {}
373
Roger Meier0a7c69c2014-05-02 21:15:45 +0200374 virtual void testVoid(tcxx::function<void()> cob) {
Roger Meier7e056e72011-07-17 07:28:28 +0000375 _delegate->testVoid();
376 cob();
377 }
378
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100379 virtual void testString(tcxx::function<void(std::string const& _return)> cob,
380 const std::string& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000381 std::string res;
382 _delegate->testString(res, thing);
383 cob(res);
384 }
385
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900386 virtual void testBool(tcxx::function<void(bool const& _return)> cob, const bool thing) {
387 bool res = _delegate->testBool(thing);
388 cob(res);
389 }
390
Roger Meier0a7c69c2014-05-02 21:15:45 +0200391 virtual void testByte(tcxx::function<void(int8_t const& _return)> cob, const int8_t thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000392 int8_t res = _delegate->testByte(thing);
393 cob(res);
394 }
395
Roger Meier0a7c69c2014-05-02 21:15:45 +0200396 virtual void testI32(tcxx::function<void(int32_t const& _return)> cob, const int32_t thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000397 int32_t res = _delegate->testI32(thing);
398 cob(res);
399 }
400
Roger Meier0a7c69c2014-05-02 21:15:45 +0200401 virtual void testI64(tcxx::function<void(int64_t const& _return)> cob, const int64_t thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000402 int64_t res = _delegate->testI64(thing);
403 cob(res);
404 }
405
Roger Meier0a7c69c2014-05-02 21:15:45 +0200406 virtual void testDouble(tcxx::function<void(double const& _return)> cob, const double thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000407 double res = _delegate->testDouble(thing);
408 cob(res);
409 }
410
Konrad Grochowski1f6e3802015-05-18 18:10:06 +0200411 virtual void testBinary(tcxx::function<void(std::string const& _return)> cob,
412 const std::string& thing) {
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100413 std::string res;
414 _delegate->testBinary(res, thing);
415 cob(res);
416 }
417
Roger Meier0a7c69c2014-05-02 21:15:45 +0200418 virtual void testStruct(tcxx::function<void(Xtruct const& _return)> cob, const Xtruct& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000419 Xtruct res;
420 _delegate->testStruct(res, thing);
421 cob(res);
422 }
423
Roger Meier0a7c69c2014-05-02 21:15:45 +0200424 virtual void testNest(tcxx::function<void(Xtruct2 const& _return)> cob, const Xtruct2& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000425 Xtruct2 res;
426 _delegate->testNest(res, thing);
427 cob(res);
428 }
429
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100430 virtual void testMap(tcxx::function<void(std::map<int32_t, int32_t> const& _return)> cob,
431 const std::map<int32_t, int32_t>& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000432 std::map<int32_t, int32_t> res;
433 _delegate->testMap(res, thing);
434 cob(res);
435 }
436
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100437 virtual void testStringMap(
438 tcxx::function<void(std::map<std::string, std::string> const& _return)> cob,
439 const std::map<std::string, std::string>& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000440 std::map<std::string, std::string> res;
441 _delegate->testStringMap(res, thing);
442 cob(res);
443 }
444
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100445 virtual void testSet(tcxx::function<void(std::set<int32_t> const& _return)> cob,
446 const std::set<int32_t>& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000447 std::set<int32_t> res;
448 _delegate->testSet(res, thing);
449 cob(res);
450 }
451
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100452 virtual void testList(tcxx::function<void(std::vector<int32_t> const& _return)> cob,
453 const std::vector<int32_t>& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000454 std::vector<int32_t> res;
455 _delegate->testList(res, thing);
456 cob(res);
457 }
458
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100459 virtual void testEnum(tcxx::function<void(Numberz::type const& _return)> cob,
460 const Numberz::type thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000461 Numberz::type res = _delegate->testEnum(thing);
462 cob(res);
463 }
464
Roger Meier0a7c69c2014-05-02 21:15:45 +0200465 virtual void testTypedef(tcxx::function<void(UserId const& _return)> cob, const UserId thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000466 UserId res = _delegate->testTypedef(thing);
467 cob(res);
468 }
469
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100470 virtual void testMapMap(
471 tcxx::function<void(std::map<int32_t, std::map<int32_t, int32_t> > const& _return)> cob,
472 const int32_t hello) {
Roger Meier7e056e72011-07-17 07:28:28 +0000473 std::map<int32_t, std::map<int32_t, int32_t> > res;
474 _delegate->testMapMap(res, hello);
475 cob(res);
476 }
477
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100478 virtual void testInsanity(
479 tcxx::function<void(std::map<UserId, std::map<Numberz::type, Insanity> > const& _return)> cob,
480 const Insanity& argument) {
Jake Farrell5d02b802014-01-07 21:42:01 -0500481 std::map<UserId, std::map<Numberz::type, Insanity> > res;
Roger Meier7e056e72011-07-17 07:28:28 +0000482 _delegate->testInsanity(res, argument);
483 cob(res);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100484 }
Roger Meier7e056e72011-07-17 07:28:28 +0000485
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100486 virtual void testMulti(tcxx::function<void(Xtruct const& _return)> cob,
487 const int8_t arg0,
488 const int32_t arg1,
489 const int64_t arg2,
490 const std::map<int16_t, std::string>& arg3,
491 const Numberz::type arg4,
492 const UserId arg5) {
Roger Meier7e056e72011-07-17 07:28:28 +0000493 Xtruct res;
494 _delegate->testMulti(res, arg0, arg1, arg2, arg3, arg4, arg5);
495 cob(res);
496 }
497
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100498 virtual void testException(
499 tcxx::function<void()> cob,
500 tcxx::function<void(::apache::thrift::TDelayedException* _throw)> exn_cob,
501 const std::string& arg) {
Roger Meier7e056e72011-07-17 07:28:28 +0000502 try {
503 _delegate->testException(arg);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100504 } catch (const apache::thrift::TException& e) {
Roger Meier7e056e72011-07-17 07:28:28 +0000505 exn_cob(apache::thrift::TDelayedException::delayException(e));
506 return;
507 }
508 cob();
509 }
510
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100511 virtual void testMultiException(
512 tcxx::function<void(Xtruct const& _return)> cob,
513 tcxx::function<void(::apache::thrift::TDelayedException* _throw)> exn_cob,
514 const std::string& arg0,
515 const std::string& arg1) {
Roger Meier7e056e72011-07-17 07:28:28 +0000516 Xtruct res;
517 try {
518 _delegate->testMultiException(res, arg0, arg1);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100519 } catch (const apache::thrift::TException& e) {
Roger Meier7e056e72011-07-17 07:28:28 +0000520 exn_cob(apache::thrift::TDelayedException::delayException(e));
521 return;
522 }
523 cob(res);
524 }
525
Roger Meier0a7c69c2014-05-02 21:15:45 +0200526 virtual void testOneway(tcxx::function<void()> cob, const int32_t secondsToSleep) {
Roger Meier7e056e72011-07-17 07:28:28 +0000527 _delegate->testOneway(secondsToSleep);
528 cob();
529 }
530
531protected:
Roger Meier611f90c2011-12-11 22:08:51 +0000532 boost::shared_ptr<TestHandler> _delegate;
Roger Meier7e056e72011-07-17 07:28:28 +0000533};
534
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100535int main(int argc, char** argv) {
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +0530536
537 string file_path = boost::filesystem::system_complete(argv[0]).string();
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100538 string dir_path = file_path.substr(0, file_path.size() - EXECUTABLE_FILE_NAME_LENGTH);
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +0530539
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100540#if _WIN32
Jake Farrell5d02b802014-01-07 21:42:01 -0500541 transport::TWinsockSingleton::create();
542#endif
Mark Sleee8540632006-05-30 09:24:40 +0000543 int port = 9090;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000544 bool ssl = false;
Roger Meierca142b02011-06-07 17:59:07 +0000545 string transport_type = "buffered";
546 string protocol_type = "binary";
547 string server_type = "simple";
548 string domain_socket = "";
549 size_t workers = 4;
Marc Slemko6be374b2006-08-04 03:16:25 +0000550
Jake Farrell5d02b802014-01-07 21:42:01 -0500551 boost::program_options::options_description desc("Allowed options");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100552 desc.add_options()("help,h", "produce help message")(
553 "port",
554 boost::program_options::value<int>(&port)->default_value(port),
555 "Port number to listen")("domain-socket",
556 boost::program_options::value<string>(&domain_socket)
557 ->default_value(domain_socket),
558 "Unix Domain Socket (e.g. /tmp/ThriftTest.thrift)")(
559 "server-type",
560 boost::program_options::value<string>(&server_type)->default_value(server_type),
561 "type of server, \"simple\", \"thread-pool\", \"threaded\", or \"nonblocking\"")(
562 "transport",
563 boost::program_options::value<string>(&transport_type)->default_value(transport_type),
564 "transport: buffered, framed, http")(
565 "protocol",
566 boost::program_options::value<string>(&protocol_type)->default_value(protocol_type),
567 "protocol: binary, compact, json")("ssl", "Encrypted Transport using SSL")(
568 "processor-events",
569 "processor-events")("workers,n",
570 boost::program_options::value<size_t>(&workers)->default_value(workers),
571 "Number of thread pools workers. Only valid for thread-pool server type");
Marc Slemko6be374b2006-08-04 03:16:25 +0000572
Jake Farrell5d02b802014-01-07 21:42:01 -0500573 boost::program_options::variables_map vm;
574 boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
575 boost::program_options::notify(vm);
Marc Slemko6be374b2006-08-04 03:16:25 +0000576
Roger Meierca142b02011-06-07 17:59:07 +0000577 if (vm.count("help")) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100578 cout << desc << "\n";
579 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000580 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500581
Marc Slemko6be374b2006-08-04 03:16:25 +0000582 try {
Roger Meierca142b02011-06-07 17:59:07 +0000583 if (!server_type.empty()) {
584 if (server_type == "simple") {
585 } else if (server_type == "thread-pool") {
586 } else if (server_type == "threaded") {
587 } else if (server_type == "nonblocking") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000588 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100589 throw invalid_argument("Unknown server type " + server_type);
Roger Meierca142b02011-06-07 17:59:07 +0000590 }
591 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500592
Roger Meierca142b02011-06-07 17:59:07 +0000593 if (!protocol_type.empty()) {
594 if (protocol_type == "binary") {
Roger Meier284101c2014-03-11 21:20:35 +0100595 } else if (protocol_type == "compact") {
Roger Meierca142b02011-06-07 17:59:07 +0000596 } else if (protocol_type == "json") {
597 } 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
Mark Sleee8540632006-05-30 09:24:40 +0000621 // Dispatcher
Roger Meier611f90c2011-12-11 22:08:51 +0000622 boost::shared_ptr<TProtocolFactory> protocolFactory;
Roger Meierca142b02011-06-07 17:59:07 +0000623 if (protocol_type == "json") {
Roger Meier611f90c2011-12-11 22:08:51 +0000624 boost::shared_ptr<TProtocolFactory> jsonProtocolFactory(new TJSONProtocolFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000625 protocolFactory = jsonProtocolFactory;
Roger Meier023192f2014-02-12 09:35:12 +0100626 } else if (protocol_type == "compact") {
627 boost::shared_ptr<TProtocolFactory> compactProtocolFactory(new TCompactProtocolFactory());
628 protocolFactory = compactProtocolFactory;
Roger Meierca142b02011-06-07 17:59:07 +0000629 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100630 boost::shared_ptr<TProtocolFactory> binaryProtocolFactory(
631 new TBinaryProtocolFactoryT<TBufferBase>());
Roger Meierca142b02011-06-07 17:59:07 +0000632 protocolFactory = binaryProtocolFactory;
633 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000634
Roger Meierca142b02011-06-07 17:59:07 +0000635 // Processor
Roger Meier611f90c2011-12-11 22:08:51 +0000636 boost::shared_ptr<TestHandler> testHandler(new TestHandler());
637 boost::shared_ptr<ThriftTestProcessor> testProcessor(new ThriftTestProcessor(testHandler));
Jake Farrell5d02b802014-01-07 21:42:01 -0500638
Roger Meierca142b02011-06-07 17:59:07 +0000639 if (vm.count("processor-events")) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100640 testProcessor->setEventHandler(
641 boost::shared_ptr<TProcessorEventHandler>(new TestProcessorEventHandler()));
David Reissd7192062010-10-06 17:09:33 +0000642 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500643
Mark Sleee8540632006-05-30 09:24:40 +0000644 // Transport
Roger Meier611f90c2011-12-11 22:08:51 +0000645 boost::shared_ptr<TSSLSocketFactory> sslSocketFactory;
646 boost::shared_ptr<TServerSocket> serverSocket;
Mark Sleee8540632006-05-30 09:24:40 +0000647
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000648 if (ssl) {
Roger Meier611f90c2011-12-11 22:08:51 +0000649 sslSocketFactory = boost::shared_ptr<TSSLSocketFactory>(new TSSLSocketFactory());
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +0530650 sslSocketFactory->loadCertificate((dir_path + "../keys/server.crt").c_str());
651 sslSocketFactory->loadPrivateKey((dir_path + "../keys/server.key").c_str());
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000652 sslSocketFactory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
Roger Meier611f90c2011-12-11 22:08:51 +0000653 serverSocket = boost::shared_ptr<TServerSocket>(new TSSLServerSocket(port, sslSocketFactory));
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000654 } else {
Roger Meierc94b2932014-02-22 20:07:33 +0100655 if (domain_socket != "") {
656 unlink(domain_socket.c_str());
657 serverSocket = boost::shared_ptr<TServerSocket>(new TServerSocket(domain_socket));
658 port = 0;
659 } else {
Roger Meier611f90c2011-12-11 22:08:51 +0000660 serverSocket = boost::shared_ptr<TServerSocket>(new TServerSocket(port));
Roger Meierc94b2932014-02-22 20:07:33 +0100661 }
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000662 }
Roger Meierca142b02011-06-07 17:59:07 +0000663
Mark Sleed788b2e2006-09-07 01:26:35 +0000664 // Factory
Roger Meier611f90c2011-12-11 22:08:51 +0000665 boost::shared_ptr<TTransportFactory> transportFactory;
Jake Farrell5d02b802014-01-07 21:42:01 -0500666
Roger Meier7e056e72011-07-17 07:28:28 +0000667 if (transport_type == "http" && server_type != "nonblocking") {
Jake Farrell5d02b802014-01-07 21:42:01 -0500668 boost::shared_ptr<TTransportFactory> httpTransportFactory(new THttpServerTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000669 transportFactory = httpTransportFactory;
670 } else if (transport_type == "framed") {
Jake Farrell5d02b802014-01-07 21:42:01 -0500671 boost::shared_ptr<TTransportFactory> framedTransportFactory(new TFramedTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000672 transportFactory = framedTransportFactory;
673 } else {
Jake Farrell5d02b802014-01-07 21:42:01 -0500674 boost::shared_ptr<TTransportFactory> bufferedTransportFactory(new TBufferedTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000675 transportFactory = bufferedTransportFactory;
676 }
Mark Sleed788b2e2006-09-07 01:26:35 +0000677
Roger Meierca142b02011-06-07 17:59:07 +0000678 // Server Info
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100679 cout << "Starting \"" << server_type << "\" server (" << transport_type << "/" << protocol_type
680 << ") listen on: " << domain_socket;
Roger Meierca142b02011-06-07 17:59:07 +0000681 if (port != 0) {
682 cout << port;
683 }
684 cout << endl;
Mark Sleee8540632006-05-30 09:24:40 +0000685
Roger Meierca142b02011-06-07 17:59:07 +0000686 // Server
Jake Farrell5d02b802014-01-07 21:42:01 -0500687 boost::shared_ptr<apache::thrift::server::TServer> server;
688
Roger Meierca142b02011-06-07 17:59:07 +0000689 if (server_type == "simple") {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100690 server.reset(new TSimpleServer(testProcessor, serverSocket, transportFactory, protocolFactory));
Roger Meierca142b02011-06-07 17:59:07 +0000691 } else if (server_type == "thread-pool") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000692
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100693 boost::shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workers);
Marc Slemko6be374b2006-08-04 03:16:25 +0000694
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100695 boost::shared_ptr<PlatformThreadFactory> threadFactory
696 = boost::shared_ptr<PlatformThreadFactory>(new PlatformThreadFactory());
Marc Slemko6be374b2006-08-04 03:16:25 +0000697
698 threadManager->threadFactory(threadFactory);
699
700 threadManager->start();
701
Jake Farrell5d02b802014-01-07 21:42:01 -0500702 server.reset(new TThreadPoolServer(testProcessor,
Roger Meierca142b02011-06-07 17:59:07 +0000703 serverSocket,
Mark Sleed788b2e2006-09-07 01:26:35 +0000704 transportFactory,
Mark Sleea3302652006-10-25 19:03:32 +0000705 protocolFactory,
Jake Farrell5d02b802014-01-07 21:42:01 -0500706 threadManager));
Roger Meierca142b02011-06-07 17:59:07 +0000707 } else if (server_type == "threaded") {
David Reiss0c90f6f2008-02-06 22:18:40 +0000708
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100709 server.reset(
710 new TThreadedServer(testProcessor, serverSocket, transportFactory, protocolFactory));
Roger Meierca142b02011-06-07 17:59:07 +0000711 } else if (server_type == "nonblocking") {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100712 if (transport_type == "http") {
Roger Meier611f90c2011-12-11 22:08:51 +0000713 boost::shared_ptr<TestHandlerAsync> testHandlerAsync(new TestHandlerAsync(testHandler));
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100714 boost::shared_ptr<TAsyncProcessor> testProcessorAsync(
715 new ThriftTestAsyncProcessor(testHandlerAsync));
716 boost::shared_ptr<TAsyncBufferProcessor> testBufferProcessor(
717 new TAsyncProtocolProcessor(testProcessorAsync, protocolFactory));
Jake Farrell5d02b802014-01-07 21:42:01 -0500718
719 // not loading nonblockingServer into "server" because
720 // TEvhttpServer doesn't inherit from TServer, and doesn't
721 // provide a stop method.
Roger Meier7e056e72011-07-17 07:28:28 +0000722 TEvhttpServer nonblockingServer(testBufferProcessor, port);
723 nonblockingServer.serve();
Jake Farrell5d02b802014-01-07 21:42:01 -0500724 } else {
725 server.reset(new TNonblockingServer(testProcessor, port));
Roger Meier7e056e72011-07-17 07:28:28 +0000726 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000727 }
728
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100729 if (server.get() != NULL) {
Jake Farrell5d02b802014-01-07 21:42:01 -0500730 apache::thrift::concurrency::PlatformThreadFactory factory;
731 factory.setDetached(false);
732 boost::shared_ptr<apache::thrift::concurrency::Runnable> serverThreadRunner(server);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100733 boost::shared_ptr<apache::thrift::concurrency::Thread> thread
734 = factory.newThread(serverThreadRunner);
Jake Farrell5d02b802014-01-07 21:42:01 -0500735 thread->start();
736
Roger Meier284101c2014-03-11 21:20:35 +0100737 // HACK: cross language test suite is unable to handle cin properly
738 // that's why we stay in a endless loop here
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100739 while (1) {
740 }
Roger Meier284101c2014-03-11 21:20:35 +0100741 // FIXME: find another way to stop the server (e.g. a signal)
742 // cout<<"Press enter to stop the server."<<endl;
743 // cin.ignore(); //wait until a key is pressed
Jake Farrell5d02b802014-01-07 21:42:01 -0500744
745 server->stop();
746 thread->join();
747 server.reset();
748 }
749
Roger Meierca142b02011-06-07 17:59:07 +0000750 cout << "done." << endl;
Mark Sleee8540632006-05-30 09:24:40 +0000751 return 0;
752}