blob: eb95720bb03b86042f308d4f391d774abe830988 [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 }
Mark Sleee8540632006-05-30 09:24:40 +0000218};
219
220int main(int argc, char **argv) {
Marc Slemko6be374b2006-08-04 03:16:25 +0000221
Mark Sleee8540632006-05-30 09:24:40 +0000222 int port = 9090;
Marc Slemko6be374b2006-08-04 03:16:25 +0000223 string serverType = "simple";
224 string protocolType = "binary";
225 size_t workerCount = 4;
226
227 ostringstream usage;
228
229 usage <<
230 argv[0] << " [--port=<port number>] [--server-type=<server-type>] [--protocol-type=<protocol-type>] [--workers=<worker-count>]" << endl <<
231
232 "\t\tserver-type\t\ttype of server, \"simple-server\" or \"thread-pool\". Default is " << serverType << endl <<
233
234 "\t\tprotocol-type\t\ttype of protocol, \"binary\", \"ascii\", or \"xml\". Default is " << protocolType << endl <<
235
236 "\t\tworkers\t\tNumber of thread pools workers. Only valid for thread-pool server type. Default is " << workerCount << endl;
237
238 map<string, string> args;
239
240 for(int ix = 1; ix < argc; ix++) {
241
242 string arg(argv[ix]);
243
244 if(arg.compare(0,2, "--") == 0) {
245
246 size_t end = arg.find_first_of("=", 2);
247
248 if(end != string::npos) {
249 args[string(arg, 2, end - 2)] = string(arg, end + 1);
250 } else {
251 args[string(arg, 2, end - 2)] = "true";
252 }
253 ix++;
254 } else {
255 throw invalid_argument("Unexcepted command line token: "+arg);
256 }
Mark Sleee8540632006-05-30 09:24:40 +0000257 }
Marc Slemko6be374b2006-08-04 03:16:25 +0000258
259 try {
260
261 if(!args["port"].empty()) {
262 port = atoi(args["port"].c_str());
263 }
264
265 if(!args["server-type"].empty()) {
266 serverType = args["server-type"];
267
268 if(serverType == "simple") {
269
270 } else if(serverType == "thread-pool") {
271
272 } else {
273
274 throw invalid_argument("Unknown server type "+serverType);
275 }
276 }
277
278 if(!args["protocol-type"].empty()) {
279 protocolType = args["protocol-type"];
280
281 if(protocolType == "binary") {
282 } else if(protocolType == "ascii") {
283 throw invalid_argument("ASCII protocol not supported");
284 } else if(protocolType == "xml") {
285 throw invalid_argument("XML protocol not supported");
286 } else {
287 throw invalid_argument("Unknown protocol type "+protocolType);
288 }
289 }
290
291 if(!args["workers"].empty()) {
292 workerCount = atoi(args["workers"].c_str());
293 }
294 } catch(exception& e) {
295 cerr << e.what() << endl;
296 cerr << usage;
297 }
298
Mark Sleee8540632006-05-30 09:24:40 +0000299 // Dispatcher
Marc Slemko6be374b2006-08-04 03:16:25 +0000300 shared_ptr<TBinaryProtocol> binaryProtocol(new TBinaryProtocol);
301
302 shared_ptr<TestServer> testServer(new TestServer(binaryProtocol));
Mark Sleee8540632006-05-30 09:24:40 +0000303
304 // Options
Marc Slemko6be374b2006-08-04 03:16:25 +0000305 shared_ptr<TServerOptions> serverOptions(new TServerOptions());
Mark Sleee8540632006-05-30 09:24:40 +0000306
307 // Transport
Marc Slemko6be374b2006-08-04 03:16:25 +0000308 shared_ptr<TServerSocket> serverSocket(new TServerSocket(port));
Mark Sleee8540632006-05-30 09:24:40 +0000309
Marc Slemko6be374b2006-08-04 03:16:25 +0000310 if(serverType == "simple") {
Mark Sleee8540632006-05-30 09:24:40 +0000311
Marc Slemko6be374b2006-08-04 03:16:25 +0000312 // Server
313 TSimpleServer simpleServer(testServer,
314 serverOptions,
315 serverSocket);
316
317 printf("Starting the server on port %d...\n", port);
318 simpleServer.run();
319
320 } else if(serverType == "thread-pool") {
321
322 shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workerCount);
323
324 shared_ptr<PosixThreadFactory> threadFactory = shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
325
326 threadManager->threadFactory(threadFactory);
327
328 threadManager->start();
329
330 TThreadPoolServer threadPoolServer(testServer,
331 serverOptions,
332 serverSocket,
333 threadManager);
334
335 printf("Starting the server on port %d...\n", port);
336 threadPoolServer.run();
337 }
338
Mark Sleee8540632006-05-30 09:24:40 +0000339 printf("done.\n");
340 return 0;
341}