David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 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 | |
Roger Meier | d3b9dca | 2011-06-24 14:01:10 +0000 | [diff] [blame] | 20 | #define __STDC_FORMAT_MACROS |
| 21 | #include <inttypes.h> |
| 22 | |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 23 | #include <thrift/concurrency/ThreadManager.h> |
| 24 | #include <thrift/concurrency/PlatformThreadFactory.h> |
| 25 | #include <thrift/protocol/TBinaryProtocol.h> |
Roger Meier | 023192f | 2014-02-12 09:35:12 +0100 | [diff] [blame] | 26 | #include <thrift/protocol/TCompactProtocol.h> |
Dave Watson | 792db4e | 2015-01-16 11:22:01 -0800 | [diff] [blame] | 27 | #include <thrift/protocol/THeaderProtocol.h> |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 28 | #include <thrift/protocol/TJSONProtocol.h> |
| 29 | #include <thrift/server/TSimpleServer.h> |
| 30 | #include <thrift/server/TThreadedServer.h> |
| 31 | #include <thrift/server/TThreadPoolServer.h> |
| 32 | #include <thrift/async/TEvhttpServer.h> |
| 33 | #include <thrift/async/TAsyncBufferProcessor.h> |
| 34 | #include <thrift/async/TAsyncProtocolProcessor.h> |
| 35 | #include <thrift/server/TNonblockingServer.h> |
| 36 | #include <thrift/transport/TServerSocket.h> |
| 37 | #include <thrift/transport/TSSLServerSocket.h> |
| 38 | #include <thrift/transport/TSSLSocket.h> |
| 39 | #include <thrift/transport/THttpServer.h> |
| 40 | #include <thrift/transport/THttpTransport.h> |
| 41 | #include <thrift/transport/TTransportUtils.h> |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 42 | #include "ThriftTest.h" |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 43 | |
| 44 | #include <iostream> |
| 45 | #include <stdexcept> |
| 46 | #include <sstream> |
| 47 | |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 48 | #include <boost/program_options.hpp> |
cdwijayarathna | a07ec0b | 2014-08-09 17:45:56 +0530 | [diff] [blame] | 49 | #include <boost/filesystem.hpp> |
Roger Meier | 0a7c69c | 2014-05-02 21:15:45 +0200 | [diff] [blame] | 50 | #include <thrift/cxxfunctional.h> |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 51 | |
Bryan Duxbury | cd9aea1 | 2011-02-22 18:12:06 +0000 | [diff] [blame] | 52 | #include <signal.h> |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 53 | #if _WIN32 |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 54 | #include <thrift/windows/TWinsockSingleton.h> |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 55 | #endif |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 56 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 57 | using namespace std; |
| 58 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 59 | using namespace apache::thrift; |
| 60 | using namespace apache::thrift::concurrency; |
| 61 | using namespace apache::thrift::protocol; |
| 62 | using namespace apache::thrift::transport; |
| 63 | using namespace apache::thrift::server; |
Roger Meier | 7e056e7 | 2011-07-17 07:28:28 +0000 | [diff] [blame] | 64 | using namespace apache::thrift::async; |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 65 | |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 66 | using namespace thrift::test; |
| 67 | |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 68 | class TestHandler : public ThriftTestIf { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 69 | public: |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 70 | TestHandler() {} |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 71 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 72 | void testVoid() { printf("testVoid()\n"); } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 73 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 74 | void testString(string& out, const string& thing) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 75 | printf("testString(\"%s\")\n", thing.c_str()); |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 76 | out = thing; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Nobuaki Sukegawa | a649e74 | 2015-09-21 13:53:25 +0900 | [diff] [blame] | 79 | bool testBool(const bool thing) { |
| 80 | printf("testBool(%s)\n", thing ? "true" : "false"); |
| 81 | return thing; |
| 82 | } |
| 83 | |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 84 | int8_t testByte(const int8_t thing) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 85 | printf("testByte(%d)\n", (int)thing); |
| 86 | return thing; |
| 87 | } |
| 88 | |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 89 | int32_t testI32(const int32_t thing) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 90 | printf("testI32(%d)\n", thing); |
| 91 | return thing; |
| 92 | } |
| 93 | |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 94 | int64_t testI64(const int64_t thing) { |
Roger Meier | 0e81480 | 2014-01-17 21:07:58 +0100 | [diff] [blame] | 95 | printf("testI64(%" PRId64 ")\n", thing); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 96 | return thing; |
| 97 | } |
| 98 | |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 99 | double testDouble(const double thing) { |
Roger Meier | a8cef6e | 2011-07-17 18:55:59 +0000 | [diff] [blame] | 100 | printf("testDouble(%f)\n", thing); |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 101 | return thing; |
| 102 | } |
| 103 | |
Jens Geyer | 8bcfdd9 | 2014-12-14 03:14:26 +0100 | [diff] [blame] | 104 | void testBinary(std::string& _return, const std::string& thing) { |
| 105 | std::ostringstream hexstr; |
| 106 | hexstr << std::hex << thing; |
| 107 | printf("testBinary(%s)\n", hexstr.str().c_str()); |
| 108 | _return = thing; |
| 109 | } |
| 110 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 111 | void testStruct(Xtruct& out, const Xtruct& thing) { |
| 112 | printf("testStruct({\"%s\", %d, %d, %" PRId64 "})\n", |
| 113 | thing.string_thing.c_str(), |
| 114 | (int)thing.byte_thing, |
| 115 | thing.i32_thing, |
| 116 | thing.i64_thing); |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 117 | out = thing; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 120 | void testNest(Xtruct2& out, const Xtruct2& nest) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 121 | const Xtruct& thing = nest.struct_thing; |
| 122 | printf("testNest({%d, {\"%s\", %d, %d, %" PRId64 "}, %d})\n", |
| 123 | (int)nest.byte_thing, |
| 124 | thing.string_thing.c_str(), |
| 125 | (int)thing.byte_thing, |
| 126 | thing.i32_thing, |
| 127 | thing.i64_thing, |
| 128 | nest.i32_thing); |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 129 | out = nest; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 132 | void testMap(map<int32_t, int32_t>& out, const map<int32_t, int32_t>& thing) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 133 | printf("testMap({"); |
| 134 | map<int32_t, int32_t>::const_iterator m_iter; |
| 135 | bool first = true; |
| 136 | for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) { |
| 137 | if (first) { |
| 138 | first = false; |
| 139 | } else { |
| 140 | printf(", "); |
| 141 | } |
| 142 | printf("%d => %d", m_iter->first, m_iter->second); |
| 143 | } |
| 144 | printf("})\n"); |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 145 | out = thing; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 148 | void testStringMap(map<std::string, std::string>& out, |
| 149 | const map<std::string, std::string>& thing) { |
Roger Meier | d3b9dca | 2011-06-24 14:01:10 +0000 | [diff] [blame] | 150 | printf("testMap({"); |
| 151 | map<std::string, std::string>::const_iterator m_iter; |
| 152 | bool first = true; |
| 153 | for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) { |
| 154 | if (first) { |
| 155 | first = false; |
| 156 | } else { |
| 157 | printf(", "); |
| 158 | } |
| 159 | printf("%s => %s", (m_iter->first).c_str(), (m_iter->second).c_str()); |
| 160 | } |
| 161 | printf("})\n"); |
| 162 | out = thing; |
| 163 | } |
| 164 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 165 | void testSet(set<int32_t>& out, const set<int32_t>& thing) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 166 | printf("testSet({"); |
| 167 | set<int32_t>::const_iterator s_iter; |
| 168 | bool first = true; |
| 169 | for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) { |
| 170 | if (first) { |
| 171 | first = false; |
| 172 | } else { |
| 173 | printf(", "); |
| 174 | } |
| 175 | printf("%d", *s_iter); |
| 176 | } |
| 177 | printf("})\n"); |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 178 | out = thing; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 181 | void testList(vector<int32_t>& out, const vector<int32_t>& thing) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 182 | printf("testList({"); |
Mark Slee | b9acf98 | 2006-10-10 01:57:32 +0000 | [diff] [blame] | 183 | vector<int32_t>::const_iterator l_iter; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 184 | bool first = true; |
| 185 | for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) { |
| 186 | if (first) { |
| 187 | first = false; |
| 188 | } else { |
| 189 | printf(", "); |
| 190 | } |
| 191 | printf("%d", *l_iter); |
| 192 | } |
| 193 | printf("})\n"); |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 194 | out = thing; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 197 | Numberz::type testEnum(const Numberz::type thing) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 198 | printf("testEnum(%d)\n", thing); |
| 199 | return thing; |
| 200 | } |
| 201 | |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 202 | UserId testTypedef(const UserId thing) { |
Roger Meier | 0e81480 | 2014-01-17 21:07:58 +0100 | [diff] [blame] | 203 | printf("testTypedef(%" PRId64 ")\n", thing); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 204 | return thing; |
| 205 | } |
| 206 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 207 | void testMapMap(map<int32_t, map<int32_t, int32_t> >& mapmap, const int32_t hello) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 208 | printf("testMapMap(%d)\n", hello); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 209 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 210 | map<int32_t, int32_t> pos; |
| 211 | map<int32_t, int32_t> neg; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 212 | for (int i = 1; i < 5; i++) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 213 | pos.insert(make_pair(i, i)); |
| 214 | neg.insert(make_pair(-i, -i)); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | mapmap.insert(make_pair(4, pos)); |
| 218 | mapmap.insert(make_pair(-4, neg)); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 221 | void testInsanity(map<UserId, map<Numberz::type, Insanity> >& insane, const Insanity& argument) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 222 | printf("testInsanity()\n"); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 223 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 224 | Insanity looney; |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 225 | map<Numberz::type, Insanity> first_map; |
| 226 | map<Numberz::type, Insanity> second_map; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 227 | |
Nobuaki Sukegawa | 01ede04 | 2015-09-29 02:16:53 +0900 | [diff] [blame] | 228 | first_map.insert(make_pair(Numberz::TWO, argument)); |
| 229 | first_map.insert(make_pair(Numberz::THREE, argument)); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 230 | |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 231 | second_map.insert(make_pair(Numberz::SIX, looney)); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 232 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 233 | insane.insert(make_pair(1, first_map)); |
| 234 | insane.insert(make_pair(2, second_map)); |
| 235 | |
| 236 | printf("return"); |
| 237 | printf(" = {"); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 238 | map<UserId, map<Numberz::type, Insanity> >::const_iterator i_iter; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 239 | for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) { |
Roger Meier | 0e81480 | 2014-01-17 21:07:58 +0100 | [diff] [blame] | 240 | printf("%" PRId64 " => {", i_iter->first); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 241 | map<Numberz::type, Insanity>::const_iterator i2_iter; |
| 242 | for (i2_iter = i_iter->second.begin(); i2_iter != i_iter->second.end(); ++i2_iter) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 243 | printf("%d => {", i2_iter->first); |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 244 | map<Numberz::type, UserId> userMap = i2_iter->second.userMap; |
| 245 | map<Numberz::type, UserId>::const_iterator um; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 246 | printf("{"); |
| 247 | for (um = userMap.begin(); um != userMap.end(); ++um) { |
Roger Meier | 0e81480 | 2014-01-17 21:07:58 +0100 | [diff] [blame] | 248 | printf("%d => %" PRId64 ", ", um->first, um->second); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 249 | } |
| 250 | printf("}, "); |
| 251 | |
Mark Slee | b9acf98 | 2006-10-10 01:57:32 +0000 | [diff] [blame] | 252 | vector<Xtruct> xtructs = i2_iter->second.xtructs; |
| 253 | vector<Xtruct>::const_iterator x; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 254 | printf("{"); |
| 255 | for (x = xtructs.begin(); x != xtructs.end(); ++x) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 256 | printf("{\"%s\", %d, %d, %" PRId64 "}, ", |
| 257 | x->string_thing.c_str(), |
| 258 | (int)x->byte_thing, |
| 259 | x->i32_thing, |
| 260 | x->i64_thing); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 261 | } |
| 262 | printf("}"); |
| 263 | |
| 264 | printf("}, "); |
| 265 | } |
| 266 | printf("}, "); |
| 267 | } |
| 268 | printf("}\n"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 269 | } |
Marc Slemko | e6889de | 2006-08-12 00:32:53 +0000 | [diff] [blame] | 270 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 271 | void testMulti(Xtruct& hello, |
| 272 | const int8_t arg0, |
| 273 | const int32_t arg1, |
| 274 | const int64_t arg2, |
| 275 | const std::map<int16_t, std::string>& arg3, |
| 276 | const Numberz::type arg4, |
| 277 | const UserId arg5) { |
| 278 | (void)arg3; |
| 279 | (void)arg4; |
| 280 | (void)arg5; |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 281 | |
Marc Slemko | e6889de | 2006-08-12 00:32:53 +0000 | [diff] [blame] | 282 | printf("testMulti()\n"); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 283 | |
Marc Slemko | e6889de | 2006-08-12 00:32:53 +0000 | [diff] [blame] | 284 | hello.string_thing = "Hello2"; |
| 285 | hello.byte_thing = arg0; |
| 286 | hello.i32_thing = arg1; |
| 287 | hello.i64_thing = (int64_t)arg2; |
Marc Slemko | e6889de | 2006-08-12 00:32:53 +0000 | [diff] [blame] | 288 | } |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 289 | |
ben-craig | fae08e7 | 2015-07-15 11:34:47 -0500 | [diff] [blame] | 290 | void testException(const std::string& arg) { |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 291 | printf("testException(%s)\n", arg.c_str()); |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 292 | if (arg.compare("Xception") == 0) { |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 293 | Xception e; |
| 294 | e.errorCode = 1001; |
David Reiss | 9813cbe | 2009-01-31 21:39:11 +0000 | [diff] [blame] | 295 | e.message = arg; |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 296 | throw e; |
Roger Meier | 1f8b48f | 2012-05-02 22:56:47 +0000 | [diff] [blame] | 297 | } else if (arg.compare("TException") == 0) { |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 298 | apache::thrift::TException e; |
David Reiss | 55ff70f | 2008-06-11 00:58:25 +0000 | [diff] [blame] | 299 | throw e; |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 300 | } else { |
| 301 | Xtruct result; |
| 302 | result.string_thing = arg; |
| 303 | return; |
| 304 | } |
| 305 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 306 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 307 | void testMultiException(Xtruct& result, |
| 308 | const std::string& arg0, |
ben-craig | fae08e7 | 2015-07-15 11:34:47 -0500 | [diff] [blame] | 309 | const std::string& arg1) { |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 310 | |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 311 | printf("testMultiException(%s, %s)\n", arg0.c_str(), arg1.c_str()); |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 312 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 313 | if (arg0.compare("Xception") == 0) { |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 314 | Xception e; |
| 315 | e.errorCode = 1001; |
| 316 | e.message = "This is an Xception"; |
| 317 | throw e; |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 318 | } else if (arg0.compare("Xception2") == 0) { |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 319 | Xception2 e; |
| 320 | e.errorCode = 2002; |
| 321 | e.struct_thing.string_thing = "This is an Xception2"; |
| 322 | throw e; |
| 323 | } else { |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 324 | result.string_thing = arg1; |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 325 | return; |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 326 | } |
| 327 | } |
David Reiss | 2ab6fe8 | 2008-02-18 02:11:44 +0000 | [diff] [blame] | 328 | |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 329 | void testOneway(const int32_t sleepFor) { |
David Reiss | 6ce401d | 2009-03-24 20:01:58 +0000 | [diff] [blame] | 330 | printf("testOneway(%d): Sleeping...\n", sleepFor); |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 331 | THRIFT_SLEEP_SEC(sleepFor); |
David Reiss | 6ce401d | 2009-03-24 20:01:58 +0000 | [diff] [blame] | 332 | printf("testOneway(%d): done sleeping!\n", sleepFor); |
David Reiss | 2ab6fe8 | 2008-02-18 02:11:44 +0000 | [diff] [blame] | 333 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 334 | }; |
| 335 | |
David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 336 | class TestProcessorEventHandler : public TProcessorEventHandler { |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 337 | virtual void* getContext(const char* fn_name, void* serverContext) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 338 | (void)serverContext; |
David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 339 | return new std::string(fn_name); |
| 340 | } |
| 341 | virtual void freeContext(void* ctx, const char* fn_name) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 342 | (void)fn_name; |
David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 343 | delete static_cast<std::string*>(ctx); |
| 344 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 345 | virtual void preRead(void* ctx, const char* fn_name) { communicate("preRead", ctx, fn_name); } |
David Reiss | ef7200f | 2010-10-06 17:09:42 +0000 | [diff] [blame] | 346 | virtual void postRead(void* ctx, const char* fn_name, uint32_t bytes) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 347 | (void)bytes; |
David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 348 | communicate("postRead", ctx, fn_name); |
| 349 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 350 | virtual void preWrite(void* ctx, const char* fn_name) { communicate("preWrite", ctx, fn_name); } |
David Reiss | ef7200f | 2010-10-06 17:09:42 +0000 | [diff] [blame] | 351 | virtual void postWrite(void* ctx, const char* fn_name, uint32_t bytes) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 352 | (void)bytes; |
David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 353 | communicate("postWrite", ctx, fn_name); |
| 354 | } |
| 355 | virtual void asyncComplete(void* ctx, const char* fn_name) { |
| 356 | communicate("asyncComplete", ctx, fn_name); |
| 357 | } |
| 358 | virtual void handlerError(void* ctx, const char* fn_name) { |
| 359 | communicate("handlerError", ctx, fn_name); |
| 360 | } |
| 361 | |
| 362 | void communicate(const char* event, void* ctx, const char* fn_name) { |
| 363 | std::cout << event << ": " << *static_cast<std::string*>(ctx) << " = " << fn_name << std::endl; |
| 364 | } |
| 365 | }; |
| 366 | |
Roger Meier | 7e056e7 | 2011-07-17 07:28:28 +0000 | [diff] [blame] | 367 | class TestHandlerAsync : public ThriftTestCobSvIf { |
| 368 | public: |
Roger Meier | 611f90c | 2011-12-11 22:08:51 +0000 | [diff] [blame] | 369 | TestHandlerAsync(boost::shared_ptr<TestHandler>& handler) : _delegate(handler) {} |
Roger Meier | 7e056e7 | 2011-07-17 07:28:28 +0000 | [diff] [blame] | 370 | virtual ~TestHandlerAsync() {} |
| 371 | |
Roger Meier | 0a7c69c | 2014-05-02 21:15:45 +0200 | [diff] [blame] | 372 | virtual void testVoid(tcxx::function<void()> cob) { |
Roger Meier | 7e056e7 | 2011-07-17 07:28:28 +0000 | [diff] [blame] | 373 | _delegate->testVoid(); |
| 374 | cob(); |
| 375 | } |
| 376 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 377 | virtual void testString(tcxx::function<void(std::string const& _return)> cob, |
| 378 | const std::string& thing) { |
Roger Meier | 7e056e7 | 2011-07-17 07:28:28 +0000 | [diff] [blame] | 379 | std::string res; |
| 380 | _delegate->testString(res, thing); |
| 381 | cob(res); |
| 382 | } |
| 383 | |
Nobuaki Sukegawa | a649e74 | 2015-09-21 13:53:25 +0900 | [diff] [blame] | 384 | virtual void testBool(tcxx::function<void(bool const& _return)> cob, const bool thing) { |
| 385 | bool res = _delegate->testBool(thing); |
| 386 | cob(res); |
| 387 | } |
| 388 | |
Roger Meier | 0a7c69c | 2014-05-02 21:15:45 +0200 | [diff] [blame] | 389 | virtual void testByte(tcxx::function<void(int8_t const& _return)> cob, const int8_t thing) { |
Roger Meier | 7e056e7 | 2011-07-17 07:28:28 +0000 | [diff] [blame] | 390 | int8_t res = _delegate->testByte(thing); |
| 391 | cob(res); |
| 392 | } |
| 393 | |
Roger Meier | 0a7c69c | 2014-05-02 21:15:45 +0200 | [diff] [blame] | 394 | virtual void testI32(tcxx::function<void(int32_t const& _return)> cob, const int32_t thing) { |
Roger Meier | 7e056e7 | 2011-07-17 07:28:28 +0000 | [diff] [blame] | 395 | int32_t res = _delegate->testI32(thing); |
| 396 | cob(res); |
| 397 | } |
| 398 | |
Roger Meier | 0a7c69c | 2014-05-02 21:15:45 +0200 | [diff] [blame] | 399 | virtual void testI64(tcxx::function<void(int64_t const& _return)> cob, const int64_t thing) { |
Roger Meier | 7e056e7 | 2011-07-17 07:28:28 +0000 | [diff] [blame] | 400 | int64_t res = _delegate->testI64(thing); |
| 401 | cob(res); |
| 402 | } |
| 403 | |
Roger Meier | 0a7c69c | 2014-05-02 21:15:45 +0200 | [diff] [blame] | 404 | virtual void testDouble(tcxx::function<void(double const& _return)> cob, const double thing) { |
Roger Meier | 7e056e7 | 2011-07-17 07:28:28 +0000 | [diff] [blame] | 405 | double res = _delegate->testDouble(thing); |
| 406 | cob(res); |
| 407 | } |
| 408 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 409 | virtual void testBinary(tcxx::function<void(std::string const& _return)> cob, |
| 410 | const std::string& thing) { |
Jens Geyer | 8bcfdd9 | 2014-12-14 03:14:26 +0100 | [diff] [blame] | 411 | std::string res; |
| 412 | _delegate->testBinary(res, thing); |
| 413 | cob(res); |
| 414 | } |
| 415 | |
Roger Meier | 0a7c69c | 2014-05-02 21:15:45 +0200 | [diff] [blame] | 416 | virtual void testStruct(tcxx::function<void(Xtruct const& _return)> cob, const Xtruct& thing) { |
Roger Meier | 7e056e7 | 2011-07-17 07:28:28 +0000 | [diff] [blame] | 417 | Xtruct res; |
| 418 | _delegate->testStruct(res, thing); |
| 419 | cob(res); |
| 420 | } |
| 421 | |
Roger Meier | 0a7c69c | 2014-05-02 21:15:45 +0200 | [diff] [blame] | 422 | virtual void testNest(tcxx::function<void(Xtruct2 const& _return)> cob, const Xtruct2& thing) { |
Roger Meier | 7e056e7 | 2011-07-17 07:28:28 +0000 | [diff] [blame] | 423 | Xtruct2 res; |
| 424 | _delegate->testNest(res, thing); |
| 425 | cob(res); |
| 426 | } |
| 427 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 428 | virtual void testMap(tcxx::function<void(std::map<int32_t, int32_t> const& _return)> cob, |
| 429 | const std::map<int32_t, int32_t>& thing) { |
Roger Meier | 7e056e7 | 2011-07-17 07:28:28 +0000 | [diff] [blame] | 430 | std::map<int32_t, int32_t> res; |
| 431 | _delegate->testMap(res, thing); |
| 432 | cob(res); |
| 433 | } |
| 434 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 435 | virtual void testStringMap( |
| 436 | tcxx::function<void(std::map<std::string, std::string> const& _return)> cob, |
| 437 | const std::map<std::string, std::string>& thing) { |
Roger Meier | 7e056e7 | 2011-07-17 07:28:28 +0000 | [diff] [blame] | 438 | std::map<std::string, std::string> res; |
| 439 | _delegate->testStringMap(res, thing); |
| 440 | cob(res); |
| 441 | } |
| 442 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 443 | virtual void testSet(tcxx::function<void(std::set<int32_t> const& _return)> cob, |
| 444 | const std::set<int32_t>& thing) { |
Roger Meier | 7e056e7 | 2011-07-17 07:28:28 +0000 | [diff] [blame] | 445 | std::set<int32_t> res; |
| 446 | _delegate->testSet(res, thing); |
| 447 | cob(res); |
| 448 | } |
| 449 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 450 | virtual void testList(tcxx::function<void(std::vector<int32_t> const& _return)> cob, |
| 451 | const std::vector<int32_t>& thing) { |
Roger Meier | 7e056e7 | 2011-07-17 07:28:28 +0000 | [diff] [blame] | 452 | std::vector<int32_t> res; |
| 453 | _delegate->testList(res, thing); |
| 454 | cob(res); |
| 455 | } |
| 456 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 457 | virtual void testEnum(tcxx::function<void(Numberz::type const& _return)> cob, |
| 458 | const Numberz::type thing) { |
Roger Meier | 7e056e7 | 2011-07-17 07:28:28 +0000 | [diff] [blame] | 459 | Numberz::type res = _delegate->testEnum(thing); |
| 460 | cob(res); |
| 461 | } |
| 462 | |
Roger Meier | 0a7c69c | 2014-05-02 21:15:45 +0200 | [diff] [blame] | 463 | virtual void testTypedef(tcxx::function<void(UserId const& _return)> cob, const UserId thing) { |
Roger Meier | 7e056e7 | 2011-07-17 07:28:28 +0000 | [diff] [blame] | 464 | UserId res = _delegate->testTypedef(thing); |
| 465 | cob(res); |
| 466 | } |
| 467 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 468 | virtual void testMapMap( |
| 469 | tcxx::function<void(std::map<int32_t, std::map<int32_t, int32_t> > const& _return)> cob, |
| 470 | const int32_t hello) { |
Roger Meier | 7e056e7 | 2011-07-17 07:28:28 +0000 | [diff] [blame] | 471 | std::map<int32_t, std::map<int32_t, int32_t> > res; |
| 472 | _delegate->testMapMap(res, hello); |
| 473 | cob(res); |
| 474 | } |
| 475 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 476 | virtual void testInsanity( |
| 477 | tcxx::function<void(std::map<UserId, std::map<Numberz::type, Insanity> > const& _return)> cob, |
| 478 | const Insanity& argument) { |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 479 | std::map<UserId, std::map<Numberz::type, Insanity> > res; |
Roger Meier | 7e056e7 | 2011-07-17 07:28:28 +0000 | [diff] [blame] | 480 | _delegate->testInsanity(res, argument); |
| 481 | cob(res); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 482 | } |
Roger Meier | 7e056e7 | 2011-07-17 07:28:28 +0000 | [diff] [blame] | 483 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 484 | virtual void testMulti(tcxx::function<void(Xtruct const& _return)> cob, |
| 485 | const int8_t arg0, |
| 486 | const int32_t arg1, |
| 487 | const int64_t arg2, |
| 488 | const std::map<int16_t, std::string>& arg3, |
| 489 | const Numberz::type arg4, |
| 490 | const UserId arg5) { |
Roger Meier | 7e056e7 | 2011-07-17 07:28:28 +0000 | [diff] [blame] | 491 | Xtruct res; |
| 492 | _delegate->testMulti(res, arg0, arg1, arg2, arg3, arg4, arg5); |
| 493 | cob(res); |
| 494 | } |
| 495 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 496 | virtual void testException( |
| 497 | tcxx::function<void()> cob, |
| 498 | tcxx::function<void(::apache::thrift::TDelayedException* _throw)> exn_cob, |
| 499 | const std::string& arg) { |
Roger Meier | 7e056e7 | 2011-07-17 07:28:28 +0000 | [diff] [blame] | 500 | try { |
| 501 | _delegate->testException(arg); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 502 | } catch (const apache::thrift::TException& e) { |
Roger Meier | 7e056e7 | 2011-07-17 07:28:28 +0000 | [diff] [blame] | 503 | exn_cob(apache::thrift::TDelayedException::delayException(e)); |
| 504 | return; |
| 505 | } |
| 506 | cob(); |
| 507 | } |
| 508 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 509 | virtual void testMultiException( |
| 510 | tcxx::function<void(Xtruct const& _return)> cob, |
| 511 | tcxx::function<void(::apache::thrift::TDelayedException* _throw)> exn_cob, |
| 512 | const std::string& arg0, |
| 513 | const std::string& arg1) { |
Roger Meier | 7e056e7 | 2011-07-17 07:28:28 +0000 | [diff] [blame] | 514 | Xtruct res; |
| 515 | try { |
| 516 | _delegate->testMultiException(res, arg0, arg1); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 517 | } catch (const apache::thrift::TException& e) { |
Roger Meier | 7e056e7 | 2011-07-17 07:28:28 +0000 | [diff] [blame] | 518 | exn_cob(apache::thrift::TDelayedException::delayException(e)); |
| 519 | return; |
| 520 | } |
| 521 | cob(res); |
| 522 | } |
| 523 | |
Roger Meier | 0a7c69c | 2014-05-02 21:15:45 +0200 | [diff] [blame] | 524 | virtual void testOneway(tcxx::function<void()> cob, const int32_t secondsToSleep) { |
Roger Meier | 7e056e7 | 2011-07-17 07:28:28 +0000 | [diff] [blame] | 525 | _delegate->testOneway(secondsToSleep); |
| 526 | cob(); |
| 527 | } |
| 528 | |
| 529 | protected: |
Roger Meier | 611f90c | 2011-12-11 22:08:51 +0000 | [diff] [blame] | 530 | boost::shared_ptr<TestHandler> _delegate; |
Roger Meier | 7e056e7 | 2011-07-17 07:28:28 +0000 | [diff] [blame] | 531 | }; |
| 532 | |
Nobuaki Sukegawa | 8565061 | 2016-01-08 03:26:44 +0900 | [diff] [blame] | 533 | namespace po = boost::program_options; |
| 534 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 535 | int main(int argc, char** argv) { |
cdwijayarathna | a07ec0b | 2014-08-09 17:45:56 +0530 | [diff] [blame] | 536 | |
Nobuaki Sukegawa | c874408 | 2016-03-09 19:55:56 +0900 | [diff] [blame^] | 537 | string testDir = boost::filesystem::system_complete(argv[0]).parent_path().parent_path().parent_path().string(); |
| 538 | string certPath = testDir + "/keys/server.crt"; |
| 539 | string keyPath = testDir + "/keys/server.key"; |
cdwijayarathna | a07ec0b | 2014-08-09 17:45:56 +0530 | [diff] [blame] | 540 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 541 | #if _WIN32 |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 542 | transport::TWinsockSingleton::create(); |
| 543 | #endif |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 544 | int port = 9090; |
Bryan Duxbury | cd9aea1 | 2011-02-22 18:12:06 +0000 | [diff] [blame] | 545 | bool ssl = false; |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 546 | string transport_type = "buffered"; |
| 547 | string protocol_type = "binary"; |
| 548 | string server_type = "simple"; |
| 549 | string domain_socket = ""; |
pavlo | dd08f6e | 2015-10-08 16:43:56 -0400 | [diff] [blame] | 550 | bool abstract_namespace = false; |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 551 | size_t workers = 4; |
Nobuaki Sukegawa | 8565061 | 2016-01-08 03:26:44 +0900 | [diff] [blame] | 552 | int string_limit = 0; |
| 553 | int container_limit = 0; |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 554 | |
Nobuaki Sukegawa | 8565061 | 2016-01-08 03:26:44 +0900 | [diff] [blame] | 555 | po::options_description desc("Allowed options"); |
| 556 | desc.add_options() |
| 557 | ("help,h", "produce help message") |
| 558 | ("port", po::value<int>(&port)->default_value(port), "Port number to listen") |
| 559 | ("domain-socket", po::value<string>(&domain_socket) ->default_value(domain_socket), "Unix Domain Socket (e.g. /tmp/ThriftTest.thrift)") |
| 560 | ("abstract-namespace", "Create the domain socket in the Abstract Namespace (no connection with filesystem pathnames)") |
| 561 | ("server-type", po::value<string>(&server_type)->default_value(server_type), "type of server, \"simple\", \"thread-pool\", \"threaded\", or \"nonblocking\"") |
| 562 | ("transport", po::value<string>(&transport_type)->default_value(transport_type), "transport: buffered, framed, http") |
| 563 | ("protocol", po::value<string>(&protocol_type)->default_value(protocol_type), "protocol: binary, compact, header, json") |
| 564 | ("ssl", "Encrypted Transport using SSL") |
| 565 | ("processor-events", "processor-events") |
| 566 | ("workers,n", po::value<size_t>(&workers)->default_value(workers), "Number of thread pools workers. Only valid for thread-pool server type") |
| 567 | ("string-limit", po::value<int>(&string_limit)) |
| 568 | ("container-limit", po::value<int>(&container_limit)); |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 569 | |
Nobuaki Sukegawa | 8565061 | 2016-01-08 03:26:44 +0900 | [diff] [blame] | 570 | po::variables_map vm; |
| 571 | po::store(po::parse_command_line(argc, argv, desc), vm); |
| 572 | po::notify(vm); |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 573 | |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 574 | if (vm.count("help")) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 575 | cout << desc << "\n"; |
| 576 | return 1; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 577 | } |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 578 | |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 579 | try { |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 580 | if (!server_type.empty()) { |
| 581 | if (server_type == "simple") { |
| 582 | } else if (server_type == "thread-pool") { |
| 583 | } else if (server_type == "threaded") { |
| 584 | } else if (server_type == "nonblocking") { |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 585 | } else { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 586 | throw invalid_argument("Unknown server type " + server_type); |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 587 | } |
| 588 | } |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 589 | |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 590 | if (!protocol_type.empty()) { |
| 591 | if (protocol_type == "binary") { |
Roger Meier | 284101c | 2014-03-11 21:20:35 +0100 | [diff] [blame] | 592 | } else if (protocol_type == "compact") { |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 593 | } else if (protocol_type == "json") { |
Dave Watson | 792db4e | 2015-01-16 11:22:01 -0800 | [diff] [blame] | 594 | } else if (protocol_type == "header") { |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 595 | } else { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 596 | throw invalid_argument("Unknown protocol type " + protocol_type); |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | |
Roger Meier | 284101c | 2014-03-11 21:20:35 +0100 | [diff] [blame] | 600 | if (!transport_type.empty()) { |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 601 | if (transport_type == "buffered") { |
| 602 | } else if (transport_type == "framed") { |
| 603 | } else if (transport_type == "http") { |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 604 | } else { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 605 | throw invalid_argument("Unknown transport type " + transport_type); |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 606 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 607 | } |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 608 | |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 609 | } catch (std::exception& e) { |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 610 | cerr << e.what() << endl; |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 611 | cout << desc << "\n"; |
| 612 | return 1; |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 613 | } |
| 614 | |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 615 | if (vm.count("ssl")) { |
Bryan Duxbury | cd9aea1 | 2011-02-22 18:12:06 +0000 | [diff] [blame] | 616 | ssl = true; |
Bryan Duxbury | cd9aea1 | 2011-02-22 18:12:06 +0000 | [diff] [blame] | 617 | } |
| 618 | |
pavlo | dd08f6e | 2015-10-08 16:43:56 -0400 | [diff] [blame] | 619 | if (vm.count("abstract-namespace")) { |
| 620 | abstract_namespace = true; |
| 621 | } |
| 622 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 623 | // Dispatcher |
Roger Meier | 611f90c | 2011-12-11 22:08:51 +0000 | [diff] [blame] | 624 | boost::shared_ptr<TProtocolFactory> protocolFactory; |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 625 | if (protocol_type == "json") { |
Roger Meier | 611f90c | 2011-12-11 22:08:51 +0000 | [diff] [blame] | 626 | boost::shared_ptr<TProtocolFactory> jsonProtocolFactory(new TJSONProtocolFactory()); |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 627 | protocolFactory = jsonProtocolFactory; |
Roger Meier | 023192f | 2014-02-12 09:35:12 +0100 | [diff] [blame] | 628 | } else if (protocol_type == "compact") { |
Nobuaki Sukegawa | 8565061 | 2016-01-08 03:26:44 +0900 | [diff] [blame] | 629 | TCompactProtocolFactoryT<TBufferBase> *compactProtocolFactory = new TCompactProtocolFactoryT<TBufferBase>(); |
| 630 | compactProtocolFactory->setContainerSizeLimit(container_limit); |
| 631 | compactProtocolFactory->setStringSizeLimit(string_limit); |
| 632 | protocolFactory.reset(compactProtocolFactory); |
Dave Watson | 792db4e | 2015-01-16 11:22:01 -0800 | [diff] [blame] | 633 | } else if (protocol_type == "header") { |
| 634 | boost::shared_ptr<TProtocolFactory> headerProtocolFactory(new THeaderProtocolFactory()); |
| 635 | protocolFactory = headerProtocolFactory; |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 636 | } else { |
Nobuaki Sukegawa | 8565061 | 2016-01-08 03:26:44 +0900 | [diff] [blame] | 637 | TBinaryProtocolFactoryT<TBufferBase>* binaryProtocolFactory = new TBinaryProtocolFactoryT<TBufferBase>(); |
| 638 | binaryProtocolFactory->setContainerSizeLimit(container_limit); |
| 639 | binaryProtocolFactory->setStringSizeLimit(string_limit); |
| 640 | protocolFactory.reset(binaryProtocolFactory); |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 641 | } |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 642 | |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 643 | // Processor |
Roger Meier | 611f90c | 2011-12-11 22:08:51 +0000 | [diff] [blame] | 644 | boost::shared_ptr<TestHandler> testHandler(new TestHandler()); |
| 645 | boost::shared_ptr<ThriftTestProcessor> testProcessor(new ThriftTestProcessor(testHandler)); |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 646 | |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 647 | if (vm.count("processor-events")) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 648 | testProcessor->setEventHandler( |
| 649 | boost::shared_ptr<TProcessorEventHandler>(new TestProcessorEventHandler())); |
David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 650 | } |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 651 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 652 | // Transport |
Roger Meier | 611f90c | 2011-12-11 22:08:51 +0000 | [diff] [blame] | 653 | boost::shared_ptr<TSSLSocketFactory> sslSocketFactory; |
| 654 | boost::shared_ptr<TServerSocket> serverSocket; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 655 | |
Bryan Duxbury | cd9aea1 | 2011-02-22 18:12:06 +0000 | [diff] [blame] | 656 | if (ssl) { |
Roger Meier | 611f90c | 2011-12-11 22:08:51 +0000 | [diff] [blame] | 657 | sslSocketFactory = boost::shared_ptr<TSSLSocketFactory>(new TSSLSocketFactory()); |
Nobuaki Sukegawa | c874408 | 2016-03-09 19:55:56 +0900 | [diff] [blame^] | 658 | sslSocketFactory->loadCertificate(certPath.c_str()); |
| 659 | sslSocketFactory->loadPrivateKey(keyPath.c_str()); |
Bryan Duxbury | cd9aea1 | 2011-02-22 18:12:06 +0000 | [diff] [blame] | 660 | sslSocketFactory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH"); |
Roger Meier | 611f90c | 2011-12-11 22:08:51 +0000 | [diff] [blame] | 661 | serverSocket = boost::shared_ptr<TServerSocket>(new TSSLServerSocket(port, sslSocketFactory)); |
Bryan Duxbury | cd9aea1 | 2011-02-22 18:12:06 +0000 | [diff] [blame] | 662 | } else { |
Roger Meier | c94b293 | 2014-02-22 20:07:33 +0100 | [diff] [blame] | 663 | if (domain_socket != "") { |
pavlo | dd08f6e | 2015-10-08 16:43:56 -0400 | [diff] [blame] | 664 | if (abstract_namespace) { |
| 665 | std::string abstract_socket("\0", 1); |
| 666 | abstract_socket += domain_socket; |
| 667 | serverSocket = boost::shared_ptr<TServerSocket>(new TServerSocket(abstract_socket)); |
| 668 | } else { |
| 669 | unlink(domain_socket.c_str()); |
| 670 | serverSocket = boost::shared_ptr<TServerSocket>(new TServerSocket(domain_socket)); |
| 671 | } |
Roger Meier | c94b293 | 2014-02-22 20:07:33 +0100 | [diff] [blame] | 672 | port = 0; |
| 673 | } else { |
Roger Meier | 611f90c | 2011-12-11 22:08:51 +0000 | [diff] [blame] | 674 | serverSocket = boost::shared_ptr<TServerSocket>(new TServerSocket(port)); |
Roger Meier | c94b293 | 2014-02-22 20:07:33 +0100 | [diff] [blame] | 675 | } |
Bryan Duxbury | cd9aea1 | 2011-02-22 18:12:06 +0000 | [diff] [blame] | 676 | } |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 677 | |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 678 | // Factory |
Roger Meier | 611f90c | 2011-12-11 22:08:51 +0000 | [diff] [blame] | 679 | boost::shared_ptr<TTransportFactory> transportFactory; |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 680 | |
Roger Meier | 7e056e7 | 2011-07-17 07:28:28 +0000 | [diff] [blame] | 681 | if (transport_type == "http" && server_type != "nonblocking") { |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 682 | boost::shared_ptr<TTransportFactory> httpTransportFactory(new THttpServerTransportFactory()); |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 683 | transportFactory = httpTransportFactory; |
| 684 | } else if (transport_type == "framed") { |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 685 | boost::shared_ptr<TTransportFactory> framedTransportFactory(new TFramedTransportFactory()); |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 686 | transportFactory = framedTransportFactory; |
| 687 | } else { |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 688 | boost::shared_ptr<TTransportFactory> bufferedTransportFactory(new TBufferedTransportFactory()); |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 689 | transportFactory = bufferedTransportFactory; |
| 690 | } |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 691 | |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 692 | // Server Info |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 693 | cout << "Starting \"" << server_type << "\" server (" << transport_type << "/" << protocol_type |
pavlo | dd08f6e | 2015-10-08 16:43:56 -0400 | [diff] [blame] | 694 | << ") listen on: "; |
| 695 | if (abstract_namespace) { |
| 696 | cout << '@'; |
| 697 | } |
| 698 | cout << domain_socket; |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 699 | if (port != 0) { |
| 700 | cout << port; |
| 701 | } |
| 702 | cout << endl; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 703 | |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 704 | // Server |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 705 | boost::shared_ptr<apache::thrift::server::TServer> server; |
| 706 | |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 707 | if (server_type == "simple") { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 708 | server.reset(new TSimpleServer(testProcessor, serverSocket, transportFactory, protocolFactory)); |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 709 | } else if (server_type == "thread-pool") { |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 710 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 711 | boost::shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workers); |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 712 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 713 | boost::shared_ptr<PlatformThreadFactory> threadFactory |
| 714 | = boost::shared_ptr<PlatformThreadFactory>(new PlatformThreadFactory()); |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 715 | |
| 716 | threadManager->threadFactory(threadFactory); |
| 717 | |
| 718 | threadManager->start(); |
| 719 | |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 720 | server.reset(new TThreadPoolServer(testProcessor, |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 721 | serverSocket, |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 722 | transportFactory, |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 723 | protocolFactory, |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 724 | threadManager)); |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 725 | } else if (server_type == "threaded") { |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 726 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 727 | server.reset( |
| 728 | new TThreadedServer(testProcessor, serverSocket, transportFactory, protocolFactory)); |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 729 | } else if (server_type == "nonblocking") { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 730 | if (transport_type == "http") { |
Roger Meier | 611f90c | 2011-12-11 22:08:51 +0000 | [diff] [blame] | 731 | boost::shared_ptr<TestHandlerAsync> testHandlerAsync(new TestHandlerAsync(testHandler)); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 732 | boost::shared_ptr<TAsyncProcessor> testProcessorAsync( |
| 733 | new ThriftTestAsyncProcessor(testHandlerAsync)); |
| 734 | boost::shared_ptr<TAsyncBufferProcessor> testBufferProcessor( |
| 735 | new TAsyncProtocolProcessor(testProcessorAsync, protocolFactory)); |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 736 | |
| 737 | // not loading nonblockingServer into "server" because |
| 738 | // TEvhttpServer doesn't inherit from TServer, and doesn't |
| 739 | // provide a stop method. |
Roger Meier | 7e056e7 | 2011-07-17 07:28:28 +0000 | [diff] [blame] | 740 | TEvhttpServer nonblockingServer(testBufferProcessor, port); |
| 741 | nonblockingServer.serve(); |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 742 | } else { |
Dave Watson | 792db4e | 2015-01-16 11:22:01 -0800 | [diff] [blame] | 743 | server.reset(new TNonblockingServer(testProcessor, protocolFactory, port)); |
Roger Meier | 7e056e7 | 2011-07-17 07:28:28 +0000 | [diff] [blame] | 744 | } |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 747 | if (server.get() != NULL) { |
Dave Watson | 792db4e | 2015-01-16 11:22:01 -0800 | [diff] [blame] | 748 | if (protocol_type == "header") { |
| 749 | // Tell the server to use the same protocol for input / output |
| 750 | // if using header |
| 751 | server->setOutputProtocolFactory(boost::shared_ptr<TProtocolFactory>()); |
| 752 | } |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 753 | apache::thrift::concurrency::PlatformThreadFactory factory; |
| 754 | factory.setDetached(false); |
| 755 | boost::shared_ptr<apache::thrift::concurrency::Runnable> serverThreadRunner(server); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 756 | boost::shared_ptr<apache::thrift::concurrency::Thread> thread |
| 757 | = factory.newThread(serverThreadRunner); |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 758 | thread->start(); |
| 759 | |
Roger Meier | 284101c | 2014-03-11 21:20:35 +0100 | [diff] [blame] | 760 | // HACK: cross language test suite is unable to handle cin properly |
| 761 | // that's why we stay in a endless loop here |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 762 | while (1) { |
| 763 | } |
Roger Meier | 284101c | 2014-03-11 21:20:35 +0100 | [diff] [blame] | 764 | // FIXME: find another way to stop the server (e.g. a signal) |
| 765 | // cout<<"Press enter to stop the server."<<endl; |
| 766 | // cin.ignore(); //wait until a key is pressed |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 767 | |
| 768 | server->stop(); |
| 769 | thread->join(); |
| 770 | server.reset(); |
| 771 | } |
| 772 | |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 773 | cout << "done." << endl; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 774 | return 0; |
| 775 | } |