blob: 047dd331dd3eb8adc265fdbdc3219ed29a84fd4a [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>
Bryan Duxburycd9aea12011-02-22 18:12:06 +000037#include <signal.h>
David Reissbc3dddb2007-08-22 23:20:24 +000038
Mark Sleee8540632006-05-30 09:24:40 +000039using namespace std;
Mark Slee0c2dff32007-03-09 19:26:57 +000040using namespace boost;
Mark Sleee8540632006-05-30 09:24:40 +000041
T Jake Lucianib5e62212009-01-31 22:36:20 +000042using namespace apache::thrift;
43using namespace apache::thrift::concurrency;
44using namespace apache::thrift::protocol;
45using namespace apache::thrift::transport;
46using namespace apache::thrift::server;
Marc Slemko6be374b2006-08-04 03:16:25 +000047
Marc Slemkobf4fd192006-08-15 21:29:39 +000048using namespace thrift::test;
49
Mark Sleed2655522006-09-05 22:09:57 +000050class TestHandler : public ThriftTestIf {
Mark Sleee8540632006-05-30 09:24:40 +000051 public:
Mark Sleed2655522006-09-05 22:09:57 +000052 TestHandler() {}
Mark Sleee8540632006-05-30 09:24:40 +000053
54 void testVoid() {
55 printf("testVoid()\n");
56 }
57
Mark Slee1921d202007-01-24 19:43:06 +000058 void testString(string& out, const string &thing) {
Mark Sleee8540632006-05-30 09:24:40 +000059 printf("testString(\"%s\")\n", thing.c_str());
Mark Slee1921d202007-01-24 19:43:06 +000060 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +000061 }
62
Mark Slee1921d202007-01-24 19:43:06 +000063 int8_t testByte(const int8_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +000064 printf("testByte(%d)\n", (int)thing);
65 return thing;
66 }
67
Mark Slee1921d202007-01-24 19:43:06 +000068 int32_t testI32(const int32_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +000069 printf("testI32(%d)\n", thing);
70 return thing;
71 }
72
Mark Slee1921d202007-01-24 19:43:06 +000073 int64_t testI64(const int64_t thing) {
David Reissbc3dddb2007-08-22 23:20:24 +000074 printf("testI64(%"PRId64")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +000075 return thing;
76 }
77
Mark Slee1921d202007-01-24 19:43:06 +000078 double testDouble(const double thing) {
Mark Sleec98d0502006-09-06 02:42:25 +000079 printf("testDouble(%lf)\n", thing);
80 return thing;
81 }
82
Mark Slee1921d202007-01-24 19:43:06 +000083 void testStruct(Xtruct& out, const Xtruct &thing) {
David Reissbc3dddb2007-08-22 23:20:24 +000084 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 +000085 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +000086 }
87
Mark Slee1921d202007-01-24 19:43:06 +000088 void testNest(Xtruct2& out, const Xtruct2& nest) {
89 const Xtruct &thing = nest.struct_thing;
David Reissbc3dddb2007-08-22 23:20:24 +000090 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 +000091 out = nest;
Mark Sleee8540632006-05-30 09:24:40 +000092 }
93
Mark Slee1921d202007-01-24 19:43:06 +000094 void testMap(map<int32_t, int32_t> &out, const map<int32_t, int32_t> &thing) {
Mark Sleee8540632006-05-30 09:24:40 +000095 printf("testMap({");
96 map<int32_t, int32_t>::const_iterator m_iter;
97 bool first = true;
98 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
99 if (first) {
100 first = false;
101 } else {
102 printf(", ");
103 }
104 printf("%d => %d", m_iter->first, m_iter->second);
105 }
106 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000107 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000108 }
109
Mark Slee1921d202007-01-24 19:43:06 +0000110 void testSet(set<int32_t> &out, const set<int32_t> &thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000111 printf("testSet({");
112 set<int32_t>::const_iterator s_iter;
113 bool first = true;
114 for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) {
115 if (first) {
116 first = false;
117 } else {
118 printf(", ");
119 }
120 printf("%d", *s_iter);
121 }
122 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000123 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000124 }
125
Mark Slee1921d202007-01-24 19:43:06 +0000126 void testList(vector<int32_t> &out, const vector<int32_t> &thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000127 printf("testList({");
Mark Sleeb9acf982006-10-10 01:57:32 +0000128 vector<int32_t>::const_iterator l_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000129 bool first = true;
130 for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) {
131 if (first) {
132 first = false;
133 } else {
134 printf(", ");
135 }
136 printf("%d", *l_iter);
137 }
138 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000139 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000140 }
141
Bryan Duxbury833ae492010-09-27 17:26:02 +0000142 Numberz::type testEnum(const Numberz::type thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000143 printf("testEnum(%d)\n", thing);
144 return thing;
145 }
146
Mark Slee1921d202007-01-24 19:43:06 +0000147 UserId testTypedef(const UserId thing) {
David Reissbc3dddb2007-08-22 23:20:24 +0000148 printf("testTypedef(%"PRId64")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +0000149 return thing;
150 }
151
Mark Slee1921d202007-01-24 19:43:06 +0000152 void testMapMap(map<int32_t, map<int32_t,int32_t> > &mapmap, const int32_t hello) {
Mark Sleee8540632006-05-30 09:24:40 +0000153 printf("testMapMap(%d)\n", hello);
Mark Sleee8540632006-05-30 09:24:40 +0000154
155 map<int32_t,int32_t> pos;
156 map<int32_t,int32_t> neg;
157 for (int i = 1; i < 5; i++) {
158 pos.insert(make_pair(i,i));
159 neg.insert(make_pair(-i,-i));
160 }
161
162 mapmap.insert(make_pair(4, pos));
163 mapmap.insert(make_pair(-4, neg));
164
Mark Sleee8540632006-05-30 09:24:40 +0000165 }
166
Bryan Duxbury833ae492010-09-27 17:26:02 +0000167 void testInsanity(map<UserId, map<Numberz::type,Insanity> > &insane, const Insanity &argument) {
Mark Sleee8540632006-05-30 09:24:40 +0000168 printf("testInsanity()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000169
Mark Sleee8540632006-05-30 09:24:40 +0000170 Xtruct hello;
171 hello.string_thing = "Hello2";
172 hello.byte_thing = 2;
Mark Sleee8540632006-05-30 09:24:40 +0000173 hello.i32_thing = 2;
Mark Sleee8540632006-05-30 09:24:40 +0000174 hello.i64_thing = 2;
175
176 Xtruct goodbye;
177 goodbye.string_thing = "Goodbye4";
178 goodbye.byte_thing = 4;
Mark Sleee8540632006-05-30 09:24:40 +0000179 goodbye.i32_thing = 4;
Mark Sleee8540632006-05-30 09:24:40 +0000180 goodbye.i64_thing = 4;
181
182 Insanity crazy;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000183 crazy.userMap.insert(make_pair(Numberz::EIGHT, 8));
Mark Sleee8540632006-05-30 09:24:40 +0000184 crazy.xtructs.push_back(goodbye);
185
186 Insanity looney;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000187 crazy.userMap.insert(make_pair(Numberz::FIVE, 5));
Mark Sleee8540632006-05-30 09:24:40 +0000188 crazy.xtructs.push_back(hello);
189
Bryan Duxbury833ae492010-09-27 17:26:02 +0000190 map<Numberz::type, Insanity> first_map;
191 map<Numberz::type, Insanity> second_map;
Mark Sleee8540632006-05-30 09:24:40 +0000192
Bryan Duxbury833ae492010-09-27 17:26:02 +0000193 first_map.insert(make_pair(Numberz::TWO, crazy));
194 first_map.insert(make_pair(Numberz::THREE, crazy));
Mark Sleee8540632006-05-30 09:24:40 +0000195
Bryan Duxbury833ae492010-09-27 17:26:02 +0000196 second_map.insert(make_pair(Numberz::SIX, looney));
Mark Sleee8540632006-05-30 09:24:40 +0000197
Mark Sleee8540632006-05-30 09:24:40 +0000198 insane.insert(make_pair(1, first_map));
199 insane.insert(make_pair(2, second_map));
200
201 printf("return");
202 printf(" = {");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000203 map<UserId, map<Numberz::type,Insanity> >::const_iterator i_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000204 for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) {
David Reissbc3dddb2007-08-22 23:20:24 +0000205 printf("%"PRId64" => {", i_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000206 map<Numberz::type,Insanity>::const_iterator i2_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000207 for (i2_iter = i_iter->second.begin();
208 i2_iter != i_iter->second.end();
209 ++i2_iter) {
210 printf("%d => {", i2_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000211 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
212 map<Numberz::type, UserId>::const_iterator um;
Mark Sleee8540632006-05-30 09:24:40 +0000213 printf("{");
214 for (um = userMap.begin(); um != userMap.end(); ++um) {
David Reissbc3dddb2007-08-22 23:20:24 +0000215 printf("%d => %"PRId64", ", um->first, um->second);
Mark Sleee8540632006-05-30 09:24:40 +0000216 }
217 printf("}, ");
218
Mark Sleeb9acf982006-10-10 01:57:32 +0000219 vector<Xtruct> xtructs = i2_iter->second.xtructs;
220 vector<Xtruct>::const_iterator x;
Mark Sleee8540632006-05-30 09:24:40 +0000221 printf("{");
222 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
David Reissbc3dddb2007-08-22 23:20:24 +0000223 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 +0000224 }
225 printf("}");
226
227 printf("}, ");
228 }
229 printf("}, ");
230 }
231 printf("}\n");
232
David Reiss0c90f6f2008-02-06 22:18:40 +0000233
Mark Sleee8540632006-05-30 09:24:40 +0000234 }
Marc Slemkoe6889de2006-08-12 00:32:53 +0000235
Bryan Duxbury833ae492010-09-27 17:26:02 +0000236 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 +0000237 printf("testMulti()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000238
Marc Slemkoe6889de2006-08-12 00:32:53 +0000239 hello.string_thing = "Hello2";
240 hello.byte_thing = arg0;
241 hello.i32_thing = arg1;
242 hello.i64_thing = (int64_t)arg2;
Marc Slemkoe6889de2006-08-12 00:32:53 +0000243 }
Marc Slemkobf4fd192006-08-15 21:29:39 +0000244
David Reiss55ff70f2008-06-11 00:58:25 +0000245 void testException(const std::string &arg)
T Jake Lucianib5e62212009-01-31 22:36:20 +0000246 throw(Xception, apache::thrift::TException)
David Reiss55ff70f2008-06-11 00:58:25 +0000247 {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000248 printf("testException(%s)\n", arg.c_str());
Mark Sleed3d733a2006-09-01 22:19:06 +0000249 if (arg.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000250 Xception e;
251 e.errorCode = 1001;
David Reiss9813cbe2009-01-31 21:39:11 +0000252 e.message = arg;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000253 throw e;
David Reiss55ff70f2008-06-11 00:58:25 +0000254 } else if (arg.compare("ApplicationException") == 0) {
T Jake Lucianib5e62212009-01-31 22:36:20 +0000255 apache::thrift::TException e;
David Reiss55ff70f2008-06-11 00:58:25 +0000256 throw e;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000257 } else {
258 Xtruct result;
259 result.string_thing = arg;
260 return;
261 }
262 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000263
Mark Slee1921d202007-01-24 19:43:06 +0000264 void testMultiException(Xtruct &result, const std::string &arg0, const std::string &arg1) throw(Xception, Xception2) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000265
Marc Slemko71d4e472006-08-15 22:34:04 +0000266 printf("testMultiException(%s, %s)\n", arg0.c_str(), arg1.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000267
Mark Sleef5f2be42006-09-05 21:05:31 +0000268 if (arg0.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000269 Xception e;
270 e.errorCode = 1001;
271 e.message = "This is an Xception";
272 throw e;
Mark Sleef5f2be42006-09-05 21:05:31 +0000273 } else if (arg0.compare("Xception2") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000274 Xception2 e;
275 e.errorCode = 2002;
276 e.struct_thing.string_thing = "This is an Xception2";
277 throw e;
278 } else {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000279 result.string_thing = arg1;
Mark Slee1921d202007-01-24 19:43:06 +0000280 return;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000281 }
282 }
David Reiss2ab6fe82008-02-18 02:11:44 +0000283
David Reiss6ce401d2009-03-24 20:01:58 +0000284 void testOneway(int sleepFor) {
285 printf("testOneway(%d): Sleeping...\n", sleepFor);
David Reiss2ab6fe82008-02-18 02:11:44 +0000286 sleep(sleepFor);
David Reiss6ce401d2009-03-24 20:01:58 +0000287 printf("testOneway(%d): done sleeping!\n", sleepFor);
David Reiss2ab6fe82008-02-18 02:11:44 +0000288 }
Mark Sleee8540632006-05-30 09:24:40 +0000289};
290
David Reissd7192062010-10-06 17:09:33 +0000291
292class TestProcessorEventHandler : public TProcessorEventHandler {
David Reiss23248712010-10-06 17:10:08 +0000293 virtual void* getContext(const char* fn_name, void* serverContext) {
David Reissd7192062010-10-06 17:09:33 +0000294 return new std::string(fn_name);
295 }
296 virtual void freeContext(void* ctx, const char* fn_name) {
297 delete static_cast<std::string*>(ctx);
298 }
299 virtual void preRead(void* ctx, const char* fn_name) {
300 communicate("preRead", ctx, fn_name);
301 }
David Reissef7200f2010-10-06 17:09:42 +0000302 virtual void postRead(void* ctx, const char* fn_name, uint32_t bytes) {
David Reissd7192062010-10-06 17:09:33 +0000303 communicate("postRead", ctx, fn_name);
304 }
305 virtual void preWrite(void* ctx, const char* fn_name) {
306 communicate("preWrite", ctx, fn_name);
307 }
David Reissef7200f2010-10-06 17:09:42 +0000308 virtual void postWrite(void* ctx, const char* fn_name, uint32_t bytes) {
David Reissd7192062010-10-06 17:09:33 +0000309 communicate("postWrite", ctx, fn_name);
310 }
311 virtual void asyncComplete(void* ctx, const char* fn_name) {
312 communicate("asyncComplete", ctx, fn_name);
313 }
314 virtual void handlerError(void* ctx, const char* fn_name) {
315 communicate("handlerError", ctx, fn_name);
316 }
317
318 void communicate(const char* event, void* ctx, const char* fn_name) {
319 std::cout << event << ": " << *static_cast<std::string*>(ctx) << " = " << fn_name << std::endl;
320 }
321};
322
323
Mark Sleee8540632006-05-30 09:24:40 +0000324int main(int argc, char **argv) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000325
Mark Sleee8540632006-05-30 09:24:40 +0000326 int port = 9090;
Marc Slemko6be374b2006-08-04 03:16:25 +0000327 string serverType = "simple";
328 string protocolType = "binary";
329 size_t workerCount = 4;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000330 bool ssl = false;
Marc Slemko6be374b2006-08-04 03:16:25 +0000331
332 ostringstream usage;
333
334 usage <<
David Reissd7192062010-10-06 17:09:33 +0000335 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 +0000336
Mark Slee0c2dff32007-03-09 19:26:57 +0000337 "\t\tserver-type\t\ttype of server, \"simple\", \"thread-pool\", \"threaded\", or \"nonblocking\". Default is " << serverType << endl <<
Marc Slemko6be374b2006-08-04 03:16:25 +0000338
339 "\t\tprotocol-type\t\ttype of protocol, \"binary\", \"ascii\", or \"xml\". Default is " << protocolType << endl <<
340
341 "\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 +0000342
Marc Slemko6be374b2006-08-04 03:16:25 +0000343 map<string, string> args;
David Reiss0c90f6f2008-02-06 22:18:40 +0000344
Mark Sleef5f2be42006-09-05 21:05:31 +0000345 for (int ix = 1; ix < argc; ix++) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000346 string arg(argv[ix]);
Mark Sleef5f2be42006-09-05 21:05:31 +0000347 if (arg.compare(0,2, "--") == 0) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000348 size_t end = arg.find_first_of("=", 2);
Mark Sleef5f2be42006-09-05 21:05:31 +0000349 if (end != string::npos) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000350 args[string(arg, 2, end - 2)] = string(arg, end + 1);
351 } else {
Mark Sleea3302652006-10-25 19:03:32 +0000352 args[string(arg, 2)] = "true";
Marc Slemko6be374b2006-08-04 03:16:25 +0000353 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000354 } else {
355 throw invalid_argument("Unexcepted command line token: "+arg);
356 }
Mark Sleee8540632006-05-30 09:24:40 +0000357 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000358
359 try {
360
Mark Sleef5f2be42006-09-05 21:05:31 +0000361 if (!args["port"].empty()) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000362 port = atoi(args["port"].c_str());
363 }
364
Mark Sleef5f2be42006-09-05 21:05:31 +0000365 if (!args["server-type"].empty()) {
David Reiss0c90f6f2008-02-06 22:18:40 +0000366 serverType = args["server-type"];
Mark Sleef5f2be42006-09-05 21:05:31 +0000367 if (serverType == "simple") {
368 } else if (serverType == "thread-pool") {
Mark Slee0c2dff32007-03-09 19:26:57 +0000369 } else if (serverType == "threaded") {
Mark Sleeb9acf982006-10-10 01:57:32 +0000370 } else if (serverType == "nonblocking") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000371 } else {
Marc Slemko6be374b2006-08-04 03:16:25 +0000372 throw invalid_argument("Unknown server type "+serverType);
373 }
374 }
375
Mark Sleef5f2be42006-09-05 21:05:31 +0000376 if (!args["protocol-type"].empty()) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000377 protocolType = args["protocol-type"];
Mark Sleef5f2be42006-09-05 21:05:31 +0000378 if (protocolType == "binary") {
379 } else if (protocolType == "ascii") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000380 throw invalid_argument("ASCII protocol not supported");
Mark Sleef5f2be42006-09-05 21:05:31 +0000381 } else if (protocolType == "xml") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000382 throw invalid_argument("XML protocol not supported");
383 } else {
384 throw invalid_argument("Unknown protocol type "+protocolType);
385 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000386 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000387
Mark Sleef5f2be42006-09-05 21:05:31 +0000388 if (!args["workers"].empty()) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000389 workerCount = atoi(args["workers"].c_str());
390 }
Bryan Duxbury833ae492010-09-27 17:26:02 +0000391 } catch (std::exception& e) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000392 cerr << e.what() << endl;
393 cerr << usage;
394 }
395
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000396 if (args["ssl"] == "true") {
397 ssl = true;
398 signal(SIGPIPE, SIG_IGN);
399 }
400
Mark Sleee8540632006-05-30 09:24:40 +0000401 // Dispatcher
David Reissb7762a02010-10-06 17:10:00 +0000402 shared_ptr<TProtocolFactory> protocolFactory(
403 new TBinaryProtocolFactoryT<TBufferBase>());
Marc Slemko6be374b2006-08-04 03:16:25 +0000404
Mark Sleed2655522006-09-05 22:09:57 +0000405 shared_ptr<TestHandler> testHandler(new TestHandler());
406
David Reissb7762a02010-10-06 17:10:00 +0000407 shared_ptr<TProcessor> testProcessor(
408 new ThriftTestProcessorT< TBinaryProtocolT<TBufferBase> >(testHandler));
Mark Sleee8540632006-05-30 09:24:40 +0000409
David Reissd7192062010-10-06 17:09:33 +0000410
411 if (!args["processor-events"].empty()) {
412 testProcessor->setEventHandler(shared_ptr<TProcessorEventHandler>(
413 new TestProcessorEventHandler()));
414 }
415
Mark Sleee8540632006-05-30 09:24:40 +0000416 // Transport
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000417 shared_ptr<TSSLSocketFactory> sslSocketFactory;
418 shared_ptr<TServerSocket> serverSocket;
Mark Sleee8540632006-05-30 09:24:40 +0000419
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000420 if (ssl) {
421 sslSocketFactory = shared_ptr<TSSLSocketFactory>(new TSSLSocketFactory());
422 sslSocketFactory->loadCertificate("./server-certificate.pem");
423 sslSocketFactory->loadPrivateKey("./server-private-key.pem");
424 sslSocketFactory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
425 serverSocket = shared_ptr<TServerSocket>(new TSSLServerSocket(port, sslSocketFactory));
426 } else {
427 serverSocket = shared_ptr<TServerSocket>(new TServerSocket(port));
428 }
Mark Sleed788b2e2006-09-07 01:26:35 +0000429 // Factory
430 shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
431
Mark Sleed3d733a2006-09-01 22:19:06 +0000432 if (serverType == "simple") {
Mark Sleee8540632006-05-30 09:24:40 +0000433
Marc Slemko6be374b2006-08-04 03:16:25 +0000434 // Server
Mark Slee018b6992006-09-07 21:31:12 +0000435 TSimpleServer simpleServer(testProcessor,
Mark Sleed788b2e2006-09-07 01:26:35 +0000436 serverSocket,
437 transportFactory,
Mark Sleea3302652006-10-25 19:03:32 +0000438 protocolFactory);
Marc Slemko6be374b2006-08-04 03:16:25 +0000439
440 printf("Starting the server on port %d...\n", port);
Mark Slee794993d2006-09-20 01:56:10 +0000441 simpleServer.serve();
Marc Slemko6be374b2006-08-04 03:16:25 +0000442
Mark Sleed3d733a2006-09-01 22:19:06 +0000443 } else if (serverType == "thread-pool") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000444
Mark Sleef5f2be42006-09-05 21:05:31 +0000445 shared_ptr<ThreadManager> threadManager =
446 ThreadManager::newSimpleThreadManager(workerCount);
Marc Slemko6be374b2006-08-04 03:16:25 +0000447
Mark Sleef5f2be42006-09-05 21:05:31 +0000448 shared_ptr<PosixThreadFactory> threadFactory =
449 shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
Marc Slemko6be374b2006-08-04 03:16:25 +0000450
451 threadManager->threadFactory(threadFactory);
452
453 threadManager->start();
454
Mark Slee018b6992006-09-07 21:31:12 +0000455 TThreadPoolServer threadPoolServer(testProcessor,
Marc Slemko6be374b2006-08-04 03:16:25 +0000456 serverSocket,
Mark Sleed788b2e2006-09-07 01:26:35 +0000457 transportFactory,
Mark Sleea3302652006-10-25 19:03:32 +0000458 protocolFactory,
459 threadManager);
Marc Slemko6be374b2006-08-04 03:16:25 +0000460
461 printf("Starting the server on port %d...\n", port);
Mark Slee794993d2006-09-20 01:56:10 +0000462 threadPoolServer.serve();
Mark Sleeb9acf982006-10-10 01:57:32 +0000463
Mark Slee0c2dff32007-03-09 19:26:57 +0000464 } else if (serverType == "threaded") {
David Reiss0c90f6f2008-02-06 22:18:40 +0000465
Mark Slee0c2dff32007-03-09 19:26:57 +0000466 TThreadedServer threadedServer(testProcessor,
467 serverSocket,
468 transportFactory,
469 protocolFactory);
470
471 printf("Starting the server on port %d...\n", port);
472 threadedServer.serve();
473
Mark Sleeb9acf982006-10-10 01:57:32 +0000474 } else if (serverType == "nonblocking") {
Mark Sleea3302652006-10-25 19:03:32 +0000475 TNonblockingServer nonblockingServer(testProcessor, port);
Mark Sleeb9acf982006-10-10 01:57:32 +0000476 printf("Starting the nonblocking server on port %d...\n", port);
477 nonblockingServer.serve();
Marc Slemko6be374b2006-08-04 03:16:25 +0000478 }
479
Mark Sleee8540632006-05-30 09:24:40 +0000480 printf("done.\n");
481 return 0;
482}