| 30 |
ashish |
1 |
/*
|
|
|
2 |
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
3 |
* or more contributor license agreements. See the NOTICE file
|
|
|
4 |
* distributed with this work for additional information
|
|
|
5 |
* regarding copyright ownership. The ASF licenses this file
|
|
|
6 |
* to you under the Apache License, Version 2.0 (the
|
|
|
7 |
* "License"); you may not use this file except in compliance
|
|
|
8 |
* with the License. You may obtain a copy of the License at
|
|
|
9 |
*
|
|
|
10 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
11 |
*
|
|
|
12 |
* Unless required by applicable law or agreed to in writing,
|
|
|
13 |
* software distributed under the License is distributed on an
|
|
|
14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
15 |
* KIND, either express or implied. See the License for the
|
|
|
16 |
* specific language governing permissions and limitations
|
|
|
17 |
* under the License.
|
|
|
18 |
*/
|
|
|
19 |
|
|
|
20 |
#include <concurrency/ThreadManager.h>
|
|
|
21 |
#include <concurrency/PosixThreadFactory.h>
|
|
|
22 |
#include <protocol/TBinaryProtocol.h>
|
|
|
23 |
#include <server/TSimpleServer.h>
|
|
|
24 |
#include <server/TThreadedServer.h>
|
|
|
25 |
#include <server/TThreadPoolServer.h>
|
|
|
26 |
#include <server/TNonblockingServer.h>
|
|
|
27 |
#include <transport/TServerSocket.h>
|
|
|
28 |
#include <transport/TTransportUtils.h>
|
|
|
29 |
#include "ThriftTest.h"
|
|
|
30 |
|
|
|
31 |
#include <iostream>
|
|
|
32 |
#include <stdexcept>
|
|
|
33 |
#include <sstream>
|
|
|
34 |
|
|
|
35 |
#define __STDC_FORMAT_MACROS
|
|
|
36 |
#include <inttypes.h>
|
|
|
37 |
|
|
|
38 |
using namespace std;
|
|
|
39 |
using namespace boost;
|
|
|
40 |
|
|
|
41 |
using namespace apache::thrift;
|
|
|
42 |
using namespace apache::thrift::concurrency;
|
|
|
43 |
using namespace apache::thrift::protocol;
|
|
|
44 |
using namespace apache::thrift::transport;
|
|
|
45 |
using namespace apache::thrift::server;
|
|
|
46 |
|
|
|
47 |
using namespace thrift::test;
|
|
|
48 |
|
|
|
49 |
class TestHandler : public ThriftTestIf {
|
|
|
50 |
public:
|
|
|
51 |
TestHandler() {}
|
|
|
52 |
|
|
|
53 |
void testVoid() {
|
|
|
54 |
printf("testVoid()\n");
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
void testString(string& out, const string &thing) {
|
|
|
58 |
printf("testString(\"%s\")\n", thing.c_str());
|
|
|
59 |
out = thing;
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
int8_t testByte(const int8_t thing) {
|
|
|
63 |
printf("testByte(%d)\n", (int)thing);
|
|
|
64 |
return thing;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
int32_t testI32(const int32_t thing) {
|
|
|
68 |
printf("testI32(%d)\n", thing);
|
|
|
69 |
return thing;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
int64_t testI64(const int64_t thing) {
|
|
|
73 |
printf("testI64(%"PRId64")\n", thing);
|
|
|
74 |
return thing;
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
double testDouble(const double thing) {
|
|
|
78 |
printf("testDouble(%lf)\n", thing);
|
|
|
79 |
return thing;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
void testStruct(Xtruct& out, const Xtruct &thing) {
|
|
|
83 |
printf("testStruct({\"%s\", %d, %d, %"PRId64"})\n", thing.string_thing.c_str(), (int)thing.byte_thing, thing.i32_thing, thing.i64_thing);
|
|
|
84 |
out = thing;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
void testNest(Xtruct2& out, const Xtruct2& nest) {
|
|
|
88 |
const Xtruct &thing = nest.struct_thing;
|
|
|
89 |
printf("testNest({%d, {\"%s\", %d, %d, %"PRId64"}, %d})\n", (int)nest.byte_thing, thing.string_thing.c_str(), (int)thing.byte_thing, thing.i32_thing, thing.i64_thing, nest.i32_thing);
|
|
|
90 |
out = nest;
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
void testMap(map<int32_t, int32_t> &out, const map<int32_t, int32_t> &thing) {
|
|
|
94 |
printf("testMap({");
|
|
|
95 |
map<int32_t, int32_t>::const_iterator m_iter;
|
|
|
96 |
bool first = true;
|
|
|
97 |
for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
|
|
|
98 |
if (first) {
|
|
|
99 |
first = false;
|
|
|
100 |
} else {
|
|
|
101 |
printf(", ");
|
|
|
102 |
}
|
|
|
103 |
printf("%d => %d", m_iter->first, m_iter->second);
|
|
|
104 |
}
|
|
|
105 |
printf("})\n");
|
|
|
106 |
out = thing;
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
void testSet(set<int32_t> &out, const set<int32_t> &thing) {
|
|
|
110 |
printf("testSet({");
|
|
|
111 |
set<int32_t>::const_iterator s_iter;
|
|
|
112 |
bool first = true;
|
|
|
113 |
for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) {
|
|
|
114 |
if (first) {
|
|
|
115 |
first = false;
|
|
|
116 |
} else {
|
|
|
117 |
printf(", ");
|
|
|
118 |
}
|
|
|
119 |
printf("%d", *s_iter);
|
|
|
120 |
}
|
|
|
121 |
printf("})\n");
|
|
|
122 |
out = thing;
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
void testList(vector<int32_t> &out, const vector<int32_t> &thing) {
|
|
|
126 |
printf("testList({");
|
|
|
127 |
vector<int32_t>::const_iterator l_iter;
|
|
|
128 |
bool first = true;
|
|
|
129 |
for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) {
|
|
|
130 |
if (first) {
|
|
|
131 |
first = false;
|
|
|
132 |
} else {
|
|
|
133 |
printf(", ");
|
|
|
134 |
}
|
|
|
135 |
printf("%d", *l_iter);
|
|
|
136 |
}
|
|
|
137 |
printf("})\n");
|
|
|
138 |
out = thing;
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
Numberz testEnum(const Numberz thing) {
|
|
|
142 |
printf("testEnum(%d)\n", thing);
|
|
|
143 |
return thing;
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
UserId testTypedef(const UserId thing) {
|
|
|
147 |
printf("testTypedef(%"PRId64")\n", thing);
|
|
|
148 |
return thing;
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
void testMapMap(map<int32_t, map<int32_t,int32_t> > &mapmap, const int32_t hello) {
|
|
|
152 |
printf("testMapMap(%d)\n", hello);
|
|
|
153 |
|
|
|
154 |
map<int32_t,int32_t> pos;
|
|
|
155 |
map<int32_t,int32_t> neg;
|
|
|
156 |
for (int i = 1; i < 5; i++) {
|
|
|
157 |
pos.insert(make_pair(i,i));
|
|
|
158 |
neg.insert(make_pair(-i,-i));
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
mapmap.insert(make_pair(4, pos));
|
|
|
162 |
mapmap.insert(make_pair(-4, neg));
|
|
|
163 |
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
void testInsanity(map<UserId, map<Numberz,Insanity> > &insane, const Insanity &argument) {
|
|
|
167 |
printf("testInsanity()\n");
|
|
|
168 |
|
|
|
169 |
Xtruct hello;
|
|
|
170 |
hello.string_thing = "Hello2";
|
|
|
171 |
hello.byte_thing = 2;
|
|
|
172 |
hello.i32_thing = 2;
|
|
|
173 |
hello.i64_thing = 2;
|
|
|
174 |
|
|
|
175 |
Xtruct goodbye;
|
|
|
176 |
goodbye.string_thing = "Goodbye4";
|
|
|
177 |
goodbye.byte_thing = 4;
|
|
|
178 |
goodbye.i32_thing = 4;
|
|
|
179 |
goodbye.i64_thing = 4;
|
|
|
180 |
|
|
|
181 |
Insanity crazy;
|
|
|
182 |
crazy.userMap.insert(make_pair(EIGHT, 8));
|
|
|
183 |
crazy.xtructs.push_back(goodbye);
|
|
|
184 |
|
|
|
185 |
Insanity looney;
|
|
|
186 |
crazy.userMap.insert(make_pair(FIVE, 5));
|
|
|
187 |
crazy.xtructs.push_back(hello);
|
|
|
188 |
|
|
|
189 |
map<Numberz, Insanity> first_map;
|
|
|
190 |
map<Numberz, Insanity> second_map;
|
|
|
191 |
|
|
|
192 |
first_map.insert(make_pair(TWO, crazy));
|
|
|
193 |
first_map.insert(make_pair(THREE, crazy));
|
|
|
194 |
|
|
|
195 |
second_map.insert(make_pair(SIX, looney));
|
|
|
196 |
|
|
|
197 |
insane.insert(make_pair(1, first_map));
|
|
|
198 |
insane.insert(make_pair(2, second_map));
|
|
|
199 |
|
|
|
200 |
printf("return");
|
|
|
201 |
printf(" = {");
|
|
|
202 |
map<UserId, map<Numberz,Insanity> >::const_iterator i_iter;
|
|
|
203 |
for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) {
|
|
|
204 |
printf("%"PRId64" => {", i_iter->first);
|
|
|
205 |
map<Numberz,Insanity>::const_iterator i2_iter;
|
|
|
206 |
for (i2_iter = i_iter->second.begin();
|
|
|
207 |
i2_iter != i_iter->second.end();
|
|
|
208 |
++i2_iter) {
|
|
|
209 |
printf("%d => {", i2_iter->first);
|
|
|
210 |
map<Numberz, UserId> userMap = i2_iter->second.userMap;
|
|
|
211 |
map<Numberz, UserId>::const_iterator um;
|
|
|
212 |
printf("{");
|
|
|
213 |
for (um = userMap.begin(); um != userMap.end(); ++um) {
|
|
|
214 |
printf("%d => %"PRId64", ", um->first, um->second);
|
|
|
215 |
}
|
|
|
216 |
printf("}, ");
|
|
|
217 |
|
|
|
218 |
vector<Xtruct> xtructs = i2_iter->second.xtructs;
|
|
|
219 |
vector<Xtruct>::const_iterator x;
|
|
|
220 |
printf("{");
|
|
|
221 |
for (x = xtructs.begin(); x != xtructs.end(); ++x) {
|
|
|
222 |
printf("{\"%s\", %d, %d, %"PRId64"}, ", x->string_thing.c_str(), (int)x->byte_thing, x->i32_thing, x->i64_thing);
|
|
|
223 |
}
|
|
|
224 |
printf("}");
|
|
|
225 |
|
|
|
226 |
printf("}, ");
|
|
|
227 |
}
|
|
|
228 |
printf("}, ");
|
|
|
229 |
}
|
|
|
230 |
printf("}\n");
|
|
|
231 |
|
|
|
232 |
|
|
|
233 |
}
|
|
|
234 |
|
|
|
235 |
void testMulti(Xtruct &hello, const int8_t arg0, const int32_t arg1, const int64_t arg2, const std::map<int16_t, std::string> &arg3, const Numberz arg4, const UserId arg5) {
|
|
|
236 |
printf("testMulti()\n");
|
|
|
237 |
|
|
|
238 |
hello.string_thing = "Hello2";
|
|
|
239 |
hello.byte_thing = arg0;
|
|
|
240 |
hello.i32_thing = arg1;
|
|
|
241 |
hello.i64_thing = (int64_t)arg2;
|
|
|
242 |
}
|
|
|
243 |
|
|
|
244 |
void testException(const std::string &arg)
|
|
|
245 |
throw(Xception, apache::thrift::TException)
|
|
|
246 |
{
|
|
|
247 |
printf("testException(%s)\n", arg.c_str());
|
|
|
248 |
if (arg.compare("Xception") == 0) {
|
|
|
249 |
Xception e;
|
|
|
250 |
e.errorCode = 1001;
|
|
|
251 |
e.message = arg;
|
|
|
252 |
throw e;
|
|
|
253 |
} else if (arg.compare("ApplicationException") == 0) {
|
|
|
254 |
apache::thrift::TException e;
|
|
|
255 |
throw e;
|
|
|
256 |
} else {
|
|
|
257 |
Xtruct result;
|
|
|
258 |
result.string_thing = arg;
|
|
|
259 |
return;
|
|
|
260 |
}
|
|
|
261 |
}
|
|
|
262 |
|
|
|
263 |
void testMultiException(Xtruct &result, const std::string &arg0, const std::string &arg1) throw(Xception, Xception2) {
|
|
|
264 |
|
|
|
265 |
printf("testMultiException(%s, %s)\n", arg0.c_str(), arg1.c_str());
|
|
|
266 |
|
|
|
267 |
if (arg0.compare("Xception") == 0) {
|
|
|
268 |
Xception e;
|
|
|
269 |
e.errorCode = 1001;
|
|
|
270 |
e.message = "This is an Xception";
|
|
|
271 |
throw e;
|
|
|
272 |
} else if (arg0.compare("Xception2") == 0) {
|
|
|
273 |
Xception2 e;
|
|
|
274 |
e.errorCode = 2002;
|
|
|
275 |
e.struct_thing.string_thing = "This is an Xception2";
|
|
|
276 |
throw e;
|
|
|
277 |
} else {
|
|
|
278 |
result.string_thing = arg1;
|
|
|
279 |
return;
|
|
|
280 |
}
|
|
|
281 |
}
|
|
|
282 |
|
|
|
283 |
void testOneway(int sleepFor) {
|
|
|
284 |
printf("testOneway(%d): Sleeping...\n", sleepFor);
|
|
|
285 |
sleep(sleepFor);
|
|
|
286 |
printf("testOneway(%d): done sleeping!\n", sleepFor);
|
|
|
287 |
}
|
|
|
288 |
};
|
|
|
289 |
|
|
|
290 |
int main(int argc, char **argv) {
|
|
|
291 |
|
|
|
292 |
int port = 9090;
|
|
|
293 |
string serverType = "simple";
|
|
|
294 |
string protocolType = "binary";
|
|
|
295 |
size_t workerCount = 4;
|
|
|
296 |
|
|
|
297 |
ostringstream usage;
|
|
|
298 |
|
|
|
299 |
usage <<
|
|
|
300 |
argv[0] << " [--port=<port number>] [--server-type=<server-type>] [--protocol-type=<protocol-type>] [--workers=<worker-count>]" << endl <<
|
|
|
301 |
|
|
|
302 |
"\t\tserver-type\t\ttype of server, \"simple\", \"thread-pool\", \"threaded\", or \"nonblocking\". Default is " << serverType << endl <<
|
|
|
303 |
|
|
|
304 |
"\t\tprotocol-type\t\ttype of protocol, \"binary\", \"ascii\", or \"xml\". Default is " << protocolType << endl <<
|
|
|
305 |
|
|
|
306 |
"\t\tworkers\t\tNumber of thread pools workers. Only valid for thread-pool server type. Default is " << workerCount << endl;
|
|
|
307 |
|
|
|
308 |
map<string, string> args;
|
|
|
309 |
|
|
|
310 |
for (int ix = 1; ix < argc; ix++) {
|
|
|
311 |
string arg(argv[ix]);
|
|
|
312 |
if (arg.compare(0,2, "--") == 0) {
|
|
|
313 |
size_t end = arg.find_first_of("=", 2);
|
|
|
314 |
if (end != string::npos) {
|
|
|
315 |
args[string(arg, 2, end - 2)] = string(arg, end + 1);
|
|
|
316 |
} else {
|
|
|
317 |
args[string(arg, 2)] = "true";
|
|
|
318 |
}
|
|
|
319 |
} else {
|
|
|
320 |
throw invalid_argument("Unexcepted command line token: "+arg);
|
|
|
321 |
}
|
|
|
322 |
}
|
|
|
323 |
|
|
|
324 |
try {
|
|
|
325 |
|
|
|
326 |
if (!args["port"].empty()) {
|
|
|
327 |
port = atoi(args["port"].c_str());
|
|
|
328 |
}
|
|
|
329 |
|
|
|
330 |
if (!args["server-type"].empty()) {
|
|
|
331 |
serverType = args["server-type"];
|
|
|
332 |
if (serverType == "simple") {
|
|
|
333 |
} else if (serverType == "thread-pool") {
|
|
|
334 |
} else if (serverType == "threaded") {
|
|
|
335 |
} else if (serverType == "nonblocking") {
|
|
|
336 |
} else {
|
|
|
337 |
throw invalid_argument("Unknown server type "+serverType);
|
|
|
338 |
}
|
|
|
339 |
}
|
|
|
340 |
|
|
|
341 |
if (!args["protocol-type"].empty()) {
|
|
|
342 |
protocolType = args["protocol-type"];
|
|
|
343 |
if (protocolType == "binary") {
|
|
|
344 |
} else if (protocolType == "ascii") {
|
|
|
345 |
throw invalid_argument("ASCII protocol not supported");
|
|
|
346 |
} else if (protocolType == "xml") {
|
|
|
347 |
throw invalid_argument("XML protocol not supported");
|
|
|
348 |
} else {
|
|
|
349 |
throw invalid_argument("Unknown protocol type "+protocolType);
|
|
|
350 |
}
|
|
|
351 |
}
|
|
|
352 |
|
|
|
353 |
if (!args["workers"].empty()) {
|
|
|
354 |
workerCount = atoi(args["workers"].c_str());
|
|
|
355 |
}
|
|
|
356 |
} catch (exception& e) {
|
|
|
357 |
cerr << e.what() << endl;
|
|
|
358 |
cerr << usage;
|
|
|
359 |
}
|
|
|
360 |
|
|
|
361 |
// Dispatcher
|
|
|
362 |
shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
|
|
|
363 |
|
|
|
364 |
shared_ptr<TestHandler> testHandler(new TestHandler());
|
|
|
365 |
|
|
|
366 |
shared_ptr<ThriftTestProcessor> testProcessor(new ThriftTestProcessor(testHandler));
|
|
|
367 |
|
|
|
368 |
// Transport
|
|
|
369 |
shared_ptr<TServerSocket> serverSocket(new TServerSocket(port));
|
|
|
370 |
|
|
|
371 |
// Factory
|
|
|
372 |
shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
|
|
|
373 |
|
|
|
374 |
if (serverType == "simple") {
|
|
|
375 |
|
|
|
376 |
// Server
|
|
|
377 |
TSimpleServer simpleServer(testProcessor,
|
|
|
378 |
serverSocket,
|
|
|
379 |
transportFactory,
|
|
|
380 |
protocolFactory);
|
|
|
381 |
|
|
|
382 |
printf("Starting the server on port %d...\n", port);
|
|
|
383 |
simpleServer.serve();
|
|
|
384 |
|
|
|
385 |
} else if (serverType == "thread-pool") {
|
|
|
386 |
|
|
|
387 |
shared_ptr<ThreadManager> threadManager =
|
|
|
388 |
ThreadManager::newSimpleThreadManager(workerCount);
|
|
|
389 |
|
|
|
390 |
shared_ptr<PosixThreadFactory> threadFactory =
|
|
|
391 |
shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
|
|
|
392 |
|
|
|
393 |
threadManager->threadFactory(threadFactory);
|
|
|
394 |
|
|
|
395 |
threadManager->start();
|
|
|
396 |
|
|
|
397 |
TThreadPoolServer threadPoolServer(testProcessor,
|
|
|
398 |
serverSocket,
|
|
|
399 |
transportFactory,
|
|
|
400 |
protocolFactory,
|
|
|
401 |
threadManager);
|
|
|
402 |
|
|
|
403 |
printf("Starting the server on port %d...\n", port);
|
|
|
404 |
threadPoolServer.serve();
|
|
|
405 |
|
|
|
406 |
} else if (serverType == "threaded") {
|
|
|
407 |
|
|
|
408 |
TThreadedServer threadedServer(testProcessor,
|
|
|
409 |
serverSocket,
|
|
|
410 |
transportFactory,
|
|
|
411 |
protocolFactory);
|
|
|
412 |
|
|
|
413 |
printf("Starting the server on port %d...\n", port);
|
|
|
414 |
threadedServer.serve();
|
|
|
415 |
|
|
|
416 |
} else if (serverType == "nonblocking") {
|
|
|
417 |
TNonblockingServer nonblockingServer(testProcessor, port);
|
|
|
418 |
printf("Starting the nonblocking server on port %d...\n", port);
|
|
|
419 |
nonblockingServer.serve();
|
|
|
420 |
}
|
|
|
421 |
|
|
|
422 |
printf("done.\n");
|
|
|
423 |
return 0;
|
|
|
424 |
}
|