blob: c99fbac01268b2aebadb6a6da4413f88c513b123 [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>
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) {
Roger Meiera8cef6e2011-07-17 18:55:59 +000091 printf("testDouble(%f)\n", thing);
Mark Sleec98d0502006-09-06 02:42:25 +000092 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) {
Roger Meiera8cef6e2011-07-17 18:55:59 +0000196 (void) argument;
Mark Sleee8540632006-05-30 09:24:40 +0000197 printf("testInsanity()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000198
Mark Sleee8540632006-05-30 09:24:40 +0000199 Xtruct hello;
200 hello.string_thing = "Hello2";
201 hello.byte_thing = 2;
Mark Sleee8540632006-05-30 09:24:40 +0000202 hello.i32_thing = 2;
Mark Sleee8540632006-05-30 09:24:40 +0000203 hello.i64_thing = 2;
204
205 Xtruct goodbye;
206 goodbye.string_thing = "Goodbye4";
207 goodbye.byte_thing = 4;
Mark Sleee8540632006-05-30 09:24:40 +0000208 goodbye.i32_thing = 4;
Mark Sleee8540632006-05-30 09:24:40 +0000209 goodbye.i64_thing = 4;
210
211 Insanity crazy;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000212 crazy.userMap.insert(make_pair(Numberz::EIGHT, 8));
Mark Sleee8540632006-05-30 09:24:40 +0000213 crazy.xtructs.push_back(goodbye);
214
215 Insanity looney;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000216 crazy.userMap.insert(make_pair(Numberz::FIVE, 5));
Mark Sleee8540632006-05-30 09:24:40 +0000217 crazy.xtructs.push_back(hello);
218
Bryan Duxbury833ae492010-09-27 17:26:02 +0000219 map<Numberz::type, Insanity> first_map;
220 map<Numberz::type, Insanity> second_map;
Mark Sleee8540632006-05-30 09:24:40 +0000221
Bryan Duxbury833ae492010-09-27 17:26:02 +0000222 first_map.insert(make_pair(Numberz::TWO, crazy));
223 first_map.insert(make_pair(Numberz::THREE, crazy));
Mark Sleee8540632006-05-30 09:24:40 +0000224
Bryan Duxbury833ae492010-09-27 17:26:02 +0000225 second_map.insert(make_pair(Numberz::SIX, looney));
Mark Sleee8540632006-05-30 09:24:40 +0000226
Mark Sleee8540632006-05-30 09:24:40 +0000227 insane.insert(make_pair(1, first_map));
228 insane.insert(make_pair(2, second_map));
229
230 printf("return");
231 printf(" = {");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000232 map<UserId, map<Numberz::type,Insanity> >::const_iterator i_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000233 for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) {
David Reissbc3dddb2007-08-22 23:20:24 +0000234 printf("%"PRId64" => {", i_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000235 map<Numberz::type,Insanity>::const_iterator i2_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000236 for (i2_iter = i_iter->second.begin();
237 i2_iter != i_iter->second.end();
238 ++i2_iter) {
239 printf("%d => {", i2_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000240 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
241 map<Numberz::type, UserId>::const_iterator um;
Mark Sleee8540632006-05-30 09:24:40 +0000242 printf("{");
243 for (um = userMap.begin(); um != userMap.end(); ++um) {
David Reissbc3dddb2007-08-22 23:20:24 +0000244 printf("%d => %"PRId64", ", um->first, um->second);
Mark Sleee8540632006-05-30 09:24:40 +0000245 }
246 printf("}, ");
247
Mark Sleeb9acf982006-10-10 01:57:32 +0000248 vector<Xtruct> xtructs = i2_iter->second.xtructs;
249 vector<Xtruct>::const_iterator x;
Mark Sleee8540632006-05-30 09:24:40 +0000250 printf("{");
251 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
David Reissbc3dddb2007-08-22 23:20:24 +0000252 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 +0000253 }
254 printf("}");
255
256 printf("}, ");
257 }
258 printf("}, ");
259 }
260 printf("}\n");
261
David Reiss0c90f6f2008-02-06 22:18:40 +0000262
Mark Sleee8540632006-05-30 09:24:40 +0000263 }
Marc Slemkoe6889de2006-08-12 00:32:53 +0000264
Bryan Duxbury833ae492010-09-27 17:26:02 +0000265 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 +0000266 (void) arg3;
267 (void) arg4;
268 (void) arg5;
269
Marc Slemkoe6889de2006-08-12 00:32:53 +0000270 printf("testMulti()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000271
Marc Slemkoe6889de2006-08-12 00:32:53 +0000272 hello.string_thing = "Hello2";
273 hello.byte_thing = arg0;
274 hello.i32_thing = arg1;
275 hello.i64_thing = (int64_t)arg2;
Marc Slemkoe6889de2006-08-12 00:32:53 +0000276 }
Marc Slemkobf4fd192006-08-15 21:29:39 +0000277
David Reiss55ff70f2008-06-11 00:58:25 +0000278 void testException(const std::string &arg)
T Jake Lucianib5e62212009-01-31 22:36:20 +0000279 throw(Xception, apache::thrift::TException)
David Reiss55ff70f2008-06-11 00:58:25 +0000280 {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000281 printf("testException(%s)\n", arg.c_str());
Mark Sleed3d733a2006-09-01 22:19:06 +0000282 if (arg.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000283 Xception e;
284 e.errorCode = 1001;
David Reiss9813cbe2009-01-31 21:39:11 +0000285 e.message = arg;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000286 throw e;
Roger Meier1f8b48f2012-05-02 22:56:47 +0000287 } else if (arg.compare("TException") == 0) {
T Jake Lucianib5e62212009-01-31 22:36:20 +0000288 apache::thrift::TException e;
David Reiss55ff70f2008-06-11 00:58:25 +0000289 throw e;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000290 } else {
291 Xtruct result;
292 result.string_thing = arg;
293 return;
294 }
295 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000296
Mark Slee1921d202007-01-24 19:43:06 +0000297 void testMultiException(Xtruct &result, const std::string &arg0, const std::string &arg1) throw(Xception, Xception2) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000298
Marc Slemko71d4e472006-08-15 22:34:04 +0000299 printf("testMultiException(%s, %s)\n", arg0.c_str(), arg1.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000300
Mark Sleef5f2be42006-09-05 21:05:31 +0000301 if (arg0.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000302 Xception e;
303 e.errorCode = 1001;
304 e.message = "This is an Xception";
305 throw e;
Mark Sleef5f2be42006-09-05 21:05:31 +0000306 } else if (arg0.compare("Xception2") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000307 Xception2 e;
308 e.errorCode = 2002;
309 e.struct_thing.string_thing = "This is an Xception2";
310 throw e;
311 } else {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000312 result.string_thing = arg1;
Mark Slee1921d202007-01-24 19:43:06 +0000313 return;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000314 }
315 }
David Reiss2ab6fe82008-02-18 02:11:44 +0000316
David Reiss6ce401d2009-03-24 20:01:58 +0000317 void testOneway(int sleepFor) {
318 printf("testOneway(%d): Sleeping...\n", sleepFor);
David Reiss2ab6fe82008-02-18 02:11:44 +0000319 sleep(sleepFor);
David Reiss6ce401d2009-03-24 20:01:58 +0000320 printf("testOneway(%d): done sleeping!\n", sleepFor);
David Reiss2ab6fe82008-02-18 02:11:44 +0000321 }
Mark Sleee8540632006-05-30 09:24:40 +0000322};
323
David Reissd7192062010-10-06 17:09:33 +0000324
325class TestProcessorEventHandler : public TProcessorEventHandler {
David Reiss23248712010-10-06 17:10:08 +0000326 virtual void* getContext(const char* fn_name, void* serverContext) {
Roger Meiera8cef6e2011-07-17 18:55:59 +0000327 (void) serverContext;
David Reissd7192062010-10-06 17:09:33 +0000328 return new std::string(fn_name);
329 }
330 virtual void freeContext(void* ctx, const char* fn_name) {
Roger Meiera8cef6e2011-07-17 18:55:59 +0000331 (void) fn_name;
David Reissd7192062010-10-06 17:09:33 +0000332 delete static_cast<std::string*>(ctx);
333 }
334 virtual void preRead(void* ctx, const char* fn_name) {
335 communicate("preRead", ctx, fn_name);
336 }
David Reissef7200f2010-10-06 17:09:42 +0000337 virtual void postRead(void* ctx, const char* fn_name, uint32_t bytes) {
Roger Meiera8cef6e2011-07-17 18:55:59 +0000338 (void) bytes;
David Reissd7192062010-10-06 17:09:33 +0000339 communicate("postRead", ctx, fn_name);
340 }
341 virtual void preWrite(void* ctx, const char* fn_name) {
342 communicate("preWrite", ctx, fn_name);
343 }
David Reissef7200f2010-10-06 17:09:42 +0000344 virtual void postWrite(void* ctx, const char* fn_name, uint32_t bytes) {
Roger Meiera8cef6e2011-07-17 18:55:59 +0000345 (void) bytes;
David Reissd7192062010-10-06 17:09:33 +0000346 communicate("postWrite", ctx, fn_name);
347 }
348 virtual void asyncComplete(void* ctx, const char* fn_name) {
349 communicate("asyncComplete", ctx, fn_name);
350 }
351 virtual void handlerError(void* ctx, const char* fn_name) {
352 communicate("handlerError", ctx, fn_name);
353 }
354
355 void communicate(const char* event, void* ctx, const char* fn_name) {
356 std::cout << event << ": " << *static_cast<std::string*>(ctx) << " = " << fn_name << std::endl;
357 }
358};
359
360
Roger Meier7e056e72011-07-17 07:28:28 +0000361class TestHandlerAsync : public ThriftTestCobSvIf {
362public:
Roger Meier611f90c2011-12-11 22:08:51 +0000363 TestHandlerAsync(boost::shared_ptr<TestHandler>& handler) : _delegate(handler) {}
Roger Meier7e056e72011-07-17 07:28:28 +0000364 virtual ~TestHandlerAsync() {}
365
366 virtual void testVoid(std::tr1::function<void()> cob) {
367 _delegate->testVoid();
368 cob();
369 }
370
371 virtual void testString(std::tr1::function<void(std::string const& _return)> cob, const std::string& thing) {
372 std::string res;
373 _delegate->testString(res, thing);
374 cob(res);
375 }
376
377 virtual void testByte(std::tr1::function<void(int8_t const& _return)> cob, const int8_t thing) {
378 int8_t res = _delegate->testByte(thing);
379 cob(res);
380 }
381
382 virtual void testI32(std::tr1::function<void(int32_t const& _return)> cob, const int32_t thing) {
383 int32_t res = _delegate->testI32(thing);
384 cob(res);
385 }
386
387 virtual void testI64(std::tr1::function<void(int64_t const& _return)> cob, const int64_t thing) {
388 int64_t res = _delegate->testI64(thing);
389 cob(res);
390 }
391
392 virtual void testDouble(std::tr1::function<void(double const& _return)> cob, const double thing) {
393 double res = _delegate->testDouble(thing);
394 cob(res);
395 }
396
397 virtual void testStruct(std::tr1::function<void(Xtruct const& _return)> cob, const Xtruct& thing) {
398 Xtruct res;
399 _delegate->testStruct(res, thing);
400 cob(res);
401 }
402
403 virtual void testNest(std::tr1::function<void(Xtruct2 const& _return)> cob, const Xtruct2& thing) {
404 Xtruct2 res;
405 _delegate->testNest(res, thing);
406 cob(res);
407 }
408
409 virtual void testMap(std::tr1::function<void(std::map<int32_t, int32_t> const& _return)> cob, const std::map<int32_t, int32_t> & thing) {
410 std::map<int32_t, int32_t> res;
411 _delegate->testMap(res, thing);
412 cob(res);
413 }
414
415 virtual void testStringMap(std::tr1::function<void(std::map<std::string, std::string> const& _return)> cob, const std::map<std::string, std::string> & thing) {
416 std::map<std::string, std::string> res;
417 _delegate->testStringMap(res, thing);
418 cob(res);
419 }
420
421 virtual void testSet(std::tr1::function<void(std::set<int32_t> const& _return)> cob, const std::set<int32_t> & thing) {
422 std::set<int32_t> res;
423 _delegate->testSet(res, thing);
424 cob(res);
425 }
426
427 virtual void testList(std::tr1::function<void(std::vector<int32_t> const& _return)> cob, const std::vector<int32_t> & thing) {
428 std::vector<int32_t> res;
429 _delegate->testList(res, thing);
430 cob(res);
431 }
432
433 virtual void testEnum(std::tr1::function<void(Numberz::type const& _return)> cob, const Numberz::type thing) {
434 Numberz::type res = _delegate->testEnum(thing);
435 cob(res);
436 }
437
438 virtual void testTypedef(std::tr1::function<void(UserId const& _return)> cob, const UserId thing) {
439 UserId res = _delegate->testTypedef(thing);
440 cob(res);
441 }
442
443 virtual void testMapMap(std::tr1::function<void(std::map<int32_t, std::map<int32_t, int32_t> > const& _return)> cob, const int32_t hello) {
444 std::map<int32_t, std::map<int32_t, int32_t> > res;
445 _delegate->testMapMap(res, hello);
446 cob(res);
447 }
448
449 virtual void testInsanity(std::tr1::function<void(std::map<UserId, std::map<Numberz::type, Insanity> > const& _return)> cob, const Insanity& argument) {
450 std::map<UserId, std::map<Numberz::type, Insanity> > res;
451 _delegate->testInsanity(res, argument);
452 cob(res);
453 }
454
455 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) {
456 Xtruct res;
457 _delegate->testMulti(res, arg0, arg1, arg2, arg3, arg4, arg5);
458 cob(res);
459 }
460
461 virtual void testException(std::tr1::function<void()> cob, std::tr1::function<void(::apache::thrift::TDelayedException* _throw)> exn_cob, const std::string& arg) {
462 try {
463 _delegate->testException(arg);
464 } catch(const apache::thrift::TException& e) {
465 exn_cob(apache::thrift::TDelayedException::delayException(e));
466 return;
467 }
468 cob();
469 }
470
471 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) {
472 Xtruct res;
473 try {
474 _delegate->testMultiException(res, arg0, arg1);
475 } catch(const apache::thrift::TException& e) {
476 exn_cob(apache::thrift::TDelayedException::delayException(e));
477 return;
478 }
479 cob(res);
480 }
481
482 virtual void testOneway(std::tr1::function<void()> cob, const int32_t secondsToSleep) {
483 _delegate->testOneway(secondsToSleep);
484 cob();
485 }
486
487protected:
Roger Meier611f90c2011-12-11 22:08:51 +0000488 boost::shared_ptr<TestHandler> _delegate;
Roger Meier7e056e72011-07-17 07:28:28 +0000489};
490
491
Mark Sleee8540632006-05-30 09:24:40 +0000492int main(int argc, char **argv) {
493 int port = 9090;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000494 bool ssl = false;
Roger Meierca142b02011-06-07 17:59:07 +0000495 string transport_type = "buffered";
496 string protocol_type = "binary";
497 string server_type = "simple";
498 string domain_socket = "";
499 size_t workers = 4;
Marc Slemko6be374b2006-08-04 03:16:25 +0000500
Roger Meierca142b02011-06-07 17:59:07 +0000501
502 program_options::options_description desc("Allowed options");
503 desc.add_options()
504 ("help,h", "produce help message")
505 ("port", program_options::value<int>(&port)->default_value(port), "Port number to listen")
506 ("domain-socket", program_options::value<string>(&domain_socket)->default_value(domain_socket),
507 "Unix Domain Socket (e.g. /tmp/ThriftTest.thrift)")
508 ("server-type", program_options::value<string>(&server_type)->default_value(server_type),
509 "type of server, \"simple\", \"thread-pool\", \"threaded\", or \"nonblocking\"")
510 ("transport", program_options::value<string>(&transport_type)->default_value(transport_type),
511 "transport: buffered, framed, http")
512 ("protocol", program_options::value<string>(&protocol_type)->default_value(protocol_type),
513 "protocol: binary, json")
514 ("ssl", "Encrypted Transport using SSL")
515 ("processor-events", "processor-events")
516 ("workers,n", program_options::value<size_t>(&workers)->default_value(workers),
517 "Number of thread pools workers. Only valid for thread-pool server type")
518 ;
Marc Slemko6be374b2006-08-04 03:16:25 +0000519
Roger Meierca142b02011-06-07 17:59:07 +0000520 program_options::variables_map vm;
521 program_options::store(program_options::parse_command_line(argc, argv, desc), vm);
522 program_options::notify(vm);
Marc Slemko6be374b2006-08-04 03:16:25 +0000523
Roger Meierca142b02011-06-07 17:59:07 +0000524 if (vm.count("help")) {
525 cout << desc << "\n";
526 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000527 }
Roger Meierca142b02011-06-07 17:59:07 +0000528
Marc Slemko6be374b2006-08-04 03:16:25 +0000529 try {
Roger Meierca142b02011-06-07 17:59:07 +0000530 if (!server_type.empty()) {
531 if (server_type == "simple") {
532 } else if (server_type == "thread-pool") {
533 } else if (server_type == "threaded") {
534 } else if (server_type == "nonblocking") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000535 } else {
Roger Meierca142b02011-06-07 17:59:07 +0000536 throw invalid_argument("Unknown server type "+server_type);
537 }
538 }
539
540 if (!protocol_type.empty()) {
541 if (protocol_type == "binary") {
542 } else if (protocol_type == "json") {
543 } else {
544 throw invalid_argument("Unknown protocol type "+protocol_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000545 }
546 }
547
Roger Meierca142b02011-06-07 17:59:07 +0000548 if (!transport_type.empty()) {
549 if (transport_type == "buffered") {
550 } else if (transport_type == "framed") {
551 } else if (transport_type == "http") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000552 } else {
Roger Meierca142b02011-06-07 17:59:07 +0000553 throw invalid_argument("Unknown transport type "+transport_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000554 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000555 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000556
Bryan Duxbury833ae492010-09-27 17:26:02 +0000557 } catch (std::exception& e) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000558 cerr << e.what() << endl;
Roger Meierca142b02011-06-07 17:59:07 +0000559 cout << desc << "\n";
560 return 1;
Marc Slemko6be374b2006-08-04 03:16:25 +0000561 }
562
Roger Meierca142b02011-06-07 17:59:07 +0000563 if (vm.count("ssl")) {
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000564 ssl = true;
565 signal(SIGPIPE, SIG_IGN);
566 }
567
Mark Sleee8540632006-05-30 09:24:40 +0000568 // Dispatcher
Roger Meier611f90c2011-12-11 22:08:51 +0000569 boost::shared_ptr<TProtocolFactory> protocolFactory;
Roger Meierca142b02011-06-07 17:59:07 +0000570 if (protocol_type == "json") {
Roger Meier611f90c2011-12-11 22:08:51 +0000571 boost::shared_ptr<TProtocolFactory> jsonProtocolFactory(new TJSONProtocolFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000572 protocolFactory = jsonProtocolFactory;
573 } else {
Roger Meier611f90c2011-12-11 22:08:51 +0000574 boost::shared_ptr<TProtocolFactory> binaryProtocolFactory(new TBinaryProtocolFactoryT<TBufferBase>());
Roger Meierca142b02011-06-07 17:59:07 +0000575 protocolFactory = binaryProtocolFactory;
576 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000577
Roger Meierca142b02011-06-07 17:59:07 +0000578 // Processor
Roger Meier611f90c2011-12-11 22:08:51 +0000579 boost::shared_ptr<TestHandler> testHandler(new TestHandler());
580 boost::shared_ptr<ThriftTestProcessor> testProcessor(new ThriftTestProcessor(testHandler));
Roger Meierca142b02011-06-07 17:59:07 +0000581
582 if (vm.count("processor-events")) {
Roger Meier611f90c2011-12-11 22:08:51 +0000583 testProcessor->setEventHandler(boost::shared_ptr<TProcessorEventHandler>(
David Reissd7192062010-10-06 17:09:33 +0000584 new TestProcessorEventHandler()));
585 }
Roger Meierca142b02011-06-07 17:59:07 +0000586
Mark Sleee8540632006-05-30 09:24:40 +0000587 // Transport
Roger Meier611f90c2011-12-11 22:08:51 +0000588 boost::shared_ptr<TSSLSocketFactory> sslSocketFactory;
589 boost::shared_ptr<TServerSocket> serverSocket;
Mark Sleee8540632006-05-30 09:24:40 +0000590
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000591 if (ssl) {
Roger Meier611f90c2011-12-11 22:08:51 +0000592 sslSocketFactory = boost::shared_ptr<TSSLSocketFactory>(new TSSLSocketFactory());
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000593 sslSocketFactory->loadCertificate("./server-certificate.pem");
594 sslSocketFactory->loadPrivateKey("./server-private-key.pem");
595 sslSocketFactory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
Roger Meier611f90c2011-12-11 22:08:51 +0000596 serverSocket = boost::shared_ptr<TServerSocket>(new TSSLServerSocket(port, sslSocketFactory));
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000597 } else {
Roger Meierca142b02011-06-07 17:59:07 +0000598 if (domain_socket != "") {
599 unlink(domain_socket.c_str());
Roger Meier611f90c2011-12-11 22:08:51 +0000600 serverSocket = boost::shared_ptr<TServerSocket>(new TServerSocket(domain_socket));
Roger Meierca142b02011-06-07 17:59:07 +0000601 port = 0;
602 }
603 else {
Roger Meier611f90c2011-12-11 22:08:51 +0000604 serverSocket = boost::shared_ptr<TServerSocket>(new TServerSocket(port));
Roger Meierca142b02011-06-07 17:59:07 +0000605 }
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000606 }
Roger Meierca142b02011-06-07 17:59:07 +0000607
Mark Sleed788b2e2006-09-07 01:26:35 +0000608 // Factory
Roger Meier611f90c2011-12-11 22:08:51 +0000609 boost::shared_ptr<TTransportFactory> transportFactory;
Roger Meierca142b02011-06-07 17:59:07 +0000610
Roger Meier7e056e72011-07-17 07:28:28 +0000611 if (transport_type == "http" && server_type != "nonblocking") {
Roger Meier611f90c2011-12-11 22:08:51 +0000612 boost::shared_ptr<TTransportFactory> httpTransportFactory(new THttpServerTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000613 transportFactory = httpTransportFactory;
614 } else if (transport_type == "framed") {
Roger Meier611f90c2011-12-11 22:08:51 +0000615 boost::shared_ptr<TTransportFactory> framedTransportFactory(new TFramedTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000616 transportFactory = framedTransportFactory;
617 } else {
Roger Meier611f90c2011-12-11 22:08:51 +0000618 boost::shared_ptr<TTransportFactory> bufferedTransportFactory(new TBufferedTransportFactory());
Roger Meierca142b02011-06-07 17:59:07 +0000619 transportFactory = bufferedTransportFactory;
620 }
Mark Sleed788b2e2006-09-07 01:26:35 +0000621
Roger Meierca142b02011-06-07 17:59:07 +0000622 // Server Info
623 cout << "Starting \"" << server_type << "\" server ("
624 << transport_type << "/" << protocol_type << ") listen on: " << domain_socket;
625 if (port != 0) {
626 cout << port;
627 }
628 cout << endl;
Mark Sleee8540632006-05-30 09:24:40 +0000629
Roger Meierca142b02011-06-07 17:59:07 +0000630 // Server
631 if (server_type == "simple") {
Mark Slee018b6992006-09-07 21:31:12 +0000632 TSimpleServer simpleServer(testProcessor,
Roger Meierca142b02011-06-07 17:59:07 +0000633 serverSocket,
Mark Sleed788b2e2006-09-07 01:26:35 +0000634 transportFactory,
Mark Sleea3302652006-10-25 19:03:32 +0000635 protocolFactory);
Marc Slemko6be374b2006-08-04 03:16:25 +0000636
Mark Slee794993d2006-09-20 01:56:10 +0000637 simpleServer.serve();
Marc Slemko6be374b2006-08-04 03:16:25 +0000638
Roger Meierca142b02011-06-07 17:59:07 +0000639 } else if (server_type == "thread-pool") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000640
Roger Meier611f90c2011-12-11 22:08:51 +0000641 boost::shared_ptr<ThreadManager> threadManager =
Roger Meierca142b02011-06-07 17:59:07 +0000642 ThreadManager::newSimpleThreadManager(workers);
Marc Slemko6be374b2006-08-04 03:16:25 +0000643
Roger Meier611f90c2011-12-11 22:08:51 +0000644 boost::shared_ptr<PlatformThreadFactory> threadFactory =
645 boost::shared_ptr<PlatformThreadFactory>(new PlatformThreadFactory());
Marc Slemko6be374b2006-08-04 03:16:25 +0000646
647 threadManager->threadFactory(threadFactory);
648
649 threadManager->start();
650
Mark Slee018b6992006-09-07 21:31:12 +0000651 TThreadPoolServer threadPoolServer(testProcessor,
Roger Meierca142b02011-06-07 17:59:07 +0000652 serverSocket,
Mark Sleed788b2e2006-09-07 01:26:35 +0000653 transportFactory,
Mark Sleea3302652006-10-25 19:03:32 +0000654 protocolFactory,
Roger Meierca142b02011-06-07 17:59:07 +0000655 threadManager);
Marc Slemko6be374b2006-08-04 03:16:25 +0000656
Mark Slee794993d2006-09-20 01:56:10 +0000657 threadPoolServer.serve();
Mark Sleeb9acf982006-10-10 01:57:32 +0000658
Roger Meierca142b02011-06-07 17:59:07 +0000659 } else if (server_type == "threaded") {
David Reiss0c90f6f2008-02-06 22:18:40 +0000660
Mark Slee0c2dff32007-03-09 19:26:57 +0000661 TThreadedServer threadedServer(testProcessor,
662 serverSocket,
663 transportFactory,
664 protocolFactory);
665
Mark Slee0c2dff32007-03-09 19:26:57 +0000666 threadedServer.serve();
667
Roger Meierca142b02011-06-07 17:59:07 +0000668 } else if (server_type == "nonblocking") {
Roger Meier7e056e72011-07-17 07:28:28 +0000669 if(transport_type == "http") {
Roger Meier611f90c2011-12-11 22:08:51 +0000670 boost::shared_ptr<TestHandlerAsync> testHandlerAsync(new TestHandlerAsync(testHandler));
671 boost::shared_ptr<TAsyncProcessor> testProcessorAsync(new ThriftTestAsyncProcessor(testHandlerAsync));
672 boost::shared_ptr<TAsyncBufferProcessor> testBufferProcessor(new TAsyncProtocolProcessor(testProcessorAsync, protocolFactory));
Roger Meier7e056e72011-07-17 07:28:28 +0000673
674 TEvhttpServer nonblockingServer(testBufferProcessor, port);
675 nonblockingServer.serve();
676} else {
677 TNonblockingServer nonblockingServer(testProcessor, port);
678 nonblockingServer.serve();
679 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000680 }
681
Roger Meierca142b02011-06-07 17:59:07 +0000682 cout << "done." << endl;
Mark Sleee8540632006-05-30 09:24:40 +0000683 return 0;
684}