blob: 840164706d3c5445cf1e1cc6e565c90b9d1a66ba [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>
Roger Meierbb09f442011-05-31 20:35:37 +000028#include <transport/TSSLServerSocket.h>
29#include <transport/TSSLSocket.h>
Mark Sleea3302652006-10-25 19:03:32 +000030#include <transport/TTransportUtils.h>
Mark Slee95771002006-06-07 06:53:25 +000031#include "ThriftTest.h"
Marc Slemko6be374b2006-08-04 03:16:25 +000032
33#include <iostream>
34#include <stdexcept>
35#include <sstream>
36
David Reissbc3dddb2007-08-22 23:20:24 +000037#define __STDC_FORMAT_MACROS
38#include <inttypes.h>
Bryan Duxburycd9aea12011-02-22 18:12:06 +000039#include <signal.h>
David Reissbc3dddb2007-08-22 23:20:24 +000040
Mark Sleee8540632006-05-30 09:24:40 +000041using namespace std;
Mark Slee0c2dff32007-03-09 19:26:57 +000042using namespace boost;
Mark Sleee8540632006-05-30 09:24:40 +000043
T Jake Lucianib5e62212009-01-31 22:36:20 +000044using namespace apache::thrift;
45using namespace apache::thrift::concurrency;
46using namespace apache::thrift::protocol;
47using namespace apache::thrift::transport;
48using namespace apache::thrift::server;
Marc Slemko6be374b2006-08-04 03:16:25 +000049
Marc Slemkobf4fd192006-08-15 21:29:39 +000050using namespace thrift::test;
51
Mark Sleed2655522006-09-05 22:09:57 +000052class TestHandler : public ThriftTestIf {
Mark Sleee8540632006-05-30 09:24:40 +000053 public:
Mark Sleed2655522006-09-05 22:09:57 +000054 TestHandler() {}
Mark Sleee8540632006-05-30 09:24:40 +000055
56 void testVoid() {
57 printf("testVoid()\n");
58 }
59
Mark Slee1921d202007-01-24 19:43:06 +000060 void testString(string& out, const string &thing) {
Mark Sleee8540632006-05-30 09:24:40 +000061 printf("testString(\"%s\")\n", thing.c_str());
Mark Slee1921d202007-01-24 19:43:06 +000062 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +000063 }
64
Mark Slee1921d202007-01-24 19:43:06 +000065 int8_t testByte(const int8_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +000066 printf("testByte(%d)\n", (int)thing);
67 return thing;
68 }
69
Mark Slee1921d202007-01-24 19:43:06 +000070 int32_t testI32(const int32_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +000071 printf("testI32(%d)\n", thing);
72 return thing;
73 }
74
Mark Slee1921d202007-01-24 19:43:06 +000075 int64_t testI64(const int64_t thing) {
David Reissbc3dddb2007-08-22 23:20:24 +000076 printf("testI64(%"PRId64")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +000077 return thing;
78 }
79
Mark Slee1921d202007-01-24 19:43:06 +000080 double testDouble(const double thing) {
Mark Sleec98d0502006-09-06 02:42:25 +000081 printf("testDouble(%lf)\n", thing);
82 return thing;
83 }
84
Mark Slee1921d202007-01-24 19:43:06 +000085 void testStruct(Xtruct& out, const Xtruct &thing) {
David Reissbc3dddb2007-08-22 23:20:24 +000086 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 +000087 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +000088 }
89
Mark Slee1921d202007-01-24 19:43:06 +000090 void testNest(Xtruct2& out, const Xtruct2& nest) {
91 const Xtruct &thing = nest.struct_thing;
David Reissbc3dddb2007-08-22 23:20:24 +000092 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 +000093 out = nest;
Mark Sleee8540632006-05-30 09:24:40 +000094 }
95
Mark Slee1921d202007-01-24 19:43:06 +000096 void testMap(map<int32_t, int32_t> &out, const map<int32_t, int32_t> &thing) {
Mark Sleee8540632006-05-30 09:24:40 +000097 printf("testMap({");
98 map<int32_t, int32_t>::const_iterator m_iter;
99 bool first = true;
100 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
101 if (first) {
102 first = false;
103 } else {
104 printf(", ");
105 }
106 printf("%d => %d", m_iter->first, m_iter->second);
107 }
108 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000109 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000110 }
111
Mark Slee1921d202007-01-24 19:43:06 +0000112 void testSet(set<int32_t> &out, const set<int32_t> &thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000113 printf("testSet({");
114 set<int32_t>::const_iterator s_iter;
115 bool first = true;
116 for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) {
117 if (first) {
118 first = false;
119 } else {
120 printf(", ");
121 }
122 printf("%d", *s_iter);
123 }
124 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000125 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000126 }
127
Mark Slee1921d202007-01-24 19:43:06 +0000128 void testList(vector<int32_t> &out, const vector<int32_t> &thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000129 printf("testList({");
Mark Sleeb9acf982006-10-10 01:57:32 +0000130 vector<int32_t>::const_iterator l_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000131 bool first = true;
132 for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) {
133 if (first) {
134 first = false;
135 } else {
136 printf(", ");
137 }
138 printf("%d", *l_iter);
139 }
140 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000141 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000142 }
143
Bryan Duxbury833ae492010-09-27 17:26:02 +0000144 Numberz::type testEnum(const Numberz::type thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000145 printf("testEnum(%d)\n", thing);
146 return thing;
147 }
148
Mark Slee1921d202007-01-24 19:43:06 +0000149 UserId testTypedef(const UserId thing) {
David Reissbc3dddb2007-08-22 23:20:24 +0000150 printf("testTypedef(%"PRId64")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +0000151 return thing;
152 }
153
Mark Slee1921d202007-01-24 19:43:06 +0000154 void testMapMap(map<int32_t, map<int32_t,int32_t> > &mapmap, const int32_t hello) {
Mark Sleee8540632006-05-30 09:24:40 +0000155 printf("testMapMap(%d)\n", hello);
Mark Sleee8540632006-05-30 09:24:40 +0000156
157 map<int32_t,int32_t> pos;
158 map<int32_t,int32_t> neg;
159 for (int i = 1; i < 5; i++) {
160 pos.insert(make_pair(i,i));
161 neg.insert(make_pair(-i,-i));
162 }
163
164 mapmap.insert(make_pair(4, pos));
165 mapmap.insert(make_pair(-4, neg));
166
Mark Sleee8540632006-05-30 09:24:40 +0000167 }
168
Bryan Duxbury833ae492010-09-27 17:26:02 +0000169 void testInsanity(map<UserId, map<Numberz::type,Insanity> > &insane, const Insanity &argument) {
Mark Sleee8540632006-05-30 09:24:40 +0000170 printf("testInsanity()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000171
Mark Sleee8540632006-05-30 09:24:40 +0000172 Xtruct hello;
173 hello.string_thing = "Hello2";
174 hello.byte_thing = 2;
Mark Sleee8540632006-05-30 09:24:40 +0000175 hello.i32_thing = 2;
Mark Sleee8540632006-05-30 09:24:40 +0000176 hello.i64_thing = 2;
177
178 Xtruct goodbye;
179 goodbye.string_thing = "Goodbye4";
180 goodbye.byte_thing = 4;
Mark Sleee8540632006-05-30 09:24:40 +0000181 goodbye.i32_thing = 4;
Mark Sleee8540632006-05-30 09:24:40 +0000182 goodbye.i64_thing = 4;
183
184 Insanity crazy;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000185 crazy.userMap.insert(make_pair(Numberz::EIGHT, 8));
Mark Sleee8540632006-05-30 09:24:40 +0000186 crazy.xtructs.push_back(goodbye);
187
188 Insanity looney;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000189 crazy.userMap.insert(make_pair(Numberz::FIVE, 5));
Mark Sleee8540632006-05-30 09:24:40 +0000190 crazy.xtructs.push_back(hello);
191
Bryan Duxbury833ae492010-09-27 17:26:02 +0000192 map<Numberz::type, Insanity> first_map;
193 map<Numberz::type, Insanity> second_map;
Mark Sleee8540632006-05-30 09:24:40 +0000194
Bryan Duxbury833ae492010-09-27 17:26:02 +0000195 first_map.insert(make_pair(Numberz::TWO, crazy));
196 first_map.insert(make_pair(Numberz::THREE, crazy));
Mark Sleee8540632006-05-30 09:24:40 +0000197
Bryan Duxbury833ae492010-09-27 17:26:02 +0000198 second_map.insert(make_pair(Numberz::SIX, looney));
Mark Sleee8540632006-05-30 09:24:40 +0000199
Mark Sleee8540632006-05-30 09:24:40 +0000200 insane.insert(make_pair(1, first_map));
201 insane.insert(make_pair(2, second_map));
202
203 printf("return");
204 printf(" = {");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000205 map<UserId, map<Numberz::type,Insanity> >::const_iterator i_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000206 for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) {
David Reissbc3dddb2007-08-22 23:20:24 +0000207 printf("%"PRId64" => {", i_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000208 map<Numberz::type,Insanity>::const_iterator i2_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000209 for (i2_iter = i_iter->second.begin();
210 i2_iter != i_iter->second.end();
211 ++i2_iter) {
212 printf("%d => {", i2_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000213 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
214 map<Numberz::type, UserId>::const_iterator um;
Mark Sleee8540632006-05-30 09:24:40 +0000215 printf("{");
216 for (um = userMap.begin(); um != userMap.end(); ++um) {
David Reissbc3dddb2007-08-22 23:20:24 +0000217 printf("%d => %"PRId64", ", um->first, um->second);
Mark Sleee8540632006-05-30 09:24:40 +0000218 }
219 printf("}, ");
220
Mark Sleeb9acf982006-10-10 01:57:32 +0000221 vector<Xtruct> xtructs = i2_iter->second.xtructs;
222 vector<Xtruct>::const_iterator x;
Mark Sleee8540632006-05-30 09:24:40 +0000223 printf("{");
224 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
David Reissbc3dddb2007-08-22 23:20:24 +0000225 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 +0000226 }
227 printf("}");
228
229 printf("}, ");
230 }
231 printf("}, ");
232 }
233 printf("}\n");
234
David Reiss0c90f6f2008-02-06 22:18:40 +0000235
Mark Sleee8540632006-05-30 09:24:40 +0000236 }
Marc Slemkoe6889de2006-08-12 00:32:53 +0000237
Bryan Duxbury833ae492010-09-27 17:26:02 +0000238 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 +0000239 printf("testMulti()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000240
Marc Slemkoe6889de2006-08-12 00:32:53 +0000241 hello.string_thing = "Hello2";
242 hello.byte_thing = arg0;
243 hello.i32_thing = arg1;
244 hello.i64_thing = (int64_t)arg2;
Marc Slemkoe6889de2006-08-12 00:32:53 +0000245 }
Marc Slemkobf4fd192006-08-15 21:29:39 +0000246
David Reiss55ff70f2008-06-11 00:58:25 +0000247 void testException(const std::string &arg)
T Jake Lucianib5e62212009-01-31 22:36:20 +0000248 throw(Xception, apache::thrift::TException)
David Reiss55ff70f2008-06-11 00:58:25 +0000249 {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000250 printf("testException(%s)\n", arg.c_str());
Mark Sleed3d733a2006-09-01 22:19:06 +0000251 if (arg.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000252 Xception e;
253 e.errorCode = 1001;
David Reiss9813cbe2009-01-31 21:39:11 +0000254 e.message = arg;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000255 throw e;
David Reiss55ff70f2008-06-11 00:58:25 +0000256 } else if (arg.compare("ApplicationException") == 0) {
T Jake Lucianib5e62212009-01-31 22:36:20 +0000257 apache::thrift::TException e;
David Reiss55ff70f2008-06-11 00:58:25 +0000258 throw e;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000259 } else {
260 Xtruct result;
261 result.string_thing = arg;
262 return;
263 }
264 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000265
Mark Slee1921d202007-01-24 19:43:06 +0000266 void testMultiException(Xtruct &result, const std::string &arg0, const std::string &arg1) throw(Xception, Xception2) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000267
Marc Slemko71d4e472006-08-15 22:34:04 +0000268 printf("testMultiException(%s, %s)\n", arg0.c_str(), arg1.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000269
Mark Sleef5f2be42006-09-05 21:05:31 +0000270 if (arg0.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000271 Xception e;
272 e.errorCode = 1001;
273 e.message = "This is an Xception";
274 throw e;
Mark Sleef5f2be42006-09-05 21:05:31 +0000275 } else if (arg0.compare("Xception2") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000276 Xception2 e;
277 e.errorCode = 2002;
278 e.struct_thing.string_thing = "This is an Xception2";
279 throw e;
280 } else {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000281 result.string_thing = arg1;
Mark Slee1921d202007-01-24 19:43:06 +0000282 return;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000283 }
284 }
David Reiss2ab6fe82008-02-18 02:11:44 +0000285
David Reiss6ce401d2009-03-24 20:01:58 +0000286 void testOneway(int sleepFor) {
287 printf("testOneway(%d): Sleeping...\n", sleepFor);
David Reiss2ab6fe82008-02-18 02:11:44 +0000288 sleep(sleepFor);
David Reiss6ce401d2009-03-24 20:01:58 +0000289 printf("testOneway(%d): done sleeping!\n", sleepFor);
David Reiss2ab6fe82008-02-18 02:11:44 +0000290 }
Mark Sleee8540632006-05-30 09:24:40 +0000291};
292
David Reissd7192062010-10-06 17:09:33 +0000293
294class TestProcessorEventHandler : public TProcessorEventHandler {
David Reiss23248712010-10-06 17:10:08 +0000295 virtual void* getContext(const char* fn_name, void* serverContext) {
David Reissd7192062010-10-06 17:09:33 +0000296 return new std::string(fn_name);
297 }
298 virtual void freeContext(void* ctx, const char* fn_name) {
299 delete static_cast<std::string*>(ctx);
300 }
301 virtual void preRead(void* ctx, const char* fn_name) {
302 communicate("preRead", ctx, fn_name);
303 }
David Reissef7200f2010-10-06 17:09:42 +0000304 virtual void postRead(void* ctx, const char* fn_name, uint32_t bytes) {
David Reissd7192062010-10-06 17:09:33 +0000305 communicate("postRead", ctx, fn_name);
306 }
307 virtual void preWrite(void* ctx, const char* fn_name) {
308 communicate("preWrite", ctx, fn_name);
309 }
David Reissef7200f2010-10-06 17:09:42 +0000310 virtual void postWrite(void* ctx, const char* fn_name, uint32_t bytes) {
David Reissd7192062010-10-06 17:09:33 +0000311 communicate("postWrite", ctx, fn_name);
312 }
313 virtual void asyncComplete(void* ctx, const char* fn_name) {
314 communicate("asyncComplete", ctx, fn_name);
315 }
316 virtual void handlerError(void* ctx, const char* fn_name) {
317 communicate("handlerError", ctx, fn_name);
318 }
319
320 void communicate(const char* event, void* ctx, const char* fn_name) {
321 std::cout << event << ": " << *static_cast<std::string*>(ctx) << " = " << fn_name << std::endl;
322 }
323};
324
325
Mark Sleee8540632006-05-30 09:24:40 +0000326int main(int argc, char **argv) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000327
Mark Sleee8540632006-05-30 09:24:40 +0000328 int port = 9090;
Marc Slemko6be374b2006-08-04 03:16:25 +0000329 string serverType = "simple";
330 string protocolType = "binary";
331 size_t workerCount = 4;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000332 bool ssl = false;
Marc Slemko6be374b2006-08-04 03:16:25 +0000333
334 ostringstream usage;
335
336 usage <<
David Reissd7192062010-10-06 17:09:33 +0000337 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 +0000338
Mark Slee0c2dff32007-03-09 19:26:57 +0000339 "\t\tserver-type\t\ttype of server, \"simple\", \"thread-pool\", \"threaded\", or \"nonblocking\". Default is " << serverType << endl <<
Marc Slemko6be374b2006-08-04 03:16:25 +0000340
341 "\t\tprotocol-type\t\ttype of protocol, \"binary\", \"ascii\", or \"xml\". Default is " << protocolType << endl <<
342
343 "\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 +0000344
Marc Slemko6be374b2006-08-04 03:16:25 +0000345 map<string, string> args;
David Reiss0c90f6f2008-02-06 22:18:40 +0000346
Mark Sleef5f2be42006-09-05 21:05:31 +0000347 for (int ix = 1; ix < argc; ix++) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000348 string arg(argv[ix]);
Mark Sleef5f2be42006-09-05 21:05:31 +0000349 if (arg.compare(0,2, "--") == 0) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000350 size_t end = arg.find_first_of("=", 2);
Mark Sleef5f2be42006-09-05 21:05:31 +0000351 if (end != string::npos) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000352 args[string(arg, 2, end - 2)] = string(arg, end + 1);
353 } else {
Mark Sleea3302652006-10-25 19:03:32 +0000354 args[string(arg, 2)] = "true";
Marc Slemko6be374b2006-08-04 03:16:25 +0000355 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000356 } else {
357 throw invalid_argument("Unexcepted command line token: "+arg);
358 }
Mark Sleee8540632006-05-30 09:24:40 +0000359 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000360
361 try {
362
Mark Sleef5f2be42006-09-05 21:05:31 +0000363 if (!args["port"].empty()) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000364 port = atoi(args["port"].c_str());
365 }
366
Mark Sleef5f2be42006-09-05 21:05:31 +0000367 if (!args["server-type"].empty()) {
David Reiss0c90f6f2008-02-06 22:18:40 +0000368 serverType = args["server-type"];
Mark Sleef5f2be42006-09-05 21:05:31 +0000369 if (serverType == "simple") {
370 } else if (serverType == "thread-pool") {
Mark Slee0c2dff32007-03-09 19:26:57 +0000371 } else if (serverType == "threaded") {
Mark Sleeb9acf982006-10-10 01:57:32 +0000372 } else if (serverType == "nonblocking") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000373 } else {
Marc Slemko6be374b2006-08-04 03:16:25 +0000374 throw invalid_argument("Unknown server type "+serverType);
375 }
376 }
377
Mark Sleef5f2be42006-09-05 21:05:31 +0000378 if (!args["protocol-type"].empty()) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000379 protocolType = args["protocol-type"];
Mark Sleef5f2be42006-09-05 21:05:31 +0000380 if (protocolType == "binary") {
381 } else if (protocolType == "ascii") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000382 throw invalid_argument("ASCII protocol not supported");
Mark Sleef5f2be42006-09-05 21:05:31 +0000383 } else if (protocolType == "xml") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000384 throw invalid_argument("XML protocol not supported");
385 } else {
386 throw invalid_argument("Unknown protocol type "+protocolType);
387 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000388 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000389
Mark Sleef5f2be42006-09-05 21:05:31 +0000390 if (!args["workers"].empty()) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000391 workerCount = atoi(args["workers"].c_str());
392 }
Bryan Duxbury833ae492010-09-27 17:26:02 +0000393 } catch (std::exception& e) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000394 cerr << e.what() << endl;
395 cerr << usage;
396 }
397
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000398 if (args["ssl"] == "true") {
399 ssl = true;
400 signal(SIGPIPE, SIG_IGN);
401 }
402
Mark Sleee8540632006-05-30 09:24:40 +0000403 // Dispatcher
David Reissb7762a02010-10-06 17:10:00 +0000404 shared_ptr<TProtocolFactory> protocolFactory(
405 new TBinaryProtocolFactoryT<TBufferBase>());
Marc Slemko6be374b2006-08-04 03:16:25 +0000406
Mark Sleed2655522006-09-05 22:09:57 +0000407 shared_ptr<TestHandler> testHandler(new TestHandler());
408
David Reissb7762a02010-10-06 17:10:00 +0000409 shared_ptr<TProcessor> testProcessor(
410 new ThriftTestProcessorT< TBinaryProtocolT<TBufferBase> >(testHandler));
Mark Sleee8540632006-05-30 09:24:40 +0000411
David Reissd7192062010-10-06 17:09:33 +0000412
413 if (!args["processor-events"].empty()) {
414 testProcessor->setEventHandler(shared_ptr<TProcessorEventHandler>(
415 new TestProcessorEventHandler()));
416 }
417
Mark Sleee8540632006-05-30 09:24:40 +0000418 // Transport
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000419 shared_ptr<TSSLSocketFactory> sslSocketFactory;
420 shared_ptr<TServerSocket> serverSocket;
Mark Sleee8540632006-05-30 09:24:40 +0000421
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000422 if (ssl) {
423 sslSocketFactory = shared_ptr<TSSLSocketFactory>(new TSSLSocketFactory());
424 sslSocketFactory->loadCertificate("./server-certificate.pem");
425 sslSocketFactory->loadPrivateKey("./server-private-key.pem");
426 sslSocketFactory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
427 serverSocket = shared_ptr<TServerSocket>(new TSSLServerSocket(port, sslSocketFactory));
428 } else {
429 serverSocket = shared_ptr<TServerSocket>(new TServerSocket(port));
430 }
Mark Sleed788b2e2006-09-07 01:26:35 +0000431 // Factory
432 shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
433
Mark Sleed3d733a2006-09-01 22:19:06 +0000434 if (serverType == "simple") {
Mark Sleee8540632006-05-30 09:24:40 +0000435
Marc Slemko6be374b2006-08-04 03:16:25 +0000436 // Server
Mark Slee018b6992006-09-07 21:31:12 +0000437 TSimpleServer simpleServer(testProcessor,
Mark Sleed788b2e2006-09-07 01:26:35 +0000438 serverSocket,
439 transportFactory,
Mark Sleea3302652006-10-25 19:03:32 +0000440 protocolFactory);
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 simpleServer.serve();
Marc Slemko6be374b2006-08-04 03:16:25 +0000444
Mark Sleed3d733a2006-09-01 22:19:06 +0000445 } else if (serverType == "thread-pool") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000446
Mark Sleef5f2be42006-09-05 21:05:31 +0000447 shared_ptr<ThreadManager> threadManager =
448 ThreadManager::newSimpleThreadManager(workerCount);
Marc Slemko6be374b2006-08-04 03:16:25 +0000449
Mark Sleef5f2be42006-09-05 21:05:31 +0000450 shared_ptr<PosixThreadFactory> threadFactory =
451 shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
Marc Slemko6be374b2006-08-04 03:16:25 +0000452
453 threadManager->threadFactory(threadFactory);
454
455 threadManager->start();
456
Mark Slee018b6992006-09-07 21:31:12 +0000457 TThreadPoolServer threadPoolServer(testProcessor,
Marc Slemko6be374b2006-08-04 03:16:25 +0000458 serverSocket,
Mark Sleed788b2e2006-09-07 01:26:35 +0000459 transportFactory,
Mark Sleea3302652006-10-25 19:03:32 +0000460 protocolFactory,
461 threadManager);
Marc Slemko6be374b2006-08-04 03:16:25 +0000462
463 printf("Starting the server on port %d...\n", port);
Mark Slee794993d2006-09-20 01:56:10 +0000464 threadPoolServer.serve();
Mark Sleeb9acf982006-10-10 01:57:32 +0000465
Mark Slee0c2dff32007-03-09 19:26:57 +0000466 } else if (serverType == "threaded") {
David Reiss0c90f6f2008-02-06 22:18:40 +0000467
Mark Slee0c2dff32007-03-09 19:26:57 +0000468 TThreadedServer threadedServer(testProcessor,
469 serverSocket,
470 transportFactory,
471 protocolFactory);
472
473 printf("Starting the server on port %d...\n", port);
474 threadedServer.serve();
475
Mark Sleeb9acf982006-10-10 01:57:32 +0000476 } else if (serverType == "nonblocking") {
Mark Sleea3302652006-10-25 19:03:32 +0000477 TNonblockingServer nonblockingServer(testProcessor, port);
Mark Sleeb9acf982006-10-10 01:57:32 +0000478 printf("Starting the nonblocking server on port %d...\n", port);
479 nonblockingServer.serve();
Marc Slemko6be374b2006-08-04 03:16:25 +0000480 }
481
Mark Sleee8540632006-05-30 09:24:40 +0000482 printf("done.\n");
483 return 0;
484}