]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/test/cpp/src/TestServer.cpp
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / thrift / test / cpp / src / TestServer.cpp
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 <thrift/async/TAsyncBufferProcessor.h>
21 #include <thrift/async/TAsyncProtocolProcessor.h>
22 #include <thrift/async/TEvhttpServer.h>
23 #include <thrift/concurrency/ThreadFactory.h>
24 #include <thrift/concurrency/ThreadManager.h>
25 #include <thrift/processor/TMultiplexedProcessor.h>
26 #include <thrift/protocol/TBinaryProtocol.h>
27 #include <thrift/protocol/TCompactProtocol.h>
28 #include <thrift/protocol/THeaderProtocol.h>
29 #include <thrift/protocol/TJSONProtocol.h>
30 #include <thrift/server/TNonblockingServer.h>
31 #include <thrift/server/TSimpleServer.h>
32 #include <thrift/server/TThreadPoolServer.h>
33 #include <thrift/server/TThreadedServer.h>
34 #include <thrift/transport/THttpServer.h>
35 #include <thrift/transport/THttpTransport.h>
36 #include <thrift/transport/TNonblockingSSLServerSocket.h>
37 #include <thrift/transport/TNonblockingServerSocket.h>
38 #include <thrift/transport/TSSLServerSocket.h>
39 #include <thrift/transport/TSSLSocket.h>
40 #include <thrift/transport/TServerSocket.h>
41 #include <thrift/transport/TTransportUtils.h>
42 #include <thrift/transport/TZlibTransport.h>
43
44 #include "SecondService.h"
45 #include "ThriftTest.h"
46
47 #ifdef HAVE_STDINT_H
48 #include <stdint.h>
49 #endif
50 #ifdef HAVE_INTTYPES_H
51 #include <inttypes.h>
52 #endif
53 #ifdef HAVE_SIGNAL_H
54 #include <signal.h>
55 #endif
56
57 #include <iostream>
58 #include <stdexcept>
59 #include <sstream>
60
61 #include <boost/algorithm/string.hpp>
62 #include <boost/program_options.hpp>
63 #include <boost/filesystem.hpp>
64
65 #if _WIN32
66 #include <thrift/windows/TWinsockSingleton.h>
67 #endif
68
69 using namespace std;
70
71 using namespace apache::thrift;
72 using namespace apache::thrift::async;
73 using namespace apache::thrift::concurrency;
74 using namespace apache::thrift::protocol;
75 using namespace apache::thrift::transport;
76 using namespace apache::thrift::server;
77
78 using namespace thrift::test;
79
80 // to handle a controlled shutdown, signal handling is mandatory
81 #ifdef HAVE_SIGNAL_H
82 apache::thrift::concurrency::Monitor gMonitor;
83 void signal_handler(int signum)
84 {
85 if (signum == SIGINT) {
86 gMonitor.notifyAll();
87 }
88 }
89 #endif
90
91 class TestHandler : public ThriftTestIf {
92 public:
93 TestHandler() = default;
94
95 void testVoid() override { printf("testVoid()\n"); }
96
97 void testString(string& out, const string& thing) override {
98 printf("testString(\"%s\")\n", thing.c_str());
99 out = thing;
100 }
101
102 bool testBool(const bool thing) override {
103 printf("testBool(%s)\n", thing ? "true" : "false");
104 return thing;
105 }
106
107 int8_t testByte(const int8_t thing) override {
108 printf("testByte(%d)\n", (int)thing);
109 return thing;
110 }
111
112 int32_t testI32(const int32_t thing) override {
113 printf("testI32(%d)\n", thing);
114 return thing;
115 }
116
117 int64_t testI64(const int64_t thing) override {
118 printf("testI64(%" PRId64 ")\n", thing);
119 return thing;
120 }
121
122 double testDouble(const double thing) override {
123 printf("testDouble(%f)\n", thing);
124 return thing;
125 }
126
127 void testBinary(std::string& _return, const std::string& thing) override {
128 std::ostringstream hexstr;
129 hexstr << std::hex << thing;
130 printf("testBinary(%lu: %s)\n", safe_numeric_cast<unsigned long>(thing.size()), hexstr.str().c_str());
131 _return = thing;
132 }
133
134 void testStruct(Xtruct& out, const Xtruct& thing) override {
135 printf("testStruct({\"%s\", %d, %d, %" PRId64 "})\n",
136 thing.string_thing.c_str(),
137 (int)thing.byte_thing,
138 thing.i32_thing,
139 thing.i64_thing);
140 out = thing;
141 }
142
143 void testNest(Xtruct2& out, const Xtruct2& nest) override {
144 const Xtruct& thing = nest.struct_thing;
145 printf("testNest({%d, {\"%s\", %d, %d, %" PRId64 "}, %d})\n",
146 (int)nest.byte_thing,
147 thing.string_thing.c_str(),
148 (int)thing.byte_thing,
149 thing.i32_thing,
150 thing.i64_thing,
151 nest.i32_thing);
152 out = nest;
153 }
154
155 void testMap(map<int32_t, int32_t>& out, const map<int32_t, int32_t>& thing) override {
156 printf("testMap({");
157 map<int32_t, int32_t>::const_iterator m_iter;
158 bool first = true;
159 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
160 if (first) {
161 first = false;
162 } else {
163 printf(", ");
164 }
165 printf("%d => %d", m_iter->first, m_iter->second);
166 }
167 printf("})\n");
168 out = thing;
169 }
170
171 void testStringMap(map<std::string, std::string>& out,
172 const map<std::string, std::string>& thing) override {
173 printf("testMap({");
174 map<std::string, std::string>::const_iterator m_iter;
175 bool first = true;
176 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
177 if (first) {
178 first = false;
179 } else {
180 printf(", ");
181 }
182 printf("%s => %s", (m_iter->first).c_str(), (m_iter->second).c_str());
183 }
184 printf("})\n");
185 out = thing;
186 }
187
188 void testSet(set<int32_t>& out, const set<int32_t>& thing) override {
189 printf("testSet({");
190 set<int32_t>::const_iterator s_iter;
191 bool first = true;
192 for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) {
193 if (first) {
194 first = false;
195 } else {
196 printf(", ");
197 }
198 printf("%d", *s_iter);
199 }
200 printf("})\n");
201 out = thing;
202 }
203
204 void testList(vector<int32_t>& out, const vector<int32_t>& thing) override {
205 printf("testList({");
206 vector<int32_t>::const_iterator l_iter;
207 bool first = true;
208 for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) {
209 if (first) {
210 first = false;
211 } else {
212 printf(", ");
213 }
214 printf("%d", *l_iter);
215 }
216 printf("})\n");
217 out = thing;
218 }
219
220 Numberz::type testEnum(const Numberz::type thing) override {
221 printf("testEnum(%d)\n", thing);
222 return thing;
223 }
224
225 UserId testTypedef(const UserId thing) override {
226 printf("testTypedef(%" PRId64 ")\n", thing);
227 return thing;
228 }
229
230 void testMapMap(map<int32_t, map<int32_t, int32_t> >& mapmap, const int32_t hello) override {
231 printf("testMapMap(%d)\n", hello);
232
233 map<int32_t, int32_t> pos;
234 map<int32_t, int32_t> neg;
235 for (int i = 1; i < 5; i++) {
236 pos.insert(make_pair(i, i));
237 neg.insert(make_pair(-i, -i));
238 }
239
240 mapmap.insert(make_pair(4, pos));
241 mapmap.insert(make_pair(-4, neg));
242 }
243
244 void testInsanity(map<UserId, map<Numberz::type, Insanity> >& insane, const Insanity& argument) override {
245 printf("testInsanity()\n");
246
247 Insanity looney;
248 map<Numberz::type, Insanity> first_map;
249 map<Numberz::type, Insanity> second_map;
250
251 first_map.insert(make_pair(Numberz::TWO, argument));
252 first_map.insert(make_pair(Numberz::THREE, argument));
253
254 second_map.insert(make_pair(Numberz::SIX, looney));
255
256 insane.insert(make_pair(1, first_map));
257 insane.insert(make_pair(2, second_map));
258
259 printf("return");
260 printf(" = {");
261 map<UserId, map<Numberz::type, Insanity> >::const_iterator i_iter;
262 for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) {
263 printf("%" PRId64 " => {", i_iter->first);
264 map<Numberz::type, Insanity>::const_iterator i2_iter;
265 for (i2_iter = i_iter->second.begin(); i2_iter != i_iter->second.end(); ++i2_iter) {
266 printf("%d => {", i2_iter->first);
267 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
268 map<Numberz::type, UserId>::const_iterator um;
269 printf("{");
270 for (um = userMap.begin(); um != userMap.end(); ++um) {
271 printf("%d => %" PRId64 ", ", um->first, um->second);
272 }
273 printf("}, ");
274
275 vector<Xtruct> xtructs = i2_iter->second.xtructs;
276 vector<Xtruct>::const_iterator x;
277 printf("{");
278 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
279 printf("{\"%s\", %d, %d, %" PRId64 "}, ",
280 x->string_thing.c_str(),
281 (int)x->byte_thing,
282 x->i32_thing,
283 x->i64_thing);
284 }
285 printf("}");
286
287 printf("}, ");
288 }
289 printf("}, ");
290 }
291 printf("}\n");
292 }
293
294 void testMulti(Xtruct& hello,
295 const int8_t arg0,
296 const int32_t arg1,
297 const int64_t arg2,
298 const std::map<int16_t, std::string>& arg3,
299 const Numberz::type arg4,
300 const UserId arg5) override {
301 (void)arg3;
302 (void)arg4;
303 (void)arg5;
304
305 printf("testMulti()\n");
306
307 hello.string_thing = "Hello2";
308 hello.byte_thing = arg0;
309 hello.i32_thing = arg1;
310 hello.i64_thing = (int64_t)arg2;
311 }
312
313 void testException(const std::string& arg) override {
314 printf("testException(%s)\n", arg.c_str());
315 if (arg.compare("Xception") == 0) {
316 Xception e;
317 e.errorCode = 1001;
318 e.message = arg;
319 throw e;
320 } else if (arg.compare("TException") == 0) {
321 apache::thrift::TException e;
322 throw e;
323 } else {
324 Xtruct result;
325 result.string_thing = arg;
326 return;
327 }
328 }
329
330 void testMultiException(Xtruct& result,
331 const std::string& arg0,
332 const std::string& arg1) override {
333
334 printf("testMultiException(%s, %s)\n", arg0.c_str(), arg1.c_str());
335
336 if (arg0.compare("Xception") == 0) {
337 Xception e;
338 e.errorCode = 1001;
339 e.message = "This is an Xception";
340 throw e;
341 } else if (arg0.compare("Xception2") == 0) {
342 Xception2 e;
343 e.errorCode = 2002;
344 e.struct_thing.string_thing = "This is an Xception2";
345 throw e;
346 } else {
347 result.string_thing = arg1;
348 return;
349 }
350 }
351
352 void testOneway(const int32_t aNum) override {
353 printf("testOneway(%d): call received\n", aNum);
354 }
355 };
356
357 class SecondHandler : public SecondServiceIf
358 {
359 public:
360 void secondtestString(std::string& result, const std::string& thing) override
361 { result = "testString(\"" + thing + "\")"; }
362 };
363
364 class TestProcessorEventHandler : public TProcessorEventHandler {
365 void* getContext(const char* fn_name, void* serverContext) override {
366 (void)serverContext;
367 return new std::string(fn_name);
368 }
369 void freeContext(void* ctx, const char* fn_name) override {
370 (void)fn_name;
371 delete static_cast<std::string*>(ctx);
372 }
373 void preRead(void* ctx, const char* fn_name) override { communicate("preRead", ctx, fn_name); }
374 void postRead(void* ctx, const char* fn_name, uint32_t bytes) override {
375 (void)bytes;
376 communicate("postRead", ctx, fn_name);
377 }
378 void preWrite(void* ctx, const char* fn_name) override { communicate("preWrite", ctx, fn_name); }
379 void postWrite(void* ctx, const char* fn_name, uint32_t bytes) override {
380 (void)bytes;
381 communicate("postWrite", ctx, fn_name);
382 }
383 void asyncComplete(void* ctx, const char* fn_name) override {
384 communicate("asyncComplete", ctx, fn_name);
385 }
386 void handlerError(void* ctx, const char* fn_name) override {
387 communicate("handlerError", ctx, fn_name);
388 }
389
390 void communicate(const char* event, void* ctx, const char* fn_name) {
391 std::cout << event << ": " << *static_cast<std::string*>(ctx) << " = " << fn_name << std::endl;
392 }
393 };
394
395 class TestHandlerAsync : public ThriftTestCobSvIf {
396 public:
397 TestHandlerAsync(std::shared_ptr<TestHandler>& handler) : _delegate(handler) {}
398 ~TestHandlerAsync() override = default;
399
400 void testVoid(std::function<void()> cob) override {
401 _delegate->testVoid();
402 cob();
403 }
404
405 void testString(std::function<void(std::string const& _return)> cob,
406 const std::string& thing) override {
407 std::string res;
408 _delegate->testString(res, thing);
409 cob(res);
410 }
411
412 void testBool(std::function<void(bool const& _return)> cob, const bool thing) override {
413 bool res = _delegate->testBool(thing);
414 cob(res);
415 }
416
417 void testByte(std::function<void(int8_t const& _return)> cob, const int8_t thing) override {
418 int8_t res = _delegate->testByte(thing);
419 cob(res);
420 }
421
422 void testI32(std::function<void(int32_t const& _return)> cob, const int32_t thing) override {
423 int32_t res = _delegate->testI32(thing);
424 cob(res);
425 }
426
427 void testI64(std::function<void(int64_t const& _return)> cob, const int64_t thing) override {
428 int64_t res = _delegate->testI64(thing);
429 cob(res);
430 }
431
432 void testDouble(std::function<void(double const& _return)> cob, const double thing) override {
433 double res = _delegate->testDouble(thing);
434 cob(res);
435 }
436
437 void testBinary(std::function<void(std::string const& _return)> cob,
438 const std::string& thing) override {
439 std::string res;
440 _delegate->testBinary(res, thing);
441 cob(res);
442 }
443
444 void testStruct(std::function<void(Xtruct const& _return)> cob, const Xtruct& thing) override {
445 Xtruct res;
446 _delegate->testStruct(res, thing);
447 cob(res);
448 }
449
450 void testNest(std::function<void(Xtruct2 const& _return)> cob, const Xtruct2& thing) override {
451 Xtruct2 res;
452 _delegate->testNest(res, thing);
453 cob(res);
454 }
455
456 void testMap(std::function<void(std::map<int32_t, int32_t> const& _return)> cob,
457 const std::map<int32_t, int32_t>& thing) override {
458 std::map<int32_t, int32_t> res;
459 _delegate->testMap(res, thing);
460 cob(res);
461 }
462
463 void testStringMap(
464 std::function<void(std::map<std::string, std::string> const& _return)> cob,
465 const std::map<std::string, std::string>& thing) override {
466 std::map<std::string, std::string> res;
467 _delegate->testStringMap(res, thing);
468 cob(res);
469 }
470
471 void testSet(std::function<void(std::set<int32_t> const& _return)> cob,
472 const std::set<int32_t>& thing) override {
473 std::set<int32_t> res;
474 _delegate->testSet(res, thing);
475 cob(res);
476 }
477
478 void testList(std::function<void(std::vector<int32_t> const& _return)> cob,
479 const std::vector<int32_t>& thing) override {
480 std::vector<int32_t> res;
481 _delegate->testList(res, thing);
482 cob(res);
483 }
484
485 void testEnum(std::function<void(Numberz::type const& _return)> cob,
486 const Numberz::type thing) override {
487 Numberz::type res = _delegate->testEnum(thing);
488 cob(res);
489 }
490
491 void testTypedef(std::function<void(UserId const& _return)> cob, const UserId thing) override {
492 UserId res = _delegate->testTypedef(thing);
493 cob(res);
494 }
495
496 void testMapMap(
497 std::function<void(std::map<int32_t, std::map<int32_t, int32_t> > const& _return)> cob,
498 const int32_t hello) override {
499 std::map<int32_t, std::map<int32_t, int32_t> > res;
500 _delegate->testMapMap(res, hello);
501 cob(res);
502 }
503
504 void testInsanity(
505 std::function<void(std::map<UserId, std::map<Numberz::type, Insanity> > const& _return)> cob,
506 const Insanity& argument) override {
507 std::map<UserId, std::map<Numberz::type, Insanity> > res;
508 _delegate->testInsanity(res, argument);
509 cob(res);
510 }
511
512 void testMulti(std::function<void(Xtruct const& _return)> cob,
513 const int8_t arg0,
514 const int32_t arg1,
515 const int64_t arg2,
516 const std::map<int16_t, std::string>& arg3,
517 const Numberz::type arg4,
518 const UserId arg5) override {
519 Xtruct res;
520 _delegate->testMulti(res, arg0, arg1, arg2, arg3, arg4, arg5);
521 cob(res);
522 }
523
524 void testException(
525 std::function<void()> cob,
526 std::function<void(::apache::thrift::TDelayedException* _throw)> exn_cob,
527 const std::string& arg) override {
528 try {
529 _delegate->testException(arg);
530 } catch (const apache::thrift::TException& e) {
531 exn_cob(apache::thrift::TDelayedException::delayException(e));
532 return;
533 }
534 cob();
535 }
536
537 void testMultiException(
538 std::function<void(Xtruct const& _return)> cob,
539 std::function<void(::apache::thrift::TDelayedException* _throw)> exn_cob,
540 const std::string& arg0,
541 const std::string& arg1) override {
542 Xtruct res;
543 try {
544 _delegate->testMultiException(res, arg0, arg1);
545 } catch (const apache::thrift::TException& e) {
546 exn_cob(apache::thrift::TDelayedException::delayException(e));
547 return;
548 }
549 cob(res);
550 }
551
552 void testOneway(std::function<void()> cob, const int32_t secondsToSleep) override {
553 _delegate->testOneway(secondsToSleep);
554 cob();
555 }
556
557 protected:
558 std::shared_ptr<TestHandler> _delegate;
559 };
560
561 namespace po = boost::program_options;
562
563 int main(int argc, char** argv) {
564
565 string testDir = boost::filesystem::system_complete(argv[0]).parent_path().parent_path().parent_path().string();
566 string certPath = testDir + "/keys/server.crt";
567 string keyPath = testDir + "/keys/server.key";
568
569 #if _WIN32
570 transport::TWinsockSingleton::create();
571 #endif
572 int port = 9090;
573 bool ssl = false;
574 bool zlib = false;
575 string transport_type = "buffered";
576 string protocol_type = "binary";
577 string server_type = "simple";
578 string domain_socket = "";
579 bool abstract_namespace = false;
580 size_t workers = 4;
581 int string_limit = 0;
582 int container_limit = 0;
583
584 po::options_description desc("Allowed options");
585 desc.add_options()
586 ("help,h", "produce help message")
587 ("port", po::value<int>(&port)->default_value(port), "Port number to listen")
588 ("domain-socket", po::value<string>(&domain_socket) ->default_value(domain_socket), "Unix Domain Socket (e.g. /tmp/ThriftTest.thrift)")
589 ("abstract-namespace", "Create the domain socket in the Abstract Namespace (no connection with filesystem pathnames)")
590 ("server-type", po::value<string>(&server_type)->default_value(server_type), "type of server, \"simple\", \"thread-pool\", \"threaded\", or \"nonblocking\"")
591 ("transport", po::value<string>(&transport_type)->default_value(transport_type), "transport: buffered, framed, http, zlib")
592 ("protocol", po::value<string>(&protocol_type)->default_value(protocol_type), "protocol: binary, compact, header, json, multi, multic, multih, multij")
593 ("ssl", "Encrypted Transport using SSL")
594 ("zlib", "Wrapped Transport using Zlib")
595 ("processor-events", "processor-events")
596 ("workers,n", po::value<size_t>(&workers)->default_value(workers), "Number of thread pools workers. Only valid for thread-pool server type")
597 ("string-limit", po::value<int>(&string_limit))
598 ("container-limit", po::value<int>(&container_limit));
599
600 po::variables_map vm;
601 po::store(po::parse_command_line(argc, argv, desc), vm);
602 po::notify(vm);
603
604 if (vm.count("help")) {
605 cout << desc << "\n";
606 return 1;
607 }
608
609 try {
610 if (!server_type.empty()) {
611 if (server_type == "simple") {
612 } else if (server_type == "thread-pool") {
613 } else if (server_type == "threaded") {
614 } else if (server_type == "nonblocking") {
615 } else {
616 throw invalid_argument("Unknown server type " + server_type);
617 }
618 }
619
620 if (!protocol_type.empty()) {
621 if (protocol_type == "binary") {
622 } else if (protocol_type == "compact") {
623 } else if (protocol_type == "json") {
624 } else if (protocol_type == "header") {
625 } else if (protocol_type == "multi") { // multiplexed binary
626 } else if (protocol_type == "multic") { // multiplexed compact
627 } else if (protocol_type == "multih") { // multiplexed header
628 } else if (protocol_type == "multij") { // multiplexed json
629 } else {
630 throw invalid_argument("Unknown protocol type " + protocol_type);
631 }
632 }
633
634 if (!transport_type.empty()) {
635 if (transport_type == "buffered") {
636 } else if (transport_type == "framed") {
637 } else if (transport_type == "http") {
638 } else if (transport_type == "zlib") {
639 // crosstester will pass zlib as a flag and a transport right now...
640 } else {
641 throw invalid_argument("Unknown transport type " + transport_type);
642 }
643 }
644
645 } catch (std::exception& e) {
646 cerr << e.what() << endl;
647 cout << desc << "\n";
648 return 1;
649 }
650
651 if (vm.count("ssl")) {
652 ssl = true;
653 }
654
655 if (vm.count("zlib")) {
656 zlib = true;
657 }
658
659 #if defined(HAVE_SIGNAL_H) && defined(SIGPIPE)
660 if (ssl) {
661 signal(SIGPIPE, SIG_IGN); // for OpenSSL, otherwise we end abruptly
662 }
663 #endif
664
665 if (vm.count("abstract-namespace")) {
666 abstract_namespace = true;
667 }
668
669 // Dispatcher
670 std::shared_ptr<TProtocolFactory> protocolFactory;
671 if (protocol_type == "json" || protocol_type == "multij") {
672 std::shared_ptr<TProtocolFactory> jsonProtocolFactory(new TJSONProtocolFactory());
673 protocolFactory = jsonProtocolFactory;
674 } else if (protocol_type == "compact" || protocol_type == "multic") {
675 auto *compactProtocolFactory = new TCompactProtocolFactoryT<TBufferBase>();
676 compactProtocolFactory->setContainerSizeLimit(container_limit);
677 compactProtocolFactory->setStringSizeLimit(string_limit);
678 protocolFactory.reset(compactProtocolFactory);
679 } else if (protocol_type == "header" || protocol_type == "multih") {
680 std::shared_ptr<TProtocolFactory> headerProtocolFactory(new THeaderProtocolFactory());
681 protocolFactory = headerProtocolFactory;
682 } else {
683 auto* binaryProtocolFactory = new TBinaryProtocolFactoryT<TBufferBase>();
684 binaryProtocolFactory->setContainerSizeLimit(container_limit);
685 binaryProtocolFactory->setStringSizeLimit(string_limit);
686 protocolFactory.reset(binaryProtocolFactory);
687 }
688
689 // Processors
690 std::shared_ptr<TestHandler> testHandler(new TestHandler());
691 std::shared_ptr<TProcessor> testProcessor(new ThriftTestProcessor(testHandler));
692
693 if (vm.count("processor-events")) {
694 testProcessor->setEventHandler(
695 std::shared_ptr<TProcessorEventHandler>(new TestProcessorEventHandler()));
696 }
697
698 // Transport
699 std::shared_ptr<TSSLSocketFactory> sslSocketFactory;
700 std::shared_ptr<TServerSocket> serverSocket;
701
702 if (ssl) {
703 sslSocketFactory = std::shared_ptr<TSSLSocketFactory>(new TSSLSocketFactory());
704 sslSocketFactory->loadCertificate(certPath.c_str());
705 sslSocketFactory->loadPrivateKey(keyPath.c_str());
706 sslSocketFactory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
707 if (server_type != "nonblocking") {
708 serverSocket = std::shared_ptr<TServerSocket>(new TSSLServerSocket(port, sslSocketFactory));
709 }
710 } else {
711 if (domain_socket != "") {
712 if (abstract_namespace) {
713 std::string abstract_socket("\0", 1);
714 abstract_socket += domain_socket;
715 serverSocket = std::shared_ptr<TServerSocket>(new TServerSocket(abstract_socket));
716 } else {
717 unlink(domain_socket.c_str());
718 serverSocket = std::shared_ptr<TServerSocket>(new TServerSocket(domain_socket));
719 }
720 port = 0;
721 } else {
722 serverSocket = std::shared_ptr<TServerSocket>(new TServerSocket(port));
723 }
724 }
725
726 // Factory
727 std::shared_ptr<TTransportFactory> transportFactory;
728
729 if (transport_type == "http" && server_type != "nonblocking") {
730 transportFactory = std::make_shared<THttpServerTransportFactory>();
731 } else if (transport_type == "framed") {
732 transportFactory = std::make_shared<TFramedTransportFactory>();
733 } else {
734 transportFactory = std::make_shared<TBufferedTransportFactory>();
735 }
736
737 if (zlib) {
738 // hmm.. doesn't seem to be a way to make it wrap the others...
739 transportFactory = std::make_shared<TZlibTransportFactory>();
740 }
741
742 // Server Info
743 cout << "Starting \"" << server_type << "\" server (" << transport_type << "/" << protocol_type
744 << ") listen on: ";
745 if (abstract_namespace) {
746 cout << '@';
747 }
748 cout << domain_socket;
749 if (port != 0) {
750 cout << port;
751 }
752 cout << endl;
753
754 // Multiplexed Processor if needed
755 if (boost::starts_with(protocol_type, "multi")) {
756 std::shared_ptr<SecondHandler> secondHandler(new SecondHandler());
757 std::shared_ptr<SecondServiceProcessor> secondProcessor(new SecondServiceProcessor(secondHandler));
758
759 std::shared_ptr<TMultiplexedProcessor> multiplexedProcessor(new TMultiplexedProcessor());
760 multiplexedProcessor->registerDefault(testProcessor); // non-multi clients go to the default processor (multi:binary, multic:compact, ...)
761 multiplexedProcessor->registerProcessor("ThriftTest", testProcessor);
762 multiplexedProcessor->registerProcessor("SecondService", secondProcessor);
763 testProcessor = std::dynamic_pointer_cast<TProcessor>(multiplexedProcessor);
764 }
765
766 // Server
767 std::shared_ptr<apache::thrift::server::TServer> server;
768
769 if (server_type == "simple") {
770 server.reset(new TSimpleServer(testProcessor, serverSocket, transportFactory, protocolFactory));
771 } else if (server_type == "thread-pool") {
772
773 std::shared_ptr<ThreadFactory> threadFactory
774 = std::shared_ptr<ThreadFactory>(new ThreadFactory());
775
776 std::shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workers);
777 threadManager->threadFactory(threadFactory);
778 threadManager->start();
779
780 server.reset(new TThreadPoolServer(testProcessor,
781 serverSocket,
782 transportFactory,
783 protocolFactory,
784 threadManager));
785 } else if (server_type == "threaded") {
786 server.reset(
787 new TThreadedServer(testProcessor, serverSocket, transportFactory, protocolFactory));
788 } else if (server_type == "nonblocking") {
789 if (transport_type == "http") {
790 std::shared_ptr<TestHandlerAsync> testHandlerAsync(new TestHandlerAsync(testHandler));
791 std::shared_ptr<TAsyncProcessor> testProcessorAsync(
792 new ThriftTestAsyncProcessor(testHandlerAsync));
793 std::shared_ptr<TAsyncBufferProcessor> testBufferProcessor(
794 new TAsyncProtocolProcessor(testProcessorAsync, protocolFactory));
795
796 // not loading nonblockingServer into "server" because
797 // TEvhttpServer doesn't inherit from TServer, and doesn't
798 // provide a stop method.
799 TEvhttpServer nonblockingServer(testBufferProcessor, port);
800 nonblockingServer.serve();
801 } else if (transport_type == "framed") {
802 std::shared_ptr<transport::TNonblockingServerTransport> nbSocket;
803 nbSocket.reset(
804 ssl ? new transport::TNonblockingSSLServerSocket(port, sslSocketFactory)
805 : new transport::TNonblockingServerSocket(port));
806 server.reset(new TNonblockingServer(testProcessor, protocolFactory, nbSocket));
807 } else {
808 cerr << "server-type nonblocking requires transport of http or framed" << endl;
809 exit(1);
810 }
811 }
812
813 if (server.get() != nullptr) {
814 if (protocol_type == "header") {
815 // Tell the server to use the same protocol for input / output
816 // if using header
817 server->setOutputProtocolFactory(std::shared_ptr<TProtocolFactory>());
818 }
819
820 apache::thrift::concurrency::ThreadFactory factory;
821 factory.setDetached(false);
822 std::shared_ptr<apache::thrift::concurrency::Runnable> serverThreadRunner(server);
823 std::shared_ptr<apache::thrift::concurrency::Thread> thread
824 = factory.newThread(serverThreadRunner);
825
826 #ifdef HAVE_SIGNAL_H
827 signal(SIGINT, signal_handler);
828 #endif
829
830 thread->start();
831 gMonitor.waitForever(); // wait for a shutdown signal
832
833 #ifdef HAVE_SIGNAL_H
834 signal(SIGINT, SIG_DFL);
835 #endif
836
837 server->stop();
838 thread->join();
839 server.reset();
840 }
841
842 cout << "done." << endl;
843 return 0;
844 }
845