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