blob: 11ed3599bcc2b46e6d3137c5a3b4a7c0d0844dc6 [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>
48
Bryan Duxburycd9aea12011-02-22 18:12:06 +000049#include <signal.h>
Jake Farrell5d02b802014-01-07 21:42:01 -050050#if _WIN32
51 #include <thrift/windows/TWinsockSingleton.h>
52#endif
David Reissbc3dddb2007-08-22 23:20:24 +000053
Mark Sleee8540632006-05-30 09:24:40 +000054using namespace std;
55
T Jake Lucianib5e62212009-01-31 22:36:20 +000056using namespace apache::thrift;
57using namespace apache::thrift::concurrency;
58using namespace apache::thrift::protocol;
59using namespace apache::thrift::transport;
60using namespace apache::thrift::server;
Roger Meier7e056e72011-07-17 07:28:28 +000061using namespace apache::thrift::async;
Marc Slemko6be374b2006-08-04 03:16:25 +000062
Marc Slemkobf4fd192006-08-15 21:29:39 +000063using namespace thrift::test;
64
Mark Sleed2655522006-09-05 22:09:57 +000065class TestHandler : public ThriftTestIf {
Mark Sleee8540632006-05-30 09:24:40 +000066 public:
Mark Sleed2655522006-09-05 22:09:57 +000067 TestHandler() {}
Mark Sleee8540632006-05-30 09:24:40 +000068
69 void testVoid() {
70 printf("testVoid()\n");
71 }
72
Mark Slee1921d202007-01-24 19:43:06 +000073 void testString(string& out, const string &thing) {
Mark Sleee8540632006-05-30 09:24:40 +000074 printf("testString(\"%s\")\n", thing.c_str());
Mark Slee1921d202007-01-24 19:43:06 +000075 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +000076 }
77
Mark Slee1921d202007-01-24 19:43:06 +000078 int8_t testByte(const int8_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +000079 printf("testByte(%d)\n", (int)thing);
80 return thing;
81 }
82
Mark Slee1921d202007-01-24 19:43:06 +000083 int32_t testI32(const int32_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +000084 printf("testI32(%d)\n", thing);
85 return thing;
86 }
87
Mark Slee1921d202007-01-24 19:43:06 +000088 int64_t testI64(const int64_t thing) {
Roger Meier0e814802014-01-17 21:07:58 +010089 printf("testI64(%" PRId64 ")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +000090 return thing;
91 }
92
Mark Slee1921d202007-01-24 19:43:06 +000093 double testDouble(const double thing) {
Roger Meiera8cef6e2011-07-17 18:55:59 +000094 printf("testDouble(%f)\n", thing);
Mark Sleec98d0502006-09-06 02:42:25 +000095 return thing;
96 }
97
Mark Slee1921d202007-01-24 19:43:06 +000098 void testStruct(Xtruct& out, const Xtruct &thing) {
Roger Meier0e814802014-01-17 21:07:58 +010099 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 +0000100 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000101 }
102
Mark Slee1921d202007-01-24 19:43:06 +0000103 void testNest(Xtruct2& out, const Xtruct2& nest) {
104 const Xtruct &thing = nest.struct_thing;
Roger Meier0e814802014-01-17 21:07:58 +0100105 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 +0000106 out = nest;
Mark Sleee8540632006-05-30 09:24:40 +0000107 }
108
Mark Slee1921d202007-01-24 19:43:06 +0000109 void testMap(map<int32_t, int32_t> &out, const map<int32_t, int32_t> &thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000110 printf("testMap({");
111 map<int32_t, int32_t>::const_iterator m_iter;
112 bool first = true;
113 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
114 if (first) {
115 first = false;
116 } else {
117 printf(", ");
118 }
119 printf("%d => %d", m_iter->first, m_iter->second);
120 }
121 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000122 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000123 }
124
Roger Meierd3b9dca2011-06-24 14:01:10 +0000125 void testStringMap(map<std::string, std::string> &out, const map<std::string, std::string> &thing) {
126 printf("testMap({");
127 map<std::string, std::string>::const_iterator m_iter;
128 bool first = true;
129 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
130 if (first) {
131 first = false;
132 } else {
133 printf(", ");
134 }
135 printf("%s => %s", (m_iter->first).c_str(), (m_iter->second).c_str());
136 }
137 printf("})\n");
138 out = thing;
139 }
140
Mark Slee1921d202007-01-24 19:43:06 +0000141 void testSet(set<int32_t> &out, const set<int32_t> &thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000142 printf("testSet({");
143 set<int32_t>::const_iterator s_iter;
144 bool first = true;
145 for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) {
146 if (first) {
147 first = false;
148 } else {
149 printf(", ");
150 }
151 printf("%d", *s_iter);
152 }
153 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000154 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000155 }
156
Mark Slee1921d202007-01-24 19:43:06 +0000157 void testList(vector<int32_t> &out, const vector<int32_t> &thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000158 printf("testList({");
Mark Sleeb9acf982006-10-10 01:57:32 +0000159 vector<int32_t>::const_iterator l_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000160 bool first = true;
161 for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) {
162 if (first) {
163 first = false;
164 } else {
165 printf(", ");
166 }
167 printf("%d", *l_iter);
168 }
169 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000170 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000171 }
172
Bryan Duxbury833ae492010-09-27 17:26:02 +0000173 Numberz::type testEnum(const Numberz::type thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000174 printf("testEnum(%d)\n", thing);
175 return thing;
176 }
177
Mark Slee1921d202007-01-24 19:43:06 +0000178 UserId testTypedef(const UserId thing) {
Roger Meier0e814802014-01-17 21:07:58 +0100179 printf("testTypedef(%" PRId64 ")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +0000180 return thing;
181 }
182
Mark Slee1921d202007-01-24 19:43:06 +0000183 void testMapMap(map<int32_t, map<int32_t,int32_t> > &mapmap, const int32_t hello) {
Mark Sleee8540632006-05-30 09:24:40 +0000184 printf("testMapMap(%d)\n", hello);
Mark Sleee8540632006-05-30 09:24:40 +0000185
186 map<int32_t,int32_t> pos;
187 map<int32_t,int32_t> neg;
188 for (int i = 1; i < 5; i++) {
189 pos.insert(make_pair(i,i));
190 neg.insert(make_pair(-i,-i));
191 }
192
193 mapmap.insert(make_pair(4, pos));
194 mapmap.insert(make_pair(-4, neg));
195
Mark Sleee8540632006-05-30 09:24:40 +0000196 }
197
Bryan Duxbury833ae492010-09-27 17:26:02 +0000198 void testInsanity(map<UserId, map<Numberz::type,Insanity> > &insane, const Insanity &argument) {
Roger Meiera8cef6e2011-07-17 18:55:59 +0000199 (void) argument;
Mark Sleee8540632006-05-30 09:24:40 +0000200 printf("testInsanity()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000201
Mark Sleee8540632006-05-30 09:24:40 +0000202 Xtruct hello;
203 hello.string_thing = "Hello2";
204 hello.byte_thing = 2;
Mark Sleee8540632006-05-30 09:24:40 +0000205 hello.i32_thing = 2;
Mark Sleee8540632006-05-30 09:24:40 +0000206 hello.i64_thing = 2;
207
208 Xtruct goodbye;
209 goodbye.string_thing = "Goodbye4";
210 goodbye.byte_thing = 4;
Mark Sleee8540632006-05-30 09:24:40 +0000211 goodbye.i32_thing = 4;
Mark Sleee8540632006-05-30 09:24:40 +0000212 goodbye.i64_thing = 4;
213
214 Insanity crazy;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000215 crazy.userMap.insert(make_pair(Numberz::EIGHT, 8));
Mark Sleee8540632006-05-30 09:24:40 +0000216 crazy.xtructs.push_back(goodbye);
217
218 Insanity looney;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000219 crazy.userMap.insert(make_pair(Numberz::FIVE, 5));
Mark Sleee8540632006-05-30 09:24:40 +0000220 crazy.xtructs.push_back(hello);
221
Bryan Duxbury833ae492010-09-27 17:26:02 +0000222 map<Numberz::type, Insanity> first_map;
223 map<Numberz::type, Insanity> second_map;
Mark Sleee8540632006-05-30 09:24:40 +0000224
Bryan Duxbury833ae492010-09-27 17:26:02 +0000225 first_map.insert(make_pair(Numberz::TWO, crazy));
226 first_map.insert(make_pair(Numberz::THREE, crazy));
Mark Sleee8540632006-05-30 09:24:40 +0000227
Bryan Duxbury833ae492010-09-27 17:26:02 +0000228 second_map.insert(make_pair(Numberz::SIX, looney));
Mark Sleee8540632006-05-30 09:24:40 +0000229
Mark Sleee8540632006-05-30 09:24:40 +0000230 insane.insert(make_pair(1, first_map));
231 insane.insert(make_pair(2, second_map));
232
233 printf("return");
234 printf(" = {");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000235 map<UserId, map<Numberz::type,Insanity> >::const_iterator i_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000236 for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) {
Roger Meier0e814802014-01-17 21:07:58 +0100237 printf("%" PRId64 " => {", i_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000238 map<Numberz::type,Insanity>::const_iterator i2_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000239 for (i2_iter = i_iter->second.begin();
240 i2_iter != i_iter->second.end();
241 ++i2_iter) {
242 printf("%d => {", i2_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000243 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
244 map<Numberz::type, UserId>::const_iterator um;
Mark Sleee8540632006-05-30 09:24:40 +0000245 printf("{");
246 for (um = userMap.begin(); um != userMap.end(); ++um) {
Roger Meier0e814802014-01-17 21:07:58 +0100247 printf("%d => %" PRId64 ", ", um->first, um->second);
Mark Sleee8540632006-05-30 09:24:40 +0000248 }
249 printf("}, ");
250
Mark Sleeb9acf982006-10-10 01:57:32 +0000251 vector<Xtruct> xtructs = i2_iter->second.xtructs;
252 vector<Xtruct>::const_iterator x;
Mark Sleee8540632006-05-30 09:24:40 +0000253 printf("{");
254 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
Roger Meier0e814802014-01-17 21:07:58 +0100255 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 +0000256 }
257 printf("}");
258
259 printf("}, ");
260 }
261 printf("}, ");
262 }
263 printf("}\n");
264
David Reiss0c90f6f2008-02-06 22:18:40 +0000265
Mark Sleee8540632006-05-30 09:24:40 +0000266 }
Marc Slemkoe6889de2006-08-12 00:32:53 +0000267
Bryan Duxbury833ae492010-09-27 17:26:02 +0000268 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 +0000269 (void) arg3;
270 (void) arg4;
271 (void) arg5;
Jake Farrell5d02b802014-01-07 21:42:01 -0500272
Marc Slemkoe6889de2006-08-12 00:32:53 +0000273 printf("testMulti()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000274
Marc Slemkoe6889de2006-08-12 00:32:53 +0000275 hello.string_thing = "Hello2";
276 hello.byte_thing = arg0;
277 hello.i32_thing = arg1;
278 hello.i64_thing = (int64_t)arg2;
Marc Slemkoe6889de2006-08-12 00:32:53 +0000279 }
Marc Slemkobf4fd192006-08-15 21:29:39 +0000280
David Reiss55ff70f2008-06-11 00:58:25 +0000281 void testException(const std::string &arg)
T Jake Lucianib5e62212009-01-31 22:36:20 +0000282 throw(Xception, apache::thrift::TException)
David Reiss55ff70f2008-06-11 00:58:25 +0000283 {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000284 printf("testException(%s)\n", arg.c_str());
Mark Sleed3d733a2006-09-01 22:19:06 +0000285 if (arg.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000286 Xception e;
287 e.errorCode = 1001;
David Reiss9813cbe2009-01-31 21:39:11 +0000288 e.message = arg;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000289 throw e;
Roger Meier1f8b48f2012-05-02 22:56:47 +0000290 } else if (arg.compare("TException") == 0) {
T Jake Lucianib5e62212009-01-31 22:36:20 +0000291 apache::thrift::TException e;
David Reiss55ff70f2008-06-11 00:58:25 +0000292 throw e;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000293 } else {
294 Xtruct result;
295 result.string_thing = arg;
296 return;
297 }
298 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000299
Mark Slee1921d202007-01-24 19:43:06 +0000300 void testMultiException(Xtruct &result, const std::string &arg0, const std::string &arg1) throw(Xception, Xception2) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000301
Marc Slemko71d4e472006-08-15 22:34:04 +0000302 printf("testMultiException(%s, %s)\n", arg0.c_str(), arg1.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000303
Mark Sleef5f2be42006-09-05 21:05:31 +0000304 if (arg0.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000305 Xception e;
306 e.errorCode = 1001;
307 e.message = "This is an Xception";
308 throw e;
Mark Sleef5f2be42006-09-05 21:05:31 +0000309 } else if (arg0.compare("Xception2") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000310 Xception2 e;
311 e.errorCode = 2002;
312 e.struct_thing.string_thing = "This is an Xception2";
313 throw e;
314 } else {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000315 result.string_thing = arg1;
Mark Slee1921d202007-01-24 19:43:06 +0000316 return;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000317 }
318 }
David Reiss2ab6fe82008-02-18 02:11:44 +0000319
Jake Farrell5d02b802014-01-07 21:42:01 -0500320 void testOneway(const int32_t sleepFor) {
David Reiss6ce401d2009-03-24 20:01:58 +0000321 printf("testOneway(%d): Sleeping...\n", sleepFor);
Jake Farrell5d02b802014-01-07 21:42:01 -0500322 THRIFT_SLEEP_SEC(sleepFor);
David Reiss6ce401d2009-03-24 20:01:58 +0000323 printf("testOneway(%d): done sleeping!\n", sleepFor);
David Reiss2ab6fe82008-02-18 02:11:44 +0000324 }
Mark Sleee8540632006-05-30 09:24:40 +0000325};
326
David Reissd7192062010-10-06 17:09:33 +0000327
328class TestProcessorEventHandler : public TProcessorEventHandler {
David Reiss23248712010-10-06 17:10:08 +0000329 virtual void* getContext(const char* fn_name, void* serverContext) {
Roger Meiera8cef6e2011-07-17 18:55:59 +0000330 (void) serverContext;
David Reissd7192062010-10-06 17:09:33 +0000331 return new std::string(fn_name);
332 }
333 virtual void freeContext(void* ctx, const char* fn_name) {
Roger Meiera8cef6e2011-07-17 18:55:59 +0000334 (void) fn_name;
David Reissd7192062010-10-06 17:09:33 +0000335 delete static_cast<std::string*>(ctx);
336 }
337 virtual void preRead(void* ctx, const char* fn_name) {
338 communicate("preRead", ctx, fn_name);
339 }
David Reissef7200f2010-10-06 17:09:42 +0000340 virtual void postRead(void* ctx, const char* fn_name, uint32_t bytes) {
Roger Meiera8cef6e2011-07-17 18:55:59 +0000341 (void) bytes;
David Reissd7192062010-10-06 17:09:33 +0000342 communicate("postRead", ctx, fn_name);
343 }
344 virtual void preWrite(void* ctx, const char* fn_name) {
345 communicate("preWrite", ctx, fn_name);
346 }
David Reissef7200f2010-10-06 17:09:42 +0000347 virtual void postWrite(void* ctx, const char* fn_name, uint32_t bytes) {
Roger Meiera8cef6e2011-07-17 18:55:59 +0000348 (void) bytes;
David Reissd7192062010-10-06 17:09:33 +0000349 communicate("postWrite", ctx, fn_name);
350 }
351 virtual void asyncComplete(void* ctx, const char* fn_name) {
352 communicate("asyncComplete", ctx, fn_name);
353 }
354 virtual void handlerError(void* ctx, const char* fn_name) {
355 communicate("handlerError", ctx, fn_name);
356 }
357
358 void communicate(const char* event, void* ctx, const char* fn_name) {
359 std::cout << event << ": " << *static_cast<std::string*>(ctx) << " = " << fn_name << std::endl;
360 }
361};
362
363
Roger Meier7e056e72011-07-17 07:28:28 +0000364class TestHandlerAsync : public ThriftTestCobSvIf {
365public:
Roger Meier611f90c2011-12-11 22:08:51 +0000366 TestHandlerAsync(boost::shared_ptr<TestHandler>& handler) : _delegate(handler) {}
Roger Meier7e056e72011-07-17 07:28:28 +0000367 virtual ~TestHandlerAsync() {}
368
369 virtual void testVoid(std::tr1::function<void()> cob) {
370 _delegate->testVoid();
371 cob();
372 }
373
374 virtual void testString(std::tr1::function<void(std::string const& _return)> cob, const std::string& thing) {
375 std::string res;
376 _delegate->testString(res, thing);
377 cob(res);
378 }
379
380 virtual void testByte(std::tr1::function<void(int8_t const& _return)> cob, const int8_t thing) {
381 int8_t res = _delegate->testByte(thing);
382 cob(res);
383 }
384
385 virtual void testI32(std::tr1::function<void(int32_t const& _return)> cob, const int32_t thing) {
386 int32_t res = _delegate->testI32(thing);
387 cob(res);
388 }
389
390 virtual void testI64(std::tr1::function<void(int64_t const& _return)> cob, const int64_t thing) {
391 int64_t res = _delegate->testI64(thing);
392 cob(res);
393 }
394
395 virtual void testDouble(std::tr1::function<void(double const& _return)> cob, const double thing) {
396 double res = _delegate->testDouble(thing);
397 cob(res);
398 }
399
400 virtual void testStruct(std::tr1::function<void(Xtruct const& _return)> cob, const Xtruct& thing) {
401 Xtruct res;
402 _delegate->testStruct(res, thing);
403 cob(res);
404 }
405
406 virtual void testNest(std::tr1::function<void(Xtruct2 const& _return)> cob, const Xtruct2& thing) {
407 Xtruct2 res;
408 _delegate->testNest(res, thing);
409 cob(res);
410 }
411
412 virtual void testMap(std::tr1::function<void(std::map<int32_t, int32_t> const& _return)> cob, const std::map<int32_t, int32_t> & thing) {
413 std::map<int32_t, int32_t> res;
414 _delegate->testMap(res, thing);
415 cob(res);
416 }
417
418 virtual void testStringMap(std::tr1::function<void(std::map<std::string, std::string> const& _return)> cob, const std::map<std::string, std::string> & thing) {
419 std::map<std::string, std::string> res;
420 _delegate->testStringMap(res, thing);
421 cob(res);
422 }
423
424 virtual void testSet(std::tr1::function<void(std::set<int32_t> const& _return)> cob, const std::set<int32_t> & thing) {
425 std::set<int32_t> res;
426 _delegate->testSet(res, thing);
427 cob(res);
428 }
429
430 virtual void testList(std::tr1::function<void(std::vector<int32_t> const& _return)> cob, const std::vector<int32_t> & thing) {
431 std::vector<int32_t> res;
432 _delegate->testList(res, thing);
433 cob(res);
434 }
435
436 virtual void testEnum(std::tr1::function<void(Numberz::type const& _return)> cob, const Numberz::type thing) {
437 Numberz::type res = _delegate->testEnum(thing);
438 cob(res);
439 }
440
441 virtual void testTypedef(std::tr1::function<void(UserId const& _return)> cob, const UserId thing) {
442 UserId res = _delegate->testTypedef(thing);
443 cob(res);
444 }
445
446 virtual void testMapMap(std::tr1::function<void(std::map<int32_t, std::map<int32_t, int32_t> > const& _return)> cob, const int32_t hello) {
447 std::map<int32_t, std::map<int32_t, int32_t> > res;
448 _delegate->testMapMap(res, hello);
449 cob(res);
450 }
451
452 virtual void testInsanity(std::tr1::function<void(std::map<UserId, std::map<Numberz::type, Insanity> > const& _return)> cob, const Insanity& argument) {
Jake Farrell5d02b802014-01-07 21:42:01 -0500453 std::map<UserId, std::map<Numberz::type, Insanity> > res;
Roger Meier7e056e72011-07-17 07:28:28 +0000454 _delegate->testInsanity(res, argument);
455 cob(res);
456 }
457
458 virtual void testMulti(std::tr1::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) {
459 Xtruct res;
460 _delegate->testMulti(res, arg0, arg1, arg2, arg3, arg4, arg5);
461 cob(res);
462 }
463
464 virtual void testException(std::tr1::function<void()> cob, std::tr1::function<void(::apache::thrift::TDelayedException* _throw)> exn_cob, const std::string& arg) {
465 try {
466 _delegate->testException(arg);
467 } catch(const apache::thrift::TException& e) {
468 exn_cob(apache::thrift::TDelayedException::delayException(e));
469 return;
470 }
471 cob();
472 }
473
474 virtual void testMultiException(std::tr1::function<void(Xtruct const& _return)> cob, std::tr1::function<void(::apache::thrift::TDelayedException* _throw)> exn_cob, const std::string& arg0, const std::string& arg1) {
475 Xtruct res;
476 try {
477 _delegate->testMultiException(res, arg0, arg1);
478 } catch(const apache::thrift::TException& e) {
479 exn_cob(apache::thrift::TDelayedException::delayException(e));
480 return;
481 }
482 cob(res);
483 }
484
485 virtual void testOneway(std::tr1::function<void()> cob, const int32_t secondsToSleep) {
486 _delegate->testOneway(secondsToSleep);
487 cob();
488 }
489
490protected:
Roger Meier611f90c2011-12-11 22:08:51 +0000491 boost::shared_ptr<TestHandler> _delegate;
Roger Meier7e056e72011-07-17 07:28:28 +0000492};
493
494
Mark Sleee8540632006-05-30 09:24:40 +0000495int main(int argc, char **argv) {
Jake Farrell5d02b802014-01-07 21:42:01 -0500496#if _WIN32
497 transport::TWinsockSingleton::create();
498#endif
Mark Sleee8540632006-05-30 09:24:40 +0000499 int port = 9090;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000500 bool ssl = false;
Roger Meierca142b02011-06-07 17:59:07 +0000501 string transport_type = "buffered";
502 string protocol_type = "binary";
503 string server_type = "simple";
504 string domain_socket = "";
505 size_t workers = 4;
Marc Slemko6be374b2006-08-04 03:16:25 +0000506
Jake Farrell5d02b802014-01-07 21:42:01 -0500507
508 boost::program_options::options_description desc("Allowed options");
Roger Meierca142b02011-06-07 17:59:07 +0000509 desc.add_options()
510 ("help,h", "produce help message")
Jake Farrell5d02b802014-01-07 21:42:01 -0500511 ("port", boost::program_options::value<int>(&port)->default_value(port), "Port number to listen")
512 ("domain-socket", boost::program_options::value<string>(&domain_socket)->default_value(domain_socket),
Roger Meierca142b02011-06-07 17:59:07 +0000513 "Unix Domain Socket (e.g. /tmp/ThriftTest.thrift)")
Jake Farrell5d02b802014-01-07 21:42:01 -0500514 ("server-type", boost::program_options::value<string>(&server_type)->default_value(server_type),
Roger Meierca142b02011-06-07 17:59:07 +0000515 "type of server, \"simple\", \"thread-pool\", \"threaded\", or \"nonblocking\"")
Jake Farrell5d02b802014-01-07 21:42:01 -0500516 ("transport", boost::program_options::value<string>(&transport_type)->default_value(transport_type),
Roger Meierca142b02011-06-07 17:59:07 +0000517 "transport: buffered, framed, http")
Jake Farrell5d02b802014-01-07 21:42:01 -0500518 ("protocol", boost::program_options::value<string>(&protocol_type)->default_value(protocol_type),
Roger Meier023192f2014-02-12 09:35:12 +0100519 "protocol: binary, compact, json")
Roger Meierca142b02011-06-07 17:59:07 +0000520 ("ssl", "Encrypted Transport using SSL")
521 ("processor-events", "processor-events")
Jake Farrell5d02b802014-01-07 21:42:01 -0500522 ("workers,n", boost::program_options::value<size_t>(&workers)->default_value(workers),
Roger Meierca142b02011-06-07 17:59:07 +0000523 "Number of thread pools workers. Only valid for thread-pool server type")
524 ;
Marc Slemko6be374b2006-08-04 03:16:25 +0000525
Jake Farrell5d02b802014-01-07 21:42:01 -0500526 boost::program_options::variables_map vm;
527 boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
528 boost::program_options::notify(vm);
Marc Slemko6be374b2006-08-04 03:16:25 +0000529
Roger Meierca142b02011-06-07 17:59:07 +0000530 if (vm.count("help")) {
531 cout << desc << "\n";
532 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000533 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500534
Marc Slemko6be374b2006-08-04 03:16:25 +0000535 try {
Roger Meierca142b02011-06-07 17:59:07 +0000536 if (!server_type.empty()) {
537 if (server_type == "simple") {
538 } else if (server_type == "thread-pool") {
539 } else if (server_type == "threaded") {
540 } else if (server_type == "nonblocking") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000541 } else {
Roger Meierca142b02011-06-07 17:59:07 +0000542 throw invalid_argument("Unknown server type "+server_type);
543 }
544 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500545
Roger Meierca142b02011-06-07 17:59:07 +0000546 if (!protocol_type.empty()) {
547 if (protocol_type == "binary") {
Roger Meier284101c2014-03-11 21:20:35 +0100548 } else if (protocol_type == "compact") {
Roger Meierca142b02011-06-07 17:59:07 +0000549 } else if (protocol_type == "json") {
550 } else {
551 throw invalid_argument("Unknown protocol type "+protocol_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000552 }
553 }
554
Roger Meier284101c2014-03-11 21:20:35 +0100555 if (!transport_type.empty()) {
Roger Meierca142b02011-06-07 17:59:07 +0000556 if (transport_type == "buffered") {
557 } else if (transport_type == "framed") {
558 } else if (transport_type == "http") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000559 } else {
Roger Meierca142b02011-06-07 17:59:07 +0000560 throw invalid_argument("Unknown transport type "+transport_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000561 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000562 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000563
Bryan Duxbury833ae492010-09-27 17:26:02 +0000564 } catch (std::exception& e) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000565 cerr << e.what() << endl;
Roger Meierca142b02011-06-07 17:59:07 +0000566 cout << desc << "\n";
567 return 1;
Marc Slemko6be374b2006-08-04 03:16:25 +0000568 }
569
Roger Meierca142b02011-06-07 17:59:07 +0000570 if (vm.count("ssl")) {
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000571 ssl = true;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000572 }
573
Mark Sleee8540632006-05-30 09:24:40 +0000574 // Dispatcher
Roger Meier611f90c2011-12-11 22:08:51 +0000575 boost::shared_ptr<TProtocolFactory> protocolFactory;
Roger Meierca142b02011-06-07 17:59:07 +0000576 if (protocol_type == "json") {
Roger Meier611f90c2011-12-11 22:08:51 +0000577 boost::shared_ptr<TProtocolFactory> jsonProtocolFactory(new TJSONProtocolFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000578 protocolFactory = jsonProtocolFactory;
Roger Meier023192f2014-02-12 09:35:12 +0100579 } else if (protocol_type == "compact") {
580 boost::shared_ptr<TProtocolFactory> compactProtocolFactory(new TCompactProtocolFactory());
581 protocolFactory = compactProtocolFactory;
Roger Meierca142b02011-06-07 17:59:07 +0000582 } else {
Roger Meier611f90c2011-12-11 22:08:51 +0000583 boost::shared_ptr<TProtocolFactory> binaryProtocolFactory(new TBinaryProtocolFactoryT<TBufferBase>());
Roger Meierca142b02011-06-07 17:59:07 +0000584 protocolFactory = binaryProtocolFactory;
585 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000586
Roger Meierca142b02011-06-07 17:59:07 +0000587 // Processor
Roger Meier611f90c2011-12-11 22:08:51 +0000588 boost::shared_ptr<TestHandler> testHandler(new TestHandler());
589 boost::shared_ptr<ThriftTestProcessor> testProcessor(new ThriftTestProcessor(testHandler));
Jake Farrell5d02b802014-01-07 21:42:01 -0500590
Roger Meierca142b02011-06-07 17:59:07 +0000591 if (vm.count("processor-events")) {
Roger Meier611f90c2011-12-11 22:08:51 +0000592 testProcessor->setEventHandler(boost::shared_ptr<TProcessorEventHandler>(
David Reissd7192062010-10-06 17:09:33 +0000593 new TestProcessorEventHandler()));
594 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500595
Mark Sleee8540632006-05-30 09:24:40 +0000596 // Transport
Roger Meier611f90c2011-12-11 22:08:51 +0000597 boost::shared_ptr<TSSLSocketFactory> sslSocketFactory;
598 boost::shared_ptr<TServerSocket> serverSocket;
Mark Sleee8540632006-05-30 09:24:40 +0000599
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000600 if (ssl) {
Roger Meier611f90c2011-12-11 22:08:51 +0000601 sslSocketFactory = boost::shared_ptr<TSSLSocketFactory>(new TSSLSocketFactory());
Roger Meierc94b2932014-02-22 20:07:33 +0100602 sslSocketFactory->loadCertificate("keys/server.crt");
603 sslSocketFactory->loadPrivateKey("keys/server.key");
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000604 sslSocketFactory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
Roger Meier611f90c2011-12-11 22:08:51 +0000605 serverSocket = boost::shared_ptr<TServerSocket>(new TSSLServerSocket(port, sslSocketFactory));
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000606 } else {
Roger Meierc94b2932014-02-22 20:07:33 +0100607 if (domain_socket != "") {
608 unlink(domain_socket.c_str());
609 serverSocket = boost::shared_ptr<TServerSocket>(new TServerSocket(domain_socket));
610 port = 0;
611 } else {
Roger Meier611f90c2011-12-11 22:08:51 +0000612 serverSocket = boost::shared_ptr<TServerSocket>(new TServerSocket(port));
Roger Meierc94b2932014-02-22 20:07:33 +0100613 }
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000614 }
Roger Meierca142b02011-06-07 17:59:07 +0000615
Mark Sleed788b2e2006-09-07 01:26:35 +0000616 // Factory
Roger Meier611f90c2011-12-11 22:08:51 +0000617 boost::shared_ptr<TTransportFactory> transportFactory;
Jake Farrell5d02b802014-01-07 21:42:01 -0500618
Roger Meier7e056e72011-07-17 07:28:28 +0000619 if (transport_type == "http" && server_type != "nonblocking") {
Jake Farrell5d02b802014-01-07 21:42:01 -0500620 boost::shared_ptr<TTransportFactory> httpTransportFactory(new THttpServerTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000621 transportFactory = httpTransportFactory;
622 } else if (transport_type == "framed") {
Jake Farrell5d02b802014-01-07 21:42:01 -0500623 boost::shared_ptr<TTransportFactory> framedTransportFactory(new TFramedTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000624 transportFactory = framedTransportFactory;
625 } else {
Jake Farrell5d02b802014-01-07 21:42:01 -0500626 boost::shared_ptr<TTransportFactory> bufferedTransportFactory(new TBufferedTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000627 transportFactory = bufferedTransportFactory;
628 }
Mark Sleed788b2e2006-09-07 01:26:35 +0000629
Roger Meierca142b02011-06-07 17:59:07 +0000630 // Server Info
631 cout << "Starting \"" << server_type << "\" server ("
632 << transport_type << "/" << protocol_type << ") listen on: " << domain_socket;
633 if (port != 0) {
634 cout << port;
635 }
636 cout << endl;
Mark Sleee8540632006-05-30 09:24:40 +0000637
Roger Meierca142b02011-06-07 17:59:07 +0000638 // Server
Jake Farrell5d02b802014-01-07 21:42:01 -0500639 boost::shared_ptr<apache::thrift::server::TServer> server;
640
Roger Meierca142b02011-06-07 17:59:07 +0000641 if (server_type == "simple") {
Jake Farrell5d02b802014-01-07 21:42:01 -0500642 server.reset(new TSimpleServer(testProcessor,
Roger Meierca142b02011-06-07 17:59:07 +0000643 serverSocket,
Mark Sleed788b2e2006-09-07 01:26:35 +0000644 transportFactory,
Jake Farrell5d02b802014-01-07 21:42:01 -0500645 protocolFactory));
Roger Meierca142b02011-06-07 17:59:07 +0000646 } else if (server_type == "thread-pool") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000647
Roger Meier611f90c2011-12-11 22:08:51 +0000648 boost::shared_ptr<ThreadManager> threadManager =
Roger Meierca142b02011-06-07 17:59:07 +0000649 ThreadManager::newSimpleThreadManager(workers);
Marc Slemko6be374b2006-08-04 03:16:25 +0000650
Roger Meier611f90c2011-12-11 22:08:51 +0000651 boost::shared_ptr<PlatformThreadFactory> threadFactory =
652 boost::shared_ptr<PlatformThreadFactory>(new PlatformThreadFactory());
Marc Slemko6be374b2006-08-04 03:16:25 +0000653
654 threadManager->threadFactory(threadFactory);
655
656 threadManager->start();
657
Jake Farrell5d02b802014-01-07 21:42:01 -0500658 server.reset(new TThreadPoolServer(testProcessor,
Roger Meierca142b02011-06-07 17:59:07 +0000659 serverSocket,
Mark Sleed788b2e2006-09-07 01:26:35 +0000660 transportFactory,
Mark Sleea3302652006-10-25 19:03:32 +0000661 protocolFactory,
Jake Farrell5d02b802014-01-07 21:42:01 -0500662 threadManager));
Roger Meierca142b02011-06-07 17:59:07 +0000663 } else if (server_type == "threaded") {
David Reiss0c90f6f2008-02-06 22:18:40 +0000664
Jake Farrell5d02b802014-01-07 21:42:01 -0500665 server.reset(new TThreadedServer(testProcessor,
Mark Slee0c2dff32007-03-09 19:26:57 +0000666 serverSocket,
667 transportFactory,
Jake Farrell5d02b802014-01-07 21:42:01 -0500668 protocolFactory));
Roger Meierca142b02011-06-07 17:59:07 +0000669 } else if (server_type == "nonblocking") {
Roger Meier7e056e72011-07-17 07:28:28 +0000670 if(transport_type == "http") {
Roger Meier611f90c2011-12-11 22:08:51 +0000671 boost::shared_ptr<TestHandlerAsync> testHandlerAsync(new TestHandlerAsync(testHandler));
672 boost::shared_ptr<TAsyncProcessor> testProcessorAsync(new ThriftTestAsyncProcessor(testHandlerAsync));
673 boost::shared_ptr<TAsyncBufferProcessor> testBufferProcessor(new TAsyncProtocolProcessor(testProcessorAsync, protocolFactory));
Jake Farrell5d02b802014-01-07 21:42:01 -0500674
675 // not loading nonblockingServer into "server" because
676 // TEvhttpServer doesn't inherit from TServer, and doesn't
677 // provide a stop method.
Roger Meier7e056e72011-07-17 07:28:28 +0000678 TEvhttpServer nonblockingServer(testBufferProcessor, port);
679 nonblockingServer.serve();
Jake Farrell5d02b802014-01-07 21:42:01 -0500680 } else {
681 server.reset(new TNonblockingServer(testProcessor, port));
Roger Meier7e056e72011-07-17 07:28:28 +0000682 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000683 }
684
Jake Farrell5d02b802014-01-07 21:42:01 -0500685 if(server.get() != NULL)
686 {
687 apache::thrift::concurrency::PlatformThreadFactory factory;
688 factory.setDetached(false);
689 boost::shared_ptr<apache::thrift::concurrency::Runnable> serverThreadRunner(server);
690 boost::shared_ptr<apache::thrift::concurrency::Thread> thread = factory.newThread(serverThreadRunner);
691 thread->start();
692
Roger Meier284101c2014-03-11 21:20:35 +0100693 // HACK: cross language test suite is unable to handle cin properly
694 // that's why we stay in a endless loop here
695 while(1){}
696 // FIXME: find another way to stop the server (e.g. a signal)
697 // cout<<"Press enter to stop the server."<<endl;
698 // cin.ignore(); //wait until a key is pressed
Jake Farrell5d02b802014-01-07 21:42:01 -0500699
700 server->stop();
701 thread->join();
702 server.reset();
703 }
704
Roger Meierca142b02011-06-07 17:59:07 +0000705 cout << "done." << endl;
Mark Sleee8540632006-05-30 09:24:40 +0000706 return 0;
707}