blob: 2c9d43a4815ad63be18cfe761ff0ed4495a968b2 [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
Marc Slemko6be374b2006-08-04 03:16:25 +000023#include <concurrency/ThreadManager.h>
24#include <concurrency/PosixThreadFactory.h>
25#include <protocol/TBinaryProtocol.h>
Roger Meierca142b02011-06-07 17:59:07 +000026#include <protocol/TJSONProtocol.h>
Marc Slemko6be374b2006-08-04 03:16:25 +000027#include <server/TSimpleServer.h>
Mark Slee0c2dff32007-03-09 19:26:57 +000028#include <server/TThreadedServer.h>
Marc Slemko6be374b2006-08-04 03:16:25 +000029#include <server/TThreadPoolServer.h>
Roger Meier7e056e72011-07-17 07:28:28 +000030#include <async/TEvhttpServer.h>
31#include <async/TAsyncBufferProcessor.h>
32#include <async/TAsyncProtocolProcessor.h>
Mark Sleeb9acf982006-10-10 01:57:32 +000033#include <server/TNonblockingServer.h>
Marc Slemko6be374b2006-08-04 03:16:25 +000034#include <transport/TServerSocket.h>
Roger Meierbb09f442011-05-31 20:35:37 +000035#include <transport/TSSLServerSocket.h>
36#include <transport/TSSLSocket.h>
Roger Meierca142b02011-06-07 17:59:07 +000037#include <transport/THttpServer.h>
38#include <transport/THttpTransport.h>
Mark Sleea3302652006-10-25 19:03:32 +000039#include <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>
David Reissbc3dddb2007-08-22 23:20:24 +000049
Mark Sleee8540632006-05-30 09:24:40 +000050using namespace std;
Mark Slee0c2dff32007-03-09 19:26:57 +000051using namespace boost;
Mark Sleee8540632006-05-30 09:24:40 +000052
T Jake Lucianib5e62212009-01-31 22:36:20 +000053using namespace apache::thrift;
54using namespace apache::thrift::concurrency;
55using namespace apache::thrift::protocol;
56using namespace apache::thrift::transport;
57using namespace apache::thrift::server;
Roger Meier7e056e72011-07-17 07:28:28 +000058using namespace apache::thrift::async;
Marc Slemko6be374b2006-08-04 03:16:25 +000059
Marc Slemkobf4fd192006-08-15 21:29:39 +000060using namespace thrift::test;
61
Mark Sleed2655522006-09-05 22:09:57 +000062class TestHandler : public ThriftTestIf {
Mark Sleee8540632006-05-30 09:24:40 +000063 public:
Mark Sleed2655522006-09-05 22:09:57 +000064 TestHandler() {}
Mark Sleee8540632006-05-30 09:24:40 +000065
66 void testVoid() {
67 printf("testVoid()\n");
68 }
69
Mark Slee1921d202007-01-24 19:43:06 +000070 void testString(string& out, const string &thing) {
Mark Sleee8540632006-05-30 09:24:40 +000071 printf("testString(\"%s\")\n", thing.c_str());
Mark Slee1921d202007-01-24 19:43:06 +000072 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +000073 }
74
Mark Slee1921d202007-01-24 19:43:06 +000075 int8_t testByte(const int8_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +000076 printf("testByte(%d)\n", (int)thing);
77 return thing;
78 }
79
Mark Slee1921d202007-01-24 19:43:06 +000080 int32_t testI32(const int32_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +000081 printf("testI32(%d)\n", thing);
82 return thing;
83 }
84
Mark Slee1921d202007-01-24 19:43:06 +000085 int64_t testI64(const int64_t thing) {
David Reissbc3dddb2007-08-22 23:20:24 +000086 printf("testI64(%"PRId64")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +000087 return thing;
88 }
89
Mark Slee1921d202007-01-24 19:43:06 +000090 double testDouble(const double thing) {
Mark Sleec98d0502006-09-06 02:42:25 +000091 printf("testDouble(%lf)\n", thing);
92 return thing;
93 }
94
Mark Slee1921d202007-01-24 19:43:06 +000095 void testStruct(Xtruct& out, const Xtruct &thing) {
David Reissbc3dddb2007-08-22 23:20:24 +000096 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 +000097 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +000098 }
99
Mark Slee1921d202007-01-24 19:43:06 +0000100 void testNest(Xtruct2& out, const Xtruct2& nest) {
101 const Xtruct &thing = nest.struct_thing;
David Reissbc3dddb2007-08-22 23:20:24 +0000102 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 +0000103 out = nest;
Mark Sleee8540632006-05-30 09:24:40 +0000104 }
105
Mark Slee1921d202007-01-24 19:43:06 +0000106 void testMap(map<int32_t, int32_t> &out, const map<int32_t, int32_t> &thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000107 printf("testMap({");
108 map<int32_t, int32_t>::const_iterator m_iter;
109 bool first = true;
110 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
111 if (first) {
112 first = false;
113 } else {
114 printf(", ");
115 }
116 printf("%d => %d", m_iter->first, m_iter->second);
117 }
118 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000119 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000120 }
121
Roger Meierd3b9dca2011-06-24 14:01:10 +0000122 void testStringMap(map<std::string, std::string> &out, const map<std::string, std::string> &thing) {
123 printf("testMap({");
124 map<std::string, std::string>::const_iterator m_iter;
125 bool first = true;
126 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
127 if (first) {
128 first = false;
129 } else {
130 printf(", ");
131 }
132 printf("%s => %s", (m_iter->first).c_str(), (m_iter->second).c_str());
133 }
134 printf("})\n");
135 out = thing;
136 }
137
Mark Slee1921d202007-01-24 19:43:06 +0000138 void testSet(set<int32_t> &out, const set<int32_t> &thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000139 printf("testSet({");
140 set<int32_t>::const_iterator s_iter;
141 bool first = true;
142 for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) {
143 if (first) {
144 first = false;
145 } else {
146 printf(", ");
147 }
148 printf("%d", *s_iter);
149 }
150 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000151 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000152 }
153
Mark Slee1921d202007-01-24 19:43:06 +0000154 void testList(vector<int32_t> &out, const vector<int32_t> &thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000155 printf("testList({");
Mark Sleeb9acf982006-10-10 01:57:32 +0000156 vector<int32_t>::const_iterator l_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000157 bool first = true;
158 for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) {
159 if (first) {
160 first = false;
161 } else {
162 printf(", ");
163 }
164 printf("%d", *l_iter);
165 }
166 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000167 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000168 }
169
Bryan Duxbury833ae492010-09-27 17:26:02 +0000170 Numberz::type testEnum(const Numberz::type thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000171 printf("testEnum(%d)\n", thing);
172 return thing;
173 }
174
Mark Slee1921d202007-01-24 19:43:06 +0000175 UserId testTypedef(const UserId thing) {
David Reissbc3dddb2007-08-22 23:20:24 +0000176 printf("testTypedef(%"PRId64")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +0000177 return thing;
178 }
179
Mark Slee1921d202007-01-24 19:43:06 +0000180 void testMapMap(map<int32_t, map<int32_t,int32_t> > &mapmap, const int32_t hello) {
Mark Sleee8540632006-05-30 09:24:40 +0000181 printf("testMapMap(%d)\n", hello);
Mark Sleee8540632006-05-30 09:24:40 +0000182
183 map<int32_t,int32_t> pos;
184 map<int32_t,int32_t> neg;
185 for (int i = 1; i < 5; i++) {
186 pos.insert(make_pair(i,i));
187 neg.insert(make_pair(-i,-i));
188 }
189
190 mapmap.insert(make_pair(4, pos));
191 mapmap.insert(make_pair(-4, neg));
192
Mark Sleee8540632006-05-30 09:24:40 +0000193 }
194
Bryan Duxbury833ae492010-09-27 17:26:02 +0000195 void testInsanity(map<UserId, map<Numberz::type,Insanity> > &insane, const Insanity &argument) {
Mark Sleee8540632006-05-30 09:24:40 +0000196 printf("testInsanity()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000197
Mark Sleee8540632006-05-30 09:24:40 +0000198 Xtruct hello;
199 hello.string_thing = "Hello2";
200 hello.byte_thing = 2;
Mark Sleee8540632006-05-30 09:24:40 +0000201 hello.i32_thing = 2;
Mark Sleee8540632006-05-30 09:24:40 +0000202 hello.i64_thing = 2;
203
204 Xtruct goodbye;
205 goodbye.string_thing = "Goodbye4";
206 goodbye.byte_thing = 4;
Mark Sleee8540632006-05-30 09:24:40 +0000207 goodbye.i32_thing = 4;
Mark Sleee8540632006-05-30 09:24:40 +0000208 goodbye.i64_thing = 4;
209
210 Insanity crazy;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000211 crazy.userMap.insert(make_pair(Numberz::EIGHT, 8));
Mark Sleee8540632006-05-30 09:24:40 +0000212 crazy.xtructs.push_back(goodbye);
213
214 Insanity looney;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000215 crazy.userMap.insert(make_pair(Numberz::FIVE, 5));
Mark Sleee8540632006-05-30 09:24:40 +0000216 crazy.xtructs.push_back(hello);
217
Bryan Duxbury833ae492010-09-27 17:26:02 +0000218 map<Numberz::type, Insanity> first_map;
219 map<Numberz::type, Insanity> second_map;
Mark Sleee8540632006-05-30 09:24:40 +0000220
Bryan Duxbury833ae492010-09-27 17:26:02 +0000221 first_map.insert(make_pair(Numberz::TWO, crazy));
222 first_map.insert(make_pair(Numberz::THREE, crazy));
Mark Sleee8540632006-05-30 09:24:40 +0000223
Bryan Duxbury833ae492010-09-27 17:26:02 +0000224 second_map.insert(make_pair(Numberz::SIX, looney));
Mark Sleee8540632006-05-30 09:24:40 +0000225
Mark Sleee8540632006-05-30 09:24:40 +0000226 insane.insert(make_pair(1, first_map));
227 insane.insert(make_pair(2, second_map));
228
229 printf("return");
230 printf(" = {");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000231 map<UserId, map<Numberz::type,Insanity> >::const_iterator i_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000232 for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) {
David Reissbc3dddb2007-08-22 23:20:24 +0000233 printf("%"PRId64" => {", i_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000234 map<Numberz::type,Insanity>::const_iterator i2_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000235 for (i2_iter = i_iter->second.begin();
236 i2_iter != i_iter->second.end();
237 ++i2_iter) {
238 printf("%d => {", i2_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000239 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
240 map<Numberz::type, UserId>::const_iterator um;
Mark Sleee8540632006-05-30 09:24:40 +0000241 printf("{");
242 for (um = userMap.begin(); um != userMap.end(); ++um) {
David Reissbc3dddb2007-08-22 23:20:24 +0000243 printf("%d => %"PRId64", ", um->first, um->second);
Mark Sleee8540632006-05-30 09:24:40 +0000244 }
245 printf("}, ");
246
Mark Sleeb9acf982006-10-10 01:57:32 +0000247 vector<Xtruct> xtructs = i2_iter->second.xtructs;
248 vector<Xtruct>::const_iterator x;
Mark Sleee8540632006-05-30 09:24:40 +0000249 printf("{");
250 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
David Reissbc3dddb2007-08-22 23:20:24 +0000251 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 +0000252 }
253 printf("}");
254
255 printf("}, ");
256 }
257 printf("}, ");
258 }
259 printf("}\n");
260
David Reiss0c90f6f2008-02-06 22:18:40 +0000261
Mark Sleee8540632006-05-30 09:24:40 +0000262 }
Marc Slemkoe6889de2006-08-12 00:32:53 +0000263
Bryan Duxbury833ae492010-09-27 17:26:02 +0000264 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) {
Marc Slemkoe6889de2006-08-12 00:32:53 +0000265 printf("testMulti()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000266
Marc Slemkoe6889de2006-08-12 00:32:53 +0000267 hello.string_thing = "Hello2";
268 hello.byte_thing = arg0;
269 hello.i32_thing = arg1;
270 hello.i64_thing = (int64_t)arg2;
Marc Slemkoe6889de2006-08-12 00:32:53 +0000271 }
Marc Slemkobf4fd192006-08-15 21:29:39 +0000272
David Reiss55ff70f2008-06-11 00:58:25 +0000273 void testException(const std::string &arg)
T Jake Lucianib5e62212009-01-31 22:36:20 +0000274 throw(Xception, apache::thrift::TException)
David Reiss55ff70f2008-06-11 00:58:25 +0000275 {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000276 printf("testException(%s)\n", arg.c_str());
Mark Sleed3d733a2006-09-01 22:19:06 +0000277 if (arg.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000278 Xception e;
279 e.errorCode = 1001;
David Reiss9813cbe2009-01-31 21:39:11 +0000280 e.message = arg;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000281 throw e;
David Reiss55ff70f2008-06-11 00:58:25 +0000282 } else if (arg.compare("ApplicationException") == 0) {
T Jake Lucianib5e62212009-01-31 22:36:20 +0000283 apache::thrift::TException e;
David Reiss55ff70f2008-06-11 00:58:25 +0000284 throw e;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000285 } else {
286 Xtruct result;
287 result.string_thing = arg;
288 return;
289 }
290 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000291
Mark Slee1921d202007-01-24 19:43:06 +0000292 void testMultiException(Xtruct &result, const std::string &arg0, const std::string &arg1) throw(Xception, Xception2) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000293
Marc Slemko71d4e472006-08-15 22:34:04 +0000294 printf("testMultiException(%s, %s)\n", arg0.c_str(), arg1.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000295
Mark Sleef5f2be42006-09-05 21:05:31 +0000296 if (arg0.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000297 Xception e;
298 e.errorCode = 1001;
299 e.message = "This is an Xception";
300 throw e;
Mark Sleef5f2be42006-09-05 21:05:31 +0000301 } else if (arg0.compare("Xception2") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000302 Xception2 e;
303 e.errorCode = 2002;
304 e.struct_thing.string_thing = "This is an Xception2";
305 throw e;
306 } else {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000307 result.string_thing = arg1;
Mark Slee1921d202007-01-24 19:43:06 +0000308 return;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000309 }
310 }
David Reiss2ab6fe82008-02-18 02:11:44 +0000311
David Reiss6ce401d2009-03-24 20:01:58 +0000312 void testOneway(int sleepFor) {
313 printf("testOneway(%d): Sleeping...\n", sleepFor);
David Reiss2ab6fe82008-02-18 02:11:44 +0000314 sleep(sleepFor);
David Reiss6ce401d2009-03-24 20:01:58 +0000315 printf("testOneway(%d): done sleeping!\n", sleepFor);
David Reiss2ab6fe82008-02-18 02:11:44 +0000316 }
Mark Sleee8540632006-05-30 09:24:40 +0000317};
318
David Reissd7192062010-10-06 17:09:33 +0000319
320class TestProcessorEventHandler : public TProcessorEventHandler {
David Reiss23248712010-10-06 17:10:08 +0000321 virtual void* getContext(const char* fn_name, void* serverContext) {
David Reissd7192062010-10-06 17:09:33 +0000322 return new std::string(fn_name);
323 }
324 virtual void freeContext(void* ctx, const char* fn_name) {
325 delete static_cast<std::string*>(ctx);
326 }
327 virtual void preRead(void* ctx, const char* fn_name) {
328 communicate("preRead", ctx, fn_name);
329 }
David Reissef7200f2010-10-06 17:09:42 +0000330 virtual void postRead(void* ctx, const char* fn_name, uint32_t bytes) {
David Reissd7192062010-10-06 17:09:33 +0000331 communicate("postRead", ctx, fn_name);
332 }
333 virtual void preWrite(void* ctx, const char* fn_name) {
334 communicate("preWrite", ctx, fn_name);
335 }
David Reissef7200f2010-10-06 17:09:42 +0000336 virtual void postWrite(void* ctx, const char* fn_name, uint32_t bytes) {
David Reissd7192062010-10-06 17:09:33 +0000337 communicate("postWrite", ctx, fn_name);
338 }
339 virtual void asyncComplete(void* ctx, const char* fn_name) {
340 communicate("asyncComplete", ctx, fn_name);
341 }
342 virtual void handlerError(void* ctx, const char* fn_name) {
343 communicate("handlerError", ctx, fn_name);
344 }
345
346 void communicate(const char* event, void* ctx, const char* fn_name) {
347 std::cout << event << ": " << *static_cast<std::string*>(ctx) << " = " << fn_name << std::endl;
348 }
349};
350
351
Roger Meier7e056e72011-07-17 07:28:28 +0000352class TestHandlerAsync : public ThriftTestCobSvIf {
353public:
354 TestHandlerAsync(shared_ptr<TestHandler>& handler) : _delegate(handler) {}
355 virtual ~TestHandlerAsync() {}
356
357 virtual void testVoid(std::tr1::function<void()> cob) {
358 _delegate->testVoid();
359 cob();
360 }
361
362 virtual void testString(std::tr1::function<void(std::string const& _return)> cob, const std::string& thing) {
363 std::string res;
364 _delegate->testString(res, thing);
365 cob(res);
366 }
367
368 virtual void testByte(std::tr1::function<void(int8_t const& _return)> cob, const int8_t thing) {
369 int8_t res = _delegate->testByte(thing);
370 cob(res);
371 }
372
373 virtual void testI32(std::tr1::function<void(int32_t const& _return)> cob, const int32_t thing) {
374 int32_t res = _delegate->testI32(thing);
375 cob(res);
376 }
377
378 virtual void testI64(std::tr1::function<void(int64_t const& _return)> cob, const int64_t thing) {
379 int64_t res = _delegate->testI64(thing);
380 cob(res);
381 }
382
383 virtual void testDouble(std::tr1::function<void(double const& _return)> cob, const double thing) {
384 double res = _delegate->testDouble(thing);
385 cob(res);
386 }
387
388 virtual void testStruct(std::tr1::function<void(Xtruct const& _return)> cob, const Xtruct& thing) {
389 Xtruct res;
390 _delegate->testStruct(res, thing);
391 cob(res);
392 }
393
394 virtual void testNest(std::tr1::function<void(Xtruct2 const& _return)> cob, const Xtruct2& thing) {
395 Xtruct2 res;
396 _delegate->testNest(res, thing);
397 cob(res);
398 }
399
400 virtual void testMap(std::tr1::function<void(std::map<int32_t, int32_t> const& _return)> cob, const std::map<int32_t, int32_t> & thing) {
401 std::map<int32_t, int32_t> res;
402 _delegate->testMap(res, thing);
403 cob(res);
404 }
405
406 virtual void testStringMap(std::tr1::function<void(std::map<std::string, std::string> const& _return)> cob, const std::map<std::string, std::string> & thing) {
407 std::map<std::string, std::string> res;
408 _delegate->testStringMap(res, thing);
409 cob(res);
410 }
411
412 virtual void testSet(std::tr1::function<void(std::set<int32_t> const& _return)> cob, const std::set<int32_t> & thing) {
413 std::set<int32_t> res;
414 _delegate->testSet(res, thing);
415 cob(res);
416 }
417
418 virtual void testList(std::tr1::function<void(std::vector<int32_t> const& _return)> cob, const std::vector<int32_t> & thing) {
419 std::vector<int32_t> res;
420 _delegate->testList(res, thing);
421 cob(res);
422 }
423
424 virtual void testEnum(std::tr1::function<void(Numberz::type const& _return)> cob, const Numberz::type thing) {
425 Numberz::type res = _delegate->testEnum(thing);
426 cob(res);
427 }
428
429 virtual void testTypedef(std::tr1::function<void(UserId const& _return)> cob, const UserId thing) {
430 UserId res = _delegate->testTypedef(thing);
431 cob(res);
432 }
433
434 virtual void testMapMap(std::tr1::function<void(std::map<int32_t, std::map<int32_t, int32_t> > const& _return)> cob, const int32_t hello) {
435 std::map<int32_t, std::map<int32_t, int32_t> > res;
436 _delegate->testMapMap(res, hello);
437 cob(res);
438 }
439
440 virtual void testInsanity(std::tr1::function<void(std::map<UserId, std::map<Numberz::type, Insanity> > const& _return)> cob, const Insanity& argument) {
441 std::map<UserId, std::map<Numberz::type, Insanity> > res;
442 _delegate->testInsanity(res, argument);
443 cob(res);
444 }
445
446 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) {
447 Xtruct res;
448 _delegate->testMulti(res, arg0, arg1, arg2, arg3, arg4, arg5);
449 cob(res);
450 }
451
452 virtual void testException(std::tr1::function<void()> cob, std::tr1::function<void(::apache::thrift::TDelayedException* _throw)> exn_cob, const std::string& arg) {
453 try {
454 _delegate->testException(arg);
455 } catch(const apache::thrift::TException& e) {
456 exn_cob(apache::thrift::TDelayedException::delayException(e));
457 return;
458 }
459 cob();
460 }
461
462 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) {
463 Xtruct res;
464 try {
465 _delegate->testMultiException(res, arg0, arg1);
466 } catch(const apache::thrift::TException& e) {
467 exn_cob(apache::thrift::TDelayedException::delayException(e));
468 return;
469 }
470 cob(res);
471 }
472
473 virtual void testOneway(std::tr1::function<void()> cob, const int32_t secondsToSleep) {
474 _delegate->testOneway(secondsToSleep);
475 cob();
476 }
477
478protected:
479 shared_ptr<TestHandler> _delegate;
480};
481
482
Mark Sleee8540632006-05-30 09:24:40 +0000483int main(int argc, char **argv) {
484 int port = 9090;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000485 bool ssl = false;
Roger Meierca142b02011-06-07 17:59:07 +0000486 string transport_type = "buffered";
487 string protocol_type = "binary";
488 string server_type = "simple";
489 string domain_socket = "";
490 size_t workers = 4;
Marc Slemko6be374b2006-08-04 03:16:25 +0000491
Roger Meierca142b02011-06-07 17:59:07 +0000492
493 program_options::options_description desc("Allowed options");
494 desc.add_options()
495 ("help,h", "produce help message")
496 ("port", program_options::value<int>(&port)->default_value(port), "Port number to listen")
497 ("domain-socket", program_options::value<string>(&domain_socket)->default_value(domain_socket),
498 "Unix Domain Socket (e.g. /tmp/ThriftTest.thrift)")
499 ("server-type", program_options::value<string>(&server_type)->default_value(server_type),
500 "type of server, \"simple\", \"thread-pool\", \"threaded\", or \"nonblocking\"")
501 ("transport", program_options::value<string>(&transport_type)->default_value(transport_type),
502 "transport: buffered, framed, http")
503 ("protocol", program_options::value<string>(&protocol_type)->default_value(protocol_type),
504 "protocol: binary, json")
505 ("ssl", "Encrypted Transport using SSL")
506 ("processor-events", "processor-events")
507 ("workers,n", program_options::value<size_t>(&workers)->default_value(workers),
508 "Number of thread pools workers. Only valid for thread-pool server type")
509 ;
Marc Slemko6be374b2006-08-04 03:16:25 +0000510
Roger Meierca142b02011-06-07 17:59:07 +0000511 program_options::variables_map vm;
512 program_options::store(program_options::parse_command_line(argc, argv, desc), vm);
513 program_options::notify(vm);
Marc Slemko6be374b2006-08-04 03:16:25 +0000514
Roger Meierca142b02011-06-07 17:59:07 +0000515 if (vm.count("help")) {
516 cout << desc << "\n";
517 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000518 }
Roger Meierca142b02011-06-07 17:59:07 +0000519
Marc Slemko6be374b2006-08-04 03:16:25 +0000520 try {
Roger Meierca142b02011-06-07 17:59:07 +0000521 if (!server_type.empty()) {
522 if (server_type == "simple") {
523 } else if (server_type == "thread-pool") {
524 } else if (server_type == "threaded") {
525 } else if (server_type == "nonblocking") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000526 } else {
Roger Meierca142b02011-06-07 17:59:07 +0000527 throw invalid_argument("Unknown server type "+server_type);
528 }
529 }
530
531 if (!protocol_type.empty()) {
532 if (protocol_type == "binary") {
533 } else if (protocol_type == "json") {
534 } else {
535 throw invalid_argument("Unknown protocol type "+protocol_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000536 }
537 }
538
Roger Meierca142b02011-06-07 17:59:07 +0000539 if (!transport_type.empty()) {
540 if (transport_type == "buffered") {
541 } else if (transport_type == "framed") {
542 } else if (transport_type == "http") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000543 } else {
Roger Meierca142b02011-06-07 17:59:07 +0000544 throw invalid_argument("Unknown transport type "+transport_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000545 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000546 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000547
Bryan Duxbury833ae492010-09-27 17:26:02 +0000548 } catch (std::exception& e) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000549 cerr << e.what() << endl;
Roger Meierca142b02011-06-07 17:59:07 +0000550 cout << desc << "\n";
551 return 1;
Marc Slemko6be374b2006-08-04 03:16:25 +0000552 }
553
Roger Meierca142b02011-06-07 17:59:07 +0000554 if (vm.count("ssl")) {
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000555 ssl = true;
556 signal(SIGPIPE, SIG_IGN);
557 }
558
Mark Sleee8540632006-05-30 09:24:40 +0000559 // Dispatcher
Roger Meierca142b02011-06-07 17:59:07 +0000560 shared_ptr<TProtocolFactory> protocolFactory;
561 if (protocol_type == "json") {
562 shared_ptr<TProtocolFactory> jsonProtocolFactory(new TJSONProtocolFactory());
563 protocolFactory = jsonProtocolFactory;
564 } else {
565 shared_ptr<TProtocolFactory> binaryProtocolFactory(new TBinaryProtocolFactoryT<TBufferBase>());
566 protocolFactory = binaryProtocolFactory;
567 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000568
Roger Meierca142b02011-06-07 17:59:07 +0000569 // Processor
Mark Sleed2655522006-09-05 22:09:57 +0000570 shared_ptr<TestHandler> testHandler(new TestHandler());
Roger Meierca142b02011-06-07 17:59:07 +0000571 shared_ptr<ThriftTestProcessor> testProcessor(new ThriftTestProcessor(testHandler));
572
573 if (vm.count("processor-events")) {
David Reissd7192062010-10-06 17:09:33 +0000574 testProcessor->setEventHandler(shared_ptr<TProcessorEventHandler>(
575 new TestProcessorEventHandler()));
576 }
Roger Meierca142b02011-06-07 17:59:07 +0000577
Mark Sleee8540632006-05-30 09:24:40 +0000578 // Transport
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000579 shared_ptr<TSSLSocketFactory> sslSocketFactory;
580 shared_ptr<TServerSocket> serverSocket;
Mark Sleee8540632006-05-30 09:24:40 +0000581
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000582 if (ssl) {
583 sslSocketFactory = shared_ptr<TSSLSocketFactory>(new TSSLSocketFactory());
584 sslSocketFactory->loadCertificate("./server-certificate.pem");
585 sslSocketFactory->loadPrivateKey("./server-private-key.pem");
586 sslSocketFactory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
587 serverSocket = shared_ptr<TServerSocket>(new TSSLServerSocket(port, sslSocketFactory));
588 } else {
Roger Meierca142b02011-06-07 17:59:07 +0000589 if (domain_socket != "") {
590 unlink(domain_socket.c_str());
591 serverSocket = shared_ptr<TServerSocket>(new TServerSocket(domain_socket));
592 port = 0;
593 }
594 else {
595 serverSocket = shared_ptr<TServerSocket>(new TServerSocket(port));
596 }
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000597 }
Roger Meierca142b02011-06-07 17:59:07 +0000598
Mark Sleed788b2e2006-09-07 01:26:35 +0000599 // Factory
Roger Meierca142b02011-06-07 17:59:07 +0000600 shared_ptr<TTransportFactory> transportFactory;
601
Roger Meier7e056e72011-07-17 07:28:28 +0000602 if (transport_type == "http" && server_type != "nonblocking") {
Roger Meierca142b02011-06-07 17:59:07 +0000603 shared_ptr<TTransportFactory> httpTransportFactory(new THttpServerTransportFactory());
604 transportFactory = httpTransportFactory;
605 } else if (transport_type == "framed") {
606 shared_ptr<TTransportFactory> framedTransportFactory(new TFramedTransportFactory());
607 transportFactory = framedTransportFactory;
608 } else {
609 shared_ptr<TTransportFactory> bufferedTransportFactory(new TBufferedTransportFactory());
610 transportFactory = bufferedTransportFactory;
611 }
Mark Sleed788b2e2006-09-07 01:26:35 +0000612
Roger Meierca142b02011-06-07 17:59:07 +0000613 // Server Info
614 cout << "Starting \"" << server_type << "\" server ("
615 << transport_type << "/" << protocol_type << ") listen on: " << domain_socket;
616 if (port != 0) {
617 cout << port;
618 }
619 cout << endl;
Mark Sleee8540632006-05-30 09:24:40 +0000620
Roger Meierca142b02011-06-07 17:59:07 +0000621 // Server
622 if (server_type == "simple") {
Mark Slee018b6992006-09-07 21:31:12 +0000623 TSimpleServer simpleServer(testProcessor,
Roger Meierca142b02011-06-07 17:59:07 +0000624 serverSocket,
Mark Sleed788b2e2006-09-07 01:26:35 +0000625 transportFactory,
Mark Sleea3302652006-10-25 19:03:32 +0000626 protocolFactory);
Marc Slemko6be374b2006-08-04 03:16:25 +0000627
Mark Slee794993d2006-09-20 01:56:10 +0000628 simpleServer.serve();
Marc Slemko6be374b2006-08-04 03:16:25 +0000629
Roger Meierca142b02011-06-07 17:59:07 +0000630 } else if (server_type == "thread-pool") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000631
Mark Sleef5f2be42006-09-05 21:05:31 +0000632 shared_ptr<ThreadManager> threadManager =
Roger Meierca142b02011-06-07 17:59:07 +0000633 ThreadManager::newSimpleThreadManager(workers);
Marc Slemko6be374b2006-08-04 03:16:25 +0000634
Mark Sleef5f2be42006-09-05 21:05:31 +0000635 shared_ptr<PosixThreadFactory> threadFactory =
636 shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
Marc Slemko6be374b2006-08-04 03:16:25 +0000637
638 threadManager->threadFactory(threadFactory);
639
640 threadManager->start();
641
Mark Slee018b6992006-09-07 21:31:12 +0000642 TThreadPoolServer threadPoolServer(testProcessor,
Roger Meierca142b02011-06-07 17:59:07 +0000643 serverSocket,
Mark Sleed788b2e2006-09-07 01:26:35 +0000644 transportFactory,
Mark Sleea3302652006-10-25 19:03:32 +0000645 protocolFactory,
Roger Meierca142b02011-06-07 17:59:07 +0000646 threadManager);
Marc Slemko6be374b2006-08-04 03:16:25 +0000647
Mark Slee794993d2006-09-20 01:56:10 +0000648 threadPoolServer.serve();
Mark Sleeb9acf982006-10-10 01:57:32 +0000649
Roger Meierca142b02011-06-07 17:59:07 +0000650 } else if (server_type == "threaded") {
David Reiss0c90f6f2008-02-06 22:18:40 +0000651
Mark Slee0c2dff32007-03-09 19:26:57 +0000652 TThreadedServer threadedServer(testProcessor,
653 serverSocket,
654 transportFactory,
655 protocolFactory);
656
Mark Slee0c2dff32007-03-09 19:26:57 +0000657 threadedServer.serve();
658
Roger Meierca142b02011-06-07 17:59:07 +0000659 } else if (server_type == "nonblocking") {
Roger Meier7e056e72011-07-17 07:28:28 +0000660 if(transport_type == "http") {
661 shared_ptr<TestHandlerAsync> testHandlerAsync(new TestHandlerAsync(testHandler));
662 shared_ptr<TAsyncProcessor> testProcessorAsync(new ThriftTestAsyncProcessor(testHandlerAsync));
663 shared_ptr<TAsyncBufferProcessor> testBufferProcessor(new TAsyncProtocolProcessor(testProcessorAsync, protocolFactory));
664
665 TEvhttpServer nonblockingServer(testBufferProcessor, port);
666 nonblockingServer.serve();
667} else {
668 TNonblockingServer nonblockingServer(testProcessor, port);
669 nonblockingServer.serve();
670 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000671 }
672
Roger Meierca142b02011-06-07 17:59:07 +0000673 cout << "done." << endl;
Mark Sleee8540632006-05-30 09:24:40 +0000674 return 0;
675}