blob: 4df37af1d4a3b5852935721208cba381f5d86965 [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 Sleee8540632006-05-30 09:24:40 +000022class TestServer : public ThriftTestServerIf {
23 public:
Marc Slemko6be374b2006-08-04 03:16:25 +000024 TestServer(shared_ptr<TProtocol> protocol) :
Mark Sleee8540632006-05-30 09:24:40 +000025 ThriftTestServerIf(protocol) {}
26
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
36 uint8_t testByte(uint8_t thing) {
37 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) {
Marc Slemkobf4fd192006-08-15 21:29:39 +000047 printf("testI64(%lld)\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +000048 return thing;
49 }
50
51 Xtruct testStruct(Xtruct thing) {
Marc Slemkobf4fd192006-08-15 21:29:39 +000052 printf("testStruct({\"%s\", %d, %d, %lld})\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;
Marc Slemkobf4fd192006-08-15 21:29:39 +000062 printf("testNest({%d, {\"%s\", %d, %d, %lld}, %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) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000126 printf("testTypedef(%lld)\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) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000186 printf("%lld => {", 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) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000196 printf("%d => %lld, ", 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) {
Marc Slemkobf4fd192006-08-15 21:29:39 +0000204 printf("{\"%s\", %d, %d, %lld}, ",
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
221 Xtruct testMulti(uint8_t arg0, int32_t arg1, uint64_t arg2, std::map<int16_t, std::string> arg3, Numberz arg4, UserId arg5) {
222 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
233 virtual void testException(std::string arg) throw(struct thrift::test::Xception) {
234 printf("testException(%s)\n", arg.c_str());
235 if(arg.compare("Xception") == 0) {
236 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
247 virtual struct Xtruct testMultiException(std::string arg0, std::string arg1) throw(struct Xception, struct Xception2) {
248
249 printf("testException(%s, %s)\n", arg0.c_str(), arg1.c_str());
250
251 if(arg0.compare("Xception") == 0) {
252 Xception e;
253 e.errorCode = 1001;
254 e.message = "This is an Xception";
255 throw e;
256
257 } else if(arg0.compare("Xception2") == 0) {
258 Xception2 e;
259 e.errorCode = 2002;
260 e.struct_thing.string_thing = "This is an Xception2";
261 throw e;
262 } else {
263 Xtruct result;
264 result.string_thing = arg1;
265 return result;
266 }
267 }
Mark Sleee8540632006-05-30 09:24:40 +0000268};
269
270int main(int argc, char **argv) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000271
Mark Sleee8540632006-05-30 09:24:40 +0000272 int port = 9090;
Marc Slemko6be374b2006-08-04 03:16:25 +0000273 string serverType = "simple";
274 string protocolType = "binary";
275 size_t workerCount = 4;
276
277 ostringstream usage;
278
279 usage <<
280 argv[0] << " [--port=<port number>] [--server-type=<server-type>] [--protocol-type=<protocol-type>] [--workers=<worker-count>]" << endl <<
281
Marc Slemkobf4fd192006-08-15 21:29:39 +0000282 "\t\tserver-type\t\ttype of server, \"simple\" or \"thread-pool\". Default is " << serverType << endl <<
Marc Slemko6be374b2006-08-04 03:16:25 +0000283
284 "\t\tprotocol-type\t\ttype of protocol, \"binary\", \"ascii\", or \"xml\". Default is " << protocolType << endl <<
285
286 "\t\tworkers\t\tNumber of thread pools workers. Only valid for thread-pool server type. Default is " << workerCount << endl;
287
288 map<string, string> args;
289
290 for(int ix = 1; ix < argc; ix++) {
291
292 string arg(argv[ix]);
293
294 if(arg.compare(0,2, "--") == 0) {
295
296 size_t end = arg.find_first_of("=", 2);
297
298 if(end != string::npos) {
299 args[string(arg, 2, end - 2)] = string(arg, end + 1);
300 } else {
301 args[string(arg, 2, end - 2)] = "true";
302 }
303 ix++;
304 } else {
305 throw invalid_argument("Unexcepted command line token: "+arg);
306 }
Mark Sleee8540632006-05-30 09:24:40 +0000307 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000308
309 try {
310
311 if(!args["port"].empty()) {
312 port = atoi(args["port"].c_str());
313 }
314
315 if(!args["server-type"].empty()) {
316 serverType = args["server-type"];
317
318 if(serverType == "simple") {
319
320 } else if(serverType == "thread-pool") {
321
322 } else {
323
324 throw invalid_argument("Unknown server type "+serverType);
325 }
326 }
327
328 if(!args["protocol-type"].empty()) {
329 protocolType = args["protocol-type"];
330
331 if(protocolType == "binary") {
332 } else if(protocolType == "ascii") {
333 throw invalid_argument("ASCII protocol not supported");
334 } else if(protocolType == "xml") {
335 throw invalid_argument("XML protocol not supported");
336 } else {
337 throw invalid_argument("Unknown protocol type "+protocolType);
338 }
339 }
340
341 if(!args["workers"].empty()) {
342 workerCount = atoi(args["workers"].c_str());
343 }
344 } catch(exception& e) {
345 cerr << e.what() << endl;
346 cerr << usage;
347 }
348
Mark Sleee8540632006-05-30 09:24:40 +0000349 // Dispatcher
Marc Slemko6be374b2006-08-04 03:16:25 +0000350 shared_ptr<TBinaryProtocol> binaryProtocol(new TBinaryProtocol);
351
352 shared_ptr<TestServer> testServer(new TestServer(binaryProtocol));
Mark Sleee8540632006-05-30 09:24:40 +0000353
354 // Options
Marc Slemko6be374b2006-08-04 03:16:25 +0000355 shared_ptr<TServerOptions> serverOptions(new TServerOptions());
Mark Sleee8540632006-05-30 09:24:40 +0000356
357 // Transport
Marc Slemko6be374b2006-08-04 03:16:25 +0000358 shared_ptr<TServerSocket> serverSocket(new TServerSocket(port));
Mark Sleee8540632006-05-30 09:24:40 +0000359
Marc Slemko6be374b2006-08-04 03:16:25 +0000360 if(serverType == "simple") {
Mark Sleee8540632006-05-30 09:24:40 +0000361
Marc Slemko6be374b2006-08-04 03:16:25 +0000362 // Server
363 TSimpleServer simpleServer(testServer,
364 serverOptions,
365 serverSocket);
366
367 printf("Starting the server on port %d...\n", port);
368 simpleServer.run();
369
370 } else if(serverType == "thread-pool") {
371
372 shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workerCount);
373
374 shared_ptr<PosixThreadFactory> threadFactory = shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
375
376 threadManager->threadFactory(threadFactory);
377
378 threadManager->start();
379
380 TThreadPoolServer threadPoolServer(testServer,
381 serverOptions,
382 serverSocket,
383 threadManager);
384
385 printf("Starting the server on port %d...\n", port);
386 threadPoolServer.run();
387 }
388
Mark Sleee8540632006-05-30 09:24:40 +0000389 printf("done.\n");
390 return 0;
391}