blob: 9ba05098a4481c719c7658020f8a4a034e686439 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
Roger Meierd3b9dca2011-06-24 14:01:10 +000020#define __STDC_FORMAT_MACROS
21#include <inttypes.h>
22
Marc Slemko6be374b2006-08-04 03:16:25 +000023#include <concurrency/ThreadManager.h>
24#include <concurrency/PosixThreadFactory.h>
25#include <protocol/TBinaryProtocol.h>
Roger Meierca142b02011-06-07 17:59:07 +000026#include <protocol/TJSONProtocol.h>
Marc Slemko6be374b2006-08-04 03:16:25 +000027#include <server/TSimpleServer.h>
Mark Slee0c2dff32007-03-09 19:26:57 +000028#include <server/TThreadedServer.h>
Marc Slemko6be374b2006-08-04 03:16:25 +000029#include <server/TThreadPoolServer.h>
Mark Sleeb9acf982006-10-10 01:57:32 +000030#include <server/TNonblockingServer.h>
Marc Slemko6be374b2006-08-04 03:16:25 +000031#include <transport/TServerSocket.h>
Roger Meierbb09f442011-05-31 20:35:37 +000032#include <transport/TSSLServerSocket.h>
33#include <transport/TSSLSocket.h>
Roger Meierca142b02011-06-07 17:59:07 +000034#include <transport/THttpServer.h>
35#include <transport/THttpTransport.h>
Mark Sleea3302652006-10-25 19:03:32 +000036#include <transport/TTransportUtils.h>
Mark Slee95771002006-06-07 06:53:25 +000037#include "ThriftTest.h"
Marc Slemko6be374b2006-08-04 03:16:25 +000038
39#include <iostream>
40#include <stdexcept>
41#include <sstream>
42
Roger Meierca142b02011-06-07 17:59:07 +000043#include <boost/program_options.hpp>
44
Bryan Duxburycd9aea12011-02-22 18:12:06 +000045#include <signal.h>
David Reissbc3dddb2007-08-22 23:20:24 +000046
Mark Sleee8540632006-05-30 09:24:40 +000047using namespace std;
Mark Slee0c2dff32007-03-09 19:26:57 +000048using namespace boost;
Mark Sleee8540632006-05-30 09:24:40 +000049
T Jake Lucianib5e62212009-01-31 22:36:20 +000050using namespace apache::thrift;
51using namespace apache::thrift::concurrency;
52using namespace apache::thrift::protocol;
53using namespace apache::thrift::transport;
54using namespace apache::thrift::server;
Marc Slemko6be374b2006-08-04 03:16:25 +000055
Marc Slemkobf4fd192006-08-15 21:29:39 +000056using namespace thrift::test;
57
Mark Sleed2655522006-09-05 22:09:57 +000058class TestHandler : public ThriftTestIf {
Mark Sleee8540632006-05-30 09:24:40 +000059 public:
Mark Sleed2655522006-09-05 22:09:57 +000060 TestHandler() {}
Mark Sleee8540632006-05-30 09:24:40 +000061
62 void testVoid() {
63 printf("testVoid()\n");
64 }
65
Mark Slee1921d202007-01-24 19:43:06 +000066 void testString(string& out, const string &thing) {
Mark Sleee8540632006-05-30 09:24:40 +000067 printf("testString(\"%s\")\n", thing.c_str());
Mark Slee1921d202007-01-24 19:43:06 +000068 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +000069 }
70
Mark Slee1921d202007-01-24 19:43:06 +000071 int8_t testByte(const int8_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +000072 printf("testByte(%d)\n", (int)thing);
73 return thing;
74 }
75
Mark Slee1921d202007-01-24 19:43:06 +000076 int32_t testI32(const int32_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +000077 printf("testI32(%d)\n", thing);
78 return thing;
79 }
80
Mark Slee1921d202007-01-24 19:43:06 +000081 int64_t testI64(const int64_t thing) {
David Reissbc3dddb2007-08-22 23:20:24 +000082 printf("testI64(%"PRId64")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +000083 return thing;
84 }
85
Mark Slee1921d202007-01-24 19:43:06 +000086 double testDouble(const double thing) {
Mark Sleec98d0502006-09-06 02:42:25 +000087 printf("testDouble(%lf)\n", thing);
88 return thing;
89 }
90
Mark Slee1921d202007-01-24 19:43:06 +000091 void testStruct(Xtruct& out, const Xtruct &thing) {
David Reissbc3dddb2007-08-22 23:20:24 +000092 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 +000093 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +000094 }
95
Mark Slee1921d202007-01-24 19:43:06 +000096 void testNest(Xtruct2& out, const Xtruct2& nest) {
97 const Xtruct &thing = nest.struct_thing;
David Reissbc3dddb2007-08-22 23:20:24 +000098 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 +000099 out = nest;
Mark Sleee8540632006-05-30 09:24:40 +0000100 }
101
Mark Slee1921d202007-01-24 19:43:06 +0000102 void testMap(map<int32_t, int32_t> &out, const map<int32_t, int32_t> &thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000103 printf("testMap({");
104 map<int32_t, int32_t>::const_iterator m_iter;
105 bool first = true;
106 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
107 if (first) {
108 first = false;
109 } else {
110 printf(", ");
111 }
112 printf("%d => %d", m_iter->first, m_iter->second);
113 }
114 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000115 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000116 }
117
Roger Meierd3b9dca2011-06-24 14:01:10 +0000118 void testStringMap(map<std::string, std::string> &out, const map<std::string, std::string> &thing) {
119 printf("testMap({");
120 map<std::string, std::string>::const_iterator m_iter;
121 bool first = true;
122 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
123 if (first) {
124 first = false;
125 } else {
126 printf(", ");
127 }
128 printf("%s => %s", (m_iter->first).c_str(), (m_iter->second).c_str());
129 }
130 printf("})\n");
131 out = thing;
132 }
133
Mark Slee1921d202007-01-24 19:43:06 +0000134 void testSet(set<int32_t> &out, const set<int32_t> &thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000135 printf("testSet({");
136 set<int32_t>::const_iterator s_iter;
137 bool first = true;
138 for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) {
139 if (first) {
140 first = false;
141 } else {
142 printf(", ");
143 }
144 printf("%d", *s_iter);
145 }
146 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000147 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000148 }
149
Mark Slee1921d202007-01-24 19:43:06 +0000150 void testList(vector<int32_t> &out, const vector<int32_t> &thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000151 printf("testList({");
Mark Sleeb9acf982006-10-10 01:57:32 +0000152 vector<int32_t>::const_iterator l_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000153 bool first = true;
154 for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) {
155 if (first) {
156 first = false;
157 } else {
158 printf(", ");
159 }
160 printf("%d", *l_iter);
161 }
162 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000163 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000164 }
165
Bryan Duxbury833ae492010-09-27 17:26:02 +0000166 Numberz::type testEnum(const Numberz::type thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000167 printf("testEnum(%d)\n", thing);
168 return thing;
169 }
170
Mark Slee1921d202007-01-24 19:43:06 +0000171 UserId testTypedef(const UserId thing) {
David Reissbc3dddb2007-08-22 23:20:24 +0000172 printf("testTypedef(%"PRId64")\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +0000173 return thing;
174 }
175
Mark Slee1921d202007-01-24 19:43:06 +0000176 void testMapMap(map<int32_t, map<int32_t,int32_t> > &mapmap, const int32_t hello) {
Mark Sleee8540632006-05-30 09:24:40 +0000177 printf("testMapMap(%d)\n", hello);
Mark Sleee8540632006-05-30 09:24:40 +0000178
179 map<int32_t,int32_t> pos;
180 map<int32_t,int32_t> neg;
181 for (int i = 1; i < 5; i++) {
182 pos.insert(make_pair(i,i));
183 neg.insert(make_pair(-i,-i));
184 }
185
186 mapmap.insert(make_pair(4, pos));
187 mapmap.insert(make_pair(-4, neg));
188
Mark Sleee8540632006-05-30 09:24:40 +0000189 }
190
Bryan Duxbury833ae492010-09-27 17:26:02 +0000191 void testInsanity(map<UserId, map<Numberz::type,Insanity> > &insane, const Insanity &argument) {
Mark Sleee8540632006-05-30 09:24:40 +0000192 printf("testInsanity()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000193
Mark Sleee8540632006-05-30 09:24:40 +0000194 Xtruct hello;
195 hello.string_thing = "Hello2";
196 hello.byte_thing = 2;
Mark Sleee8540632006-05-30 09:24:40 +0000197 hello.i32_thing = 2;
Mark Sleee8540632006-05-30 09:24:40 +0000198 hello.i64_thing = 2;
199
200 Xtruct goodbye;
201 goodbye.string_thing = "Goodbye4";
202 goodbye.byte_thing = 4;
Mark Sleee8540632006-05-30 09:24:40 +0000203 goodbye.i32_thing = 4;
Mark Sleee8540632006-05-30 09:24:40 +0000204 goodbye.i64_thing = 4;
205
206 Insanity crazy;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000207 crazy.userMap.insert(make_pair(Numberz::EIGHT, 8));
Mark Sleee8540632006-05-30 09:24:40 +0000208 crazy.xtructs.push_back(goodbye);
209
210 Insanity looney;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000211 crazy.userMap.insert(make_pair(Numberz::FIVE, 5));
Mark Sleee8540632006-05-30 09:24:40 +0000212 crazy.xtructs.push_back(hello);
213
Bryan Duxbury833ae492010-09-27 17:26:02 +0000214 map<Numberz::type, Insanity> first_map;
215 map<Numberz::type, Insanity> second_map;
Mark Sleee8540632006-05-30 09:24:40 +0000216
Bryan Duxbury833ae492010-09-27 17:26:02 +0000217 first_map.insert(make_pair(Numberz::TWO, crazy));
218 first_map.insert(make_pair(Numberz::THREE, crazy));
Mark Sleee8540632006-05-30 09:24:40 +0000219
Bryan Duxbury833ae492010-09-27 17:26:02 +0000220 second_map.insert(make_pair(Numberz::SIX, looney));
Mark Sleee8540632006-05-30 09:24:40 +0000221
Mark Sleee8540632006-05-30 09:24:40 +0000222 insane.insert(make_pair(1, first_map));
223 insane.insert(make_pair(2, second_map));
224
225 printf("return");
226 printf(" = {");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000227 map<UserId, map<Numberz::type,Insanity> >::const_iterator i_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000228 for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) {
David Reissbc3dddb2007-08-22 23:20:24 +0000229 printf("%"PRId64" => {", i_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000230 map<Numberz::type,Insanity>::const_iterator i2_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000231 for (i2_iter = i_iter->second.begin();
232 i2_iter != i_iter->second.end();
233 ++i2_iter) {
234 printf("%d => {", i2_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000235 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
236 map<Numberz::type, UserId>::const_iterator um;
Mark Sleee8540632006-05-30 09:24:40 +0000237 printf("{");
238 for (um = userMap.begin(); um != userMap.end(); ++um) {
David Reissbc3dddb2007-08-22 23:20:24 +0000239 printf("%d => %"PRId64", ", um->first, um->second);
Mark Sleee8540632006-05-30 09:24:40 +0000240 }
241 printf("}, ");
242
Mark Sleeb9acf982006-10-10 01:57:32 +0000243 vector<Xtruct> xtructs = i2_iter->second.xtructs;
244 vector<Xtruct>::const_iterator x;
Mark Sleee8540632006-05-30 09:24:40 +0000245 printf("{");
246 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
David Reissbc3dddb2007-08-22 23:20:24 +0000247 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 +0000248 }
249 printf("}");
250
251 printf("}, ");
252 }
253 printf("}, ");
254 }
255 printf("}\n");
256
David Reiss0c90f6f2008-02-06 22:18:40 +0000257
Mark Sleee8540632006-05-30 09:24:40 +0000258 }
Marc Slemkoe6889de2006-08-12 00:32:53 +0000259
Bryan Duxbury833ae492010-09-27 17:26:02 +0000260 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 +0000261 printf("testMulti()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000262
Marc Slemkoe6889de2006-08-12 00:32:53 +0000263 hello.string_thing = "Hello2";
264 hello.byte_thing = arg0;
265 hello.i32_thing = arg1;
266 hello.i64_thing = (int64_t)arg2;
Marc Slemkoe6889de2006-08-12 00:32:53 +0000267 }
Marc Slemkobf4fd192006-08-15 21:29:39 +0000268
David Reiss55ff70f2008-06-11 00:58:25 +0000269 void testException(const std::string &arg)
T Jake Lucianib5e62212009-01-31 22:36:20 +0000270 throw(Xception, apache::thrift::TException)
David Reiss55ff70f2008-06-11 00:58:25 +0000271 {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000272 printf("testException(%s)\n", arg.c_str());
Mark Sleed3d733a2006-09-01 22:19:06 +0000273 if (arg.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000274 Xception e;
275 e.errorCode = 1001;
David Reiss9813cbe2009-01-31 21:39:11 +0000276 e.message = arg;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000277 throw e;
David Reiss55ff70f2008-06-11 00:58:25 +0000278 } else if (arg.compare("ApplicationException") == 0) {
T Jake Lucianib5e62212009-01-31 22:36:20 +0000279 apache::thrift::TException e;
David Reiss55ff70f2008-06-11 00:58:25 +0000280 throw e;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000281 } else {
282 Xtruct result;
283 result.string_thing = arg;
284 return;
285 }
286 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000287
Mark Slee1921d202007-01-24 19:43:06 +0000288 void testMultiException(Xtruct &result, const std::string &arg0, const std::string &arg1) throw(Xception, Xception2) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000289
Marc Slemko71d4e472006-08-15 22:34:04 +0000290 printf("testMultiException(%s, %s)\n", arg0.c_str(), arg1.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000291
Mark Sleef5f2be42006-09-05 21:05:31 +0000292 if (arg0.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000293 Xception e;
294 e.errorCode = 1001;
295 e.message = "This is an Xception";
296 throw e;
Mark Sleef5f2be42006-09-05 21:05:31 +0000297 } else if (arg0.compare("Xception2") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000298 Xception2 e;
299 e.errorCode = 2002;
300 e.struct_thing.string_thing = "This is an Xception2";
301 throw e;
302 } else {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000303 result.string_thing = arg1;
Mark Slee1921d202007-01-24 19:43:06 +0000304 return;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000305 }
306 }
David Reiss2ab6fe82008-02-18 02:11:44 +0000307
David Reiss6ce401d2009-03-24 20:01:58 +0000308 void testOneway(int sleepFor) {
309 printf("testOneway(%d): Sleeping...\n", sleepFor);
David Reiss2ab6fe82008-02-18 02:11:44 +0000310 sleep(sleepFor);
David Reiss6ce401d2009-03-24 20:01:58 +0000311 printf("testOneway(%d): done sleeping!\n", sleepFor);
David Reiss2ab6fe82008-02-18 02:11:44 +0000312 }
Mark Sleee8540632006-05-30 09:24:40 +0000313};
314
David Reissd7192062010-10-06 17:09:33 +0000315
316class TestProcessorEventHandler : public TProcessorEventHandler {
David Reiss23248712010-10-06 17:10:08 +0000317 virtual void* getContext(const char* fn_name, void* serverContext) {
David Reissd7192062010-10-06 17:09:33 +0000318 return new std::string(fn_name);
319 }
320 virtual void freeContext(void* ctx, const char* fn_name) {
321 delete static_cast<std::string*>(ctx);
322 }
323 virtual void preRead(void* ctx, const char* fn_name) {
324 communicate("preRead", ctx, fn_name);
325 }
David Reissef7200f2010-10-06 17:09:42 +0000326 virtual void postRead(void* ctx, const char* fn_name, uint32_t bytes) {
David Reissd7192062010-10-06 17:09:33 +0000327 communicate("postRead", ctx, fn_name);
328 }
329 virtual void preWrite(void* ctx, const char* fn_name) {
330 communicate("preWrite", ctx, fn_name);
331 }
David Reissef7200f2010-10-06 17:09:42 +0000332 virtual void postWrite(void* ctx, const char* fn_name, uint32_t bytes) {
David Reissd7192062010-10-06 17:09:33 +0000333 communicate("postWrite", ctx, fn_name);
334 }
335 virtual void asyncComplete(void* ctx, const char* fn_name) {
336 communicate("asyncComplete", ctx, fn_name);
337 }
338 virtual void handlerError(void* ctx, const char* fn_name) {
339 communicate("handlerError", ctx, fn_name);
340 }
341
342 void communicate(const char* event, void* ctx, const char* fn_name) {
343 std::cout << event << ": " << *static_cast<std::string*>(ctx) << " = " << fn_name << std::endl;
344 }
345};
346
347
Mark Sleee8540632006-05-30 09:24:40 +0000348int main(int argc, char **argv) {
349 int port = 9090;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000350 bool ssl = false;
Roger Meierca142b02011-06-07 17:59:07 +0000351 string transport_type = "buffered";
352 string protocol_type = "binary";
353 string server_type = "simple";
354 string domain_socket = "";
355 size_t workers = 4;
Marc Slemko6be374b2006-08-04 03:16:25 +0000356
Roger Meierca142b02011-06-07 17:59:07 +0000357
358 program_options::options_description desc("Allowed options");
359 desc.add_options()
360 ("help,h", "produce help message")
361 ("port", program_options::value<int>(&port)->default_value(port), "Port number to listen")
362 ("domain-socket", program_options::value<string>(&domain_socket)->default_value(domain_socket),
363 "Unix Domain Socket (e.g. /tmp/ThriftTest.thrift)")
364 ("server-type", program_options::value<string>(&server_type)->default_value(server_type),
365 "type of server, \"simple\", \"thread-pool\", \"threaded\", or \"nonblocking\"")
366 ("transport", program_options::value<string>(&transport_type)->default_value(transport_type),
367 "transport: buffered, framed, http")
368 ("protocol", program_options::value<string>(&protocol_type)->default_value(protocol_type),
369 "protocol: binary, json")
370 ("ssl", "Encrypted Transport using SSL")
371 ("processor-events", "processor-events")
372 ("workers,n", program_options::value<size_t>(&workers)->default_value(workers),
373 "Number of thread pools workers. Only valid for thread-pool server type")
374 ;
Marc Slemko6be374b2006-08-04 03:16:25 +0000375
Roger Meierca142b02011-06-07 17:59:07 +0000376 program_options::variables_map vm;
377 program_options::store(program_options::parse_command_line(argc, argv, desc), vm);
378 program_options::notify(vm);
Marc Slemko6be374b2006-08-04 03:16:25 +0000379
Roger Meierca142b02011-06-07 17:59:07 +0000380 if (vm.count("help")) {
381 cout << desc << "\n";
382 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000383 }
Roger Meierca142b02011-06-07 17:59:07 +0000384
Marc Slemko6be374b2006-08-04 03:16:25 +0000385 try {
Roger Meierca142b02011-06-07 17:59:07 +0000386 if (!server_type.empty()) {
387 if (server_type == "simple") {
388 } else if (server_type == "thread-pool") {
389 } else if (server_type == "threaded") {
390 } else if (server_type == "nonblocking") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000391 } else {
Roger Meierca142b02011-06-07 17:59:07 +0000392 throw invalid_argument("Unknown server type "+server_type);
393 }
394 }
395
396 if (!protocol_type.empty()) {
397 if (protocol_type == "binary") {
398 } else if (protocol_type == "json") {
399 } else {
400 throw invalid_argument("Unknown protocol type "+protocol_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000401 }
402 }
403
Roger Meierca142b02011-06-07 17:59:07 +0000404 if (!transport_type.empty()) {
405 if (transport_type == "buffered") {
406 } else if (transport_type == "framed") {
407 } else if (transport_type == "http") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000408 } else {
Roger Meierca142b02011-06-07 17:59:07 +0000409 throw invalid_argument("Unknown transport type "+transport_type);
Marc Slemko6be374b2006-08-04 03:16:25 +0000410 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000411 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000412
Bryan Duxbury833ae492010-09-27 17:26:02 +0000413 } catch (std::exception& e) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000414 cerr << e.what() << endl;
Roger Meierca142b02011-06-07 17:59:07 +0000415 cout << desc << "\n";
416 return 1;
Marc Slemko6be374b2006-08-04 03:16:25 +0000417 }
418
Roger Meierca142b02011-06-07 17:59:07 +0000419 if (vm.count("ssl")) {
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000420 ssl = true;
421 signal(SIGPIPE, SIG_IGN);
422 }
423
Mark Sleee8540632006-05-30 09:24:40 +0000424 // Dispatcher
Roger Meierca142b02011-06-07 17:59:07 +0000425 shared_ptr<TProtocolFactory> protocolFactory;
426 if (protocol_type == "json") {
427 shared_ptr<TProtocolFactory> jsonProtocolFactory(new TJSONProtocolFactory());
428 protocolFactory = jsonProtocolFactory;
429 } else {
430 shared_ptr<TProtocolFactory> binaryProtocolFactory(new TBinaryProtocolFactoryT<TBufferBase>());
431 protocolFactory = binaryProtocolFactory;
432 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000433
Roger Meierca142b02011-06-07 17:59:07 +0000434 // Processor
Mark Sleed2655522006-09-05 22:09:57 +0000435 shared_ptr<TestHandler> testHandler(new TestHandler());
Roger Meierca142b02011-06-07 17:59:07 +0000436 shared_ptr<ThriftTestProcessor> testProcessor(new ThriftTestProcessor(testHandler));
437
438 if (vm.count("processor-events")) {
David Reissd7192062010-10-06 17:09:33 +0000439 testProcessor->setEventHandler(shared_ptr<TProcessorEventHandler>(
440 new TestProcessorEventHandler()));
441 }
Roger Meierca142b02011-06-07 17:59:07 +0000442
Mark Sleee8540632006-05-30 09:24:40 +0000443 // Transport
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000444 shared_ptr<TSSLSocketFactory> sslSocketFactory;
445 shared_ptr<TServerSocket> serverSocket;
Mark Sleee8540632006-05-30 09:24:40 +0000446
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000447 if (ssl) {
448 sslSocketFactory = shared_ptr<TSSLSocketFactory>(new TSSLSocketFactory());
449 sslSocketFactory->loadCertificate("./server-certificate.pem");
450 sslSocketFactory->loadPrivateKey("./server-private-key.pem");
451 sslSocketFactory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
452 serverSocket = shared_ptr<TServerSocket>(new TSSLServerSocket(port, sslSocketFactory));
453 } else {
Roger Meierca142b02011-06-07 17:59:07 +0000454 if (domain_socket != "") {
455 unlink(domain_socket.c_str());
456 serverSocket = shared_ptr<TServerSocket>(new TServerSocket(domain_socket));
457 port = 0;
458 }
459 else {
460 serverSocket = shared_ptr<TServerSocket>(new TServerSocket(port));
461 }
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000462 }
Roger Meierca142b02011-06-07 17:59:07 +0000463
Mark Sleed788b2e2006-09-07 01:26:35 +0000464 // Factory
Roger Meierca142b02011-06-07 17:59:07 +0000465 shared_ptr<TTransportFactory> transportFactory;
466
467 if (transport_type == "http") {
468 shared_ptr<TTransportFactory> httpTransportFactory(new THttpServerTransportFactory());
469 transportFactory = httpTransportFactory;
470 } else if (transport_type == "framed") {
471 shared_ptr<TTransportFactory> framedTransportFactory(new TFramedTransportFactory());
472 transportFactory = framedTransportFactory;
473 } else {
474 shared_ptr<TTransportFactory> bufferedTransportFactory(new TBufferedTransportFactory());
475 transportFactory = bufferedTransportFactory;
476 }
Mark Sleed788b2e2006-09-07 01:26:35 +0000477
Roger Meierca142b02011-06-07 17:59:07 +0000478 // Server Info
479 cout << "Starting \"" << server_type << "\" server ("
480 << transport_type << "/" << protocol_type << ") listen on: " << domain_socket;
481 if (port != 0) {
482 cout << port;
483 }
484 cout << endl;
Mark Sleee8540632006-05-30 09:24:40 +0000485
Roger Meierca142b02011-06-07 17:59:07 +0000486 // Server
487 if (server_type == "simple") {
Mark Slee018b6992006-09-07 21:31:12 +0000488 TSimpleServer simpleServer(testProcessor,
Roger Meierca142b02011-06-07 17:59:07 +0000489 serverSocket,
Mark Sleed788b2e2006-09-07 01:26:35 +0000490 transportFactory,
Mark Sleea3302652006-10-25 19:03:32 +0000491 protocolFactory);
Marc Slemko6be374b2006-08-04 03:16:25 +0000492
Mark Slee794993d2006-09-20 01:56:10 +0000493 simpleServer.serve();
Marc Slemko6be374b2006-08-04 03:16:25 +0000494
Roger Meierca142b02011-06-07 17:59:07 +0000495 } else if (server_type == "thread-pool") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000496
Mark Sleef5f2be42006-09-05 21:05:31 +0000497 shared_ptr<ThreadManager> threadManager =
Roger Meierca142b02011-06-07 17:59:07 +0000498 ThreadManager::newSimpleThreadManager(workers);
Marc Slemko6be374b2006-08-04 03:16:25 +0000499
Mark Sleef5f2be42006-09-05 21:05:31 +0000500 shared_ptr<PosixThreadFactory> threadFactory =
501 shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
Marc Slemko6be374b2006-08-04 03:16:25 +0000502
503 threadManager->threadFactory(threadFactory);
504
505 threadManager->start();
506
Mark Slee018b6992006-09-07 21:31:12 +0000507 TThreadPoolServer threadPoolServer(testProcessor,
Roger Meierca142b02011-06-07 17:59:07 +0000508 serverSocket,
Mark Sleed788b2e2006-09-07 01:26:35 +0000509 transportFactory,
Mark Sleea3302652006-10-25 19:03:32 +0000510 protocolFactory,
Roger Meierca142b02011-06-07 17:59:07 +0000511 threadManager);
Marc Slemko6be374b2006-08-04 03:16:25 +0000512
Mark Slee794993d2006-09-20 01:56:10 +0000513 threadPoolServer.serve();
Mark Sleeb9acf982006-10-10 01:57:32 +0000514
Roger Meierca142b02011-06-07 17:59:07 +0000515 } else if (server_type == "threaded") {
David Reiss0c90f6f2008-02-06 22:18:40 +0000516
Mark Slee0c2dff32007-03-09 19:26:57 +0000517 TThreadedServer threadedServer(testProcessor,
518 serverSocket,
519 transportFactory,
520 protocolFactory);
521
Mark Slee0c2dff32007-03-09 19:26:57 +0000522 threadedServer.serve();
523
Roger Meierca142b02011-06-07 17:59:07 +0000524 } else if (server_type == "nonblocking") {
Mark Sleea3302652006-10-25 19:03:32 +0000525 TNonblockingServer nonblockingServer(testProcessor, port);
Mark Sleeb9acf982006-10-10 01:57:32 +0000526 nonblockingServer.serve();
Marc Slemko6be374b2006-08-04 03:16:25 +0000527 }
528
Roger Meierca142b02011-06-07 17:59:07 +0000529 cout << "done." << endl;
Mark Sleee8540632006-05-30 09:24:40 +0000530 return 0;
531}