blob: 75b88d43fd7d227f7b3b865bb8028c7f61ece3fd [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 Slee78f58e22006-09-02 04:17:07 +000022class TestServer : public ThriftTestServer {
Mark Sleee8540632006-05-30 09:24:40 +000023 public:
Marc Slemko6be374b2006-08-04 03:16:25 +000024 TestServer(shared_ptr<TProtocol> protocol) :
Mark Slee78f58e22006-09-02 04:17:07 +000025 ThriftTestServer(protocol) {}
Mark Sleee8540632006-05-30 09:24:40 +000026
27 void testVoid() {
28 printf("testVoid()\n");
29 }
30
31 string testString(string thing) {
32 printf("testString(\"%s\")\n", thing.c_str());
33 return thing;
34 }
35
Mark Sleed3d733a2006-09-01 22:19:06 +000036 int8_t testByte(int8_t thing) {
Mark Sleee8540632006-05-30 09:24:40 +000037 printf("testByte(%d)\n", (int)thing);
38 return thing;
39 }
40
Mark Sleee8540632006-05-30 09:24:40 +000041 int32_t testI32(int32_t thing) {
42 printf("testI32(%d)\n", thing);
43 return thing;
44 }
45
Mark Sleee8540632006-05-30 09:24:40 +000046 int64_t testI64(int64_t thing) {
Mark Sleed3d733a2006-09-01 22:19:06 +000047 printf("testI64(%ld)\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +000048 return thing;
49 }
50
51 Xtruct testStruct(Xtruct thing) {
Mark Sleed3d733a2006-09-01 22:19:06 +000052 printf("testStruct({\"%s\", %d, %d, %ld})\n",
Mark Sleee8540632006-05-30 09:24:40 +000053 thing.string_thing.c_str(),
54 (int)thing.byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +000055 thing.i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +000056 thing.i64_thing);
57 return thing;
58 }
59
60 Xtruct2 testNest(Xtruct2 nest) {
61 Xtruct thing = nest.struct_thing;
Mark Sleed3d733a2006-09-01 22:19:06 +000062 printf("testNest({%d, {\"%s\", %d, %d, %ld}, %d})\n",
Mark Sleee8540632006-05-30 09:24:40 +000063 (int)nest.byte_thing,
64 thing.string_thing.c_str(),
65 (int)thing.byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +000066 thing.i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +000067 thing.i64_thing,
68 nest.i32_thing);
69 return nest;
70 }
71
72 map<int32_t, int32_t> testMap(map<int32_t, int32_t> thing) {
73 printf("testMap({");
74 map<int32_t, int32_t>::const_iterator m_iter;
75 bool first = true;
76 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
77 if (first) {
78 first = false;
79 } else {
80 printf(", ");
81 }
82 printf("%d => %d", m_iter->first, m_iter->second);
83 }
84 printf("})\n");
85 return thing;
86 }
87
88 set<int32_t> testSet(set<int32_t> thing) {
89 printf("testSet({");
90 set<int32_t>::const_iterator s_iter;
91 bool first = true;
92 for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) {
93 if (first) {
94 first = false;
95 } else {
96 printf(", ");
97 }
98 printf("%d", *s_iter);
99 }
100 printf("})\n");
101 return thing;
102 }
103
104 list<int32_t> testList(list<int32_t> thing) {
105 printf("testList({");
106 list<int32_t>::const_iterator l_iter;
107 bool first = true;
108 for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) {
109 if (first) {
110 first = false;
111 } else {
112 printf(", ");
113 }
114 printf("%d", *l_iter);
115 }
116 printf("})\n");
117 return thing;
118 }
119
Mark Slee95771002006-06-07 06:53:25 +0000120 Numberz testEnum(Numberz thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000121 printf("testEnum(%d)\n", thing);
122 return thing;
123 }
124
Mark Slee95771002006-06-07 06:53:25 +0000125 UserId testTypedef(UserId thing) {
Mark Sleed3d733a2006-09-01 22:19:06 +0000126 printf("testTypedef(%ld)\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +0000127 return thing;
128 }
129
130 map<int32_t, map<int32_t,int32_t> > testMapMap(int32_t hello) {
131 printf("testMapMap(%d)\n", hello);
132 map<int32_t, map<int32_t,int32_t> > mapmap;
133
134 map<int32_t,int32_t> pos;
135 map<int32_t,int32_t> neg;
136 for (int i = 1; i < 5; i++) {
137 pos.insert(make_pair(i,i));
138 neg.insert(make_pair(-i,-i));
139 }
140
141 mapmap.insert(make_pair(4, pos));
142 mapmap.insert(make_pair(-4, neg));
143
144 return mapmap;
145 }
146
147 map<UserId, map<Numberz,Insanity> > testInsanity(Insanity argument) {
148 printf("testInsanity()\n");
149
150 Xtruct hello;
151 hello.string_thing = "Hello2";
152 hello.byte_thing = 2;
Mark Sleee8540632006-05-30 09:24:40 +0000153 hello.i32_thing = 2;
Mark Sleee8540632006-05-30 09:24:40 +0000154 hello.i64_thing = 2;
155
156 Xtruct goodbye;
157 goodbye.string_thing = "Goodbye4";
158 goodbye.byte_thing = 4;
Mark Sleee8540632006-05-30 09:24:40 +0000159 goodbye.i32_thing = 4;
Mark Sleee8540632006-05-30 09:24:40 +0000160 goodbye.i64_thing = 4;
161
162 Insanity crazy;
163 crazy.userMap.insert(make_pair(EIGHT, 8));
164 crazy.xtructs.push_back(goodbye);
165
166 Insanity looney;
167 crazy.userMap.insert(make_pair(FIVE, 5));
168 crazy.xtructs.push_back(hello);
169
170 map<Numberz, Insanity> first_map;
171 map<Numberz, Insanity> second_map;
172
173 first_map.insert(make_pair(TWO, crazy));
174 first_map.insert(make_pair(THREE, crazy));
175
176 second_map.insert(make_pair(SIX, looney));
177
178 map<UserId, map<Numberz,Insanity> > insane;
179 insane.insert(make_pair(1, first_map));
180 insane.insert(make_pair(2, second_map));
181
182 printf("return");
183 printf(" = {");
184 map<UserId, map<Numberz,Insanity> >::const_iterator i_iter;
185 for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) {
Mark Sleed3d733a2006-09-01 22:19:06 +0000186 printf("%ld => {", i_iter->first);
Mark Sleee8540632006-05-30 09:24:40 +0000187 map<Numberz,Insanity>::const_iterator i2_iter;
188 for (i2_iter = i_iter->second.begin();
189 i2_iter != i_iter->second.end();
190 ++i2_iter) {
191 printf("%d => {", i2_iter->first);
192 map<Numberz, UserId> userMap = i2_iter->second.userMap;
193 map<Numberz, UserId>::const_iterator um;
194 printf("{");
195 for (um = userMap.begin(); um != userMap.end(); ++um) {
Mark Sleed3d733a2006-09-01 22:19:06 +0000196 printf("%d => %ld, ", um->first, um->second);
Mark Sleee8540632006-05-30 09:24:40 +0000197 }
198 printf("}, ");
199
200 list<Xtruct> xtructs = i2_iter->second.xtructs;
201 list<Xtruct>::const_iterator x;
202 printf("{");
203 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
Mark Sleed3d733a2006-09-01 22:19:06 +0000204 printf("{\"%s\", %d, %d, %ld}, ",
Mark Sleee8540632006-05-30 09:24:40 +0000205 x->string_thing.c_str(),
206 (int)x->byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000207 x->i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000208 x->i64_thing);
209 }
210 printf("}");
211
212 printf("}, ");
213 }
214 printf("}, ");
215 }
216 printf("}\n");
217
218 return insane;
219 }
Marc Slemkoe6889de2006-08-12 00:32:53 +0000220
Mark Sleed3d733a2006-09-01 22:19:06 +0000221 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 +0000222 printf("testMulti()\n");
223
224 Xtruct hello;
225 hello.string_thing = "Hello2";
226 hello.byte_thing = arg0;
227 hello.i32_thing = arg1;
228 hello.i64_thing = (int64_t)arg2;
229
230 return hello;
231 }
Marc Slemkobf4fd192006-08-15 21:29:39 +0000232
Mark Sleed3d733a2006-09-01 22:19:06 +0000233 void testException(std::string arg) throw(Xception) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000234 printf("testException(%s)\n", arg.c_str());
Mark Sleed3d733a2006-09-01 22:19:06 +0000235 if (arg.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000236 Xception e;
237 e.errorCode = 1001;
238 e.message = "This is an Xception";
239 throw e;
240 } else {
241 Xtruct result;
242 result.string_thing = arg;
243 return;
244 }
245 }
246
Mark Sleed3d733a2006-09-01 22:19:06 +0000247 Xtruct testMultiException(std::string arg0, std::string arg1) throw(Xception, Xception2) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000248
Marc Slemko71d4e472006-08-15 22:34:04 +0000249 printf("testMultiException(%s, %s)\n", arg0.c_str(), arg1.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000250
Mark Sleef5f2be42006-09-05 21:05:31 +0000251 if (arg0.compare("Xception") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000252 Xception e;
253 e.errorCode = 1001;
254 e.message = "This is an Xception";
255 throw e;
Mark Sleef5f2be42006-09-05 21:05:31 +0000256 } else if (arg0.compare("Xception2") == 0) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000257 Xception2 e;
258 e.errorCode = 2002;
259 e.struct_thing.string_thing = "This is an Xception2";
260 throw e;
261 } else {
262 Xtruct result;
263 result.string_thing = arg1;
264 return result;
265 }
266 }
Mark Sleee8540632006-05-30 09:24:40 +0000267};
268
269int main(int argc, char **argv) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000270
Mark Sleee8540632006-05-30 09:24:40 +0000271 int port = 9090;
Marc Slemko6be374b2006-08-04 03:16:25 +0000272 string serverType = "simple";
273 string protocolType = "binary";
274 size_t workerCount = 4;
275
276 ostringstream usage;
277
278 usage <<
279 argv[0] << " [--port=<port number>] [--server-type=<server-type>] [--protocol-type=<protocol-type>] [--workers=<worker-count>]" << endl <<
280
Marc Slemkobf4fd192006-08-15 21:29:39 +0000281 "\t\tserver-type\t\ttype of server, \"simple\" or \"thread-pool\". Default is " << serverType << endl <<
Marc Slemko6be374b2006-08-04 03:16:25 +0000282
283 "\t\tprotocol-type\t\ttype of protocol, \"binary\", \"ascii\", or \"xml\". Default is " << protocolType << endl <<
284
285 "\t\tworkers\t\tNumber of thread pools workers. Only valid for thread-pool server type. Default is " << workerCount << endl;
286
287 map<string, string> args;
288
Mark Sleef5f2be42006-09-05 21:05:31 +0000289 for (int ix = 1; ix < argc; ix++) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000290 string arg(argv[ix]);
Mark Sleef5f2be42006-09-05 21:05:31 +0000291 if (arg.compare(0,2, "--") == 0) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000292 size_t end = arg.find_first_of("=", 2);
Mark Sleef5f2be42006-09-05 21:05:31 +0000293 if (end != string::npos) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000294 args[string(arg, 2, end - 2)] = string(arg, end + 1);
295 } else {
296 args[string(arg, 2, end - 2)] = "true";
297 }
298 ix++;
299 } else {
300 throw invalid_argument("Unexcepted command line token: "+arg);
301 }
Mark Sleee8540632006-05-30 09:24:40 +0000302 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000303
304 try {
305
Mark Sleef5f2be42006-09-05 21:05:31 +0000306 if (!args["port"].empty()) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000307 port = atoi(args["port"].c_str());
308 }
309
Mark Sleef5f2be42006-09-05 21:05:31 +0000310 if (!args["server-type"].empty()) {
311 serverType = args["server-type"];
312 if (serverType == "simple") {
313 } else if (serverType == "thread-pool") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000314 } else {
Marc Slemko6be374b2006-08-04 03:16:25 +0000315 throw invalid_argument("Unknown server type "+serverType);
316 }
317 }
318
Mark Sleef5f2be42006-09-05 21:05:31 +0000319 if (!args["protocol-type"].empty()) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000320 protocolType = args["protocol-type"];
Mark Sleef5f2be42006-09-05 21:05:31 +0000321 if (protocolType == "binary") {
322 } else if (protocolType == "ascii") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000323 throw invalid_argument("ASCII protocol not supported");
Mark Sleef5f2be42006-09-05 21:05:31 +0000324 } else if (protocolType == "xml") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000325 throw invalid_argument("XML protocol not supported");
326 } else {
327 throw invalid_argument("Unknown protocol type "+protocolType);
328 }
329 }
330
Mark Sleef5f2be42006-09-05 21:05:31 +0000331 if (!args["workers"].empty()) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000332 workerCount = atoi(args["workers"].c_str());
333 }
Mark Sleef5f2be42006-09-05 21:05:31 +0000334 } catch (exception& e) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000335 cerr << e.what() << endl;
336 cerr << usage;
337 }
338
Mark Sleee8540632006-05-30 09:24:40 +0000339 // Dispatcher
Marc Slemko6be374b2006-08-04 03:16:25 +0000340 shared_ptr<TBinaryProtocol> binaryProtocol(new TBinaryProtocol);
341
342 shared_ptr<TestServer> testServer(new TestServer(binaryProtocol));
Mark Sleee8540632006-05-30 09:24:40 +0000343
344 // Options
Marc Slemko6be374b2006-08-04 03:16:25 +0000345 shared_ptr<TServerOptions> serverOptions(new TServerOptions());
Mark Sleee8540632006-05-30 09:24:40 +0000346
347 // Transport
Marc Slemko6be374b2006-08-04 03:16:25 +0000348 shared_ptr<TServerSocket> serverSocket(new TServerSocket(port));
Mark Sleee8540632006-05-30 09:24:40 +0000349
Mark Sleed3d733a2006-09-01 22:19:06 +0000350 if (serverType == "simple") {
Mark Sleee8540632006-05-30 09:24:40 +0000351
Marc Slemko6be374b2006-08-04 03:16:25 +0000352 // Server
353 TSimpleServer simpleServer(testServer,
354 serverOptions,
355 serverSocket);
356
357 printf("Starting the server on port %d...\n", port);
358 simpleServer.run();
359
Mark Sleed3d733a2006-09-01 22:19:06 +0000360 } else if (serverType == "thread-pool") {
Marc Slemko6be374b2006-08-04 03:16:25 +0000361
Mark Sleef5f2be42006-09-05 21:05:31 +0000362 shared_ptr<ThreadManager> threadManager =
363 ThreadManager::newSimpleThreadManager(workerCount);
Marc Slemko6be374b2006-08-04 03:16:25 +0000364
Mark Sleef5f2be42006-09-05 21:05:31 +0000365 shared_ptr<PosixThreadFactory> threadFactory =
366 shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
Marc Slemko6be374b2006-08-04 03:16:25 +0000367
368 threadManager->threadFactory(threadFactory);
369
370 threadManager->start();
371
372 TThreadPoolServer threadPoolServer(testServer,
373 serverOptions,
374 serverSocket,
375 threadManager);
376
377 printf("Starting the server on port %d...\n", port);
378 threadPoolServer.run();
379 }
380
Mark Sleee8540632006-05-30 09:24:40 +0000381 printf("done.\n");
382 return 0;
383}