blob: 63a75945d3d7abb3311381bbf1e2c3b876254387 [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>
5#include <server/TThreadPoolServer.h>
6#include <transport/TServerSocket.h>
Mark Slee95771002006-06-07 06:53:25 +00007#include "ThriftTest.h"
Marc Slemko6be374b2006-08-04 03:16:25 +00008
9#include <iostream>
10#include <stdexcept>
11#include <sstream>
12
Mark Sleee8540632006-05-30 09:24:40 +000013using namespace std;
14
Marc Slemko6be374b2006-08-04 03:16:25 +000015using namespace facebook::thrift;
16using namespace facebook::thrift::protocol;
17using namespace facebook::thrift::transport;
18using namespace facebook::thrift::server;
19
Marc Slemkobf4fd192006-08-15 21:29:39 +000020using namespace thrift::test;
21
Mark Sleed2655522006-09-05 22:09:57 +000022class TestHandler : public ThriftTestIf {
Mark Sleee8540632006-05-30 09:24:40 +000023 public:
Mark Sleed2655522006-09-05 22:09:57 +000024 TestHandler() {}
Mark Sleee8540632006-05-30 09:24:40 +000025
26 void testVoid() {
27 printf("testVoid()\n");
28 }
29
30 string testString(string thing) {
31 printf("testString(\"%s\")\n", thing.c_str());
32 return thing;
33 }
34
Mark Sleed3d733a2006-09-01 22:19:06 +000035 int8_t testByte(int8_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +000036 printf("testByte(%d)\n", (int)thing);
37 return thing;
38 }
39
Mark Sleee8540632006-05-30 09:24:40 +000040 int32_t testI32(int32_t thing) {
41 printf("testI32(%d)\n", thing);
42 return thing;
43 }
44
Mark Sleee8540632006-05-30 09:24:40 +000045 int64_t testI64(int64_t thing) {
Mark Sleed3d733a2006-09-01 22:19:06 +000046 printf("testI64(%ld)\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +000047 return thing;
48 }
49
50 Xtruct testStruct(Xtruct thing) {
Mark Sleed3d733a2006-09-01 22:19:06 +000051 printf("testStruct({\"%s\", %d, %d, %ld})\n",
Mark Sleee8540632006-05-30 09:24:40 +000052 thing.string_thing.c_str(),
53 (int)thing.byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +000054 thing.i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +000055 thing.i64_thing);
56 return thing;
57 }
58
59 Xtruct2 testNest(Xtruct2 nest) {
60 Xtruct thing = nest.struct_thing;
Mark Sleed3d733a2006-09-01 22:19:06 +000061 printf("testNest({%d, {\"%s\", %d, %d, %ld}, %d})\n",
Mark Sleee8540632006-05-30 09:24:40 +000062 (int)nest.byte_thing,
63 thing.string_thing.c_str(),
64 (int)thing.byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +000065 thing.i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +000066 thing.i64_thing,
67 nest.i32_thing);
68 return nest;
69 }
70
71 map<int32_t, int32_t> testMap(map<int32_t, int32_t> thing) {
72 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");
84 return thing;
85 }
86
87 set<int32_t> testSet(set<int32_t> thing) {
88 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");
100 return thing;
101 }
102
103 list<int32_t> testList(list<int32_t> thing) {
104 printf("testList({");
105 list<int32_t>::const_iterator l_iter;
106 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");
116 return thing;
117 }
118
Mark Slee95771002006-06-07 06:53:25 +0000119 Numberz testEnum(Numberz thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000120 printf("testEnum(%d)\n", thing);
121 return thing;
122 }
123
Mark Slee95771002006-06-07 06:53:25 +0000124 UserId testTypedef(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
129 map<int32_t, map<int32_t,int32_t> > testMapMap(int32_t hello) {
130 printf("testMapMap(%d)\n", hello);
131 map<int32_t, map<int32_t,int32_t> > mapmap;
132
133 map<int32_t,int32_t> pos;
134 map<int32_t,int32_t> neg;
135 for (int i = 1; i < 5; i++) {
136 pos.insert(make_pair(i,i));
137 neg.insert(make_pair(-i,-i));
138 }
139
140 mapmap.insert(make_pair(4, pos));
141 mapmap.insert(make_pair(-4, neg));
142
143 return mapmap;
144 }
145
146 map<UserId, map<Numberz,Insanity> > testInsanity(Insanity argument) {
147 printf("testInsanity()\n");
148
149 Xtruct hello;
150 hello.string_thing = "Hello2";
151 hello.byte_thing = 2;
Mark Sleee8540632006-05-30 09:24:40 +0000152 hello.i32_thing = 2;
Mark Sleee8540632006-05-30 09:24:40 +0000153 hello.i64_thing = 2;
154
155 Xtruct goodbye;
156 goodbye.string_thing = "Goodbye4";
157 goodbye.byte_thing = 4;
Mark Sleee8540632006-05-30 09:24:40 +0000158 goodbye.i32_thing = 4;
Mark Sleee8540632006-05-30 09:24:40 +0000159 goodbye.i64_thing = 4;
160
161 Insanity crazy;
162 crazy.userMap.insert(make_pair(EIGHT, 8));
163 crazy.xtructs.push_back(goodbye);
164
165 Insanity looney;
166 crazy.userMap.insert(make_pair(FIVE, 5));
167 crazy.xtructs.push_back(hello);
168
169 map<Numberz, Insanity> first_map;
170 map<Numberz, Insanity> second_map;
171
172 first_map.insert(make_pair(TWO, crazy));
173 first_map.insert(make_pair(THREE, crazy));
174
175 second_map.insert(make_pair(SIX, looney));
176
177 map<UserId, map<Numberz,Insanity> > insane;
178 insane.insert(make_pair(1, first_map));
179 insane.insert(make_pair(2, second_map));
180
181 printf("return");
182 printf(" = {");
183 map<UserId, map<Numberz,Insanity> >::const_iterator i_iter;
184 for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) {
Mark Sleed3d733a2006-09-01 22:19:06 +0000185 printf("%ld => {", i_iter->first);
Mark Sleee8540632006-05-30 09:24:40 +0000186 map<Numberz,Insanity>::const_iterator i2_iter;
187 for (i2_iter = i_iter->second.begin();
188 i2_iter != i_iter->second.end();
189 ++i2_iter) {
190 printf("%d => {", i2_iter->first);
191 map<Numberz, UserId> userMap = i2_iter->second.userMap;
192 map<Numberz, UserId>::const_iterator um;
193 printf("{");
194 for (um = userMap.begin(); um != userMap.end(); ++um) {
Mark Sleed3d733a2006-09-01 22:19:06 +0000195 printf("%d => %ld, ", um->first, um->second);
Mark Sleee8540632006-05-30 09:24:40 +0000196 }
197 printf("}, ");
198
199 list<Xtruct> xtructs = i2_iter->second.xtructs;
200 list<Xtruct>::const_iterator x;
201 printf("{");
202 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
Mark Sleed3d733a2006-09-01 22:19:06 +0000203 printf("{\"%s\", %d, %d, %ld}, ",
Mark Sleee8540632006-05-30 09:24:40 +0000204 x->string_thing.c_str(),
205 (int)x->byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000206 x->i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000207 x->i64_thing);
208 }
209 printf("}");
210
211 printf("}, ");
212 }
213 printf("}, ");
214 }
215 printf("}\n");
216
217 return insane;
218 }
Marc Slemkoe6889de2006-08-12 00:32:53 +0000219
Mark Sleed3d733a2006-09-01 22:19:06 +0000220 Xtruct testMulti(int8_t arg0, int32_t arg1, int64_t arg2, std::map<int16_t, std::string> arg3, Numberz arg4, UserId arg5) {
Marc Slemkoe6889de2006-08-12 00:32:53 +0000221 printf("testMulti()\n");
222
223 Xtruct hello;
224 hello.string_thing = "Hello2";
225 hello.byte_thing = arg0;
226 hello.i32_thing = arg1;
227 hello.i64_thing = (int64_t)arg2;
228
229 return hello;
230 }
Marc Slemkobf4fd192006-08-15 21:29:39 +0000231
Mark Sleed3d733a2006-09-01 22:19:06 +0000232 void testException(std::string arg) throw(Xception) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000233 printf("testException(%s)\n", arg.c_str());
Mark Sleed3d733a2006-09-01 22:19:06 +0000234 if (arg.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000235 Xception e;
236 e.errorCode = 1001;
237 e.message = "This is an Xception";
238 throw e;
239 } else {
240 Xtruct result;
241 result.string_thing = arg;
242 return;
243 }
244 }
245
Mark Sleed3d733a2006-09-01 22:19:06 +0000246 Xtruct testMultiException(std::string arg0, std::string arg1) throw(Xception, Xception2) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000247
Marc Slemko71d4e472006-08-15 22:34:04 +0000248 printf("testMultiException(%s, %s)\n", arg0.c_str(), arg1.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000249
Mark Sleef5f2be42006-09-05 21:05:31 +0000250 if (arg0.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000251 Xception e;
252 e.errorCode = 1001;
253 e.message = "This is an Xception";
254 throw e;
Mark Sleef5f2be42006-09-05 21:05:31 +0000255 } else if (arg0.compare("Xception2") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000256 Xception2 e;
257 e.errorCode = 2002;
258 e.struct_thing.string_thing = "This is an Xception2";
259 throw e;
260 } else {
261 Xtruct result;
262 result.string_thing = arg1;
263 return result;
264 }
265 }
Mark Sleee8540632006-05-30 09:24:40 +0000266};
267
268int main(int argc, char **argv) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000269
Mark Sleee8540632006-05-30 09:24:40 +0000270 int port = 9090;
Marc Slemko6be374b2006-08-04 03:16:25 +0000271 string serverType = "simple";
272 string protocolType = "binary";
273 size_t workerCount = 4;
274
275 ostringstream usage;
276
277 usage <<
278 argv[0] << " [--port=<port number>] [--server-type=<server-type>] [--protocol-type=<protocol-type>] [--workers=<worker-count>]" << endl <<
279
Marc Slemkobf4fd192006-08-15 21:29:39 +0000280 "\t\tserver-type\t\ttype of server, \"simple\" or \"thread-pool\". Default is " << serverType << endl <<
Marc Slemko6be374b2006-08-04 03:16:25 +0000281
282 "\t\tprotocol-type\t\ttype of protocol, \"binary\", \"ascii\", or \"xml\". Default is " << protocolType << endl <<
283
284 "\t\tworkers\t\tNumber of thread pools workers. Only valid for thread-pool server type. Default is " << workerCount << endl;
285
286 map<string, string> args;
287
Mark Sleef5f2be42006-09-05 21:05:31 +0000288 for (int ix = 1; ix < argc; ix++) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000289 string arg(argv[ix]);
Mark Sleef5f2be42006-09-05 21:05:31 +0000290 if (arg.compare(0,2, "--") == 0) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000291 size_t end = arg.find_first_of("=", 2);
Mark Sleef5f2be42006-09-05 21:05:31 +0000292 if (end != string::npos) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000293 args[string(arg, 2, end - 2)] = string(arg, end + 1);
294 } else {
295 args[string(arg, 2, end - 2)] = "true";
296 }
297 ix++;
298 } else {
299 throw invalid_argument("Unexcepted command line token: "+arg);
300 }
Mark Sleee8540632006-05-30 09:24:40 +0000301 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000302
303 try {
304
Mark Sleef5f2be42006-09-05 21:05:31 +0000305 if (!args["port"].empty()) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000306 port = atoi(args["port"].c_str());
307 }
308
Mark Sleef5f2be42006-09-05 21:05:31 +0000309 if (!args["server-type"].empty()) {
310 serverType = args["server-type"];
311 if (serverType == "simple") {
312 } else if (serverType == "thread-pool") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000313 } else {
Marc Slemko6be374b2006-08-04 03:16:25 +0000314 throw invalid_argument("Unknown server type "+serverType);
315 }
316 }
317
Mark Sleef5f2be42006-09-05 21:05:31 +0000318 if (!args["protocol-type"].empty()) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000319 protocolType = args["protocol-type"];
Mark Sleef5f2be42006-09-05 21:05:31 +0000320 if (protocolType == "binary") {
321 } else if (protocolType == "ascii") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000322 throw invalid_argument("ASCII protocol not supported");
Mark Sleef5f2be42006-09-05 21:05:31 +0000323 } else if (protocolType == "xml") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000324 throw invalid_argument("XML protocol not supported");
325 } else {
326 throw invalid_argument("Unknown protocol type "+protocolType);
327 }
328 }
329
Mark Sleef5f2be42006-09-05 21:05:31 +0000330 if (!args["workers"].empty()) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000331 workerCount = atoi(args["workers"].c_str());
332 }
Mark Sleef5f2be42006-09-05 21:05:31 +0000333 } catch (exception& e) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000334 cerr << e.what() << endl;
335 cerr << usage;
336 }
337
Mark Sleee8540632006-05-30 09:24:40 +0000338 // Dispatcher
Marc Slemko6be374b2006-08-04 03:16:25 +0000339 shared_ptr<TBinaryProtocol> binaryProtocol(new TBinaryProtocol);
340
Mark Sleed2655522006-09-05 22:09:57 +0000341 shared_ptr<TestHandler> testHandler(new TestHandler());
342
343 shared_ptr<ThriftTestServer> testServer(new ThriftTestServer(testHandler, binaryProtocol));
Mark Sleee8540632006-05-30 09:24:40 +0000344
345 // Options
Marc Slemko6be374b2006-08-04 03:16:25 +0000346 shared_ptr<TServerOptions> serverOptions(new TServerOptions());
Mark Sleee8540632006-05-30 09:24:40 +0000347
348 // Transport
Marc Slemko6be374b2006-08-04 03:16:25 +0000349 shared_ptr<TServerSocket> serverSocket(new TServerSocket(port));
Mark Sleee8540632006-05-30 09:24:40 +0000350
Mark Sleed3d733a2006-09-01 22:19:06 +0000351 if (serverType == "simple") {
Mark Sleee8540632006-05-30 09:24:40 +0000352
Marc Slemko6be374b2006-08-04 03:16:25 +0000353 // Server
354 TSimpleServer simpleServer(testServer,
355 serverOptions,
356 serverSocket);
357
358 printf("Starting the server on port %d...\n", port);
359 simpleServer.run();
360
Mark Sleed3d733a2006-09-01 22:19:06 +0000361 } else if (serverType == "thread-pool") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000362
Mark Sleef5f2be42006-09-05 21:05:31 +0000363 shared_ptr<ThreadManager> threadManager =
364 ThreadManager::newSimpleThreadManager(workerCount);
Marc Slemko6be374b2006-08-04 03:16:25 +0000365
Mark Sleef5f2be42006-09-05 21:05:31 +0000366 shared_ptr<PosixThreadFactory> threadFactory =
367 shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
Marc Slemko6be374b2006-08-04 03:16:25 +0000368
369 threadManager->threadFactory(threadFactory);
370
371 threadManager->start();
372
373 TThreadPoolServer threadPoolServer(testServer,
374 serverOptions,
375 serverSocket,
376 threadManager);
377
378 printf("Starting the server on port %d...\n", port);
379 threadPoolServer.run();
380 }
381
Mark Sleee8540632006-05-30 09:24:40 +0000382 printf("done.\n");
383 return 0;
384}