blob: bb3cd434527a4aefdd2ce66b82754dcd0952a63c [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>
Roger Meierca142b02011-06-07 17:59:07 +000023#include <protocol/TJSONProtocol.h>
Marc Slemko6be374b2006-08-04 03:16:25 +000024#include <server/TSimpleServer.h>
Mark Slee0c2dff32007-03-09 19:26:57 +000025#include <server/TThreadedServer.h>
Marc Slemko6be374b2006-08-04 03:16:25 +000026#include <server/TThreadPoolServer.h>
Mark Sleeb9acf982006-10-10 01:57:32 +000027#include <server/TNonblockingServer.h>
Marc Slemko6be374b2006-08-04 03:16:25 +000028#include <transport/TServerSocket.h>
Roger Meierbb09f442011-05-31 20:35:37 +000029#include <transport/TSSLServerSocket.h>
30#include <transport/TSSLSocket.h>
Roger Meierca142b02011-06-07 17:59:07 +000031#include <transport/THttpServer.h>
32#include <transport/THttpTransport.h>
Mark Sleea3302652006-10-25 19:03:32 +000033#include <transport/TTransportUtils.h>
Mark Slee95771002006-06-07 06:53:25 +000034#include "ThriftTest.h"
Marc Slemko6be374b2006-08-04 03:16:25 +000035
36#include <iostream>
37#include <stdexcept>
38#include <sstream>
39
Roger Meierca142b02011-06-07 17:59:07 +000040#include <boost/program_options.hpp>
41
David Reissbc3dddb2007-08-22 23:20:24 +000042#define __STDC_FORMAT_MACROS
43#include <inttypes.h>
Bryan Duxburycd9aea12011-02-22 18:12:06 +000044#include <signal.h>
David Reissbc3dddb2007-08-22 23:20:24 +000045
Mark Sleee8540632006-05-30 09:24:40 +000046using namespace std;
Mark Slee0c2dff32007-03-09 19:26:57 +000047using namespace boost;
Mark Sleee8540632006-05-30 09:24:40 +000048
T Jake Lucianib5e62212009-01-31 22:36:20 +000049using namespace apache::thrift;
50using namespace apache::thrift::concurrency;
51using namespace apache::thrift::protocol;
52using namespace apache::thrift::transport;
53using namespace apache::thrift::server;
Marc Slemko6be374b2006-08-04 03:16:25 +000054
Marc Slemkobf4fd192006-08-15 21:29:39 +000055using namespace thrift::test;
56
Mark Sleed2655522006-09-05 22:09:57 +000057class TestHandler : public ThriftTestIf {
Mark Sleee8540632006-05-30 09:24:40 +000058 public:
Mark Sleed2655522006-09-05 22:09:57 +000059 TestHandler() {}
Mark Sleee8540632006-05-30 09:24:40 +000060
61 void testVoid() {
62 printf("testVoid()\n");
63 }
64
Mark Slee1921d202007-01-24 19:43:06 +000065 void testString(string& out, const string &thing) {
Mark Sleee8540632006-05-30 09:24:40 +000066 printf("testString(\"%s\")\n", thing.c_str());
Mark Slee1921d202007-01-24 19:43:06 +000067 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +000068 }
69
Mark Slee1921d202007-01-24 19:43:06 +000070 int8_t testByte(const int8_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +000071 printf("testByte(%d)\n", (int)thing);
72 return thing;
73 }
74
Mark Slee1921d202007-01-24 19:43:06 +000075 int32_t testI32(const int32_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +000076 printf("testI32(%d)\n", thing);
77 return thing;
78 }
79
Mark Slee1921d202007-01-24 19:43:06 +000080 int64_t testI64(const int64_t thing) {
David Reissbc3dddb2007-08-22 23:20:24 +000081 printf("testI64(%"PRId64")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +000082 return thing;
83 }
84
Mark Slee1921d202007-01-24 19:43:06 +000085 double testDouble(const double thing) {
Mark Sleec98d0502006-09-06 02:42:25 +000086 printf("testDouble(%lf)\n", thing);
87 return thing;
88 }
89
Mark Slee1921d202007-01-24 19:43:06 +000090 void testStruct(Xtruct& out, const Xtruct &thing) {
David Reissbc3dddb2007-08-22 23:20:24 +000091 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 +000092 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +000093 }
94
Mark Slee1921d202007-01-24 19:43:06 +000095 void testNest(Xtruct2& out, const Xtruct2& nest) {
96 const Xtruct &thing = nest.struct_thing;
David Reissbc3dddb2007-08-22 23:20:24 +000097 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 +000098 out = nest;
Mark Sleee8540632006-05-30 09:24:40 +000099 }
100
Mark Slee1921d202007-01-24 19:43:06 +0000101 void testMap(map<int32_t, int32_t> &out, const map<int32_t, int32_t> &thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000102 printf("testMap({");
103 map<int32_t, int32_t>::const_iterator m_iter;
104 bool first = true;
105 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
106 if (first) {
107 first = false;
108 } else {
109 printf(", ");
110 }
111 printf("%d => %d", m_iter->first, m_iter->second);
112 }
113 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000114 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000115 }
116
Mark Slee1921d202007-01-24 19:43:06 +0000117 void testSet(set<int32_t> &out, const set<int32_t> &thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000118 printf("testSet({");
119 set<int32_t>::const_iterator s_iter;
120 bool first = true;
121 for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) {
122 if (first) {
123 first = false;
124 } else {
125 printf(", ");
126 }
127 printf("%d", *s_iter);
128 }
129 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000130 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000131 }
132
Mark Slee1921d202007-01-24 19:43:06 +0000133 void testList(vector<int32_t> &out, const vector<int32_t> &thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000134 printf("testList({");
Mark Sleeb9acf982006-10-10 01:57:32 +0000135 vector<int32_t>::const_iterator l_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000136 bool first = true;
137 for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) {
138 if (first) {
139 first = false;
140 } else {
141 printf(", ");
142 }
143 printf("%d", *l_iter);
144 }
145 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000146 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000147 }
148
Bryan Duxbury833ae492010-09-27 17:26:02 +0000149 Numberz::type testEnum(const Numberz::type thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000150 printf("testEnum(%d)\n", thing);
151 return thing;
152 }
153
Mark Slee1921d202007-01-24 19:43:06 +0000154 UserId testTypedef(const UserId thing) {
David Reissbc3dddb2007-08-22 23:20:24 +0000155 printf("testTypedef(%"PRId64")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +0000156 return thing;
157 }
158
Mark Slee1921d202007-01-24 19:43:06 +0000159 void testMapMap(map<int32_t, map<int32_t,int32_t> > &mapmap, const int32_t hello) {
Mark Sleee8540632006-05-30 09:24:40 +0000160 printf("testMapMap(%d)\n", hello);
Mark Sleee8540632006-05-30 09:24:40 +0000161
162 map<int32_t,int32_t> pos;
163 map<int32_t,int32_t> neg;
164 for (int i = 1; i < 5; i++) {
165 pos.insert(make_pair(i,i));
166 neg.insert(make_pair(-i,-i));
167 }
168
169 mapmap.insert(make_pair(4, pos));
170 mapmap.insert(make_pair(-4, neg));
171
Mark Sleee8540632006-05-30 09:24:40 +0000172 }
173
Bryan Duxbury833ae492010-09-27 17:26:02 +0000174 void testInsanity(map<UserId, map<Numberz::type,Insanity> > &insane, const Insanity &argument) {
Mark Sleee8540632006-05-30 09:24:40 +0000175 printf("testInsanity()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000176
Mark Sleee8540632006-05-30 09:24:40 +0000177 Xtruct hello;
178 hello.string_thing = "Hello2";
179 hello.byte_thing = 2;
Mark Sleee8540632006-05-30 09:24:40 +0000180 hello.i32_thing = 2;
Mark Sleee8540632006-05-30 09:24:40 +0000181 hello.i64_thing = 2;
182
183 Xtruct goodbye;
184 goodbye.string_thing = "Goodbye4";
185 goodbye.byte_thing = 4;
Mark Sleee8540632006-05-30 09:24:40 +0000186 goodbye.i32_thing = 4;
Mark Sleee8540632006-05-30 09:24:40 +0000187 goodbye.i64_thing = 4;
188
189 Insanity crazy;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000190 crazy.userMap.insert(make_pair(Numberz::EIGHT, 8));
Mark Sleee8540632006-05-30 09:24:40 +0000191 crazy.xtructs.push_back(goodbye);
192
193 Insanity looney;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000194 crazy.userMap.insert(make_pair(Numberz::FIVE, 5));
Mark Sleee8540632006-05-30 09:24:40 +0000195 crazy.xtructs.push_back(hello);
196
Bryan Duxbury833ae492010-09-27 17:26:02 +0000197 map<Numberz::type, Insanity> first_map;
198 map<Numberz::type, Insanity> second_map;
Mark Sleee8540632006-05-30 09:24:40 +0000199
Bryan Duxbury833ae492010-09-27 17:26:02 +0000200 first_map.insert(make_pair(Numberz::TWO, crazy));
201 first_map.insert(make_pair(Numberz::THREE, crazy));
Mark Sleee8540632006-05-30 09:24:40 +0000202
Bryan Duxbury833ae492010-09-27 17:26:02 +0000203 second_map.insert(make_pair(Numberz::SIX, looney));
Mark Sleee8540632006-05-30 09:24:40 +0000204
Mark Sleee8540632006-05-30 09:24:40 +0000205 insane.insert(make_pair(1, first_map));
206 insane.insert(make_pair(2, second_map));
207
208 printf("return");
209 printf(" = {");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000210 map<UserId, map<Numberz::type,Insanity> >::const_iterator i_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000211 for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) {
David Reissbc3dddb2007-08-22 23:20:24 +0000212 printf("%"PRId64" => {", i_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000213 map<Numberz::type,Insanity>::const_iterator i2_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000214 for (i2_iter = i_iter->second.begin();
215 i2_iter != i_iter->second.end();
216 ++i2_iter) {
217 printf("%d => {", i2_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000218 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
219 map<Numberz::type, UserId>::const_iterator um;
Mark Sleee8540632006-05-30 09:24:40 +0000220 printf("{");
221 for (um = userMap.begin(); um != userMap.end(); ++um) {
David Reissbc3dddb2007-08-22 23:20:24 +0000222 printf("%d => %"PRId64", ", um->first, um->second);
Mark Sleee8540632006-05-30 09:24:40 +0000223 }
224 printf("}, ");
225
Mark Sleeb9acf982006-10-10 01:57:32 +0000226 vector<Xtruct> xtructs = i2_iter->second.xtructs;
227 vector<Xtruct>::const_iterator x;
Mark Sleee8540632006-05-30 09:24:40 +0000228 printf("{");
229 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
David Reissbc3dddb2007-08-22 23:20:24 +0000230 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 +0000231 }
232 printf("}");
233
234 printf("}, ");
235 }
236 printf("}, ");
237 }
238 printf("}\n");
239
David Reiss0c90f6f2008-02-06 22:18:40 +0000240
Mark Sleee8540632006-05-30 09:24:40 +0000241 }
Marc Slemkoe6889de2006-08-12 00:32:53 +0000242
Bryan Duxbury833ae492010-09-27 17:26:02 +0000243 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 +0000244 printf("testMulti()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000245
Marc Slemkoe6889de2006-08-12 00:32:53 +0000246 hello.string_thing = "Hello2";
247 hello.byte_thing = arg0;
248 hello.i32_thing = arg1;
249 hello.i64_thing = (int64_t)arg2;
Marc Slemkoe6889de2006-08-12 00:32:53 +0000250 }
Marc Slemkobf4fd192006-08-15 21:29:39 +0000251
David Reiss55ff70f2008-06-11 00:58:25 +0000252 void testException(const std::string &arg)
T Jake Lucianib5e62212009-01-31 22:36:20 +0000253 throw(Xception, apache::thrift::TException)
David Reiss55ff70f2008-06-11 00:58:25 +0000254 {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000255 printf("testException(%s)\n", arg.c_str());
Mark Sleed3d733a2006-09-01 22:19:06 +0000256 if (arg.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000257 Xception e;
258 e.errorCode = 1001;
David Reiss9813cbe2009-01-31 21:39:11 +0000259 e.message = arg;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000260 throw e;
David Reiss55ff70f2008-06-11 00:58:25 +0000261 } else if (arg.compare("ApplicationException") == 0) {
T Jake Lucianib5e62212009-01-31 22:36:20 +0000262 apache::thrift::TException e;
David Reiss55ff70f2008-06-11 00:58:25 +0000263 throw e;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000264 } else {
265 Xtruct result;
266 result.string_thing = arg;
267 return;
268 }
269 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000270
Mark Slee1921d202007-01-24 19:43:06 +0000271 void testMultiException(Xtruct &result, const std::string &arg0, const std::string &arg1) throw(Xception, Xception2) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000272
Marc Slemko71d4e472006-08-15 22:34:04 +0000273 printf("testMultiException(%s, %s)\n", arg0.c_str(), arg1.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000274
Mark Sleef5f2be42006-09-05 21:05:31 +0000275 if (arg0.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000276 Xception e;
277 e.errorCode = 1001;
278 e.message = "This is an Xception";
279 throw e;
Mark Sleef5f2be42006-09-05 21:05:31 +0000280 } else if (arg0.compare("Xception2") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000281 Xception2 e;
282 e.errorCode = 2002;
283 e.struct_thing.string_thing = "This is an Xception2";
284 throw e;
285 } else {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000286 result.string_thing = arg1;
Mark Slee1921d202007-01-24 19:43:06 +0000287 return;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000288 }
289 }
David Reiss2ab6fe82008-02-18 02:11:44 +0000290
David Reiss6ce401d2009-03-24 20:01:58 +0000291 void testOneway(int sleepFor) {
292 printf("testOneway(%d): Sleeping...\n", sleepFor);
David Reiss2ab6fe82008-02-18 02:11:44 +0000293 sleep(sleepFor);
David Reiss6ce401d2009-03-24 20:01:58 +0000294 printf("testOneway(%d): done sleeping!\n", sleepFor);
David Reiss2ab6fe82008-02-18 02:11:44 +0000295 }
Mark Sleee8540632006-05-30 09:24:40 +0000296};
297
David Reissd7192062010-10-06 17:09:33 +0000298
299class TestProcessorEventHandler : public TProcessorEventHandler {
David Reiss23248712010-10-06 17:10:08 +0000300 virtual void* getContext(const char* fn_name, void* serverContext) {
David Reissd7192062010-10-06 17:09:33 +0000301 return new std::string(fn_name);
302 }
303 virtual void freeContext(void* ctx, const char* fn_name) {
304 delete static_cast<std::string*>(ctx);
305 }
306 virtual void preRead(void* ctx, const char* fn_name) {
307 communicate("preRead", ctx, fn_name);
308 }
David Reissef7200f2010-10-06 17:09:42 +0000309 virtual void postRead(void* ctx, const char* fn_name, uint32_t bytes) {
David Reissd7192062010-10-06 17:09:33 +0000310 communicate("postRead", ctx, fn_name);
311 }
312 virtual void preWrite(void* ctx, const char* fn_name) {
313 communicate("preWrite", ctx, fn_name);
314 }
David Reissef7200f2010-10-06 17:09:42 +0000315 virtual void postWrite(void* ctx, const char* fn_name, uint32_t bytes) {
David Reissd7192062010-10-06 17:09:33 +0000316 communicate("postWrite", ctx, fn_name);
317 }
318 virtual void asyncComplete(void* ctx, const char* fn_name) {
319 communicate("asyncComplete", ctx, fn_name);
320 }
321 virtual void handlerError(void* ctx, const char* fn_name) {
322 communicate("handlerError", ctx, fn_name);
323 }
324
325 void communicate(const char* event, void* ctx, const char* fn_name) {
326 std::cout << event << ": " << *static_cast<std::string*>(ctx) << " = " << fn_name << std::endl;
327 }
328};
329
330
Mark Sleee8540632006-05-30 09:24:40 +0000331int main(int argc, char **argv) {
332 int port = 9090;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000333 bool ssl = false;
Roger Meierca142b02011-06-07 17:59:07 +0000334 string transport_type = "buffered";
335 string protocol_type = "binary";
336 string server_type = "simple";
337 string domain_socket = "";
338 size_t workers = 4;
Marc Slemko6be374b2006-08-04 03:16:25 +0000339
Roger Meierca142b02011-06-07 17:59:07 +0000340
341 program_options::options_description desc("Allowed options");
342 desc.add_options()
343 ("help,h", "produce help message")
344 ("port", program_options::value<int>(&port)->default_value(port), "Port number to listen")
345 ("domain-socket", program_options::value<string>(&domain_socket)->default_value(domain_socket),
346 "Unix Domain Socket (e.g. /tmp/ThriftTest.thrift)")
347 ("server-type", program_options::value<string>(&server_type)->default_value(server_type),
348 "type of server, \"simple\", \"thread-pool\", \"threaded\", or \"nonblocking\"")
349 ("transport", program_options::value<string>(&transport_type)->default_value(transport_type),
350 "transport: buffered, framed, http")
351 ("protocol", program_options::value<string>(&protocol_type)->default_value(protocol_type),
352 "protocol: binary, json")
353 ("ssl", "Encrypted Transport using SSL")
354 ("processor-events", "processor-events")
355 ("workers,n", program_options::value<size_t>(&workers)->default_value(workers),
356 "Number of thread pools workers. Only valid for thread-pool server type")
357 ;
Marc Slemko6be374b2006-08-04 03:16:25 +0000358
Roger Meierca142b02011-06-07 17:59:07 +0000359 program_options::variables_map vm;
360 program_options::store(program_options::parse_command_line(argc, argv, desc), vm);
361 program_options::notify(vm);
Marc Slemko6be374b2006-08-04 03:16:25 +0000362
Roger Meierca142b02011-06-07 17:59:07 +0000363 if (vm.count("help")) {
364 cout << desc << "\n";
365 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000366 }
Roger Meierca142b02011-06-07 17:59:07 +0000367
Marc Slemko6be374b2006-08-04 03:16:25 +0000368 try {
Roger Meierca142b02011-06-07 17:59:07 +0000369 if (!server_type.empty()) {
370 if (server_type == "simple") {
371 } else if (server_type == "thread-pool") {
372 } else if (server_type == "threaded") {
373 } else if (server_type == "nonblocking") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000374 } else {
Roger Meierca142b02011-06-07 17:59:07 +0000375 throw invalid_argument("Unknown server type "+server_type);
376 }
377 }
378
379 if (!protocol_type.empty()) {
380 if (protocol_type == "binary") {
381 } else if (protocol_type == "json") {
382 } else {
383 throw invalid_argument("Unknown protocol type "+protocol_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000384 }
385 }
386
Roger Meierca142b02011-06-07 17:59:07 +0000387 if (!transport_type.empty()) {
388 if (transport_type == "buffered") {
389 } else if (transport_type == "framed") {
390 } else if (transport_type == "http") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000391 } else {
Roger Meierca142b02011-06-07 17:59:07 +0000392 throw invalid_argument("Unknown transport type "+transport_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000393 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000394 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000395
Bryan Duxbury833ae492010-09-27 17:26:02 +0000396 } catch (std::exception& e) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000397 cerr << e.what() << endl;
Roger Meierca142b02011-06-07 17:59:07 +0000398 cout << desc << "\n";
399 return 1;
Marc Slemko6be374b2006-08-04 03:16:25 +0000400 }
401
Roger Meierca142b02011-06-07 17:59:07 +0000402 if (vm.count("ssl")) {
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000403 ssl = true;
404 signal(SIGPIPE, SIG_IGN);
405 }
406
Mark Sleee8540632006-05-30 09:24:40 +0000407 // Dispatcher
Roger Meierca142b02011-06-07 17:59:07 +0000408 shared_ptr<TProtocolFactory> protocolFactory;
409 if (protocol_type == "json") {
410 shared_ptr<TProtocolFactory> jsonProtocolFactory(new TJSONProtocolFactory());
411 protocolFactory = jsonProtocolFactory;
412 } else {
413 shared_ptr<TProtocolFactory> binaryProtocolFactory(new TBinaryProtocolFactoryT<TBufferBase>());
414 protocolFactory = binaryProtocolFactory;
415 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000416
Roger Meierca142b02011-06-07 17:59:07 +0000417 // Processor
Mark Sleed2655522006-09-05 22:09:57 +0000418 shared_ptr<TestHandler> testHandler(new TestHandler());
Roger Meierca142b02011-06-07 17:59:07 +0000419 shared_ptr<ThriftTestProcessor> testProcessor(new ThriftTestProcessor(testHandler));
420
421 if (vm.count("processor-events")) {
David Reissd7192062010-10-06 17:09:33 +0000422 testProcessor->setEventHandler(shared_ptr<TProcessorEventHandler>(
423 new TestProcessorEventHandler()));
424 }
Roger Meierca142b02011-06-07 17:59:07 +0000425
Mark Sleee8540632006-05-30 09:24:40 +0000426 // Transport
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000427 shared_ptr<TSSLSocketFactory> sslSocketFactory;
428 shared_ptr<TServerSocket> serverSocket;
Mark Sleee8540632006-05-30 09:24:40 +0000429
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000430 if (ssl) {
431 sslSocketFactory = shared_ptr<TSSLSocketFactory>(new TSSLSocketFactory());
432 sslSocketFactory->loadCertificate("./server-certificate.pem");
433 sslSocketFactory->loadPrivateKey("./server-private-key.pem");
434 sslSocketFactory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
435 serverSocket = shared_ptr<TServerSocket>(new TSSLServerSocket(port, sslSocketFactory));
436 } else {
Roger Meierca142b02011-06-07 17:59:07 +0000437 if (domain_socket != "") {
438 unlink(domain_socket.c_str());
439 serverSocket = shared_ptr<TServerSocket>(new TServerSocket(domain_socket));
440 port = 0;
441 }
442 else {
443 serverSocket = shared_ptr<TServerSocket>(new TServerSocket(port));
444 }
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000445 }
Roger Meierca142b02011-06-07 17:59:07 +0000446
Mark Sleed788b2e2006-09-07 01:26:35 +0000447 // Factory
Roger Meierca142b02011-06-07 17:59:07 +0000448 shared_ptr<TTransportFactory> transportFactory;
449
450 if (transport_type == "http") {
451 shared_ptr<TTransportFactory> httpTransportFactory(new THttpServerTransportFactory());
452 transportFactory = httpTransportFactory;
453 } else if (transport_type == "framed") {
454 shared_ptr<TTransportFactory> framedTransportFactory(new TFramedTransportFactory());
455 transportFactory = framedTransportFactory;
456 } else {
457 shared_ptr<TTransportFactory> bufferedTransportFactory(new TBufferedTransportFactory());
458 transportFactory = bufferedTransportFactory;
459 }
Mark Sleed788b2e2006-09-07 01:26:35 +0000460
Roger Meierca142b02011-06-07 17:59:07 +0000461 // Server Info
462 cout << "Starting \"" << server_type << "\" server ("
463 << transport_type << "/" << protocol_type << ") listen on: " << domain_socket;
464 if (port != 0) {
465 cout << port;
466 }
467 cout << endl;
Mark Sleee8540632006-05-30 09:24:40 +0000468
Roger Meierca142b02011-06-07 17:59:07 +0000469 // Server
470 if (server_type == "simple") {
Mark Slee018b6992006-09-07 21:31:12 +0000471 TSimpleServer simpleServer(testProcessor,
Roger Meierca142b02011-06-07 17:59:07 +0000472 serverSocket,
Mark Sleed788b2e2006-09-07 01:26:35 +0000473 transportFactory,
Mark Sleea3302652006-10-25 19:03:32 +0000474 protocolFactory);
Marc Slemko6be374b2006-08-04 03:16:25 +0000475
Mark Slee794993d2006-09-20 01:56:10 +0000476 simpleServer.serve();
Marc Slemko6be374b2006-08-04 03:16:25 +0000477
Roger Meierca142b02011-06-07 17:59:07 +0000478 } else if (server_type == "thread-pool") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000479
Mark Sleef5f2be42006-09-05 21:05:31 +0000480 shared_ptr<ThreadManager> threadManager =
Roger Meierca142b02011-06-07 17:59:07 +0000481 ThreadManager::newSimpleThreadManager(workers);
Marc Slemko6be374b2006-08-04 03:16:25 +0000482
Mark Sleef5f2be42006-09-05 21:05:31 +0000483 shared_ptr<PosixThreadFactory> threadFactory =
484 shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
Marc Slemko6be374b2006-08-04 03:16:25 +0000485
486 threadManager->threadFactory(threadFactory);
487
488 threadManager->start();
489
Mark Slee018b6992006-09-07 21:31:12 +0000490 TThreadPoolServer threadPoolServer(testProcessor,
Roger Meierca142b02011-06-07 17:59:07 +0000491 serverSocket,
Mark Sleed788b2e2006-09-07 01:26:35 +0000492 transportFactory,
Mark Sleea3302652006-10-25 19:03:32 +0000493 protocolFactory,
Roger Meierca142b02011-06-07 17:59:07 +0000494 threadManager);
Marc Slemko6be374b2006-08-04 03:16:25 +0000495
Mark Slee794993d2006-09-20 01:56:10 +0000496 threadPoolServer.serve();
Mark Sleeb9acf982006-10-10 01:57:32 +0000497
Roger Meierca142b02011-06-07 17:59:07 +0000498 } else if (server_type == "threaded") {
David Reiss0c90f6f2008-02-06 22:18:40 +0000499
Mark Slee0c2dff32007-03-09 19:26:57 +0000500 TThreadedServer threadedServer(testProcessor,
501 serverSocket,
502 transportFactory,
503 protocolFactory);
504
Mark Slee0c2dff32007-03-09 19:26:57 +0000505 threadedServer.serve();
506
Roger Meierca142b02011-06-07 17:59:07 +0000507 } else if (server_type == "nonblocking") {
Mark Sleea3302652006-10-25 19:03:32 +0000508 TNonblockingServer nonblockingServer(testProcessor, port);
Mark Sleeb9acf982006-10-10 01:57:32 +0000509 nonblockingServer.serve();
Marc Slemko6be374b2006-08-04 03:16:25 +0000510 }
511
Roger Meierca142b02011-06-07 17:59:07 +0000512 cout << "done." << endl;
Mark Sleee8540632006-05-30 09:24:40 +0000513 return 0;
514}