blob: 27a983c6e2db62873cb9f6f0c6e55e9329fef5f3 [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
Mark Sleee8540632006-05-30 09:24:40 +000020class TestServer : public ThriftTestServerIf {
21 public:
Marc Slemko6be374b2006-08-04 03:16:25 +000022 TestServer(shared_ptr<TProtocol> protocol) :
Mark Sleee8540632006-05-30 09:24:40 +000023 ThriftTestServerIf(protocol) {}
24
25 void testVoid() {
26 printf("testVoid()\n");
27 }
28
29 string testString(string thing) {
30 printf("testString(\"%s\")\n", thing.c_str());
31 return thing;
32 }
33
34 uint8_t testByte(uint8_t thing) {
35 printf("testByte(%d)\n", (int)thing);
36 return thing;
37 }
38
Mark Sleee8540632006-05-30 09:24:40 +000039 int32_t testI32(int32_t thing) {
40 printf("testI32(%d)\n", thing);
41 return thing;
42 }
43
Mark Sleee8540632006-05-30 09:24:40 +000044 int64_t testI64(int64_t thing) {
45 printf("testI64(%ld)\n", thing);
46 return thing;
47 }
48
49 Xtruct testStruct(Xtruct thing) {
Mark Slee6e536442006-06-30 18:28:50 +000050 printf("testStruct({\"%s\", %d, %d, %ld})\n",
Mark Sleee8540632006-05-30 09:24:40 +000051 thing.string_thing.c_str(),
52 (int)thing.byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +000053 thing.i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +000054 thing.i64_thing);
55 return thing;
56 }
57
58 Xtruct2 testNest(Xtruct2 nest) {
59 Xtruct thing = nest.struct_thing;
Mark Slee6e536442006-06-30 18:28:50 +000060 printf("testNest({%d, {\"%s\", %d, %d, %ld}, %d})\n",
Mark Sleee8540632006-05-30 09:24:40 +000061 (int)nest.byte_thing,
62 thing.string_thing.c_str(),
63 (int)thing.byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +000064 thing.i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +000065 thing.i64_thing,
66 nest.i32_thing);
67 return nest;
68 }
69
70 map<int32_t, int32_t> testMap(map<int32_t, int32_t> thing) {
71 printf("testMap({");
72 map<int32_t, int32_t>::const_iterator m_iter;
73 bool first = true;
74 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
75 if (first) {
76 first = false;
77 } else {
78 printf(", ");
79 }
80 printf("%d => %d", m_iter->first, m_iter->second);
81 }
82 printf("})\n");
83 return thing;
84 }
85
86 set<int32_t> testSet(set<int32_t> thing) {
87 printf("testSet({");
88 set<int32_t>::const_iterator s_iter;
89 bool first = true;
90 for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) {
91 if (first) {
92 first = false;
93 } else {
94 printf(", ");
95 }
96 printf("%d", *s_iter);
97 }
98 printf("})\n");
99 return thing;
100 }
101
102 list<int32_t> testList(list<int32_t> thing) {
103 printf("testList({");
104 list<int32_t>::const_iterator l_iter;
105 bool first = true;
106 for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) {
107 if (first) {
108 first = false;
109 } else {
110 printf(", ");
111 }
112 printf("%d", *l_iter);
113 }
114 printf("})\n");
115 return thing;
116 }
117
Mark Slee95771002006-06-07 06:53:25 +0000118 Numberz testEnum(Numberz thing) {
Mark Sleee8540632006-05-30 09:24:40 +0000119 printf("testEnum(%d)\n", thing);
120 return thing;
121 }
122
Mark Slee95771002006-06-07 06:53:25 +0000123 UserId testTypedef(UserId thing) {
Mark Slee6e536442006-06-30 18:28:50 +0000124 printf("testTypedef(%ld)\n", thing);
Mark Sleee8540632006-05-30 09:24:40 +0000125 return thing;
126 }
127
128 map<int32_t, map<int32_t,int32_t> > testMapMap(int32_t hello) {
129 printf("testMapMap(%d)\n", hello);
130 map<int32_t, map<int32_t,int32_t> > mapmap;
131
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
142 return mapmap;
143 }
144
145 map<UserId, map<Numberz,Insanity> > testInsanity(Insanity argument) {
146 printf("testInsanity()\n");
147
148 Xtruct hello;
149 hello.string_thing = "Hello2";
150 hello.byte_thing = 2;
Mark Sleee8540632006-05-30 09:24:40 +0000151 hello.i32_thing = 2;
Mark Sleee8540632006-05-30 09:24:40 +0000152 hello.i64_thing = 2;
153
154 Xtruct goodbye;
155 goodbye.string_thing = "Goodbye4";
156 goodbye.byte_thing = 4;
Mark Sleee8540632006-05-30 09:24:40 +0000157 goodbye.i32_thing = 4;
Mark Sleee8540632006-05-30 09:24:40 +0000158 goodbye.i64_thing = 4;
159
160 Insanity crazy;
161 crazy.userMap.insert(make_pair(EIGHT, 8));
162 crazy.xtructs.push_back(goodbye);
163
164 Insanity looney;
165 crazy.userMap.insert(make_pair(FIVE, 5));
166 crazy.xtructs.push_back(hello);
167
168 map<Numberz, Insanity> first_map;
169 map<Numberz, Insanity> second_map;
170
171 first_map.insert(make_pair(TWO, crazy));
172 first_map.insert(make_pair(THREE, crazy));
173
174 second_map.insert(make_pair(SIX, looney));
175
176 map<UserId, map<Numberz,Insanity> > insane;
177 insane.insert(make_pair(1, first_map));
178 insane.insert(make_pair(2, second_map));
179
180 printf("return");
181 printf(" = {");
182 map<UserId, map<Numberz,Insanity> >::const_iterator i_iter;
183 for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) {
Mark Slee6e536442006-06-30 18:28:50 +0000184 printf("%ld => {", i_iter->first);
Mark Sleee8540632006-05-30 09:24:40 +0000185 map<Numberz,Insanity>::const_iterator i2_iter;
186 for (i2_iter = i_iter->second.begin();
187 i2_iter != i_iter->second.end();
188 ++i2_iter) {
189 printf("%d => {", i2_iter->first);
190 map<Numberz, UserId> userMap = i2_iter->second.userMap;
191 map<Numberz, UserId>::const_iterator um;
192 printf("{");
193 for (um = userMap.begin(); um != userMap.end(); ++um) {
Mark Slee6e536442006-06-30 18:28:50 +0000194 printf("%d => %ld, ", um->first, um->second);
Mark Sleee8540632006-05-30 09:24:40 +0000195 }
196 printf("}, ");
197
198 list<Xtruct> xtructs = i2_iter->second.xtructs;
199 list<Xtruct>::const_iterator x;
200 printf("{");
201 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
Mark Slee6e536442006-06-30 18:28:50 +0000202 printf("{\"%s\", %d, %d, %ld}, ",
Mark Sleee8540632006-05-30 09:24:40 +0000203 x->string_thing.c_str(),
204 (int)x->byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000205 x->i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000206 x->i64_thing);
207 }
208 printf("}");
209
210 printf("}, ");
211 }
212 printf("}, ");
213 }
214 printf("}\n");
215
216 return insane;
217 }
Marc Slemkoe6889de2006-08-12 00:32:53 +0000218
219 Xtruct testMulti(uint8_t arg0, int32_t arg1, uint64_t arg2, std::map<int16_t, std::string> arg3, Numberz arg4, UserId arg5) {
220 printf("testMulti()\n");
221
222 Xtruct hello;
223 hello.string_thing = "Hello2";
224 hello.byte_thing = arg0;
225 hello.i32_thing = arg1;
226 hello.i64_thing = (int64_t)arg2;
227
228 return hello;
229 }
Mark Sleee8540632006-05-30 09:24:40 +0000230};
231
232int main(int argc, char **argv) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000233
Mark Sleee8540632006-05-30 09:24:40 +0000234 int port = 9090;
Marc Slemko6be374b2006-08-04 03:16:25 +0000235 string serverType = "simple";
236 string protocolType = "binary";
237 size_t workerCount = 4;
238
239 ostringstream usage;
240
241 usage <<
242 argv[0] << " [--port=<port number>] [--server-type=<server-type>] [--protocol-type=<protocol-type>] [--workers=<worker-count>]" << endl <<
243
244 "\t\tserver-type\t\ttype of server, \"simple-server\" or \"thread-pool\". Default is " << serverType << endl <<
245
246 "\t\tprotocol-type\t\ttype of protocol, \"binary\", \"ascii\", or \"xml\". Default is " << protocolType << endl <<
247
248 "\t\tworkers\t\tNumber of thread pools workers. Only valid for thread-pool server type. Default is " << workerCount << endl;
249
250 map<string, string> args;
251
252 for(int ix = 1; ix < argc; ix++) {
253
254 string arg(argv[ix]);
255
256 if(arg.compare(0,2, "--") == 0) {
257
258 size_t end = arg.find_first_of("=", 2);
259
260 if(end != string::npos) {
261 args[string(arg, 2, end - 2)] = string(arg, end + 1);
262 } else {
263 args[string(arg, 2, end - 2)] = "true";
264 }
265 ix++;
266 } else {
267 throw invalid_argument("Unexcepted command line token: "+arg);
268 }
Mark Sleee8540632006-05-30 09:24:40 +0000269 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000270
271 try {
272
273 if(!args["port"].empty()) {
274 port = atoi(args["port"].c_str());
275 }
276
277 if(!args["server-type"].empty()) {
278 serverType = args["server-type"];
279
280 if(serverType == "simple") {
281
282 } else if(serverType == "thread-pool") {
283
284 } else {
285
286 throw invalid_argument("Unknown server type "+serverType);
287 }
288 }
289
290 if(!args["protocol-type"].empty()) {
291 protocolType = args["protocol-type"];
292
293 if(protocolType == "binary") {
294 } else if(protocolType == "ascii") {
295 throw invalid_argument("ASCII protocol not supported");
296 } else if(protocolType == "xml") {
297 throw invalid_argument("XML protocol not supported");
298 } else {
299 throw invalid_argument("Unknown protocol type "+protocolType);
300 }
301 }
302
303 if(!args["workers"].empty()) {
304 workerCount = atoi(args["workers"].c_str());
305 }
306 } catch(exception& e) {
307 cerr << e.what() << endl;
308 cerr << usage;
309 }
310
Mark Sleee8540632006-05-30 09:24:40 +0000311 // Dispatcher
Marc Slemko6be374b2006-08-04 03:16:25 +0000312 shared_ptr<TBinaryProtocol> binaryProtocol(new TBinaryProtocol);
313
314 shared_ptr<TestServer> testServer(new TestServer(binaryProtocol));
Mark Sleee8540632006-05-30 09:24:40 +0000315
316 // Options
Marc Slemko6be374b2006-08-04 03:16:25 +0000317 shared_ptr<TServerOptions> serverOptions(new TServerOptions());
Mark Sleee8540632006-05-30 09:24:40 +0000318
319 // Transport
Marc Slemko6be374b2006-08-04 03:16:25 +0000320 shared_ptr<TServerSocket> serverSocket(new TServerSocket(port));
Mark Sleee8540632006-05-30 09:24:40 +0000321
Marc Slemko6be374b2006-08-04 03:16:25 +0000322 if(serverType == "simple") {
Mark Sleee8540632006-05-30 09:24:40 +0000323
Marc Slemko6be374b2006-08-04 03:16:25 +0000324 // Server
325 TSimpleServer simpleServer(testServer,
326 serverOptions,
327 serverSocket);
328
329 printf("Starting the server on port %d...\n", port);
330 simpleServer.run();
331
332 } else if(serverType == "thread-pool") {
333
334 shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workerCount);
335
336 shared_ptr<PosixThreadFactory> threadFactory = shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
337
338 threadManager->threadFactory(threadFactory);
339
340 threadManager->start();
341
342 TThreadPoolServer threadPoolServer(testServer,
343 serverOptions,
344 serverSocket,
345 threadManager);
346
347 printf("Starting the server on port %d...\n", port);
348 threadPoolServer.run();
349 }
350
Mark Sleee8540632006-05-30 09:24:40 +0000351 printf("done.\n");
352 return 0;
353}