blob: af43d72e3e039557ac86dbfb5de783205bbd8979 [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>
26#include <thrift/protocol/TJSONProtocol.h>
27#include <thrift/server/TSimpleServer.h>
28#include <thrift/server/TThreadedServer.h>
29#include <thrift/server/TThreadPoolServer.h>
30#include <thrift/async/TEvhttpServer.h>
31#include <thrift/async/TAsyncBufferProcessor.h>
32#include <thrift/async/TAsyncProtocolProcessor.h>
33#include <thrift/server/TNonblockingServer.h>
34#include <thrift/transport/TServerSocket.h>
35#include <thrift/transport/TSSLServerSocket.h>
36#include <thrift/transport/TSSLSocket.h>
37#include <thrift/transport/THttpServer.h>
38#include <thrift/transport/THttpTransport.h>
39#include <thrift/transport/TTransportUtils.h>
Mark Slee95771002006-06-07 06:53:25 +000040#include "ThriftTest.h"
Marc Slemko6be374b2006-08-04 03:16:25 +000041
42#include <iostream>
43#include <stdexcept>
44#include <sstream>
45
Roger Meierca142b02011-06-07 17:59:07 +000046#include <boost/program_options.hpp>
47
Bryan Duxburycd9aea12011-02-22 18:12:06 +000048#include <signal.h>
Jake Farrell5d02b802014-01-07 21:42:01 -050049#if _WIN32
50 #include <thrift/windows/TWinsockSingleton.h>
51#endif
David Reissbc3dddb2007-08-22 23:20:24 +000052
Mark Sleee8540632006-05-30 09:24:40 +000053using namespace std;
54
T Jake Lucianib5e62212009-01-31 22:36:20 +000055using namespace apache::thrift;
56using namespace apache::thrift::concurrency;
57using namespace apache::thrift::protocol;
58using namespace apache::thrift::transport;
59using namespace apache::thrift::server;
Roger Meier7e056e72011-07-17 07:28:28 +000060using namespace apache::thrift::async;
Marc Slemko6be374b2006-08-04 03:16:25 +000061
Marc Slemkobf4fd192006-08-15 21:29:39 +000062using namespace thrift::test;
63
Mark Sleed2655522006-09-05 22:09:57 +000064class TestHandler : public ThriftTestIf {
Mark Sleee8540632006-05-30 09:24:40 +000065 public:
Mark Sleed2655522006-09-05 22:09:57 +000066 TestHandler() {}
Mark Sleee8540632006-05-30 09:24:40 +000067
68 void testVoid() {
69 printf("testVoid()\n");
70 }
71
Mark Slee1921d202007-01-24 19:43:06 +000072 void testString(string& out, const string &thing) {
Mark Sleee8540632006-05-30 09:24:40 +000073 printf("testString(\"%s\")\n", thing.c_str());
Mark Slee1921d202007-01-24 19:43:06 +000074 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +000075 }
76
Mark Slee1921d202007-01-24 19:43:06 +000077 int8_t testByte(const int8_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +000078 printf("testByte(%d)\n", (int)thing);
79 return thing;
80 }
81
Mark Slee1921d202007-01-24 19:43:06 +000082 int32_t testI32(const int32_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +000083 printf("testI32(%d)\n", thing);
84 return thing;
85 }
86
Mark Slee1921d202007-01-24 19:43:06 +000087 int64_t testI64(const int64_t thing) {
Roger Meier0e814802014-01-17 21:07:58 +010088 printf("testI64(%" PRId64 ")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +000089 return thing;
90 }
91
Mark Slee1921d202007-01-24 19:43:06 +000092 double testDouble(const double thing) {
Roger Meiera8cef6e2011-07-17 18:55:59 +000093 printf("testDouble(%f)\n", thing);
Mark Sleec98d0502006-09-06 02:42:25 +000094 return thing;
95 }
96
Mark Slee1921d202007-01-24 19:43:06 +000097 void testStruct(Xtruct& out, const Xtruct &thing) {
Roger Meier0e814802014-01-17 21:07:58 +010098 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 +000099 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000100 }
101
Mark Slee1921d202007-01-24 19:43:06 +0000102 void testNest(Xtruct2& out, const Xtruct2& nest) {
103 const Xtruct &thing = nest.struct_thing;
Roger Meier0e814802014-01-17 21:07:58 +0100104 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 +0000105 out = nest;
Mark Sleee8540632006-05-30 09:24:40 +0000106 }
107
Mark Slee1921d202007-01-24 19:43:06 +0000108 void testMap(map<int32_t, int32_t> &out, const map<int32_t, int32_t> &thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000109 printf("testMap({");
110 map<int32_t, int32_t>::const_iterator m_iter;
111 bool first = true;
112 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
113 if (first) {
114 first = false;
115 } else {
116 printf(", ");
117 }
118 printf("%d => %d", m_iter->first, m_iter->second);
119 }
120 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000121 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000122 }
123
Roger Meierd3b9dca2011-06-24 14:01:10 +0000124 void testStringMap(map<std::string, std::string> &out, const map<std::string, std::string> &thing) {
125 printf("testMap({");
126 map<std::string, std::string>::const_iterator m_iter;
127 bool first = true;
128 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
129 if (first) {
130 first = false;
131 } else {
132 printf(", ");
133 }
134 printf("%s => %s", (m_iter->first).c_str(), (m_iter->second).c_str());
135 }
136 printf("})\n");
137 out = thing;
138 }
139
Mark Slee1921d202007-01-24 19:43:06 +0000140 void testSet(set<int32_t> &out, const set<int32_t> &thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000141 printf("testSet({");
142 set<int32_t>::const_iterator s_iter;
143 bool first = true;
144 for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) {
145 if (first) {
146 first = false;
147 } else {
148 printf(", ");
149 }
150 printf("%d", *s_iter);
151 }
152 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000153 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000154 }
155
Mark Slee1921d202007-01-24 19:43:06 +0000156 void testList(vector<int32_t> &out, const vector<int32_t> &thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000157 printf("testList({");
Mark Sleeb9acf982006-10-10 01:57:32 +0000158 vector<int32_t>::const_iterator l_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000159 bool first = true;
160 for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) {
161 if (first) {
162 first = false;
163 } else {
164 printf(", ");
165 }
166 printf("%d", *l_iter);
167 }
168 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000169 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000170 }
171
Bryan Duxbury833ae492010-09-27 17:26:02 +0000172 Numberz::type testEnum(const Numberz::type thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000173 printf("testEnum(%d)\n", thing);
174 return thing;
175 }
176
Mark Slee1921d202007-01-24 19:43:06 +0000177 UserId testTypedef(const UserId thing) {
Roger Meier0e814802014-01-17 21:07:58 +0100178 printf("testTypedef(%" PRId64 ")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +0000179 return thing;
180 }
181
Mark Slee1921d202007-01-24 19:43:06 +0000182 void testMapMap(map<int32_t, map<int32_t,int32_t> > &mapmap, const int32_t hello) {
Mark Sleee8540632006-05-30 09:24:40 +0000183 printf("testMapMap(%d)\n", hello);
Mark Sleee8540632006-05-30 09:24:40 +0000184
185 map<int32_t,int32_t> pos;
186 map<int32_t,int32_t> neg;
187 for (int i = 1; i < 5; i++) {
188 pos.insert(make_pair(i,i));
189 neg.insert(make_pair(-i,-i));
190 }
191
192 mapmap.insert(make_pair(4, pos));
193 mapmap.insert(make_pair(-4, neg));
194
Mark Sleee8540632006-05-30 09:24:40 +0000195 }
196
Bryan Duxbury833ae492010-09-27 17:26:02 +0000197 void testInsanity(map<UserId, map<Numberz::type,Insanity> > &insane, const Insanity &argument) {
Roger Meiera8cef6e2011-07-17 18:55:59 +0000198 (void) argument;
Mark Sleee8540632006-05-30 09:24:40 +0000199 printf("testInsanity()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000200
Mark Sleee8540632006-05-30 09:24:40 +0000201 Xtruct hello;
202 hello.string_thing = "Hello2";
203 hello.byte_thing = 2;
Mark Sleee8540632006-05-30 09:24:40 +0000204 hello.i32_thing = 2;
Mark Sleee8540632006-05-30 09:24:40 +0000205 hello.i64_thing = 2;
206
207 Xtruct goodbye;
208 goodbye.string_thing = "Goodbye4";
209 goodbye.byte_thing = 4;
Mark Sleee8540632006-05-30 09:24:40 +0000210 goodbye.i32_thing = 4;
Mark Sleee8540632006-05-30 09:24:40 +0000211 goodbye.i64_thing = 4;
212
213 Insanity crazy;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000214 crazy.userMap.insert(make_pair(Numberz::EIGHT, 8));
Mark Sleee8540632006-05-30 09:24:40 +0000215 crazy.xtructs.push_back(goodbye);
216
217 Insanity looney;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000218 crazy.userMap.insert(make_pair(Numberz::FIVE, 5));
Mark Sleee8540632006-05-30 09:24:40 +0000219 crazy.xtructs.push_back(hello);
220
Bryan Duxbury833ae492010-09-27 17:26:02 +0000221 map<Numberz::type, Insanity> first_map;
222 map<Numberz::type, Insanity> second_map;
Mark Sleee8540632006-05-30 09:24:40 +0000223
Bryan Duxbury833ae492010-09-27 17:26:02 +0000224 first_map.insert(make_pair(Numberz::TWO, crazy));
225 first_map.insert(make_pair(Numberz::THREE, crazy));
Mark Sleee8540632006-05-30 09:24:40 +0000226
Bryan Duxbury833ae492010-09-27 17:26:02 +0000227 second_map.insert(make_pair(Numberz::SIX, looney));
Mark Sleee8540632006-05-30 09:24:40 +0000228
Mark Sleee8540632006-05-30 09:24:40 +0000229 insane.insert(make_pair(1, first_map));
230 insane.insert(make_pair(2, second_map));
231
232 printf("return");
233 printf(" = {");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000234 map<UserId, map<Numberz::type,Insanity> >::const_iterator i_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000235 for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) {
Roger Meier0e814802014-01-17 21:07:58 +0100236 printf("%" PRId64 " => {", i_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000237 map<Numberz::type,Insanity>::const_iterator i2_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000238 for (i2_iter = i_iter->second.begin();
239 i2_iter != i_iter->second.end();
240 ++i2_iter) {
241 printf("%d => {", i2_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000242 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
243 map<Numberz::type, UserId>::const_iterator um;
Mark Sleee8540632006-05-30 09:24:40 +0000244 printf("{");
245 for (um = userMap.begin(); um != userMap.end(); ++um) {
Roger Meier0e814802014-01-17 21:07:58 +0100246 printf("%d => %" PRId64 ", ", um->first, um->second);
Mark Sleee8540632006-05-30 09:24:40 +0000247 }
248 printf("}, ");
249
Mark Sleeb9acf982006-10-10 01:57:32 +0000250 vector<Xtruct> xtructs = i2_iter->second.xtructs;
251 vector<Xtruct>::const_iterator x;
Mark Sleee8540632006-05-30 09:24:40 +0000252 printf("{");
253 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
Roger Meier0e814802014-01-17 21:07:58 +0100254 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 +0000255 }
256 printf("}");
257
258 printf("}, ");
259 }
260 printf("}, ");
261 }
262 printf("}\n");
263
David Reiss0c90f6f2008-02-06 22:18:40 +0000264
Mark Sleee8540632006-05-30 09:24:40 +0000265 }
Marc Slemkoe6889de2006-08-12 00:32:53 +0000266
Bryan Duxbury833ae492010-09-27 17:26:02 +0000267 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 +0000268 (void) arg3;
269 (void) arg4;
270 (void) arg5;
Jake Farrell5d02b802014-01-07 21:42:01 -0500271
Marc Slemkoe6889de2006-08-12 00:32:53 +0000272 printf("testMulti()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000273
Marc Slemkoe6889de2006-08-12 00:32:53 +0000274 hello.string_thing = "Hello2";
275 hello.byte_thing = arg0;
276 hello.i32_thing = arg1;
277 hello.i64_thing = (int64_t)arg2;
Marc Slemkoe6889de2006-08-12 00:32:53 +0000278 }
Marc Slemkobf4fd192006-08-15 21:29:39 +0000279
David Reiss55ff70f2008-06-11 00:58:25 +0000280 void testException(const std::string &arg)
T Jake Lucianib5e62212009-01-31 22:36:20 +0000281 throw(Xception, apache::thrift::TException)
David Reiss55ff70f2008-06-11 00:58:25 +0000282 {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000283 printf("testException(%s)\n", arg.c_str());
Mark Sleed3d733a2006-09-01 22:19:06 +0000284 if (arg.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000285 Xception e;
286 e.errorCode = 1001;
David Reiss9813cbe2009-01-31 21:39:11 +0000287 e.message = arg;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000288 throw e;
Roger Meier1f8b48f2012-05-02 22:56:47 +0000289 } else if (arg.compare("TException") == 0) {
T Jake Lucianib5e62212009-01-31 22:36:20 +0000290 apache::thrift::TException e;
David Reiss55ff70f2008-06-11 00:58:25 +0000291 throw e;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000292 } else {
293 Xtruct result;
294 result.string_thing = arg;
295 return;
296 }
297 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000298
Mark Slee1921d202007-01-24 19:43:06 +0000299 void testMultiException(Xtruct &result, const std::string &arg0, const std::string &arg1) throw(Xception, Xception2) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000300
Marc Slemko71d4e472006-08-15 22:34:04 +0000301 printf("testMultiException(%s, %s)\n", arg0.c_str(), arg1.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000302
Mark Sleef5f2be42006-09-05 21:05:31 +0000303 if (arg0.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000304 Xception e;
305 e.errorCode = 1001;
306 e.message = "This is an Xception";
307 throw e;
Mark Sleef5f2be42006-09-05 21:05:31 +0000308 } else if (arg0.compare("Xception2") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000309 Xception2 e;
310 e.errorCode = 2002;
311 e.struct_thing.string_thing = "This is an Xception2";
312 throw e;
313 } else {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000314 result.string_thing = arg1;
Mark Slee1921d202007-01-24 19:43:06 +0000315 return;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000316 }
317 }
David Reiss2ab6fe82008-02-18 02:11:44 +0000318
Jake Farrell5d02b802014-01-07 21:42:01 -0500319 void testOneway(const int32_t sleepFor) {
David Reiss6ce401d2009-03-24 20:01:58 +0000320 printf("testOneway(%d): Sleeping...\n", sleepFor);
Jake Farrell5d02b802014-01-07 21:42:01 -0500321 THRIFT_SLEEP_SEC(sleepFor);
David Reiss6ce401d2009-03-24 20:01:58 +0000322 printf("testOneway(%d): done sleeping!\n", sleepFor);
David Reiss2ab6fe82008-02-18 02:11:44 +0000323 }
Mark Sleee8540632006-05-30 09:24:40 +0000324};
325
David Reissd7192062010-10-06 17:09:33 +0000326
327class TestProcessorEventHandler : public TProcessorEventHandler {
David Reiss23248712010-10-06 17:10:08 +0000328 virtual void* getContext(const char* fn_name, void* serverContext) {
Roger Meiera8cef6e2011-07-17 18:55:59 +0000329 (void) serverContext;
David Reissd7192062010-10-06 17:09:33 +0000330 return new std::string(fn_name);
331 }
332 virtual void freeContext(void* ctx, const char* fn_name) {
Roger Meiera8cef6e2011-07-17 18:55:59 +0000333 (void) fn_name;
David Reissd7192062010-10-06 17:09:33 +0000334 delete static_cast<std::string*>(ctx);
335 }
336 virtual void preRead(void* ctx, const char* fn_name) {
337 communicate("preRead", ctx, fn_name);
338 }
David Reissef7200f2010-10-06 17:09:42 +0000339 virtual void postRead(void* ctx, const char* fn_name, uint32_t bytes) {
Roger Meiera8cef6e2011-07-17 18:55:59 +0000340 (void) bytes;
David Reissd7192062010-10-06 17:09:33 +0000341 communicate("postRead", ctx, fn_name);
342 }
343 virtual void preWrite(void* ctx, const char* fn_name) {
344 communicate("preWrite", ctx, fn_name);
345 }
David Reissef7200f2010-10-06 17:09:42 +0000346 virtual void postWrite(void* ctx, const char* fn_name, uint32_t bytes) {
Roger Meiera8cef6e2011-07-17 18:55:59 +0000347 (void) bytes;
David Reissd7192062010-10-06 17:09:33 +0000348 communicate("postWrite", ctx, fn_name);
349 }
350 virtual void asyncComplete(void* ctx, const char* fn_name) {
351 communicate("asyncComplete", ctx, fn_name);
352 }
353 virtual void handlerError(void* ctx, const char* fn_name) {
354 communicate("handlerError", ctx, fn_name);
355 }
356
357 void communicate(const char* event, void* ctx, const char* fn_name) {
358 std::cout << event << ": " << *static_cast<std::string*>(ctx) << " = " << fn_name << std::endl;
359 }
360};
361
362
Roger Meier7e056e72011-07-17 07:28:28 +0000363class TestHandlerAsync : public ThriftTestCobSvIf {
364public:
Roger Meier611f90c2011-12-11 22:08:51 +0000365 TestHandlerAsync(boost::shared_ptr<TestHandler>& handler) : _delegate(handler) {}
Roger Meier7e056e72011-07-17 07:28:28 +0000366 virtual ~TestHandlerAsync() {}
367
368 virtual void testVoid(std::tr1::function<void()> cob) {
369 _delegate->testVoid();
370 cob();
371 }
372
373 virtual void testString(std::tr1::function<void(std::string const& _return)> cob, const std::string& thing) {
374 std::string res;
375 _delegate->testString(res, thing);
376 cob(res);
377 }
378
379 virtual void testByte(std::tr1::function<void(int8_t const& _return)> cob, const int8_t thing) {
380 int8_t res = _delegate->testByte(thing);
381 cob(res);
382 }
383
384 virtual void testI32(std::tr1::function<void(int32_t const& _return)> cob, const int32_t thing) {
385 int32_t res = _delegate->testI32(thing);
386 cob(res);
387 }
388
389 virtual void testI64(std::tr1::function<void(int64_t const& _return)> cob, const int64_t thing) {
390 int64_t res = _delegate->testI64(thing);
391 cob(res);
392 }
393
394 virtual void testDouble(std::tr1::function<void(double const& _return)> cob, const double thing) {
395 double res = _delegate->testDouble(thing);
396 cob(res);
397 }
398
399 virtual void testStruct(std::tr1::function<void(Xtruct const& _return)> cob, const Xtruct& thing) {
400 Xtruct res;
401 _delegate->testStruct(res, thing);
402 cob(res);
403 }
404
405 virtual void testNest(std::tr1::function<void(Xtruct2 const& _return)> cob, const Xtruct2& thing) {
406 Xtruct2 res;
407 _delegate->testNest(res, thing);
408 cob(res);
409 }
410
411 virtual void testMap(std::tr1::function<void(std::map<int32_t, int32_t> const& _return)> cob, const std::map<int32_t, int32_t> & thing) {
412 std::map<int32_t, int32_t> res;
413 _delegate->testMap(res, thing);
414 cob(res);
415 }
416
417 virtual void testStringMap(std::tr1::function<void(std::map<std::string, std::string> const& _return)> cob, const std::map<std::string, std::string> & thing) {
418 std::map<std::string, std::string> res;
419 _delegate->testStringMap(res, thing);
420 cob(res);
421 }
422
423 virtual void testSet(std::tr1::function<void(std::set<int32_t> const& _return)> cob, const std::set<int32_t> & thing) {
424 std::set<int32_t> res;
425 _delegate->testSet(res, thing);
426 cob(res);
427 }
428
429 virtual void testList(std::tr1::function<void(std::vector<int32_t> const& _return)> cob, const std::vector<int32_t> & thing) {
430 std::vector<int32_t> res;
431 _delegate->testList(res, thing);
432 cob(res);
433 }
434
435 virtual void testEnum(std::tr1::function<void(Numberz::type const& _return)> cob, const Numberz::type thing) {
436 Numberz::type res = _delegate->testEnum(thing);
437 cob(res);
438 }
439
440 virtual void testTypedef(std::tr1::function<void(UserId const& _return)> cob, const UserId thing) {
441 UserId res = _delegate->testTypedef(thing);
442 cob(res);
443 }
444
445 virtual void testMapMap(std::tr1::function<void(std::map<int32_t, std::map<int32_t, int32_t> > const& _return)> cob, const int32_t hello) {
446 std::map<int32_t, std::map<int32_t, int32_t> > res;
447 _delegate->testMapMap(res, hello);
448 cob(res);
449 }
450
451 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 -0500452 std::map<UserId, std::map<Numberz::type, Insanity> > res;
Roger Meier7e056e72011-07-17 07:28:28 +0000453 _delegate->testInsanity(res, argument);
454 cob(res);
455 }
456
457 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) {
458 Xtruct res;
459 _delegate->testMulti(res, arg0, arg1, arg2, arg3, arg4, arg5);
460 cob(res);
461 }
462
463 virtual void testException(std::tr1::function<void()> cob, std::tr1::function<void(::apache::thrift::TDelayedException* _throw)> exn_cob, const std::string& arg) {
464 try {
465 _delegate->testException(arg);
466 } catch(const apache::thrift::TException& e) {
467 exn_cob(apache::thrift::TDelayedException::delayException(e));
468 return;
469 }
470 cob();
471 }
472
473 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) {
474 Xtruct res;
475 try {
476 _delegate->testMultiException(res, arg0, arg1);
477 } catch(const apache::thrift::TException& e) {
478 exn_cob(apache::thrift::TDelayedException::delayException(e));
479 return;
480 }
481 cob(res);
482 }
483
484 virtual void testOneway(std::tr1::function<void()> cob, const int32_t secondsToSleep) {
485 _delegate->testOneway(secondsToSleep);
486 cob();
487 }
488
489protected:
Roger Meier611f90c2011-12-11 22:08:51 +0000490 boost::shared_ptr<TestHandler> _delegate;
Roger Meier7e056e72011-07-17 07:28:28 +0000491};
492
493
Mark Sleee8540632006-05-30 09:24:40 +0000494int main(int argc, char **argv) {
Jake Farrell5d02b802014-01-07 21:42:01 -0500495#if _WIN32
496 transport::TWinsockSingleton::create();
497#endif
Mark Sleee8540632006-05-30 09:24:40 +0000498 int port = 9090;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000499 bool ssl = false;
Roger Meierca142b02011-06-07 17:59:07 +0000500 string transport_type = "buffered";
501 string protocol_type = "binary";
502 string server_type = "simple";
503 string domain_socket = "";
504 size_t workers = 4;
Marc Slemko6be374b2006-08-04 03:16:25 +0000505
Jake Farrell5d02b802014-01-07 21:42:01 -0500506
507 boost::program_options::options_description desc("Allowed options");
Roger Meierca142b02011-06-07 17:59:07 +0000508 desc.add_options()
509 ("help,h", "produce help message")
Jake Farrell5d02b802014-01-07 21:42:01 -0500510 ("port", boost::program_options::value<int>(&port)->default_value(port), "Port number to listen")
511 ("domain-socket", boost::program_options::value<string>(&domain_socket)->default_value(domain_socket),
Roger Meierca142b02011-06-07 17:59:07 +0000512 "Unix Domain Socket (e.g. /tmp/ThriftTest.thrift)")
Jake Farrell5d02b802014-01-07 21:42:01 -0500513 ("server-type", boost::program_options::value<string>(&server_type)->default_value(server_type),
Roger Meierca142b02011-06-07 17:59:07 +0000514 "type of server, \"simple\", \"thread-pool\", \"threaded\", or \"nonblocking\"")
Jake Farrell5d02b802014-01-07 21:42:01 -0500515 ("transport", boost::program_options::value<string>(&transport_type)->default_value(transport_type),
Roger Meierca142b02011-06-07 17:59:07 +0000516 "transport: buffered, framed, http")
Jake Farrell5d02b802014-01-07 21:42:01 -0500517 ("protocol", boost::program_options::value<string>(&protocol_type)->default_value(protocol_type),
Roger Meierca142b02011-06-07 17:59:07 +0000518 "protocol: binary, json")
519 ("ssl", "Encrypted Transport using SSL")
520 ("processor-events", "processor-events")
Jake Farrell5d02b802014-01-07 21:42:01 -0500521 ("workers,n", boost::program_options::value<size_t>(&workers)->default_value(workers),
Roger Meierca142b02011-06-07 17:59:07 +0000522 "Number of thread pools workers. Only valid for thread-pool server type")
523 ;
Marc Slemko6be374b2006-08-04 03:16:25 +0000524
Jake Farrell5d02b802014-01-07 21:42:01 -0500525 boost::program_options::variables_map vm;
526 boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
527 boost::program_options::notify(vm);
Marc Slemko6be374b2006-08-04 03:16:25 +0000528
Roger Meierca142b02011-06-07 17:59:07 +0000529 if (vm.count("help")) {
530 cout << desc << "\n";
531 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000532 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500533
Marc Slemko6be374b2006-08-04 03:16:25 +0000534 try {
Roger Meierca142b02011-06-07 17:59:07 +0000535 if (!server_type.empty()) {
536 if (server_type == "simple") {
537 } else if (server_type == "thread-pool") {
538 } else if (server_type == "threaded") {
539 } else if (server_type == "nonblocking") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000540 } else {
Roger Meierca142b02011-06-07 17:59:07 +0000541 throw invalid_argument("Unknown server type "+server_type);
542 }
543 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500544
Roger Meierca142b02011-06-07 17:59:07 +0000545 if (!protocol_type.empty()) {
546 if (protocol_type == "binary") {
547 } else if (protocol_type == "json") {
548 } else {
549 throw invalid_argument("Unknown protocol type "+protocol_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000550 }
551 }
552
Roger Meierca142b02011-06-07 17:59:07 +0000553 if (!transport_type.empty()) {
554 if (transport_type == "buffered") {
555 } else if (transport_type == "framed") {
556 } else if (transport_type == "http") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000557 } else {
Roger Meierca142b02011-06-07 17:59:07 +0000558 throw invalid_argument("Unknown transport type "+transport_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000559 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000560 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000561
Bryan Duxbury833ae492010-09-27 17:26:02 +0000562 } catch (std::exception& e) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000563 cerr << e.what() << endl;
Roger Meierca142b02011-06-07 17:59:07 +0000564 cout << desc << "\n";
565 return 1;
Marc Slemko6be374b2006-08-04 03:16:25 +0000566 }
567
Roger Meierca142b02011-06-07 17:59:07 +0000568 if (vm.count("ssl")) {
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000569 ssl = true;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000570 }
571
Mark Sleee8540632006-05-30 09:24:40 +0000572 // Dispatcher
Roger Meier611f90c2011-12-11 22:08:51 +0000573 boost::shared_ptr<TProtocolFactory> protocolFactory;
Roger Meierca142b02011-06-07 17:59:07 +0000574 if (protocol_type == "json") {
Roger Meier611f90c2011-12-11 22:08:51 +0000575 boost::shared_ptr<TProtocolFactory> jsonProtocolFactory(new TJSONProtocolFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000576 protocolFactory = jsonProtocolFactory;
577 } else {
Roger Meier611f90c2011-12-11 22:08:51 +0000578 boost::shared_ptr<TProtocolFactory> binaryProtocolFactory(new TBinaryProtocolFactoryT<TBufferBase>());
Roger Meierca142b02011-06-07 17:59:07 +0000579 protocolFactory = binaryProtocolFactory;
580 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000581
Roger Meierca142b02011-06-07 17:59:07 +0000582 // Processor
Roger Meier611f90c2011-12-11 22:08:51 +0000583 boost::shared_ptr<TestHandler> testHandler(new TestHandler());
584 boost::shared_ptr<ThriftTestProcessor> testProcessor(new ThriftTestProcessor(testHandler));
Jake Farrell5d02b802014-01-07 21:42:01 -0500585
Roger Meierca142b02011-06-07 17:59:07 +0000586 if (vm.count("processor-events")) {
Roger Meier611f90c2011-12-11 22:08:51 +0000587 testProcessor->setEventHandler(boost::shared_ptr<TProcessorEventHandler>(
David Reissd7192062010-10-06 17:09:33 +0000588 new TestProcessorEventHandler()));
589 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500590
Mark Sleee8540632006-05-30 09:24:40 +0000591 // Transport
Roger Meier611f90c2011-12-11 22:08:51 +0000592 boost::shared_ptr<TSSLSocketFactory> sslSocketFactory;
593 boost::shared_ptr<TServerSocket> serverSocket;
Mark Sleee8540632006-05-30 09:24:40 +0000594
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000595 if (ssl) {
Roger Meier611f90c2011-12-11 22:08:51 +0000596 sslSocketFactory = boost::shared_ptr<TSSLSocketFactory>(new TSSLSocketFactory());
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000597 sslSocketFactory->loadCertificate("./server-certificate.pem");
598 sslSocketFactory->loadPrivateKey("./server-private-key.pem");
599 sslSocketFactory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
Roger Meier611f90c2011-12-11 22:08:51 +0000600 serverSocket = boost::shared_ptr<TServerSocket>(new TSSLServerSocket(port, sslSocketFactory));
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000601 } else {
Roger Meierca142b02011-06-07 17:59:07 +0000602 if (domain_socket != "") {
603 unlink(domain_socket.c_str());
Roger Meier611f90c2011-12-11 22:08:51 +0000604 serverSocket = boost::shared_ptr<TServerSocket>(new TServerSocket(domain_socket));
Roger Meierca142b02011-06-07 17:59:07 +0000605 port = 0;
606 }
607 else {
Roger Meier611f90c2011-12-11 22:08:51 +0000608 serverSocket = boost::shared_ptr<TServerSocket>(new TServerSocket(port));
Roger Meierca142b02011-06-07 17:59:07 +0000609 }
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000610 }
Roger Meierca142b02011-06-07 17:59:07 +0000611
Mark Sleed788b2e2006-09-07 01:26:35 +0000612 // Factory
Roger Meier611f90c2011-12-11 22:08:51 +0000613 boost::shared_ptr<TTransportFactory> transportFactory;
Jake Farrell5d02b802014-01-07 21:42:01 -0500614
Roger Meier7e056e72011-07-17 07:28:28 +0000615 if (transport_type == "http" && server_type != "nonblocking") {
Jake Farrell5d02b802014-01-07 21:42:01 -0500616 boost::shared_ptr<TTransportFactory> httpTransportFactory(new THttpServerTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000617 transportFactory = httpTransportFactory;
618 } else if (transport_type == "framed") {
Jake Farrell5d02b802014-01-07 21:42:01 -0500619 boost::shared_ptr<TTransportFactory> framedTransportFactory(new TFramedTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000620 transportFactory = framedTransportFactory;
621 } else {
Jake Farrell5d02b802014-01-07 21:42:01 -0500622 boost::shared_ptr<TTransportFactory> bufferedTransportFactory(new TBufferedTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000623 transportFactory = bufferedTransportFactory;
624 }
Mark Sleed788b2e2006-09-07 01:26:35 +0000625
Roger Meierca142b02011-06-07 17:59:07 +0000626 // Server Info
627 cout << "Starting \"" << server_type << "\" server ("
628 << transport_type << "/" << protocol_type << ") listen on: " << domain_socket;
629 if (port != 0) {
630 cout << port;
631 }
632 cout << endl;
Mark Sleee8540632006-05-30 09:24:40 +0000633
Roger Meierca142b02011-06-07 17:59:07 +0000634 // Server
Jake Farrell5d02b802014-01-07 21:42:01 -0500635 boost::shared_ptr<apache::thrift::server::TServer> server;
636
Roger Meierca142b02011-06-07 17:59:07 +0000637 if (server_type == "simple") {
Jake Farrell5d02b802014-01-07 21:42:01 -0500638 server.reset(new TSimpleServer(testProcessor,
Roger Meierca142b02011-06-07 17:59:07 +0000639 serverSocket,
Mark Sleed788b2e2006-09-07 01:26:35 +0000640 transportFactory,
Jake Farrell5d02b802014-01-07 21:42:01 -0500641 protocolFactory));
Roger Meierca142b02011-06-07 17:59:07 +0000642 } else if (server_type == "thread-pool") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000643
Roger Meier611f90c2011-12-11 22:08:51 +0000644 boost::shared_ptr<ThreadManager> threadManager =
Roger Meierca142b02011-06-07 17:59:07 +0000645 ThreadManager::newSimpleThreadManager(workers);
Marc Slemko6be374b2006-08-04 03:16:25 +0000646
Roger Meier611f90c2011-12-11 22:08:51 +0000647 boost::shared_ptr<PlatformThreadFactory> threadFactory =
648 boost::shared_ptr<PlatformThreadFactory>(new PlatformThreadFactory());
Marc Slemko6be374b2006-08-04 03:16:25 +0000649
650 threadManager->threadFactory(threadFactory);
651
652 threadManager->start();
653
Jake Farrell5d02b802014-01-07 21:42:01 -0500654 server.reset(new TThreadPoolServer(testProcessor,
Roger Meierca142b02011-06-07 17:59:07 +0000655 serverSocket,
Mark Sleed788b2e2006-09-07 01:26:35 +0000656 transportFactory,
Mark Sleea3302652006-10-25 19:03:32 +0000657 protocolFactory,
Jake Farrell5d02b802014-01-07 21:42:01 -0500658 threadManager));
Roger Meierca142b02011-06-07 17:59:07 +0000659 } else if (server_type == "threaded") {
David Reiss0c90f6f2008-02-06 22:18:40 +0000660
Jake Farrell5d02b802014-01-07 21:42:01 -0500661 server.reset(new TThreadedServer(testProcessor,
Mark Slee0c2dff32007-03-09 19:26:57 +0000662 serverSocket,
663 transportFactory,
Jake Farrell5d02b802014-01-07 21:42:01 -0500664 protocolFactory));
Roger Meierca142b02011-06-07 17:59:07 +0000665 } else if (server_type == "nonblocking") {
Roger Meier7e056e72011-07-17 07:28:28 +0000666 if(transport_type == "http") {
Roger Meier611f90c2011-12-11 22:08:51 +0000667 boost::shared_ptr<TestHandlerAsync> testHandlerAsync(new TestHandlerAsync(testHandler));
668 boost::shared_ptr<TAsyncProcessor> testProcessorAsync(new ThriftTestAsyncProcessor(testHandlerAsync));
669 boost::shared_ptr<TAsyncBufferProcessor> testBufferProcessor(new TAsyncProtocolProcessor(testProcessorAsync, protocolFactory));
Jake Farrell5d02b802014-01-07 21:42:01 -0500670
671 // not loading nonblockingServer into "server" because
672 // TEvhttpServer doesn't inherit from TServer, and doesn't
673 // provide a stop method.
Roger Meier7e056e72011-07-17 07:28:28 +0000674 TEvhttpServer nonblockingServer(testBufferProcessor, port);
675 nonblockingServer.serve();
Jake Farrell5d02b802014-01-07 21:42:01 -0500676 } else {
677 server.reset(new TNonblockingServer(testProcessor, port));
Roger Meier7e056e72011-07-17 07:28:28 +0000678 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000679 }
680
Jake Farrell5d02b802014-01-07 21:42:01 -0500681 if(server.get() != NULL)
682 {
683 apache::thrift::concurrency::PlatformThreadFactory factory;
684 factory.setDetached(false);
685 boost::shared_ptr<apache::thrift::concurrency::Runnable> serverThreadRunner(server);
686 boost::shared_ptr<apache::thrift::concurrency::Thread> thread = factory.newThread(serverThreadRunner);
687 thread->start();
688
689 cout<<"Press enter to stop the server."<<endl;
690 cin.ignore(); //wait until a key is pressed
691
692 server->stop();
693 thread->join();
694 server.reset();
695 }
696
Roger Meierca142b02011-06-07 17:59:07 +0000697 cout << "done." << endl;
Mark Sleee8540632006-05-30 09:24:40 +0000698 return 0;
699}