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