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