blob: 18bdc548cd69bed586f3fdac5444eaca98a90957 [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
Marc Slemko6be374b2006-08-04 03:16:25 +000020#include <concurrency/ThreadManager.h>
21#include <concurrency/PosixThreadFactory.h>
22#include <protocol/TBinaryProtocol.h>
23#include <server/TSimpleServer.h>
Mark Slee0c2dff32007-03-09 19:26:57 +000024#include <server/TThreadedServer.h>
Marc Slemko6be374b2006-08-04 03:16:25 +000025#include <server/TThreadPoolServer.h>
Mark Sleeb9acf982006-10-10 01:57:32 +000026#include <server/TNonblockingServer.h>
Marc Slemko6be374b2006-08-04 03:16:25 +000027#include <transport/TServerSocket.h>
Mark Sleea3302652006-10-25 19:03:32 +000028#include <transport/TTransportUtils.h>
Mark Slee95771002006-06-07 06:53:25 +000029#include "ThriftTest.h"
Marc Slemko6be374b2006-08-04 03:16:25 +000030
31#include <iostream>
32#include <stdexcept>
33#include <sstream>
34
David Reissbc3dddb2007-08-22 23:20:24 +000035#define __STDC_FORMAT_MACROS
36#include <inttypes.h>
37
Mark Sleee8540632006-05-30 09:24:40 +000038using namespace std;
Mark Slee0c2dff32007-03-09 19:26:57 +000039using namespace boost;
Mark Sleee8540632006-05-30 09:24:40 +000040
T Jake Lucianib5e62212009-01-31 22:36:20 +000041using namespace apache::thrift;
42using namespace apache::thrift::concurrency;
43using namespace apache::thrift::protocol;
44using namespace apache::thrift::transport;
45using namespace apache::thrift::server;
Marc Slemko6be374b2006-08-04 03:16:25 +000046
Marc Slemkobf4fd192006-08-15 21:29:39 +000047using namespace thrift::test;
48
Mark Sleed2655522006-09-05 22:09:57 +000049class TestHandler : public ThriftTestIf {
Mark Sleee8540632006-05-30 09:24:40 +000050 public:
Mark Sleed2655522006-09-05 22:09:57 +000051 TestHandler() {}
Mark Sleee8540632006-05-30 09:24:40 +000052
53 void testVoid() {
54 printf("testVoid()\n");
55 }
56
Mark Slee1921d202007-01-24 19:43:06 +000057 void testString(string& out, const string &thing) {
Mark Sleee8540632006-05-30 09:24:40 +000058 printf("testString(\"%s\")\n", thing.c_str());
Mark Slee1921d202007-01-24 19:43:06 +000059 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +000060 }
61
Mark Slee1921d202007-01-24 19:43:06 +000062 int8_t testByte(const int8_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +000063 printf("testByte(%d)\n", (int)thing);
64 return thing;
65 }
66
Mark Slee1921d202007-01-24 19:43:06 +000067 int32_t testI32(const int32_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +000068 printf("testI32(%d)\n", thing);
69 return thing;
70 }
71
Mark Slee1921d202007-01-24 19:43:06 +000072 int64_t testI64(const int64_t thing) {
David Reissbc3dddb2007-08-22 23:20:24 +000073 printf("testI64(%"PRId64")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +000074 return thing;
75 }
76
Mark Slee1921d202007-01-24 19:43:06 +000077 double testDouble(const double thing) {
Mark Sleec98d0502006-09-06 02:42:25 +000078 printf("testDouble(%lf)\n", thing);
79 return thing;
80 }
81
Mark Slee1921d202007-01-24 19:43:06 +000082 void testStruct(Xtruct& out, const Xtruct &thing) {
David Reissbc3dddb2007-08-22 23:20:24 +000083 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 +000084 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +000085 }
86
Mark Slee1921d202007-01-24 19:43:06 +000087 void testNest(Xtruct2& out, const Xtruct2& nest) {
88 const Xtruct &thing = nest.struct_thing;
David Reissbc3dddb2007-08-22 23:20:24 +000089 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 +000090 out = nest;
Mark Sleee8540632006-05-30 09:24:40 +000091 }
92
Mark Slee1921d202007-01-24 19:43:06 +000093 void testMap(map<int32_t, int32_t> &out, const map<int32_t, int32_t> &thing) {
Mark Sleee8540632006-05-30 09:24:40 +000094 printf("testMap({");
95 map<int32_t, int32_t>::const_iterator m_iter;
96 bool first = true;
97 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
98 if (first) {
99 first = false;
100 } else {
101 printf(", ");
102 }
103 printf("%d => %d", m_iter->first, m_iter->second);
104 }
105 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000106 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000107 }
108
Mark Slee1921d202007-01-24 19:43:06 +0000109 void testSet(set<int32_t> &out, const set<int32_t> &thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000110 printf("testSet({");
111 set<int32_t>::const_iterator s_iter;
112 bool first = true;
113 for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) {
114 if (first) {
115 first = false;
116 } else {
117 printf(", ");
118 }
119 printf("%d", *s_iter);
120 }
121 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000122 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000123 }
124
Mark Slee1921d202007-01-24 19:43:06 +0000125 void testList(vector<int32_t> &out, const vector<int32_t> &thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000126 printf("testList({");
Mark Sleeb9acf982006-10-10 01:57:32 +0000127 vector<int32_t>::const_iterator l_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000128 bool first = true;
129 for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) {
130 if (first) {
131 first = false;
132 } else {
133 printf(", ");
134 }
135 printf("%d", *l_iter);
136 }
137 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000138 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000139 }
140
Bryan Duxbury833ae492010-09-27 17:26:02 +0000141 Numberz::type testEnum(const Numberz::type thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000142 printf("testEnum(%d)\n", thing);
143 return thing;
144 }
145
Mark Slee1921d202007-01-24 19:43:06 +0000146 UserId testTypedef(const UserId thing) {
David Reissbc3dddb2007-08-22 23:20:24 +0000147 printf("testTypedef(%"PRId64")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +0000148 return thing;
149 }
150
Mark Slee1921d202007-01-24 19:43:06 +0000151 void testMapMap(map<int32_t, map<int32_t,int32_t> > &mapmap, const int32_t hello) {
Mark Sleee8540632006-05-30 09:24:40 +0000152 printf("testMapMap(%d)\n", hello);
Mark Sleee8540632006-05-30 09:24:40 +0000153
154 map<int32_t,int32_t> pos;
155 map<int32_t,int32_t> neg;
156 for (int i = 1; i < 5; i++) {
157 pos.insert(make_pair(i,i));
158 neg.insert(make_pair(-i,-i));
159 }
160
161 mapmap.insert(make_pair(4, pos));
162 mapmap.insert(make_pair(-4, neg));
163
Mark Sleee8540632006-05-30 09:24:40 +0000164 }
165
Bryan Duxbury833ae492010-09-27 17:26:02 +0000166 void testInsanity(map<UserId, map<Numberz::type,Insanity> > &insane, const Insanity &argument) {
Mark Sleee8540632006-05-30 09:24:40 +0000167 printf("testInsanity()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000168
Mark Sleee8540632006-05-30 09:24:40 +0000169 Xtruct hello;
170 hello.string_thing = "Hello2";
171 hello.byte_thing = 2;
Mark Sleee8540632006-05-30 09:24:40 +0000172 hello.i32_thing = 2;
Mark Sleee8540632006-05-30 09:24:40 +0000173 hello.i64_thing = 2;
174
175 Xtruct goodbye;
176 goodbye.string_thing = "Goodbye4";
177 goodbye.byte_thing = 4;
Mark Sleee8540632006-05-30 09:24:40 +0000178 goodbye.i32_thing = 4;
Mark Sleee8540632006-05-30 09:24:40 +0000179 goodbye.i64_thing = 4;
180
181 Insanity crazy;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000182 crazy.userMap.insert(make_pair(Numberz::EIGHT, 8));
Mark Sleee8540632006-05-30 09:24:40 +0000183 crazy.xtructs.push_back(goodbye);
184
185 Insanity looney;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000186 crazy.userMap.insert(make_pair(Numberz::FIVE, 5));
Mark Sleee8540632006-05-30 09:24:40 +0000187 crazy.xtructs.push_back(hello);
188
Bryan Duxbury833ae492010-09-27 17:26:02 +0000189 map<Numberz::type, Insanity> first_map;
190 map<Numberz::type, Insanity> second_map;
Mark Sleee8540632006-05-30 09:24:40 +0000191
Bryan Duxbury833ae492010-09-27 17:26:02 +0000192 first_map.insert(make_pair(Numberz::TWO, crazy));
193 first_map.insert(make_pair(Numberz::THREE, crazy));
Mark Sleee8540632006-05-30 09:24:40 +0000194
Bryan Duxbury833ae492010-09-27 17:26:02 +0000195 second_map.insert(make_pair(Numberz::SIX, looney));
Mark Sleee8540632006-05-30 09:24:40 +0000196
Mark Sleee8540632006-05-30 09:24:40 +0000197 insane.insert(make_pair(1, first_map));
198 insane.insert(make_pair(2, second_map));
199
200 printf("return");
201 printf(" = {");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000202 map<UserId, map<Numberz::type,Insanity> >::const_iterator i_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000203 for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) {
David Reissbc3dddb2007-08-22 23:20:24 +0000204 printf("%"PRId64" => {", i_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000205 map<Numberz::type,Insanity>::const_iterator i2_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000206 for (i2_iter = i_iter->second.begin();
207 i2_iter != i_iter->second.end();
208 ++i2_iter) {
209 printf("%d => {", i2_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000210 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
211 map<Numberz::type, UserId>::const_iterator um;
Mark Sleee8540632006-05-30 09:24:40 +0000212 printf("{");
213 for (um = userMap.begin(); um != userMap.end(); ++um) {
David Reissbc3dddb2007-08-22 23:20:24 +0000214 printf("%d => %"PRId64", ", um->first, um->second);
Mark Sleee8540632006-05-30 09:24:40 +0000215 }
216 printf("}, ");
217
Mark Sleeb9acf982006-10-10 01:57:32 +0000218 vector<Xtruct> xtructs = i2_iter->second.xtructs;
219 vector<Xtruct>::const_iterator x;
Mark Sleee8540632006-05-30 09:24:40 +0000220 printf("{");
221 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
David Reissbc3dddb2007-08-22 23:20:24 +0000222 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 +0000223 }
224 printf("}");
225
226 printf("}, ");
227 }
228 printf("}, ");
229 }
230 printf("}\n");
231
David Reiss0c90f6f2008-02-06 22:18:40 +0000232
Mark Sleee8540632006-05-30 09:24:40 +0000233 }
Marc Slemkoe6889de2006-08-12 00:32:53 +0000234
Bryan Duxbury833ae492010-09-27 17:26:02 +0000235 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 +0000236 printf("testMulti()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000237
Marc Slemkoe6889de2006-08-12 00:32:53 +0000238 hello.string_thing = "Hello2";
239 hello.byte_thing = arg0;
240 hello.i32_thing = arg1;
241 hello.i64_thing = (int64_t)arg2;
Marc Slemkoe6889de2006-08-12 00:32:53 +0000242 }
Marc Slemkobf4fd192006-08-15 21:29:39 +0000243
David Reiss55ff70f2008-06-11 00:58:25 +0000244 void testException(const std::string &arg)
T Jake Lucianib5e62212009-01-31 22:36:20 +0000245 throw(Xception, apache::thrift::TException)
David Reiss55ff70f2008-06-11 00:58:25 +0000246 {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000247 printf("testException(%s)\n", arg.c_str());
Mark Sleed3d733a2006-09-01 22:19:06 +0000248 if (arg.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000249 Xception e;
250 e.errorCode = 1001;
David Reiss9813cbe2009-01-31 21:39:11 +0000251 e.message = arg;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000252 throw e;
David Reiss55ff70f2008-06-11 00:58:25 +0000253 } else if (arg.compare("ApplicationException") == 0) {
T Jake Lucianib5e62212009-01-31 22:36:20 +0000254 apache::thrift::TException e;
David Reiss55ff70f2008-06-11 00:58:25 +0000255 throw e;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000256 } else {
257 Xtruct result;
258 result.string_thing = arg;
259 return;
260 }
261 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000262
Mark Slee1921d202007-01-24 19:43:06 +0000263 void testMultiException(Xtruct &result, const std::string &arg0, const std::string &arg1) throw(Xception, Xception2) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000264
Marc Slemko71d4e472006-08-15 22:34:04 +0000265 printf("testMultiException(%s, %s)\n", arg0.c_str(), arg1.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000266
Mark Sleef5f2be42006-09-05 21:05:31 +0000267 if (arg0.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000268 Xception e;
269 e.errorCode = 1001;
270 e.message = "This is an Xception";
271 throw e;
Mark Sleef5f2be42006-09-05 21:05:31 +0000272 } else if (arg0.compare("Xception2") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000273 Xception2 e;
274 e.errorCode = 2002;
275 e.struct_thing.string_thing = "This is an Xception2";
276 throw e;
277 } else {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000278 result.string_thing = arg1;
Mark Slee1921d202007-01-24 19:43:06 +0000279 return;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000280 }
281 }
David Reiss2ab6fe82008-02-18 02:11:44 +0000282
David Reiss6ce401d2009-03-24 20:01:58 +0000283 void testOneway(int sleepFor) {
284 printf("testOneway(%d): Sleeping...\n", sleepFor);
David Reiss2ab6fe82008-02-18 02:11:44 +0000285 sleep(sleepFor);
David Reiss6ce401d2009-03-24 20:01:58 +0000286 printf("testOneway(%d): done sleeping!\n", sleepFor);
David Reiss2ab6fe82008-02-18 02:11:44 +0000287 }
Mark Sleee8540632006-05-30 09:24:40 +0000288};
289
David Reissd7192062010-10-06 17:09:33 +0000290
291class TestProcessorEventHandler : public TProcessorEventHandler {
292 virtual void* getContext(const char* fn_name) {
293 return new std::string(fn_name);
294 }
295 virtual void freeContext(void* ctx, const char* fn_name) {
296 delete static_cast<std::string*>(ctx);
297 }
298 virtual void preRead(void* ctx, const char* fn_name) {
299 communicate("preRead", ctx, fn_name);
300 }
301 virtual void postRead(void* ctx, const char* fn_name) {
302 communicate("postRead", ctx, fn_name);
303 }
304 virtual void preWrite(void* ctx, const char* fn_name) {
305 communicate("preWrite", ctx, fn_name);
306 }
307 virtual void postWrite(void* ctx, const char* fn_name) {
308 communicate("postWrite", ctx, fn_name);
309 }
310 virtual void asyncComplete(void* ctx, const char* fn_name) {
311 communicate("asyncComplete", ctx, fn_name);
312 }
313 virtual void handlerError(void* ctx, const char* fn_name) {
314 communicate("handlerError", ctx, fn_name);
315 }
316
317 void communicate(const char* event, void* ctx, const char* fn_name) {
318 std::cout << event << ": " << *static_cast<std::string*>(ctx) << " = " << fn_name << std::endl;
319 }
320};
321
322
Mark Sleee8540632006-05-30 09:24:40 +0000323int main(int argc, char **argv) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000324
Mark Sleee8540632006-05-30 09:24:40 +0000325 int port = 9090;
Marc Slemko6be374b2006-08-04 03:16:25 +0000326 string serverType = "simple";
327 string protocolType = "binary";
328 size_t workerCount = 4;
329
330 ostringstream usage;
331
332 usage <<
David Reissd7192062010-10-06 17:09:33 +0000333 argv[0] << " [--port=<port number>] [--server-type=<server-type>] [--protocol-type=<protocol-type>] [--workers=<worker-count>] [--processor-events]" << endl <<
Marc Slemko6be374b2006-08-04 03:16:25 +0000334
Mark Slee0c2dff32007-03-09 19:26:57 +0000335 "\t\tserver-type\t\ttype of server, \"simple\", \"thread-pool\", \"threaded\", or \"nonblocking\". Default is " << serverType << endl <<
Marc Slemko6be374b2006-08-04 03:16:25 +0000336
337 "\t\tprotocol-type\t\ttype of protocol, \"binary\", \"ascii\", or \"xml\". Default is " << protocolType << endl <<
338
339 "\t\tworkers\t\tNumber of thread pools workers. Only valid for thread-pool server type. Default is " << workerCount << endl;
David Reiss0c90f6f2008-02-06 22:18:40 +0000340
Marc Slemko6be374b2006-08-04 03:16:25 +0000341 map<string, string> args;
David Reiss0c90f6f2008-02-06 22:18:40 +0000342
Mark Sleef5f2be42006-09-05 21:05:31 +0000343 for (int ix = 1; ix < argc; ix++) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000344 string arg(argv[ix]);
Mark Sleef5f2be42006-09-05 21:05:31 +0000345 if (arg.compare(0,2, "--") == 0) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000346 size_t end = arg.find_first_of("=", 2);
Mark Sleef5f2be42006-09-05 21:05:31 +0000347 if (end != string::npos) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000348 args[string(arg, 2, end - 2)] = string(arg, end + 1);
349 } else {
Mark Sleea3302652006-10-25 19:03:32 +0000350 args[string(arg, 2)] = "true";
Marc Slemko6be374b2006-08-04 03:16:25 +0000351 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000352 } else {
353 throw invalid_argument("Unexcepted command line token: "+arg);
354 }
Mark Sleee8540632006-05-30 09:24:40 +0000355 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000356
357 try {
358
Mark Sleef5f2be42006-09-05 21:05:31 +0000359 if (!args["port"].empty()) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000360 port = atoi(args["port"].c_str());
361 }
362
Mark Sleef5f2be42006-09-05 21:05:31 +0000363 if (!args["server-type"].empty()) {
David Reiss0c90f6f2008-02-06 22:18:40 +0000364 serverType = args["server-type"];
Mark Sleef5f2be42006-09-05 21:05:31 +0000365 if (serverType == "simple") {
366 } else if (serverType == "thread-pool") {
Mark Slee0c2dff32007-03-09 19:26:57 +0000367 } else if (serverType == "threaded") {
Mark Sleeb9acf982006-10-10 01:57:32 +0000368 } else if (serverType == "nonblocking") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000369 } else {
Marc Slemko6be374b2006-08-04 03:16:25 +0000370 throw invalid_argument("Unknown server type "+serverType);
371 }
372 }
373
Mark Sleef5f2be42006-09-05 21:05:31 +0000374 if (!args["protocol-type"].empty()) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000375 protocolType = args["protocol-type"];
Mark Sleef5f2be42006-09-05 21:05:31 +0000376 if (protocolType == "binary") {
377 } else if (protocolType == "ascii") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000378 throw invalid_argument("ASCII protocol not supported");
Mark Sleef5f2be42006-09-05 21:05:31 +0000379 } else if (protocolType == "xml") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000380 throw invalid_argument("XML protocol not supported");
381 } else {
382 throw invalid_argument("Unknown protocol type "+protocolType);
383 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000384 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000385
Mark Sleef5f2be42006-09-05 21:05:31 +0000386 if (!args["workers"].empty()) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000387 workerCount = atoi(args["workers"].c_str());
388 }
Bryan Duxbury833ae492010-09-27 17:26:02 +0000389 } catch (std::exception& e) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000390 cerr << e.what() << endl;
391 cerr << usage;
392 }
393
Mark Sleee8540632006-05-30 09:24:40 +0000394 // Dispatcher
Mark Sleea3302652006-10-25 19:03:32 +0000395 shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
Marc Slemko6be374b2006-08-04 03:16:25 +0000396
Mark Sleed2655522006-09-05 22:09:57 +0000397 shared_ptr<TestHandler> testHandler(new TestHandler());
398
Mark Sleea3302652006-10-25 19:03:32 +0000399 shared_ptr<ThriftTestProcessor> testProcessor(new ThriftTestProcessor(testHandler));
Mark Sleee8540632006-05-30 09:24:40 +0000400
David Reissd7192062010-10-06 17:09:33 +0000401
402 if (!args["processor-events"].empty()) {
403 testProcessor->setEventHandler(shared_ptr<TProcessorEventHandler>(
404 new TestProcessorEventHandler()));
405 }
406
Mark Sleee8540632006-05-30 09:24:40 +0000407 // Transport
Marc Slemko6be374b2006-08-04 03:16:25 +0000408 shared_ptr<TServerSocket> serverSocket(new TServerSocket(port));
Mark Sleee8540632006-05-30 09:24:40 +0000409
Mark Sleed788b2e2006-09-07 01:26:35 +0000410 // Factory
411 shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
412
Mark Sleed3d733a2006-09-01 22:19:06 +0000413 if (serverType == "simple") {
Mark Sleee8540632006-05-30 09:24:40 +0000414
Marc Slemko6be374b2006-08-04 03:16:25 +0000415 // Server
Mark Slee018b6992006-09-07 21:31:12 +0000416 TSimpleServer simpleServer(testProcessor,
Mark Sleed788b2e2006-09-07 01:26:35 +0000417 serverSocket,
418 transportFactory,
Mark Sleea3302652006-10-25 19:03:32 +0000419 protocolFactory);
Marc Slemko6be374b2006-08-04 03:16:25 +0000420
421 printf("Starting the server on port %d...\n", port);
Mark Slee794993d2006-09-20 01:56:10 +0000422 simpleServer.serve();
Marc Slemko6be374b2006-08-04 03:16:25 +0000423
Mark Sleed3d733a2006-09-01 22:19:06 +0000424 } else if (serverType == "thread-pool") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000425
Mark Sleef5f2be42006-09-05 21:05:31 +0000426 shared_ptr<ThreadManager> threadManager =
427 ThreadManager::newSimpleThreadManager(workerCount);
Marc Slemko6be374b2006-08-04 03:16:25 +0000428
Mark Sleef5f2be42006-09-05 21:05:31 +0000429 shared_ptr<PosixThreadFactory> threadFactory =
430 shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
Marc Slemko6be374b2006-08-04 03:16:25 +0000431
432 threadManager->threadFactory(threadFactory);
433
434 threadManager->start();
435
Mark Slee018b6992006-09-07 21:31:12 +0000436 TThreadPoolServer threadPoolServer(testProcessor,
Marc Slemko6be374b2006-08-04 03:16:25 +0000437 serverSocket,
Mark Sleed788b2e2006-09-07 01:26:35 +0000438 transportFactory,
Mark Sleea3302652006-10-25 19:03:32 +0000439 protocolFactory,
440 threadManager);
Marc Slemko6be374b2006-08-04 03:16:25 +0000441
442 printf("Starting the server on port %d...\n", port);
Mark Slee794993d2006-09-20 01:56:10 +0000443 threadPoolServer.serve();
Mark Sleeb9acf982006-10-10 01:57:32 +0000444
Mark Slee0c2dff32007-03-09 19:26:57 +0000445 } else if (serverType == "threaded") {
David Reiss0c90f6f2008-02-06 22:18:40 +0000446
Mark Slee0c2dff32007-03-09 19:26:57 +0000447 TThreadedServer threadedServer(testProcessor,
448 serverSocket,
449 transportFactory,
450 protocolFactory);
451
452 printf("Starting the server on port %d...\n", port);
453 threadedServer.serve();
454
Mark Sleeb9acf982006-10-10 01:57:32 +0000455 } else if (serverType == "nonblocking") {
Mark Sleea3302652006-10-25 19:03:32 +0000456 TNonblockingServer nonblockingServer(testProcessor, port);
Mark Sleeb9acf982006-10-10 01:57:32 +0000457 printf("Starting the nonblocking server on port %d...\n", port);
458 nonblockingServer.serve();
Marc Slemko6be374b2006-08-04 03:16:25 +0000459 }
460
Mark Sleee8540632006-05-30 09:24:40 +0000461 printf("done.\n");
462 return 0;
463}