blob: a3432216ec480440d66b26ec84a43e53b39a4165 [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 Grochowski240120c2014-11-18 11:33:31 +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 Grochowski240120c2014-11-18 11:33:31 +010071 public:
Mark Sleed2655522006-09-05 22:09:57 +000072 TestHandler() {}
Mark Sleee8540632006-05-30 09:24:40 +000073
Konrad Grochowski240120c2014-11-18 11:33:31 +010074 void testVoid() {
75 printf("testVoid()\n");
76 }
Mark Sleee8540632006-05-30 09:24:40 +000077
Konrad Grochowski240120c2014-11-18 11:33:31 +010078 void testString(string& out, const string &thing) {
Mark Sleee8540632006-05-30 09:24:40 +000079 printf("testString(\"%s\")\n", thing.c_str());
Mark Slee1921d202007-01-24 19:43:06 +000080 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +000081 }
82
Mark Slee1921d202007-01-24 19:43:06 +000083 int8_t testByte(const int8_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +000084 printf("testByte(%d)\n", (int)thing);
85 return thing;
86 }
87
Mark Slee1921d202007-01-24 19:43:06 +000088 int32_t testI32(const int32_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +000089 printf("testI32(%d)\n", thing);
90 return thing;
91 }
92
Mark Slee1921d202007-01-24 19:43:06 +000093 int64_t testI64(const int64_t thing) {
Roger Meier0e814802014-01-17 21:07:58 +010094 printf("testI64(%" PRId64 ")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +000095 return thing;
96 }
97
Mark Slee1921d202007-01-24 19:43:06 +000098 double testDouble(const double thing) {
Roger Meiera8cef6e2011-07-17 18:55:59 +000099 printf("testDouble(%f)\n", thing);
Mark Sleec98d0502006-09-06 02:42:25 +0000100 return thing;
101 }
102
Konrad Grochowski240120c2014-11-18 11:33:31 +0100103 void testStruct(Xtruct& out, const Xtruct &thing) {
104 printf("testStruct({\"%s\", %d, %d, %" PRId64 "})\n", thing.string_thing.c_str(), (int)thing.byte_thing, thing.i32_thing, thing.i64_thing);
Mark Slee1921d202007-01-24 19:43:06 +0000105 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000106 }
107
Mark Slee1921d202007-01-24 19:43:06 +0000108 void testNest(Xtruct2& out, const Xtruct2& nest) {
Konrad Grochowski240120c2014-11-18 11:33:31 +0100109 const Xtruct &thing = nest.struct_thing;
110 printf("testNest({%d, {\"%s\", %d, %d, %" PRId64 "}, %d})\n", (int)nest.byte_thing, thing.string_thing.c_str(), (int)thing.byte_thing, thing.i32_thing, thing.i64_thing, nest.i32_thing);
Mark Slee1921d202007-01-24 19:43:06 +0000111 out = nest;
Mark Sleee8540632006-05-30 09:24:40 +0000112 }
113
Konrad Grochowski240120c2014-11-18 11:33:31 +0100114 void testMap(map<int32_t, int32_t> &out, const map<int32_t, int32_t> &thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000115 printf("testMap({");
116 map<int32_t, int32_t>::const_iterator m_iter;
117 bool first = true;
118 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
119 if (first) {
120 first = false;
121 } else {
122 printf(", ");
123 }
124 printf("%d => %d", m_iter->first, m_iter->second);
125 }
126 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000127 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000128 }
129
Konrad Grochowski240120c2014-11-18 11:33:31 +0100130 void testStringMap(map<std::string, std::string> &out, const map<std::string, std::string> &thing) {
Roger Meierd3b9dca2011-06-24 14:01:10 +0000131 printf("testMap({");
132 map<std::string, std::string>::const_iterator m_iter;
133 bool first = true;
134 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
135 if (first) {
136 first = false;
137 } else {
138 printf(", ");
139 }
140 printf("%s => %s", (m_iter->first).c_str(), (m_iter->second).c_str());
141 }
142 printf("})\n");
143 out = thing;
144 }
145
Konrad Grochowski240120c2014-11-18 11:33:31 +0100146 void testSet(set<int32_t> &out, const set<int32_t> &thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000147 printf("testSet({");
148 set<int32_t>::const_iterator s_iter;
149 bool first = true;
150 for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) {
151 if (first) {
152 first = false;
153 } else {
154 printf(", ");
155 }
156 printf("%d", *s_iter);
157 }
158 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000159 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000160 }
161
Konrad Grochowski240120c2014-11-18 11:33:31 +0100162 void testList(vector<int32_t> &out, const vector<int32_t> &thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000163 printf("testList({");
Mark Sleeb9acf982006-10-10 01:57:32 +0000164 vector<int32_t>::const_iterator l_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000165 bool first = true;
166 for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) {
167 if (first) {
168 first = false;
169 } else {
170 printf(", ");
171 }
172 printf("%d", *l_iter);
173 }
174 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000175 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000176 }
177
Bryan Duxbury833ae492010-09-27 17:26:02 +0000178 Numberz::type testEnum(const Numberz::type thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000179 printf("testEnum(%d)\n", thing);
180 return thing;
181 }
182
Mark Slee1921d202007-01-24 19:43:06 +0000183 UserId testTypedef(const UserId thing) {
Roger Meier0e814802014-01-17 21:07:58 +0100184 printf("testTypedef(%" PRId64 ")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +0000185 return thing;
186 }
187
Konrad Grochowski240120c2014-11-18 11:33:31 +0100188 void testMapMap(map<int32_t, map<int32_t,int32_t> > &mapmap, const int32_t hello) {
Mark Sleee8540632006-05-30 09:24:40 +0000189 printf("testMapMap(%d)\n", hello);
Mark Sleee8540632006-05-30 09:24:40 +0000190
Konrad Grochowski240120c2014-11-18 11:33:31 +0100191 map<int32_t,int32_t> pos;
192 map<int32_t,int32_t> neg;
Mark Sleee8540632006-05-30 09:24:40 +0000193 for (int i = 1; i < 5; i++) {
Konrad Grochowski240120c2014-11-18 11:33:31 +0100194 pos.insert(make_pair(i,i));
195 neg.insert(make_pair(-i,-i));
Mark Sleee8540632006-05-30 09:24:40 +0000196 }
197
198 mapmap.insert(make_pair(4, pos));
199 mapmap.insert(make_pair(-4, neg));
Konrad Grochowski240120c2014-11-18 11:33:31 +0100200
Mark Sleee8540632006-05-30 09:24:40 +0000201 }
202
Konrad Grochowski240120c2014-11-18 11:33:31 +0100203 void testInsanity(map<UserId, map<Numberz::type,Insanity> > &insane, const Insanity &argument) {
204 (void) argument;
Mark Sleee8540632006-05-30 09:24:40 +0000205 printf("testInsanity()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000206
Mark Sleee8540632006-05-30 09:24:40 +0000207 Xtruct hello;
208 hello.string_thing = "Hello2";
209 hello.byte_thing = 2;
Mark Sleee8540632006-05-30 09:24:40 +0000210 hello.i32_thing = 2;
Mark Sleee8540632006-05-30 09:24:40 +0000211 hello.i64_thing = 2;
212
213 Xtruct goodbye;
214 goodbye.string_thing = "Goodbye4";
215 goodbye.byte_thing = 4;
Mark Sleee8540632006-05-30 09:24:40 +0000216 goodbye.i32_thing = 4;
Mark Sleee8540632006-05-30 09:24:40 +0000217 goodbye.i64_thing = 4;
218
219 Insanity crazy;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000220 crazy.userMap.insert(make_pair(Numberz::EIGHT, 8));
Mark Sleee8540632006-05-30 09:24:40 +0000221 crazy.xtructs.push_back(goodbye);
222
223 Insanity looney;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000224 crazy.userMap.insert(make_pair(Numberz::FIVE, 5));
Mark Sleee8540632006-05-30 09:24:40 +0000225 crazy.xtructs.push_back(hello);
226
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
Bryan Duxbury833ae492010-09-27 17:26:02 +0000230 first_map.insert(make_pair(Numberz::TWO, crazy));
231 first_map.insert(make_pair(Numberz::THREE, crazy));
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 Grochowski240120c2014-11-18 11:33:31 +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 Grochowski240120c2014-11-18 11:33:31 +0100243 map<Numberz::type,Insanity>::const_iterator i2_iter;
244 for (i2_iter = i_iter->second.begin();
245 i2_iter != i_iter->second.end();
246 ++i2_iter) {
Mark Sleee8540632006-05-30 09:24:40 +0000247 printf("%d => {", i2_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000248 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
249 map<Numberz::type, UserId>::const_iterator um;
Mark Sleee8540632006-05-30 09:24:40 +0000250 printf("{");
251 for (um = userMap.begin(); um != userMap.end(); ++um) {
Roger Meier0e814802014-01-17 21:07:58 +0100252 printf("%d => %" PRId64 ", ", um->first, um->second);
Mark Sleee8540632006-05-30 09:24:40 +0000253 }
254 printf("}, ");
255
Mark Sleeb9acf982006-10-10 01:57:32 +0000256 vector<Xtruct> xtructs = i2_iter->second.xtructs;
257 vector<Xtruct>::const_iterator x;
Mark Sleee8540632006-05-30 09:24:40 +0000258 printf("{");
259 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
Konrad Grochowski240120c2014-11-18 11:33:31 +0100260 printf("{\"%s\", %d, %d, %" PRId64 "}, ", x->string_thing.c_str(), (int)x->byte_thing, x->i32_thing, x->i64_thing);
Mark Sleee8540632006-05-30 09:24:40 +0000261 }
262 printf("}");
263
264 printf("}, ");
265 }
266 printf("}, ");
267 }
268 printf("}\n");
Konrad Grochowski240120c2014-11-18 11:33:31 +0100269
270
Mark Sleee8540632006-05-30 09:24:40 +0000271 }
Marc Slemkoe6889de2006-08-12 00:32:53 +0000272
Konrad Grochowski240120c2014-11-18 11:33:31 +0100273 void testMulti(Xtruct &hello, const int8_t arg0, const int32_t arg1, const int64_t arg2, const std::map<int16_t, std::string> &arg3, const Numberz::type arg4, const UserId arg5) {
274 (void) arg3;
275 (void) arg4;
276 (void) arg5;
Jake Farrell5d02b802014-01-07 21:42:01 -0500277
Marc Slemkoe6889de2006-08-12 00:32:53 +0000278 printf("testMulti()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000279
Marc Slemkoe6889de2006-08-12 00:32:53 +0000280 hello.string_thing = "Hello2";
281 hello.byte_thing = arg0;
282 hello.i32_thing = arg1;
283 hello.i64_thing = (int64_t)arg2;
Marc Slemkoe6889de2006-08-12 00:32:53 +0000284 }
Marc Slemkobf4fd192006-08-15 21:29:39 +0000285
Konrad Grochowski240120c2014-11-18 11:33:31 +0100286 void testException(const std::string &arg)
287 throw(Xception, apache::thrift::TException)
288 {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000289 printf("testException(%s)\n", arg.c_str());
Mark Sleed3d733a2006-09-01 22:19:06 +0000290 if (arg.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000291 Xception e;
292 e.errorCode = 1001;
David Reiss9813cbe2009-01-31 21:39:11 +0000293 e.message = arg;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000294 throw e;
Roger Meier1f8b48f2012-05-02 22:56:47 +0000295 } else if (arg.compare("TException") == 0) {
T Jake Lucianib5e62212009-01-31 22:36:20 +0000296 apache::thrift::TException e;
David Reiss55ff70f2008-06-11 00:58:25 +0000297 throw e;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000298 } else {
299 Xtruct result;
300 result.string_thing = arg;
301 return;
302 }
303 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000304
Konrad Grochowski240120c2014-11-18 11:33:31 +0100305 void testMultiException(Xtruct &result, const std::string &arg0, const std::string &arg1) throw(Xception, Xception2) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000306
Marc Slemko71d4e472006-08-15 22:34:04 +0000307 printf("testMultiException(%s, %s)\n", arg0.c_str(), arg1.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000308
Mark Sleef5f2be42006-09-05 21:05:31 +0000309 if (arg0.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000310 Xception e;
311 e.errorCode = 1001;
312 e.message = "This is an Xception";
313 throw e;
Mark Sleef5f2be42006-09-05 21:05:31 +0000314 } else if (arg0.compare("Xception2") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000315 Xception2 e;
316 e.errorCode = 2002;
317 e.struct_thing.string_thing = "This is an Xception2";
318 throw e;
319 } else {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000320 result.string_thing = arg1;
Mark Slee1921d202007-01-24 19:43:06 +0000321 return;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000322 }
323 }
David Reiss2ab6fe82008-02-18 02:11:44 +0000324
Jake Farrell5d02b802014-01-07 21:42:01 -0500325 void testOneway(const int32_t sleepFor) {
David Reiss6ce401d2009-03-24 20:01:58 +0000326 printf("testOneway(%d): Sleeping...\n", sleepFor);
Jake Farrell5d02b802014-01-07 21:42:01 -0500327 THRIFT_SLEEP_SEC(sleepFor);
David Reiss6ce401d2009-03-24 20:01:58 +0000328 printf("testOneway(%d): done sleeping!\n", sleepFor);
David Reiss2ab6fe82008-02-18 02:11:44 +0000329 }
Mark Sleee8540632006-05-30 09:24:40 +0000330};
331
Konrad Grochowski240120c2014-11-18 11:33:31 +0100332
David Reissd7192062010-10-06 17:09:33 +0000333class TestProcessorEventHandler : public TProcessorEventHandler {
David Reiss23248712010-10-06 17:10:08 +0000334 virtual void* getContext(const char* fn_name, void* serverContext) {
Konrad Grochowski240120c2014-11-18 11:33:31 +0100335 (void) serverContext;
David Reissd7192062010-10-06 17:09:33 +0000336 return new std::string(fn_name);
337 }
338 virtual void freeContext(void* ctx, const char* fn_name) {
Konrad Grochowski240120c2014-11-18 11:33:31 +0100339 (void) fn_name;
David Reissd7192062010-10-06 17:09:33 +0000340 delete static_cast<std::string*>(ctx);
341 }
Konrad Grochowski240120c2014-11-18 11:33:31 +0100342 virtual void preRead(void* ctx, const char* fn_name) {
343 communicate("preRead", ctx, fn_name);
344 }
David Reissef7200f2010-10-06 17:09:42 +0000345 virtual void postRead(void* ctx, const char* fn_name, uint32_t bytes) {
Konrad Grochowski240120c2014-11-18 11:33:31 +0100346 (void) bytes;
David Reissd7192062010-10-06 17:09:33 +0000347 communicate("postRead", ctx, fn_name);
348 }
Konrad Grochowski240120c2014-11-18 11:33:31 +0100349 virtual void preWrite(void* ctx, const char* fn_name) {
350 communicate("preWrite", ctx, fn_name);
351 }
David Reissef7200f2010-10-06 17:09:42 +0000352 virtual void postWrite(void* ctx, const char* fn_name, uint32_t bytes) {
Konrad Grochowski240120c2014-11-18 11:33:31 +0100353 (void) bytes;
David Reissd7192062010-10-06 17:09:33 +0000354 communicate("postWrite", ctx, fn_name);
355 }
356 virtual void asyncComplete(void* ctx, const char* fn_name) {
357 communicate("asyncComplete", ctx, fn_name);
358 }
359 virtual void handlerError(void* ctx, const char* fn_name) {
360 communicate("handlerError", ctx, fn_name);
361 }
362
363 void communicate(const char* event, void* ctx, const char* fn_name) {
364 std::cout << event << ": " << *static_cast<std::string*>(ctx) << " = " << fn_name << std::endl;
365 }
366};
367
Konrad Grochowski240120c2014-11-18 11:33:31 +0100368
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 Grochowski240120c2014-11-18 11:33:31 +0100379 virtual void testString(tcxx::function<void(std::string const& _return)> cob, const std::string& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000380 std::string res;
381 _delegate->testString(res, thing);
382 cob(res);
383 }
384
Roger Meier0a7c69c2014-05-02 21:15:45 +0200385 virtual void testByte(tcxx::function<void(int8_t const& _return)> cob, const int8_t thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000386 int8_t res = _delegate->testByte(thing);
387 cob(res);
388 }
389
Roger Meier0a7c69c2014-05-02 21:15:45 +0200390 virtual void testI32(tcxx::function<void(int32_t const& _return)> cob, const int32_t thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000391 int32_t res = _delegate->testI32(thing);
392 cob(res);
393 }
394
Roger Meier0a7c69c2014-05-02 21:15:45 +0200395 virtual void testI64(tcxx::function<void(int64_t const& _return)> cob, const int64_t thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000396 int64_t res = _delegate->testI64(thing);
397 cob(res);
398 }
399
Roger Meier0a7c69c2014-05-02 21:15:45 +0200400 virtual void testDouble(tcxx::function<void(double const& _return)> cob, const double thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000401 double res = _delegate->testDouble(thing);
402 cob(res);
403 }
404
Roger Meier0a7c69c2014-05-02 21:15:45 +0200405 virtual void testStruct(tcxx::function<void(Xtruct const& _return)> cob, const Xtruct& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000406 Xtruct res;
407 _delegate->testStruct(res, thing);
408 cob(res);
409 }
410
Roger Meier0a7c69c2014-05-02 21:15:45 +0200411 virtual void testNest(tcxx::function<void(Xtruct2 const& _return)> cob, const Xtruct2& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000412 Xtruct2 res;
413 _delegate->testNest(res, thing);
414 cob(res);
415 }
416
Konrad Grochowski240120c2014-11-18 11:33:31 +0100417 virtual void testMap(tcxx::function<void(std::map<int32_t, int32_t> const& _return)> cob, const std::map<int32_t, int32_t> & thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000418 std::map<int32_t, int32_t> res;
419 _delegate->testMap(res, thing);
420 cob(res);
421 }
422
Konrad Grochowski240120c2014-11-18 11:33:31 +0100423 virtual void testStringMap(tcxx::function<void(std::map<std::string, std::string> const& _return)> cob, const std::map<std::string, std::string> & thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000424 std::map<std::string, std::string> res;
425 _delegate->testStringMap(res, thing);
426 cob(res);
427 }
428
Konrad Grochowski240120c2014-11-18 11:33:31 +0100429 virtual void testSet(tcxx::function<void(std::set<int32_t> const& _return)> cob, const std::set<int32_t> & thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000430 std::set<int32_t> res;
431 _delegate->testSet(res, thing);
432 cob(res);
433 }
434
Konrad Grochowski240120c2014-11-18 11:33:31 +0100435 virtual void testList(tcxx::function<void(std::vector<int32_t> const& _return)> cob, const std::vector<int32_t> & thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000436 std::vector<int32_t> res;
437 _delegate->testList(res, thing);
438 cob(res);
439 }
440
Konrad Grochowski240120c2014-11-18 11:33:31 +0100441 virtual void testEnum(tcxx::function<void(Numberz::type const& _return)> cob, const Numberz::type thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000442 Numberz::type res = _delegate->testEnum(thing);
443 cob(res);
444 }
445
Roger Meier0a7c69c2014-05-02 21:15:45 +0200446 virtual void testTypedef(tcxx::function<void(UserId const& _return)> cob, const UserId thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000447 UserId res = _delegate->testTypedef(thing);
448 cob(res);
449 }
450
Konrad Grochowski240120c2014-11-18 11:33:31 +0100451 virtual void testMapMap(tcxx::function<void(std::map<int32_t, std::map<int32_t, int32_t> > const& _return)> cob, const int32_t hello) {
Roger Meier7e056e72011-07-17 07:28:28 +0000452 std::map<int32_t, std::map<int32_t, int32_t> > res;
453 _delegate->testMapMap(res, hello);
454 cob(res);
455 }
456
Konrad Grochowski240120c2014-11-18 11:33:31 +0100457 virtual void testInsanity(tcxx::function<void(std::map<UserId, std::map<Numberz::type, Insanity> > const& _return)> cob, const Insanity& argument) {
Jake Farrell5d02b802014-01-07 21:42:01 -0500458 std::map<UserId, std::map<Numberz::type, Insanity> > res;
Roger Meier7e056e72011-07-17 07:28:28 +0000459 _delegate->testInsanity(res, argument);
460 cob(res);
Konrad Grochowski240120c2014-11-18 11:33:31 +0100461 }
Roger Meier7e056e72011-07-17 07:28:28 +0000462
Konrad Grochowski240120c2014-11-18 11:33:31 +0100463 virtual void testMulti(tcxx::function<void(Xtruct const& _return)> cob, const int8_t arg0, const int32_t arg1, const int64_t arg2, const std::map<int16_t, std::string> & arg3, const Numberz::type arg4, const UserId arg5) {
Roger Meier7e056e72011-07-17 07:28:28 +0000464 Xtruct res;
465 _delegate->testMulti(res, arg0, arg1, arg2, arg3, arg4, arg5);
466 cob(res);
467 }
468
Konrad Grochowski240120c2014-11-18 11:33:31 +0100469 virtual void testException(tcxx::function<void()> cob, tcxx::function<void(::apache::thrift::TDelayedException* _throw)> exn_cob, const std::string& arg) {
Roger Meier7e056e72011-07-17 07:28:28 +0000470 try {
471 _delegate->testException(arg);
Konrad Grochowski240120c2014-11-18 11:33:31 +0100472 } catch(const apache::thrift::TException& e) {
Roger Meier7e056e72011-07-17 07:28:28 +0000473 exn_cob(apache::thrift::TDelayedException::delayException(e));
474 return;
475 }
476 cob();
477 }
478
Konrad Grochowski240120c2014-11-18 11:33:31 +0100479 virtual void testMultiException(tcxx::function<void(Xtruct const& _return)> cob, tcxx::function<void(::apache::thrift::TDelayedException* _throw)> exn_cob, const std::string& arg0, const std::string& arg1) {
Roger Meier7e056e72011-07-17 07:28:28 +0000480 Xtruct res;
481 try {
482 _delegate->testMultiException(res, arg0, arg1);
Konrad Grochowski240120c2014-11-18 11:33:31 +0100483 } catch(const apache::thrift::TException& e) {
Roger Meier7e056e72011-07-17 07:28:28 +0000484 exn_cob(apache::thrift::TDelayedException::delayException(e));
485 return;
486 }
487 cob(res);
488 }
489
Roger Meier0a7c69c2014-05-02 21:15:45 +0200490 virtual void testOneway(tcxx::function<void()> cob, const int32_t secondsToSleep) {
Roger Meier7e056e72011-07-17 07:28:28 +0000491 _delegate->testOneway(secondsToSleep);
492 cob();
493 }
494
495protected:
Roger Meier611f90c2011-12-11 22:08:51 +0000496 boost::shared_ptr<TestHandler> _delegate;
Roger Meier7e056e72011-07-17 07:28:28 +0000497};
498
Konrad Grochowski240120c2014-11-18 11:33:31 +0100499
500int main(int argc, char **argv) {
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +0530501
502 string file_path = boost::filesystem::system_complete(argv[0]).string();
Konrad Grochowski240120c2014-11-18 11:33:31 +0100503 string dir_path = file_path.substr(0, file_path.size()-EXECUTABLE_FILE_NAME_LENGTH);
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +0530504
Konrad Grochowski240120c2014-11-18 11:33:31 +0100505 #if _WIN32
Jake Farrell5d02b802014-01-07 21:42:01 -0500506 transport::TWinsockSingleton::create();
507#endif
Mark Sleee8540632006-05-30 09:24:40 +0000508 int port = 9090;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000509 bool ssl = false;
Roger Meierca142b02011-06-07 17:59:07 +0000510 string transport_type = "buffered";
511 string protocol_type = "binary";
512 string server_type = "simple";
513 string domain_socket = "";
514 size_t workers = 4;
Marc Slemko6be374b2006-08-04 03:16:25 +0000515
Konrad Grochowski240120c2014-11-18 11:33:31 +0100516
Jake Farrell5d02b802014-01-07 21:42:01 -0500517 boost::program_options::options_description desc("Allowed options");
Konrad Grochowski240120c2014-11-18 11:33:31 +0100518 desc.add_options()
519 ("help,h", "produce help message")
520 ("port", boost::program_options::value<int>(&port)->default_value(port), "Port number to listen")
521 ("domain-socket", boost::program_options::value<string>(&domain_socket)->default_value(domain_socket),
522 "Unix Domain Socket (e.g. /tmp/ThriftTest.thrift)")
523 ("server-type", boost::program_options::value<string>(&server_type)->default_value(server_type),
524 "type of server, \"simple\", \"thread-pool\", \"threaded\", or \"nonblocking\"")
525 ("transport", boost::program_options::value<string>(&transport_type)->default_value(transport_type),
526 "transport: buffered, framed, http")
527 ("protocol", boost::program_options::value<string>(&protocol_type)->default_value(protocol_type),
528 "protocol: binary, compact, json")
529 ("ssl", "Encrypted Transport using SSL")
530 ("processor-events", "processor-events")
531 ("workers,n", boost::program_options::value<size_t>(&workers)->default_value(workers),
532 "Number of thread pools workers. Only valid for thread-pool server type")
533 ;
Marc Slemko6be374b2006-08-04 03:16:25 +0000534
Jake Farrell5d02b802014-01-07 21:42:01 -0500535 boost::program_options::variables_map vm;
536 boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
537 boost::program_options::notify(vm);
Marc Slemko6be374b2006-08-04 03:16:25 +0000538
Roger Meierca142b02011-06-07 17:59:07 +0000539 if (vm.count("help")) {
Konrad Grochowski240120c2014-11-18 11:33:31 +0100540 cout << desc << "\n";
541 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000542 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500543
Marc Slemko6be374b2006-08-04 03:16:25 +0000544 try {
Roger Meierca142b02011-06-07 17:59:07 +0000545 if (!server_type.empty()) {
546 if (server_type == "simple") {
547 } else if (server_type == "thread-pool") {
548 } else if (server_type == "threaded") {
549 } else if (server_type == "nonblocking") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000550 } else {
Konrad Grochowski240120c2014-11-18 11:33:31 +0100551 throw invalid_argument("Unknown server type "+server_type);
Roger Meierca142b02011-06-07 17:59:07 +0000552 }
553 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500554
Roger Meierca142b02011-06-07 17:59:07 +0000555 if (!protocol_type.empty()) {
556 if (protocol_type == "binary") {
Roger Meier284101c2014-03-11 21:20:35 +0100557 } else if (protocol_type == "compact") {
Roger Meierca142b02011-06-07 17:59:07 +0000558 } else if (protocol_type == "json") {
559 } else {
Konrad Grochowski240120c2014-11-18 11:33:31 +0100560 throw invalid_argument("Unknown protocol type "+protocol_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000561 }
562 }
563
Roger Meier284101c2014-03-11 21:20:35 +0100564 if (!transport_type.empty()) {
Roger Meierca142b02011-06-07 17:59:07 +0000565 if (transport_type == "buffered") {
566 } else if (transport_type == "framed") {
567 } else if (transport_type == "http") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000568 } else {
Konrad Grochowski240120c2014-11-18 11:33:31 +0100569 throw invalid_argument("Unknown transport type "+transport_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000570 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000571 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000572
Bryan Duxbury833ae492010-09-27 17:26:02 +0000573 } catch (std::exception& e) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000574 cerr << e.what() << endl;
Roger Meierca142b02011-06-07 17:59:07 +0000575 cout << desc << "\n";
576 return 1;
Marc Slemko6be374b2006-08-04 03:16:25 +0000577 }
578
Roger Meierca142b02011-06-07 17:59:07 +0000579 if (vm.count("ssl")) {
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000580 ssl = true;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000581 }
582
Mark Sleee8540632006-05-30 09:24:40 +0000583 // Dispatcher
Roger Meier611f90c2011-12-11 22:08:51 +0000584 boost::shared_ptr<TProtocolFactory> protocolFactory;
Roger Meierca142b02011-06-07 17:59:07 +0000585 if (protocol_type == "json") {
Roger Meier611f90c2011-12-11 22:08:51 +0000586 boost::shared_ptr<TProtocolFactory> jsonProtocolFactory(new TJSONProtocolFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000587 protocolFactory = jsonProtocolFactory;
Roger Meier023192f2014-02-12 09:35:12 +0100588 } else if (protocol_type == "compact") {
589 boost::shared_ptr<TProtocolFactory> compactProtocolFactory(new TCompactProtocolFactory());
590 protocolFactory = compactProtocolFactory;
Roger Meierca142b02011-06-07 17:59:07 +0000591 } else {
Konrad Grochowski240120c2014-11-18 11:33:31 +0100592 boost::shared_ptr<TProtocolFactory> binaryProtocolFactory(new TBinaryProtocolFactoryT<TBufferBase>());
Roger Meierca142b02011-06-07 17:59:07 +0000593 protocolFactory = binaryProtocolFactory;
594 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000595
Roger Meierca142b02011-06-07 17:59:07 +0000596 // Processor
Roger Meier611f90c2011-12-11 22:08:51 +0000597 boost::shared_ptr<TestHandler> testHandler(new TestHandler());
598 boost::shared_ptr<ThriftTestProcessor> testProcessor(new ThriftTestProcessor(testHandler));
Jake Farrell5d02b802014-01-07 21:42:01 -0500599
Roger Meierca142b02011-06-07 17:59:07 +0000600 if (vm.count("processor-events")) {
Konrad Grochowski240120c2014-11-18 11:33:31 +0100601 testProcessor->setEventHandler(boost::shared_ptr<TProcessorEventHandler>(
602 new TestProcessorEventHandler()));
David Reissd7192062010-10-06 17:09:33 +0000603 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500604
Mark Sleee8540632006-05-30 09:24:40 +0000605 // Transport
Roger Meier611f90c2011-12-11 22:08:51 +0000606 boost::shared_ptr<TSSLSocketFactory> sslSocketFactory;
607 boost::shared_ptr<TServerSocket> serverSocket;
Mark Sleee8540632006-05-30 09:24:40 +0000608
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000609 if (ssl) {
Roger Meier611f90c2011-12-11 22:08:51 +0000610 sslSocketFactory = boost::shared_ptr<TSSLSocketFactory>(new TSSLSocketFactory());
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +0530611 sslSocketFactory->loadCertificate((dir_path + "../keys/server.crt").c_str());
612 sslSocketFactory->loadPrivateKey((dir_path + "../keys/server.key").c_str());
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000613 sslSocketFactory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
Roger Meier611f90c2011-12-11 22:08:51 +0000614 serverSocket = boost::shared_ptr<TServerSocket>(new TSSLServerSocket(port, sslSocketFactory));
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000615 } else {
Roger Meierc94b2932014-02-22 20:07:33 +0100616 if (domain_socket != "") {
617 unlink(domain_socket.c_str());
618 serverSocket = boost::shared_ptr<TServerSocket>(new TServerSocket(domain_socket));
619 port = 0;
620 } else {
Roger Meier611f90c2011-12-11 22:08:51 +0000621 serverSocket = boost::shared_ptr<TServerSocket>(new TServerSocket(port));
Roger Meierc94b2932014-02-22 20:07:33 +0100622 }
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000623 }
Roger Meierca142b02011-06-07 17:59:07 +0000624
Mark Sleed788b2e2006-09-07 01:26:35 +0000625 // Factory
Roger Meier611f90c2011-12-11 22:08:51 +0000626 boost::shared_ptr<TTransportFactory> transportFactory;
Jake Farrell5d02b802014-01-07 21:42:01 -0500627
Roger Meier7e056e72011-07-17 07:28:28 +0000628 if (transport_type == "http" && server_type != "nonblocking") {
Jake Farrell5d02b802014-01-07 21:42:01 -0500629 boost::shared_ptr<TTransportFactory> httpTransportFactory(new THttpServerTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000630 transportFactory = httpTransportFactory;
631 } else if (transport_type == "framed") {
Jake Farrell5d02b802014-01-07 21:42:01 -0500632 boost::shared_ptr<TTransportFactory> framedTransportFactory(new TFramedTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000633 transportFactory = framedTransportFactory;
634 } else {
Jake Farrell5d02b802014-01-07 21:42:01 -0500635 boost::shared_ptr<TTransportFactory> bufferedTransportFactory(new TBufferedTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000636 transportFactory = bufferedTransportFactory;
637 }
Mark Sleed788b2e2006-09-07 01:26:35 +0000638
Roger Meierca142b02011-06-07 17:59:07 +0000639 // Server Info
Konrad Grochowski240120c2014-11-18 11:33:31 +0100640 cout << "Starting \"" << server_type << "\" server ("
641 << transport_type << "/" << protocol_type << ") listen on: " << domain_socket;
Roger Meierca142b02011-06-07 17:59:07 +0000642 if (port != 0) {
643 cout << port;
644 }
645 cout << endl;
Mark Sleee8540632006-05-30 09:24:40 +0000646
Roger Meierca142b02011-06-07 17:59:07 +0000647 // Server
Jake Farrell5d02b802014-01-07 21:42:01 -0500648 boost::shared_ptr<apache::thrift::server::TServer> server;
649
Roger Meierca142b02011-06-07 17:59:07 +0000650 if (server_type == "simple") {
Konrad Grochowski240120c2014-11-18 11:33:31 +0100651 server.reset(new TSimpleServer(testProcessor,
652 serverSocket,
653 transportFactory,
654 protocolFactory));
Roger Meierca142b02011-06-07 17:59:07 +0000655 } else if (server_type == "thread-pool") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000656
Konrad Grochowski240120c2014-11-18 11:33:31 +0100657 boost::shared_ptr<ThreadManager> threadManager =
658 ThreadManager::newSimpleThreadManager(workers);
Marc Slemko6be374b2006-08-04 03:16:25 +0000659
Konrad Grochowski240120c2014-11-18 11:33:31 +0100660 boost::shared_ptr<PlatformThreadFactory> threadFactory =
661 boost::shared_ptr<PlatformThreadFactory>(new PlatformThreadFactory());
Marc Slemko6be374b2006-08-04 03:16:25 +0000662
663 threadManager->threadFactory(threadFactory);
664
665 threadManager->start();
666
Jake Farrell5d02b802014-01-07 21:42:01 -0500667 server.reset(new TThreadPoolServer(testProcessor,
Roger Meierca142b02011-06-07 17:59:07 +0000668 serverSocket,
Mark Sleed788b2e2006-09-07 01:26:35 +0000669 transportFactory,
Mark Sleea3302652006-10-25 19:03:32 +0000670 protocolFactory,
Jake Farrell5d02b802014-01-07 21:42:01 -0500671 threadManager));
Roger Meierca142b02011-06-07 17:59:07 +0000672 } else if (server_type == "threaded") {
David Reiss0c90f6f2008-02-06 22:18:40 +0000673
Konrad Grochowski240120c2014-11-18 11:33:31 +0100674 server.reset(new TThreadedServer(testProcessor,
675 serverSocket,
676 transportFactory,
677 protocolFactory));
Roger Meierca142b02011-06-07 17:59:07 +0000678 } else if (server_type == "nonblocking") {
Konrad Grochowski240120c2014-11-18 11:33:31 +0100679 if(transport_type == "http") {
Roger Meier611f90c2011-12-11 22:08:51 +0000680 boost::shared_ptr<TestHandlerAsync> testHandlerAsync(new TestHandlerAsync(testHandler));
Konrad Grochowski240120c2014-11-18 11:33:31 +0100681 boost::shared_ptr<TAsyncProcessor> testProcessorAsync(new ThriftTestAsyncProcessor(testHandlerAsync));
682 boost::shared_ptr<TAsyncBufferProcessor> testBufferProcessor(new TAsyncProtocolProcessor(testProcessorAsync, protocolFactory));
Jake Farrell5d02b802014-01-07 21:42:01 -0500683
684 // not loading nonblockingServer into "server" because
685 // TEvhttpServer doesn't inherit from TServer, and doesn't
686 // provide a stop method.
Roger Meier7e056e72011-07-17 07:28:28 +0000687 TEvhttpServer nonblockingServer(testBufferProcessor, port);
688 nonblockingServer.serve();
Jake Farrell5d02b802014-01-07 21:42:01 -0500689 } else {
690 server.reset(new TNonblockingServer(testProcessor, port));
Roger Meier7e056e72011-07-17 07:28:28 +0000691 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000692 }
693
Konrad Grochowski240120c2014-11-18 11:33:31 +0100694 if(server.get() != NULL)
695 {
Jake Farrell5d02b802014-01-07 21:42:01 -0500696 apache::thrift::concurrency::PlatformThreadFactory factory;
697 factory.setDetached(false);
698 boost::shared_ptr<apache::thrift::concurrency::Runnable> serverThreadRunner(server);
Konrad Grochowski240120c2014-11-18 11:33:31 +0100699 boost::shared_ptr<apache::thrift::concurrency::Thread> thread = factory.newThread(serverThreadRunner);
Jake Farrell5d02b802014-01-07 21:42:01 -0500700 thread->start();
701
Roger Meier284101c2014-03-11 21:20:35 +0100702 // HACK: cross language test suite is unable to handle cin properly
703 // that's why we stay in a endless loop here
Konrad Grochowski240120c2014-11-18 11:33:31 +0100704 while(1){}
Roger Meier284101c2014-03-11 21:20:35 +0100705 // FIXME: find another way to stop the server (e.g. a signal)
706 // cout<<"Press enter to stop the server."<<endl;
707 // cin.ignore(); //wait until a key is pressed
Jake Farrell5d02b802014-01-07 21:42:01 -0500708
709 server->stop();
710 thread->join();
711 server.reset();
712 }
713
Roger Meierca142b02011-06-07 17:59:07 +0000714 cout << "done." << endl;
Mark Sleee8540632006-05-30 09:24:40 +0000715 return 0;
716}