blob: c8ecf15ce26eb60b0cbff4a99cdf21b34ef56f71 [file] [log] [blame]
Marc Slemko6be374b2006-08-04 03:16:25 +00001#include <concurrency/ThreadManager.h>
2#include <concurrency/PosixThreadFactory.h>
3#include <protocol/TBinaryProtocol.h>
4#include <server/TSimpleServer.h>
Mark Slee0c2dff32007-03-09 19:26:57 +00005#include <server/TThreadedServer.h>
Marc Slemko6be374b2006-08-04 03:16:25 +00006#include <server/TThreadPoolServer.h>
Mark Sleeb9acf982006-10-10 01:57:32 +00007#include <server/TNonblockingServer.h>
Marc Slemko6be374b2006-08-04 03:16:25 +00008#include <transport/TServerSocket.h>
Mark Sleea3302652006-10-25 19:03:32 +00009#include <transport/TTransportUtils.h>
Mark Slee95771002006-06-07 06:53:25 +000010#include "ThriftTest.h"
Marc Slemko6be374b2006-08-04 03:16:25 +000011
12#include <iostream>
13#include <stdexcept>
14#include <sstream>
15
Mark Sleee8540632006-05-30 09:24:40 +000016using namespace std;
Mark Slee0c2dff32007-03-09 19:26:57 +000017using namespace boost;
Mark Sleee8540632006-05-30 09:24:40 +000018
Marc Slemko6be374b2006-08-04 03:16:25 +000019using namespace facebook::thrift;
Mark Slee0c2dff32007-03-09 19:26:57 +000020using namespace facebook::thrift::concurrency;
Marc Slemko6be374b2006-08-04 03:16:25 +000021using namespace facebook::thrift::protocol;
22using namespace facebook::thrift::transport;
23using namespace facebook::thrift::server;
24
Marc Slemkobf4fd192006-08-15 21:29:39 +000025using namespace thrift::test;
26
Mark Sleed2655522006-09-05 22:09:57 +000027class TestHandler : public ThriftTestIf {
Mark Sleee8540632006-05-30 09:24:40 +000028 public:
Mark Sleed2655522006-09-05 22:09:57 +000029 TestHandler() {}
Mark Sleee8540632006-05-30 09:24:40 +000030
31 void testVoid() {
32 printf("testVoid()\n");
33 }
34
Mark Slee1921d202007-01-24 19:43:06 +000035 void testString(string& out, const string &thing) {
Mark Sleee8540632006-05-30 09:24:40 +000036 printf("testString(\"%s\")\n", thing.c_str());
Mark Slee1921d202007-01-24 19:43:06 +000037 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +000038 }
39
Mark Slee1921d202007-01-24 19:43:06 +000040 int8_t testByte(const int8_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +000041 printf("testByte(%d)\n", (int)thing);
42 return thing;
43 }
44
Mark Slee1921d202007-01-24 19:43:06 +000045 int32_t testI32(const int32_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +000046 printf("testI32(%d)\n", thing);
47 return thing;
48 }
49
Mark Slee1921d202007-01-24 19:43:06 +000050 int64_t testI64(const int64_t thing) {
Mark Sleed3d733a2006-09-01 22:19:06 +000051 printf("testI64(%ld)\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +000052 return thing;
53 }
54
Mark Slee1921d202007-01-24 19:43:06 +000055 double testDouble(const double thing) {
Mark Sleec98d0502006-09-06 02:42:25 +000056 printf("testDouble(%lf)\n", thing);
57 return thing;
58 }
59
Mark Slee1921d202007-01-24 19:43:06 +000060 void testStruct(Xtruct& out, const Xtruct &thing) {
Mark Sleed788b2e2006-09-07 01:26:35 +000061 printf("testStruct({\"%s\", %d, %d, %ld})\n", thing.string_thing.c_str(), (int)thing.byte_thing, thing.i32_thing, thing.i64_thing);
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 void testNest(Xtruct2& out, const Xtruct2& nest) {
66 const Xtruct &thing = nest.struct_thing;
Mark Sleed788b2e2006-09-07 01:26:35 +000067 printf("testNest({%d, {\"%s\", %d, %d, %ld}, %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 +000068 out = nest;
Mark Sleee8540632006-05-30 09:24:40 +000069 }
70
Mark Slee1921d202007-01-24 19:43:06 +000071 void testMap(map<int32_t, int32_t> &out, const map<int32_t, int32_t> &thing) {
Mark Sleee8540632006-05-30 09:24:40 +000072 printf("testMap({");
73 map<int32_t, int32_t>::const_iterator m_iter;
74 bool first = true;
75 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
76 if (first) {
77 first = false;
78 } else {
79 printf(", ");
80 }
81 printf("%d => %d", m_iter->first, m_iter->second);
82 }
83 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +000084 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +000085 }
86
Mark Slee1921d202007-01-24 19:43:06 +000087 void testSet(set<int32_t> &out, const set<int32_t> &thing) {
Mark Sleee8540632006-05-30 09:24:40 +000088 printf("testSet({");
89 set<int32_t>::const_iterator s_iter;
90 bool first = true;
91 for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) {
92 if (first) {
93 first = false;
94 } else {
95 printf(", ");
96 }
97 printf("%d", *s_iter);
98 }
99 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000100 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000101 }
102
Mark Slee1921d202007-01-24 19:43:06 +0000103 void testList(vector<int32_t> &out, const vector<int32_t> &thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000104 printf("testList({");
Mark Sleeb9acf982006-10-10 01:57:32 +0000105 vector<int32_t>::const_iterator l_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000106 bool first = true;
107 for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) {
108 if (first) {
109 first = false;
110 } else {
111 printf(", ");
112 }
113 printf("%d", *l_iter);
114 }
115 printf("})\n");
Mark Slee1921d202007-01-24 19:43:06 +0000116 out = thing;
Mark Sleee8540632006-05-30 09:24:40 +0000117 }
118
Mark Slee1921d202007-01-24 19:43:06 +0000119 Numberz testEnum(const Numberz thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000120 printf("testEnum(%d)\n", thing);
121 return thing;
122 }
123
Mark Slee1921d202007-01-24 19:43:06 +0000124 UserId testTypedef(const UserId thing) {
Mark Sleed3d733a2006-09-01 22:19:06 +0000125 printf("testTypedef(%ld)\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +0000126 return thing;
127 }
128
Mark Slee1921d202007-01-24 19:43:06 +0000129 void testMapMap(map<int32_t, map<int32_t,int32_t> > &mapmap, const int32_t hello) {
Mark Sleee8540632006-05-30 09:24:40 +0000130 printf("testMapMap(%d)\n", hello);
Mark Sleee8540632006-05-30 09:24:40 +0000131
132 map<int32_t,int32_t> pos;
133 map<int32_t,int32_t> neg;
134 for (int i = 1; i < 5; i++) {
135 pos.insert(make_pair(i,i));
136 neg.insert(make_pair(-i,-i));
137 }
138
139 mapmap.insert(make_pair(4, pos));
140 mapmap.insert(make_pair(-4, neg));
141
Mark Sleee8540632006-05-30 09:24:40 +0000142 }
143
Mark Slee1921d202007-01-24 19:43:06 +0000144 void testInsanity(map<UserId, map<Numberz,Insanity> > &insane, const Insanity &argument) {
Mark Sleee8540632006-05-30 09:24:40 +0000145 printf("testInsanity()\n");
146
147 Xtruct hello;
148 hello.string_thing = "Hello2";
149 hello.byte_thing = 2;
Mark Sleee8540632006-05-30 09:24:40 +0000150 hello.i32_thing = 2;
Mark Sleee8540632006-05-30 09:24:40 +0000151 hello.i64_thing = 2;
152
153 Xtruct goodbye;
154 goodbye.string_thing = "Goodbye4";
155 goodbye.byte_thing = 4;
Mark Sleee8540632006-05-30 09:24:40 +0000156 goodbye.i32_thing = 4;
Mark Sleee8540632006-05-30 09:24:40 +0000157 goodbye.i64_thing = 4;
158
159 Insanity crazy;
160 crazy.userMap.insert(make_pair(EIGHT, 8));
161 crazy.xtructs.push_back(goodbye);
162
163 Insanity looney;
164 crazy.userMap.insert(make_pair(FIVE, 5));
165 crazy.xtructs.push_back(hello);
166
167 map<Numberz, Insanity> first_map;
168 map<Numberz, Insanity> second_map;
169
170 first_map.insert(make_pair(TWO, crazy));
171 first_map.insert(make_pair(THREE, crazy));
172
173 second_map.insert(make_pair(SIX, looney));
174
Mark Sleee8540632006-05-30 09:24:40 +0000175 insane.insert(make_pair(1, first_map));
176 insane.insert(make_pair(2, second_map));
177
178 printf("return");
179 printf(" = {");
180 map<UserId, map<Numberz,Insanity> >::const_iterator i_iter;
181 for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) {
Mark Sleed3d733a2006-09-01 22:19:06 +0000182 printf("%ld => {", i_iter->first);
Mark Sleee8540632006-05-30 09:24:40 +0000183 map<Numberz,Insanity>::const_iterator i2_iter;
184 for (i2_iter = i_iter->second.begin();
185 i2_iter != i_iter->second.end();
186 ++i2_iter) {
187 printf("%d => {", i2_iter->first);
188 map<Numberz, UserId> userMap = i2_iter->second.userMap;
189 map<Numberz, UserId>::const_iterator um;
190 printf("{");
191 for (um = userMap.begin(); um != userMap.end(); ++um) {
Mark Sleed3d733a2006-09-01 22:19:06 +0000192 printf("%d => %ld, ", um->first, um->second);
Mark Sleee8540632006-05-30 09:24:40 +0000193 }
194 printf("}, ");
195
Mark Sleeb9acf982006-10-10 01:57:32 +0000196 vector<Xtruct> xtructs = i2_iter->second.xtructs;
197 vector<Xtruct>::const_iterator x;
Mark Sleee8540632006-05-30 09:24:40 +0000198 printf("{");
199 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
Mark Sleed788b2e2006-09-07 01:26:35 +0000200 printf("{\"%s\", %d, %d, %ld}, ", x->string_thing.c_str(), (int)x->byte_thing, x->i32_thing, x->i64_thing);
Mark Sleee8540632006-05-30 09:24:40 +0000201 }
202 printf("}");
203
204 printf("}, ");
205 }
206 printf("}, ");
207 }
208 printf("}\n");
209
Mark Slee1921d202007-01-24 19:43:06 +0000210
Mark Sleee8540632006-05-30 09:24:40 +0000211 }
Marc Slemkoe6889de2006-08-12 00:32:53 +0000212
Mark Slee1921d202007-01-24 19:43:06 +0000213 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 arg4, const UserId arg5) {
Marc Slemkoe6889de2006-08-12 00:32:53 +0000214 printf("testMulti()\n");
215
Marc Slemkoe6889de2006-08-12 00:32:53 +0000216 hello.string_thing = "Hello2";
217 hello.byte_thing = arg0;
218 hello.i32_thing = arg1;
219 hello.i64_thing = (int64_t)arg2;
Marc Slemkoe6889de2006-08-12 00:32:53 +0000220 }
Marc Slemkobf4fd192006-08-15 21:29:39 +0000221
Mark Slee1921d202007-01-24 19:43:06 +0000222 void testException(const std::string &arg) throw(Xception) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000223 printf("testException(%s)\n", arg.c_str());
Mark Sleed3d733a2006-09-01 22:19:06 +0000224 if (arg.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000225 Xception e;
226 e.errorCode = 1001;
227 e.message = "This is an Xception";
228 throw e;
229 } else {
230 Xtruct result;
231 result.string_thing = arg;
232 return;
233 }
234 }
235
Mark Slee1921d202007-01-24 19:43:06 +0000236 void testMultiException(Xtruct &result, const std::string &arg0, const std::string &arg1) throw(Xception, Xception2) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000237
Marc Slemko71d4e472006-08-15 22:34:04 +0000238 printf("testMultiException(%s, %s)\n", arg0.c_str(), arg1.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000239
Mark Sleef5f2be42006-09-05 21:05:31 +0000240 if (arg0.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000241 Xception e;
242 e.errorCode = 1001;
243 e.message = "This is an Xception";
244 throw e;
Mark Sleef5f2be42006-09-05 21:05:31 +0000245 } else if (arg0.compare("Xception2") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000246 Xception2 e;
247 e.errorCode = 2002;
248 e.struct_thing.string_thing = "This is an Xception2";
249 throw e;
250 } else {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000251 result.string_thing = arg1;
Mark Slee1921d202007-01-24 19:43:06 +0000252 return;
Marc Slemkobf4fd192006-08-15 21:29:39 +0000253 }
254 }
Mark Sleee8540632006-05-30 09:24:40 +0000255};
256
257int main(int argc, char **argv) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000258
Mark Sleee8540632006-05-30 09:24:40 +0000259 int port = 9090;
Marc Slemko6be374b2006-08-04 03:16:25 +0000260 string serverType = "simple";
261 string protocolType = "binary";
262 size_t workerCount = 4;
Mark Sleea3302652006-10-25 19:03:32 +0000263 bool frameOutput = true;
Marc Slemko6be374b2006-08-04 03:16:25 +0000264
265 ostringstream usage;
266
267 usage <<
268 argv[0] << " [--port=<port number>] [--server-type=<server-type>] [--protocol-type=<protocol-type>] [--workers=<worker-count>]" << endl <<
269
Mark Slee0c2dff32007-03-09 19:26:57 +0000270 "\t\tserver-type\t\ttype of server, \"simple\", \"thread-pool\", \"threaded\", or \"nonblocking\". Default is " << serverType << endl <<
Marc Slemko6be374b2006-08-04 03:16:25 +0000271
272 "\t\tprotocol-type\t\ttype of protocol, \"binary\", \"ascii\", or \"xml\". Default is " << protocolType << endl <<
273
274 "\t\tworkers\t\tNumber of thread pools workers. Only valid for thread-pool server type. Default is " << workerCount << endl;
275
276 map<string, string> args;
277
Mark Sleef5f2be42006-09-05 21:05:31 +0000278 for (int ix = 1; ix < argc; ix++) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000279 string arg(argv[ix]);
Mark Sleef5f2be42006-09-05 21:05:31 +0000280 if (arg.compare(0,2, "--") == 0) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000281 size_t end = arg.find_first_of("=", 2);
Mark Sleef5f2be42006-09-05 21:05:31 +0000282 if (end != string::npos) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000283 args[string(arg, 2, end - 2)] = string(arg, end + 1);
284 } else {
Mark Sleea3302652006-10-25 19:03:32 +0000285 args[string(arg, 2)] = "true";
Marc Slemko6be374b2006-08-04 03:16:25 +0000286 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000287 } else {
288 throw invalid_argument("Unexcepted command line token: "+arg);
289 }
Mark Sleee8540632006-05-30 09:24:40 +0000290 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000291
292 try {
293
Mark Sleef5f2be42006-09-05 21:05:31 +0000294 if (!args["port"].empty()) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000295 port = atoi(args["port"].c_str());
296 }
297
Mark Sleea3302652006-10-25 19:03:32 +0000298 if (!args["noframe"].empty()) {
299 frameOutput = false;
300 }
301
Mark Sleef5f2be42006-09-05 21:05:31 +0000302 if (!args["server-type"].empty()) {
303 serverType = args["server-type"];
304 if (serverType == "simple") {
305 } else if (serverType == "thread-pool") {
Mark Slee0c2dff32007-03-09 19:26:57 +0000306 } else if (serverType == "threaded") {
Mark Sleeb9acf982006-10-10 01:57:32 +0000307 } else if (serverType == "nonblocking") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000308 } else {
Marc Slemko6be374b2006-08-04 03:16:25 +0000309 throw invalid_argument("Unknown server type "+serverType);
310 }
311 }
312
Mark Sleef5f2be42006-09-05 21:05:31 +0000313 if (!args["protocol-type"].empty()) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000314 protocolType = args["protocol-type"];
Mark Sleef5f2be42006-09-05 21:05:31 +0000315 if (protocolType == "binary") {
316 } else if (protocolType == "ascii") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000317 throw invalid_argument("ASCII protocol not supported");
Mark Sleef5f2be42006-09-05 21:05:31 +0000318 } else if (protocolType == "xml") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000319 throw invalid_argument("XML protocol not supported");
320 } else {
321 throw invalid_argument("Unknown protocol type "+protocolType);
322 }
323 }
324
Mark Sleef5f2be42006-09-05 21:05:31 +0000325 if (!args["workers"].empty()) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000326 workerCount = atoi(args["workers"].c_str());
327 }
Mark Sleef5f2be42006-09-05 21:05:31 +0000328 } catch (exception& e) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000329 cerr << e.what() << endl;
330 cerr << usage;
331 }
332
Mark Sleee8540632006-05-30 09:24:40 +0000333 // Dispatcher
Mark Sleea3302652006-10-25 19:03:32 +0000334 shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
Marc Slemko6be374b2006-08-04 03:16:25 +0000335
Mark Sleed2655522006-09-05 22:09:57 +0000336 shared_ptr<TestHandler> testHandler(new TestHandler());
337
Mark Sleea3302652006-10-25 19:03:32 +0000338 shared_ptr<ThriftTestProcessor> testProcessor(new ThriftTestProcessor(testHandler));
Mark Sleee8540632006-05-30 09:24:40 +0000339
Mark Sleee8540632006-05-30 09:24:40 +0000340 // Transport
Marc Slemko6be374b2006-08-04 03:16:25 +0000341 shared_ptr<TServerSocket> serverSocket(new TServerSocket(port));
Mark Sleee8540632006-05-30 09:24:40 +0000342
Mark Sleed788b2e2006-09-07 01:26:35 +0000343 // Factory
344 shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
345
Mark Sleed3d733a2006-09-01 22:19:06 +0000346 if (serverType == "simple") {
Mark Sleee8540632006-05-30 09:24:40 +0000347
Marc Slemko6be374b2006-08-04 03:16:25 +0000348 // Server
Mark Slee018b6992006-09-07 21:31:12 +0000349 TSimpleServer simpleServer(testProcessor,
Mark Sleed788b2e2006-09-07 01:26:35 +0000350 serverSocket,
351 transportFactory,
Mark Sleea3302652006-10-25 19:03:32 +0000352 protocolFactory);
Marc Slemko6be374b2006-08-04 03:16:25 +0000353
354 printf("Starting the server on port %d...\n", port);
Mark Slee794993d2006-09-20 01:56:10 +0000355 simpleServer.serve();
Marc Slemko6be374b2006-08-04 03:16:25 +0000356
Mark Sleed3d733a2006-09-01 22:19:06 +0000357 } else if (serverType == "thread-pool") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000358
Mark Sleef5f2be42006-09-05 21:05:31 +0000359 shared_ptr<ThreadManager> threadManager =
360 ThreadManager::newSimpleThreadManager(workerCount);
Marc Slemko6be374b2006-08-04 03:16:25 +0000361
Mark Sleef5f2be42006-09-05 21:05:31 +0000362 shared_ptr<PosixThreadFactory> threadFactory =
363 shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
Marc Slemko6be374b2006-08-04 03:16:25 +0000364
365 threadManager->threadFactory(threadFactory);
366
367 threadManager->start();
368
Mark Slee018b6992006-09-07 21:31:12 +0000369 TThreadPoolServer threadPoolServer(testProcessor,
Marc Slemko6be374b2006-08-04 03:16:25 +0000370 serverSocket,
Mark Sleed788b2e2006-09-07 01:26:35 +0000371 transportFactory,
Mark Sleea3302652006-10-25 19:03:32 +0000372 protocolFactory,
373 threadManager);
Marc Slemko6be374b2006-08-04 03:16:25 +0000374
375 printf("Starting the server on port %d...\n", port);
Mark Slee794993d2006-09-20 01:56:10 +0000376 threadPoolServer.serve();
Mark Sleeb9acf982006-10-10 01:57:32 +0000377
Mark Slee0c2dff32007-03-09 19:26:57 +0000378 } else if (serverType == "threaded") {
379
380 TThreadedServer threadedServer(testProcessor,
381 serverSocket,
382 transportFactory,
383 protocolFactory);
384
385 printf("Starting the server on port %d...\n", port);
386 threadedServer.serve();
387
Mark Sleeb9acf982006-10-10 01:57:32 +0000388 } else if (serverType == "nonblocking") {
389
Mark Sleea3302652006-10-25 19:03:32 +0000390 TNonblockingServer nonblockingServer(testProcessor, port);
391 nonblockingServer.setFrameResponses(frameOutput);
392 if (frameOutput) {
393 printf("Using framed output mode\n");
394 } else {
395 printf("Using non-framed output mode\n");
396 }
397
Mark Sleeb9acf982006-10-10 01:57:32 +0000398 printf("Starting the nonblocking server on port %d...\n", port);
399 nonblockingServer.serve();
400
Marc Slemko6be374b2006-08-04 03:16:25 +0000401 }
402
Mark Sleee8540632006-05-30 09:24:40 +0000403 printf("done.\n");
404 return 0;
405}