blob: 7a16314b7d421ba1c44b4830b7603051ff1460d8 [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>
Roger Meier0a7c69c2014-05-02 21:15:45 +020048#include <thrift/cxxfunctional.h>
Roger Meierca142b02011-06-07 17:59:07 +000049
Bryan Duxburycd9aea12011-02-22 18:12:06 +000050#include <signal.h>
Jake Farrell5d02b802014-01-07 21:42:01 -050051#if _WIN32
52 #include <thrift/windows/TWinsockSingleton.h>
53#endif
David Reissbc3dddb2007-08-22 23:20:24 +000054
Mark Sleee8540632006-05-30 09:24:40 +000055using namespace std;
56
T Jake Lucianib5e62212009-01-31 22:36:20 +000057using namespace apache::thrift;
58using namespace apache::thrift::concurrency;
59using namespace apache::thrift::protocol;
60using namespace apache::thrift::transport;
61using namespace apache::thrift::server;
Roger Meier7e056e72011-07-17 07:28:28 +000062using namespace apache::thrift::async;
Marc Slemko6be374b2006-08-04 03:16:25 +000063
Marc Slemkobf4fd192006-08-15 21:29:39 +000064using namespace thrift::test;
65
Mark Sleed2655522006-09-05 22:09:57 +000066class TestHandler : public ThriftTestIf {
Mark Sleee8540632006-05-30 09:24:40 +000067 public:
Mark Sleed2655522006-09-05 22:09:57 +000068 TestHandler() {}
Mark Sleee8540632006-05-30 09:24:40 +000069
70 void testVoid() {
71 printf("testVoid()\n");
72 }
73
Mark Slee1921d202007-01-24 19:43:06 +000074 void testString(string& out, const string &thing) {
Mark Sleee8540632006-05-30 09:24:40 +000075 printf("testString(\"%s\")\n", thing.c_str());
Mark Slee1921d202007-01-24 19:43:06 +000076 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +000077 }
78
Mark Slee1921d202007-01-24 19:43:06 +000079 int8_t testByte(const int8_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +000080 printf("testByte(%d)\n", (int)thing);
81 return thing;
82 }
83
Mark Slee1921d202007-01-24 19:43:06 +000084 int32_t testI32(const int32_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +000085 printf("testI32(%d)\n", thing);
86 return thing;
87 }
88
Mark Slee1921d202007-01-24 19:43:06 +000089 int64_t testI64(const int64_t thing) {
Roger Meier0e814802014-01-17 21:07:58 +010090 printf("testI64(%" PRId64 ")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +000091 return thing;
92 }
93
Mark Slee1921d202007-01-24 19:43:06 +000094 double testDouble(const double thing) {
Roger Meiera8cef6e2011-07-17 18:55:59 +000095 printf("testDouble(%f)\n", thing);
Mark Sleec98d0502006-09-06 02:42:25 +000096 return thing;
97 }
98
Mark Slee1921d202007-01-24 19:43:06 +000099 void testStruct(Xtruct& out, const Xtruct &thing) {
Roger Meier0e814802014-01-17 21:07:58 +0100100 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 +0000101 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000102 }
103
Mark Slee1921d202007-01-24 19:43:06 +0000104 void testNest(Xtruct2& out, const Xtruct2& nest) {
105 const Xtruct &thing = nest.struct_thing;
Roger Meier0e814802014-01-17 21:07:58 +0100106 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 +0000107 out = nest;
Mark Sleee8540632006-05-30 09:24:40 +0000108 }
109
Mark Slee1921d202007-01-24 19:43:06 +0000110 void testMap(map<int32_t, int32_t> &out, const map<int32_t, int32_t> &thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000111 printf("testMap({");
112 map<int32_t, int32_t>::const_iterator m_iter;
113 bool first = true;
114 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
115 if (first) {
116 first = false;
117 } else {
118 printf(", ");
119 }
120 printf("%d => %d", m_iter->first, m_iter->second);
121 }
122 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000123 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000124 }
125
Roger Meierd3b9dca2011-06-24 14:01:10 +0000126 void testStringMap(map<std::string, std::string> &out, const map<std::string, std::string> &thing) {
127 printf("testMap({");
128 map<std::string, std::string>::const_iterator m_iter;
129 bool first = true;
130 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
131 if (first) {
132 first = false;
133 } else {
134 printf(", ");
135 }
136 printf("%s => %s", (m_iter->first).c_str(), (m_iter->second).c_str());
137 }
138 printf("})\n");
139 out = thing;
140 }
141
Mark Slee1921d202007-01-24 19:43:06 +0000142 void testSet(set<int32_t> &out, const set<int32_t> &thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000143 printf("testSet({");
144 set<int32_t>::const_iterator s_iter;
145 bool first = true;
146 for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) {
147 if (first) {
148 first = false;
149 } else {
150 printf(", ");
151 }
152 printf("%d", *s_iter);
153 }
154 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000155 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000156 }
157
Mark Slee1921d202007-01-24 19:43:06 +0000158 void testList(vector<int32_t> &out, const vector<int32_t> &thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000159 printf("testList({");
Mark Sleeb9acf982006-10-10 01:57:32 +0000160 vector<int32_t>::const_iterator l_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000161 bool first = true;
162 for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) {
163 if (first) {
164 first = false;
165 } else {
166 printf(", ");
167 }
168 printf("%d", *l_iter);
169 }
170 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000171 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000172 }
173
Bryan Duxbury833ae492010-09-27 17:26:02 +0000174 Numberz::type testEnum(const Numberz::type thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000175 printf("testEnum(%d)\n", thing);
176 return thing;
177 }
178
Mark Slee1921d202007-01-24 19:43:06 +0000179 UserId testTypedef(const UserId thing) {
Roger Meier0e814802014-01-17 21:07:58 +0100180 printf("testTypedef(%" PRId64 ")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +0000181 return thing;
182 }
183
Mark Slee1921d202007-01-24 19:43:06 +0000184 void testMapMap(map<int32_t, map<int32_t,int32_t> > &mapmap, const int32_t hello) {
Mark Sleee8540632006-05-30 09:24:40 +0000185 printf("testMapMap(%d)\n", hello);
Mark Sleee8540632006-05-30 09:24:40 +0000186
187 map<int32_t,int32_t> pos;
188 map<int32_t,int32_t> neg;
189 for (int i = 1; i < 5; i++) {
190 pos.insert(make_pair(i,i));
191 neg.insert(make_pair(-i,-i));
192 }
193
194 mapmap.insert(make_pair(4, pos));
195 mapmap.insert(make_pair(-4, neg));
196
Mark Sleee8540632006-05-30 09:24:40 +0000197 }
198
Bryan Duxbury833ae492010-09-27 17:26:02 +0000199 void testInsanity(map<UserId, map<Numberz::type,Insanity> > &insane, const Insanity &argument) {
Roger Meiera8cef6e2011-07-17 18:55:59 +0000200 (void) argument;
Mark Sleee8540632006-05-30 09:24:40 +0000201 printf("testInsanity()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000202
Mark Sleee8540632006-05-30 09:24:40 +0000203 Xtruct hello;
204 hello.string_thing = "Hello2";
205 hello.byte_thing = 2;
Mark Sleee8540632006-05-30 09:24:40 +0000206 hello.i32_thing = 2;
Mark Sleee8540632006-05-30 09:24:40 +0000207 hello.i64_thing = 2;
208
209 Xtruct goodbye;
210 goodbye.string_thing = "Goodbye4";
211 goodbye.byte_thing = 4;
Mark Sleee8540632006-05-30 09:24:40 +0000212 goodbye.i32_thing = 4;
Mark Sleee8540632006-05-30 09:24:40 +0000213 goodbye.i64_thing = 4;
214
215 Insanity crazy;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000216 crazy.userMap.insert(make_pair(Numberz::EIGHT, 8));
Mark Sleee8540632006-05-30 09:24:40 +0000217 crazy.xtructs.push_back(goodbye);
218
219 Insanity looney;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000220 crazy.userMap.insert(make_pair(Numberz::FIVE, 5));
Mark Sleee8540632006-05-30 09:24:40 +0000221 crazy.xtructs.push_back(hello);
222
Bryan Duxbury833ae492010-09-27 17:26:02 +0000223 map<Numberz::type, Insanity> first_map;
224 map<Numberz::type, Insanity> second_map;
Mark Sleee8540632006-05-30 09:24:40 +0000225
Bryan Duxbury833ae492010-09-27 17:26:02 +0000226 first_map.insert(make_pair(Numberz::TWO, crazy));
227 first_map.insert(make_pair(Numberz::THREE, crazy));
Mark Sleee8540632006-05-30 09:24:40 +0000228
Bryan Duxbury833ae492010-09-27 17:26:02 +0000229 second_map.insert(make_pair(Numberz::SIX, looney));
Mark Sleee8540632006-05-30 09:24:40 +0000230
Mark Sleee8540632006-05-30 09:24:40 +0000231 insane.insert(make_pair(1, first_map));
232 insane.insert(make_pair(2, second_map));
233
234 printf("return");
235 printf(" = {");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000236 map<UserId, map<Numberz::type,Insanity> >::const_iterator i_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000237 for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) {
Roger Meier0e814802014-01-17 21:07:58 +0100238 printf("%" PRId64 " => {", i_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000239 map<Numberz::type,Insanity>::const_iterator i2_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000240 for (i2_iter = i_iter->second.begin();
241 i2_iter != i_iter->second.end();
242 ++i2_iter) {
243 printf("%d => {", i2_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000244 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
245 map<Numberz::type, UserId>::const_iterator um;
Mark Sleee8540632006-05-30 09:24:40 +0000246 printf("{");
247 for (um = userMap.begin(); um != userMap.end(); ++um) {
Roger Meier0e814802014-01-17 21:07:58 +0100248 printf("%d => %" PRId64 ", ", um->first, um->second);
Mark Sleee8540632006-05-30 09:24:40 +0000249 }
250 printf("}, ");
251
Mark Sleeb9acf982006-10-10 01:57:32 +0000252 vector<Xtruct> xtructs = i2_iter->second.xtructs;
253 vector<Xtruct>::const_iterator x;
Mark Sleee8540632006-05-30 09:24:40 +0000254 printf("{");
255 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
Roger Meier0e814802014-01-17 21:07:58 +0100256 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 +0000257 }
258 printf("}");
259
260 printf("}, ");
261 }
262 printf("}, ");
263 }
264 printf("}\n");
265
David Reiss0c90f6f2008-02-06 22:18:40 +0000266
Mark Sleee8540632006-05-30 09:24:40 +0000267 }
Marc Slemkoe6889de2006-08-12 00:32:53 +0000268
Bryan Duxbury833ae492010-09-27 17:26:02 +0000269 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) {
Roger Meiera8cef6e2011-07-17 18:55:59 +0000270 (void) arg3;
271 (void) arg4;
272 (void) arg5;
Jake Farrell5d02b802014-01-07 21:42:01 -0500273
Marc Slemkoe6889de2006-08-12 00:32:53 +0000274 printf("testMulti()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000275
Marc Slemkoe6889de2006-08-12 00:32:53 +0000276 hello.string_thing = "Hello2";
277 hello.byte_thing = arg0;
278 hello.i32_thing = arg1;
279 hello.i64_thing = (int64_t)arg2;
Marc Slemkoe6889de2006-08-12 00:32:53 +0000280 }
Marc Slemkobf4fd192006-08-15 21:29:39 +0000281
David Reiss55ff70f2008-06-11 00:58:25 +0000282 void testException(const std::string &arg)
T Jake Lucianib5e62212009-01-31 22:36:20 +0000283 throw(Xception, apache::thrift::TException)
David Reiss55ff70f2008-06-11 00:58:25 +0000284 {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000285 printf("testException(%s)\n", arg.c_str());
Mark Sleed3d733a2006-09-01 22:19:06 +0000286 if (arg.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000287 Xception e;
288 e.errorCode = 1001;
David Reiss9813cbe2009-01-31 21:39:11 +0000289 e.message = arg;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000290 throw e;
Roger Meier1f8b48f2012-05-02 22:56:47 +0000291 } else if (arg.compare("TException") == 0) {
T Jake Lucianib5e62212009-01-31 22:36:20 +0000292 apache::thrift::TException e;
David Reiss55ff70f2008-06-11 00:58:25 +0000293 throw e;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000294 } else {
295 Xtruct result;
296 result.string_thing = arg;
297 return;
298 }
299 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000300
Mark Slee1921d202007-01-24 19:43:06 +0000301 void testMultiException(Xtruct &result, const std::string &arg0, const std::string &arg1) throw(Xception, Xception2) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000302
Marc Slemko71d4e472006-08-15 22:34:04 +0000303 printf("testMultiException(%s, %s)\n", arg0.c_str(), arg1.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000304
Mark Sleef5f2be42006-09-05 21:05:31 +0000305 if (arg0.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000306 Xception e;
307 e.errorCode = 1001;
308 e.message = "This is an Xception";
309 throw e;
Mark Sleef5f2be42006-09-05 21:05:31 +0000310 } else if (arg0.compare("Xception2") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000311 Xception2 e;
312 e.errorCode = 2002;
313 e.struct_thing.string_thing = "This is an Xception2";
314 throw e;
315 } else {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000316 result.string_thing = arg1;
Mark Slee1921d202007-01-24 19:43:06 +0000317 return;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000318 }
319 }
David Reiss2ab6fe82008-02-18 02:11:44 +0000320
Jake Farrell5d02b802014-01-07 21:42:01 -0500321 void testOneway(const int32_t sleepFor) {
David Reiss6ce401d2009-03-24 20:01:58 +0000322 printf("testOneway(%d): Sleeping...\n", sleepFor);
Jake Farrell5d02b802014-01-07 21:42:01 -0500323 THRIFT_SLEEP_SEC(sleepFor);
David Reiss6ce401d2009-03-24 20:01:58 +0000324 printf("testOneway(%d): done sleeping!\n", sleepFor);
David Reiss2ab6fe82008-02-18 02:11:44 +0000325 }
Mark Sleee8540632006-05-30 09:24:40 +0000326};
327
David Reissd7192062010-10-06 17:09:33 +0000328
329class TestProcessorEventHandler : public TProcessorEventHandler {
David Reiss23248712010-10-06 17:10:08 +0000330 virtual void* getContext(const char* fn_name, void* serverContext) {
Roger Meiera8cef6e2011-07-17 18:55:59 +0000331 (void) serverContext;
David Reissd7192062010-10-06 17:09:33 +0000332 return new std::string(fn_name);
333 }
334 virtual void freeContext(void* ctx, const char* fn_name) {
Roger Meiera8cef6e2011-07-17 18:55:59 +0000335 (void) fn_name;
David Reissd7192062010-10-06 17:09:33 +0000336 delete static_cast<std::string*>(ctx);
337 }
338 virtual void preRead(void* ctx, const char* fn_name) {
339 communicate("preRead", ctx, fn_name);
340 }
David Reissef7200f2010-10-06 17:09:42 +0000341 virtual void postRead(void* ctx, const char* fn_name, uint32_t bytes) {
Roger Meiera8cef6e2011-07-17 18:55:59 +0000342 (void) bytes;
David Reissd7192062010-10-06 17:09:33 +0000343 communicate("postRead", ctx, fn_name);
344 }
345 virtual void preWrite(void* ctx, const char* fn_name) {
346 communicate("preWrite", ctx, fn_name);
347 }
David Reissef7200f2010-10-06 17:09:42 +0000348 virtual void postWrite(void* ctx, const char* fn_name, uint32_t bytes) {
Roger Meiera8cef6e2011-07-17 18:55:59 +0000349 (void) bytes;
David Reissd7192062010-10-06 17:09:33 +0000350 communicate("postWrite", ctx, fn_name);
351 }
352 virtual void asyncComplete(void* ctx, const char* fn_name) {
353 communicate("asyncComplete", ctx, fn_name);
354 }
355 virtual void handlerError(void* ctx, const char* fn_name) {
356 communicate("handlerError", ctx, fn_name);
357 }
358
359 void communicate(const char* event, void* ctx, const char* fn_name) {
360 std::cout << event << ": " << *static_cast<std::string*>(ctx) << " = " << fn_name << std::endl;
361 }
362};
363
364
Roger Meier7e056e72011-07-17 07:28:28 +0000365class TestHandlerAsync : public ThriftTestCobSvIf {
366public:
Roger Meier611f90c2011-12-11 22:08:51 +0000367 TestHandlerAsync(boost::shared_ptr<TestHandler>& handler) : _delegate(handler) {}
Roger Meier7e056e72011-07-17 07:28:28 +0000368 virtual ~TestHandlerAsync() {}
369
Roger Meier0a7c69c2014-05-02 21:15:45 +0200370 virtual void testVoid(tcxx::function<void()> cob) {
Roger Meier7e056e72011-07-17 07:28:28 +0000371 _delegate->testVoid();
372 cob();
373 }
374
Roger Meier0a7c69c2014-05-02 21:15:45 +0200375 virtual void testString(tcxx::function<void(std::string const& _return)> cob, const std::string& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000376 std::string res;
377 _delegate->testString(res, thing);
378 cob(res);
379 }
380
Roger Meier0a7c69c2014-05-02 21:15:45 +0200381 virtual void testByte(tcxx::function<void(int8_t const& _return)> cob, const int8_t thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000382 int8_t res = _delegate->testByte(thing);
383 cob(res);
384 }
385
Roger Meier0a7c69c2014-05-02 21:15:45 +0200386 virtual void testI32(tcxx::function<void(int32_t const& _return)> cob, const int32_t thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000387 int32_t res = _delegate->testI32(thing);
388 cob(res);
389 }
390
Roger Meier0a7c69c2014-05-02 21:15:45 +0200391 virtual void testI64(tcxx::function<void(int64_t const& _return)> cob, const int64_t thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000392 int64_t res = _delegate->testI64(thing);
393 cob(res);
394 }
395
Roger Meier0a7c69c2014-05-02 21:15:45 +0200396 virtual void testDouble(tcxx::function<void(double const& _return)> cob, const double thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000397 double res = _delegate->testDouble(thing);
398 cob(res);
399 }
400
Roger Meier0a7c69c2014-05-02 21:15:45 +0200401 virtual void testStruct(tcxx::function<void(Xtruct const& _return)> cob, const Xtruct& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000402 Xtruct res;
403 _delegate->testStruct(res, thing);
404 cob(res);
405 }
406
Roger Meier0a7c69c2014-05-02 21:15:45 +0200407 virtual void testNest(tcxx::function<void(Xtruct2 const& _return)> cob, const Xtruct2& thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000408 Xtruct2 res;
409 _delegate->testNest(res, thing);
410 cob(res);
411 }
412
Roger Meier0a7c69c2014-05-02 21:15:45 +0200413 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 +0000414 std::map<int32_t, int32_t> res;
415 _delegate->testMap(res, thing);
416 cob(res);
417 }
418
Roger Meier0a7c69c2014-05-02 21:15:45 +0200419 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 +0000420 std::map<std::string, std::string> res;
421 _delegate->testStringMap(res, thing);
422 cob(res);
423 }
424
Roger Meier0a7c69c2014-05-02 21:15:45 +0200425 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 +0000426 std::set<int32_t> res;
427 _delegate->testSet(res, thing);
428 cob(res);
429 }
430
Roger Meier0a7c69c2014-05-02 21:15:45 +0200431 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 +0000432 std::vector<int32_t> res;
433 _delegate->testList(res, thing);
434 cob(res);
435 }
436
Roger Meier0a7c69c2014-05-02 21:15:45 +0200437 virtual void testEnum(tcxx::function<void(Numberz::type const& _return)> cob, const Numberz::type thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000438 Numberz::type res = _delegate->testEnum(thing);
439 cob(res);
440 }
441
Roger Meier0a7c69c2014-05-02 21:15:45 +0200442 virtual void testTypedef(tcxx::function<void(UserId const& _return)> cob, const UserId thing) {
Roger Meier7e056e72011-07-17 07:28:28 +0000443 UserId res = _delegate->testTypedef(thing);
444 cob(res);
445 }
446
Roger Meier0a7c69c2014-05-02 21:15:45 +0200447 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 +0000448 std::map<int32_t, std::map<int32_t, int32_t> > res;
449 _delegate->testMapMap(res, hello);
450 cob(res);
451 }
452
Roger Meier0a7c69c2014-05-02 21:15:45 +0200453 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 -0500454 std::map<UserId, std::map<Numberz::type, Insanity> > res;
Roger Meier7e056e72011-07-17 07:28:28 +0000455 _delegate->testInsanity(res, argument);
456 cob(res);
457 }
458
Roger Meier0a7c69c2014-05-02 21:15:45 +0200459 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 +0000460 Xtruct res;
461 _delegate->testMulti(res, arg0, arg1, arg2, arg3, arg4, arg5);
462 cob(res);
463 }
464
Roger Meier0a7c69c2014-05-02 21:15:45 +0200465 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 +0000466 try {
467 _delegate->testException(arg);
468 } catch(const apache::thrift::TException& e) {
469 exn_cob(apache::thrift::TDelayedException::delayException(e));
470 return;
471 }
472 cob();
473 }
474
Roger Meier0a7c69c2014-05-02 21:15:45 +0200475 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 +0000476 Xtruct res;
477 try {
478 _delegate->testMultiException(res, arg0, arg1);
479 } catch(const apache::thrift::TException& e) {
480 exn_cob(apache::thrift::TDelayedException::delayException(e));
481 return;
482 }
483 cob(res);
484 }
485
Roger Meier0a7c69c2014-05-02 21:15:45 +0200486 virtual void testOneway(tcxx::function<void()> cob, const int32_t secondsToSleep) {
Roger Meier7e056e72011-07-17 07:28:28 +0000487 _delegate->testOneway(secondsToSleep);
488 cob();
489 }
490
491protected:
Roger Meier611f90c2011-12-11 22:08:51 +0000492 boost::shared_ptr<TestHandler> _delegate;
Roger Meier7e056e72011-07-17 07:28:28 +0000493};
494
495
Mark Sleee8540632006-05-30 09:24:40 +0000496int main(int argc, char **argv) {
Jake Farrell5d02b802014-01-07 21:42:01 -0500497#if _WIN32
498 transport::TWinsockSingleton::create();
499#endif
Mark Sleee8540632006-05-30 09:24:40 +0000500 int port = 9090;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000501 bool ssl = false;
Roger Meierca142b02011-06-07 17:59:07 +0000502 string transport_type = "buffered";
503 string protocol_type = "binary";
504 string server_type = "simple";
505 string domain_socket = "";
506 size_t workers = 4;
Marc Slemko6be374b2006-08-04 03:16:25 +0000507
Jake Farrell5d02b802014-01-07 21:42:01 -0500508
509 boost::program_options::options_description desc("Allowed options");
Roger Meierca142b02011-06-07 17:59:07 +0000510 desc.add_options()
511 ("help,h", "produce help message")
Jake Farrell5d02b802014-01-07 21:42:01 -0500512 ("port", boost::program_options::value<int>(&port)->default_value(port), "Port number to listen")
513 ("domain-socket", boost::program_options::value<string>(&domain_socket)->default_value(domain_socket),
Roger Meierca142b02011-06-07 17:59:07 +0000514 "Unix Domain Socket (e.g. /tmp/ThriftTest.thrift)")
Jake Farrell5d02b802014-01-07 21:42:01 -0500515 ("server-type", boost::program_options::value<string>(&server_type)->default_value(server_type),
Roger Meierca142b02011-06-07 17:59:07 +0000516 "type of server, \"simple\", \"thread-pool\", \"threaded\", or \"nonblocking\"")
Jake Farrell5d02b802014-01-07 21:42:01 -0500517 ("transport", boost::program_options::value<string>(&transport_type)->default_value(transport_type),
Roger Meierca142b02011-06-07 17:59:07 +0000518 "transport: buffered, framed, http")
Jake Farrell5d02b802014-01-07 21:42:01 -0500519 ("protocol", boost::program_options::value<string>(&protocol_type)->default_value(protocol_type),
Roger Meier023192f2014-02-12 09:35:12 +0100520 "protocol: binary, compact, json")
Roger Meierca142b02011-06-07 17:59:07 +0000521 ("ssl", "Encrypted Transport using SSL")
522 ("processor-events", "processor-events")
Jake Farrell5d02b802014-01-07 21:42:01 -0500523 ("workers,n", boost::program_options::value<size_t>(&workers)->default_value(workers),
Roger Meierca142b02011-06-07 17:59:07 +0000524 "Number of thread pools workers. Only valid for thread-pool server type")
525 ;
Marc Slemko6be374b2006-08-04 03:16:25 +0000526
Jake Farrell5d02b802014-01-07 21:42:01 -0500527 boost::program_options::variables_map vm;
528 boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
529 boost::program_options::notify(vm);
Marc Slemko6be374b2006-08-04 03:16:25 +0000530
Roger Meierca142b02011-06-07 17:59:07 +0000531 if (vm.count("help")) {
532 cout << desc << "\n";
533 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000534 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500535
Marc Slemko6be374b2006-08-04 03:16:25 +0000536 try {
Roger Meierca142b02011-06-07 17:59:07 +0000537 if (!server_type.empty()) {
538 if (server_type == "simple") {
539 } else if (server_type == "thread-pool") {
540 } else if (server_type == "threaded") {
541 } else if (server_type == "nonblocking") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000542 } else {
Roger Meierca142b02011-06-07 17:59:07 +0000543 throw invalid_argument("Unknown server type "+server_type);
544 }
545 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500546
Roger Meierca142b02011-06-07 17:59:07 +0000547 if (!protocol_type.empty()) {
548 if (protocol_type == "binary") {
Roger Meier284101c2014-03-11 21:20:35 +0100549 } else if (protocol_type == "compact") {
Roger Meierca142b02011-06-07 17:59:07 +0000550 } else if (protocol_type == "json") {
551 } else {
552 throw invalid_argument("Unknown protocol type "+protocol_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000553 }
554 }
555
Roger Meier284101c2014-03-11 21:20:35 +0100556 if (!transport_type.empty()) {
Roger Meierca142b02011-06-07 17:59:07 +0000557 if (transport_type == "buffered") {
558 } else if (transport_type == "framed") {
559 } else if (transport_type == "http") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000560 } else {
Roger Meierca142b02011-06-07 17:59:07 +0000561 throw invalid_argument("Unknown transport type "+transport_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000562 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000563 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000564
Bryan Duxbury833ae492010-09-27 17:26:02 +0000565 } catch (std::exception& e) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000566 cerr << e.what() << endl;
Roger Meierca142b02011-06-07 17:59:07 +0000567 cout << desc << "\n";
568 return 1;
Marc Slemko6be374b2006-08-04 03:16:25 +0000569 }
570
Roger Meierca142b02011-06-07 17:59:07 +0000571 if (vm.count("ssl")) {
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000572 ssl = true;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000573 }
574
Mark Sleee8540632006-05-30 09:24:40 +0000575 // Dispatcher
Roger Meier611f90c2011-12-11 22:08:51 +0000576 boost::shared_ptr<TProtocolFactory> protocolFactory;
Roger Meierca142b02011-06-07 17:59:07 +0000577 if (protocol_type == "json") {
Roger Meier611f90c2011-12-11 22:08:51 +0000578 boost::shared_ptr<TProtocolFactory> jsonProtocolFactory(new TJSONProtocolFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000579 protocolFactory = jsonProtocolFactory;
Roger Meier023192f2014-02-12 09:35:12 +0100580 } else if (protocol_type == "compact") {
581 boost::shared_ptr<TProtocolFactory> compactProtocolFactory(new TCompactProtocolFactory());
582 protocolFactory = compactProtocolFactory;
Roger Meierca142b02011-06-07 17:59:07 +0000583 } else {
Roger Meier611f90c2011-12-11 22:08:51 +0000584 boost::shared_ptr<TProtocolFactory> binaryProtocolFactory(new TBinaryProtocolFactoryT<TBufferBase>());
Roger Meierca142b02011-06-07 17:59:07 +0000585 protocolFactory = binaryProtocolFactory;
586 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000587
Roger Meierca142b02011-06-07 17:59:07 +0000588 // Processor
Roger Meier611f90c2011-12-11 22:08:51 +0000589 boost::shared_ptr<TestHandler> testHandler(new TestHandler());
590 boost::shared_ptr<ThriftTestProcessor> testProcessor(new ThriftTestProcessor(testHandler));
Jake Farrell5d02b802014-01-07 21:42:01 -0500591
Roger Meierca142b02011-06-07 17:59:07 +0000592 if (vm.count("processor-events")) {
Roger Meier611f90c2011-12-11 22:08:51 +0000593 testProcessor->setEventHandler(boost::shared_ptr<TProcessorEventHandler>(
David Reissd7192062010-10-06 17:09:33 +0000594 new TestProcessorEventHandler()));
595 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500596
Mark Sleee8540632006-05-30 09:24:40 +0000597 // Transport
Roger Meier611f90c2011-12-11 22:08:51 +0000598 boost::shared_ptr<TSSLSocketFactory> sslSocketFactory;
599 boost::shared_ptr<TServerSocket> serverSocket;
Mark Sleee8540632006-05-30 09:24:40 +0000600
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000601 if (ssl) {
Roger Meier611f90c2011-12-11 22:08:51 +0000602 sslSocketFactory = boost::shared_ptr<TSSLSocketFactory>(new TSSLSocketFactory());
Roger Meierc94b2932014-02-22 20:07:33 +0100603 sslSocketFactory->loadCertificate("keys/server.crt");
604 sslSocketFactory->loadPrivateKey("keys/server.key");
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000605 sslSocketFactory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
Roger Meier611f90c2011-12-11 22:08:51 +0000606 serverSocket = boost::shared_ptr<TServerSocket>(new TSSLServerSocket(port, sslSocketFactory));
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000607 } else {
Roger Meierc94b2932014-02-22 20:07:33 +0100608 if (domain_socket != "") {
609 unlink(domain_socket.c_str());
610 serverSocket = boost::shared_ptr<TServerSocket>(new TServerSocket(domain_socket));
611 port = 0;
612 } else {
Roger Meier611f90c2011-12-11 22:08:51 +0000613 serverSocket = boost::shared_ptr<TServerSocket>(new TServerSocket(port));
Roger Meierc94b2932014-02-22 20:07:33 +0100614 }
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000615 }
Roger Meierca142b02011-06-07 17:59:07 +0000616
Mark Sleed788b2e2006-09-07 01:26:35 +0000617 // Factory
Roger Meier611f90c2011-12-11 22:08:51 +0000618 boost::shared_ptr<TTransportFactory> transportFactory;
Jake Farrell5d02b802014-01-07 21:42:01 -0500619
Roger Meier7e056e72011-07-17 07:28:28 +0000620 if (transport_type == "http" && server_type != "nonblocking") {
Jake Farrell5d02b802014-01-07 21:42:01 -0500621 boost::shared_ptr<TTransportFactory> httpTransportFactory(new THttpServerTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000622 transportFactory = httpTransportFactory;
623 } else if (transport_type == "framed") {
Jake Farrell5d02b802014-01-07 21:42:01 -0500624 boost::shared_ptr<TTransportFactory> framedTransportFactory(new TFramedTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000625 transportFactory = framedTransportFactory;
626 } else {
Jake Farrell5d02b802014-01-07 21:42:01 -0500627 boost::shared_ptr<TTransportFactory> bufferedTransportFactory(new TBufferedTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000628 transportFactory = bufferedTransportFactory;
629 }
Mark Sleed788b2e2006-09-07 01:26:35 +0000630
Roger Meierca142b02011-06-07 17:59:07 +0000631 // Server Info
632 cout << "Starting \"" << server_type << "\" server ("
633 << transport_type << "/" << protocol_type << ") listen on: " << domain_socket;
634 if (port != 0) {
635 cout << port;
636 }
637 cout << endl;
Mark Sleee8540632006-05-30 09:24:40 +0000638
Roger Meierca142b02011-06-07 17:59:07 +0000639 // Server
Jake Farrell5d02b802014-01-07 21:42:01 -0500640 boost::shared_ptr<apache::thrift::server::TServer> server;
641
Roger Meierca142b02011-06-07 17:59:07 +0000642 if (server_type == "simple") {
Jake Farrell5d02b802014-01-07 21:42:01 -0500643 server.reset(new TSimpleServer(testProcessor,
Roger Meierca142b02011-06-07 17:59:07 +0000644 serverSocket,
Mark Sleed788b2e2006-09-07 01:26:35 +0000645 transportFactory,
Jake Farrell5d02b802014-01-07 21:42:01 -0500646 protocolFactory));
Roger Meierca142b02011-06-07 17:59:07 +0000647 } else if (server_type == "thread-pool") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000648
Roger Meier611f90c2011-12-11 22:08:51 +0000649 boost::shared_ptr<ThreadManager> threadManager =
Roger Meierca142b02011-06-07 17:59:07 +0000650 ThreadManager::newSimpleThreadManager(workers);
Marc Slemko6be374b2006-08-04 03:16:25 +0000651
Roger Meier611f90c2011-12-11 22:08:51 +0000652 boost::shared_ptr<PlatformThreadFactory> threadFactory =
653 boost::shared_ptr<PlatformThreadFactory>(new PlatformThreadFactory());
Marc Slemko6be374b2006-08-04 03:16:25 +0000654
655 threadManager->threadFactory(threadFactory);
656
657 threadManager->start();
658
Jake Farrell5d02b802014-01-07 21:42:01 -0500659 server.reset(new TThreadPoolServer(testProcessor,
Roger Meierca142b02011-06-07 17:59:07 +0000660 serverSocket,
Mark Sleed788b2e2006-09-07 01:26:35 +0000661 transportFactory,
Mark Sleea3302652006-10-25 19:03:32 +0000662 protocolFactory,
Jake Farrell5d02b802014-01-07 21:42:01 -0500663 threadManager));
Roger Meierca142b02011-06-07 17:59:07 +0000664 } else if (server_type == "threaded") {
David Reiss0c90f6f2008-02-06 22:18:40 +0000665
Jake Farrell5d02b802014-01-07 21:42:01 -0500666 server.reset(new TThreadedServer(testProcessor,
Mark Slee0c2dff32007-03-09 19:26:57 +0000667 serverSocket,
668 transportFactory,
Jake Farrell5d02b802014-01-07 21:42:01 -0500669 protocolFactory));
Roger Meierca142b02011-06-07 17:59:07 +0000670 } else if (server_type == "nonblocking") {
Roger Meier7e056e72011-07-17 07:28:28 +0000671 if(transport_type == "http") {
Roger Meier611f90c2011-12-11 22:08:51 +0000672 boost::shared_ptr<TestHandlerAsync> testHandlerAsync(new TestHandlerAsync(testHandler));
673 boost::shared_ptr<TAsyncProcessor> testProcessorAsync(new ThriftTestAsyncProcessor(testHandlerAsync));
674 boost::shared_ptr<TAsyncBufferProcessor> testBufferProcessor(new TAsyncProtocolProcessor(testProcessorAsync, protocolFactory));
Jake Farrell5d02b802014-01-07 21:42:01 -0500675
676 // not loading nonblockingServer into "server" because
677 // TEvhttpServer doesn't inherit from TServer, and doesn't
678 // provide a stop method.
Roger Meier7e056e72011-07-17 07:28:28 +0000679 TEvhttpServer nonblockingServer(testBufferProcessor, port);
680 nonblockingServer.serve();
Jake Farrell5d02b802014-01-07 21:42:01 -0500681 } else {
682 server.reset(new TNonblockingServer(testProcessor, port));
Roger Meier7e056e72011-07-17 07:28:28 +0000683 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000684 }
685
Jake Farrell5d02b802014-01-07 21:42:01 -0500686 if(server.get() != NULL)
687 {
688 apache::thrift::concurrency::PlatformThreadFactory factory;
689 factory.setDetached(false);
690 boost::shared_ptr<apache::thrift::concurrency::Runnable> serverThreadRunner(server);
691 boost::shared_ptr<apache::thrift::concurrency::Thread> thread = factory.newThread(serverThreadRunner);
692 thread->start();
693
Roger Meier284101c2014-03-11 21:20:35 +0100694 // HACK: cross language test suite is unable to handle cin properly
695 // that's why we stay in a endless loop here
696 while(1){}
697 // FIXME: find another way to stop the server (e.g. a signal)
698 // cout<<"Press enter to stop the server."<<endl;
699 // cin.ignore(); //wait until a key is pressed
Jake Farrell5d02b802014-01-07 21:42:01 -0500700
701 server->stop();
702 thread->join();
703 server.reset();
704 }
705
Roger Meierca142b02011-06-07 17:59:07 +0000706 cout << "done." << endl;
Mark Sleee8540632006-05-30 09:24:40 +0000707 return 0;
708}