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 | |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 23 | #include <concurrency/ThreadManager.h> |
| 24 | #include <concurrency/PosixThreadFactory.h> |
| 25 | #include <protocol/TBinaryProtocol.h> |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 26 | #include <protocol/TJSONProtocol.h> |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 27 | #include <server/TSimpleServer.h> |
Mark Slee | 0c2dff3 | 2007-03-09 19:26:57 +0000 | [diff] [blame] | 28 | #include <server/TThreadedServer.h> |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 29 | #include <server/TThreadPoolServer.h> |
Mark Slee | b9acf98 | 2006-10-10 01:57:32 +0000 | [diff] [blame] | 30 | #include <server/TNonblockingServer.h> |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 31 | #include <transport/TServerSocket.h> |
Roger Meier | bb09f44 | 2011-05-31 20:35:37 +0000 | [diff] [blame] | 32 | #include <transport/TSSLServerSocket.h> |
| 33 | #include <transport/TSSLSocket.h> |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 34 | #include <transport/THttpServer.h> |
| 35 | #include <transport/THttpTransport.h> |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 36 | #include <transport/TTransportUtils.h> |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 37 | #include "ThriftTest.h" |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 38 | |
| 39 | #include <iostream> |
| 40 | #include <stdexcept> |
| 41 | #include <sstream> |
| 42 | |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 43 | #include <boost/program_options.hpp> |
| 44 | |
Bryan Duxbury | cd9aea1 | 2011-02-22 18:12:06 +0000 | [diff] [blame] | 45 | #include <signal.h> |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 46 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 47 | using namespace std; |
Mark Slee | 0c2dff3 | 2007-03-09 19:26:57 +0000 | [diff] [blame] | 48 | using namespace boost; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 49 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 50 | using namespace apache::thrift; |
| 51 | using namespace apache::thrift::concurrency; |
| 52 | using namespace apache::thrift::protocol; |
| 53 | using namespace apache::thrift::transport; |
| 54 | using namespace apache::thrift::server; |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 55 | |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 56 | using namespace thrift::test; |
| 57 | |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 58 | class TestHandler : public ThriftTestIf { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 59 | public: |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 60 | TestHandler() {} |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 61 | |
| 62 | void testVoid() { |
| 63 | printf("testVoid()\n"); |
| 64 | } |
| 65 | |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 66 | void testString(string& out, const string &thing) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 67 | printf("testString(\"%s\")\n", thing.c_str()); |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 68 | out = thing; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 71 | int8_t testByte(const int8_t thing) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 72 | printf("testByte(%d)\n", (int)thing); |
| 73 | return thing; |
| 74 | } |
| 75 | |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 76 | int32_t testI32(const int32_t thing) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 77 | printf("testI32(%d)\n", thing); |
| 78 | return thing; |
| 79 | } |
| 80 | |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 81 | int64_t testI64(const int64_t thing) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 82 | printf("testI64(%"PRId64")\n", thing); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 83 | return thing; |
| 84 | } |
| 85 | |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 86 | double testDouble(const double thing) { |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 87 | printf("testDouble(%lf)\n", thing); |
| 88 | return thing; |
| 89 | } |
| 90 | |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 91 | void testStruct(Xtruct& out, const Xtruct &thing) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 92 | 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] | 93 | out = thing; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 96 | void testNest(Xtruct2& out, const Xtruct2& nest) { |
| 97 | const Xtruct &thing = nest.struct_thing; |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 98 | 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] | 99 | out = nest; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 102 | 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] | 103 | printf("testMap({"); |
| 104 | map<int32_t, int32_t>::const_iterator m_iter; |
| 105 | bool first = true; |
| 106 | for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) { |
| 107 | if (first) { |
| 108 | first = false; |
| 109 | } else { |
| 110 | printf(", "); |
| 111 | } |
| 112 | printf("%d => %d", m_iter->first, m_iter->second); |
| 113 | } |
| 114 | printf("})\n"); |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 115 | out = thing; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Roger Meier | d3b9dca | 2011-06-24 14:01:10 +0000 | [diff] [blame] | 118 | void testStringMap(map<std::string, std::string> &out, const map<std::string, std::string> &thing) { |
| 119 | printf("testMap({"); |
| 120 | map<std::string, std::string>::const_iterator m_iter; |
| 121 | bool first = true; |
| 122 | for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) { |
| 123 | if (first) { |
| 124 | first = false; |
| 125 | } else { |
| 126 | printf(", "); |
| 127 | } |
| 128 | printf("%s => %s", (m_iter->first).c_str(), (m_iter->second).c_str()); |
| 129 | } |
| 130 | printf("})\n"); |
| 131 | out = thing; |
| 132 | } |
| 133 | |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 134 | void testSet(set<int32_t> &out, const set<int32_t> &thing) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 135 | printf("testSet({"); |
| 136 | set<int32_t>::const_iterator s_iter; |
| 137 | bool first = true; |
| 138 | for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) { |
| 139 | if (first) { |
| 140 | first = false; |
| 141 | } else { |
| 142 | printf(", "); |
| 143 | } |
| 144 | printf("%d", *s_iter); |
| 145 | } |
| 146 | printf("})\n"); |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 147 | out = thing; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 150 | void testList(vector<int32_t> &out, const vector<int32_t> &thing) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 151 | printf("testList({"); |
Mark Slee | b9acf98 | 2006-10-10 01:57:32 +0000 | [diff] [blame] | 152 | vector<int32_t>::const_iterator l_iter; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 153 | bool first = true; |
| 154 | for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) { |
| 155 | if (first) { |
| 156 | first = false; |
| 157 | } else { |
| 158 | printf(", "); |
| 159 | } |
| 160 | printf("%d", *l_iter); |
| 161 | } |
| 162 | printf("})\n"); |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 163 | out = thing; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 166 | Numberz::type testEnum(const Numberz::type thing) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 167 | printf("testEnum(%d)\n", thing); |
| 168 | return thing; |
| 169 | } |
| 170 | |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 171 | UserId testTypedef(const UserId thing) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 172 | printf("testTypedef(%"PRId64")\n", thing); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 173 | return thing; |
| 174 | } |
| 175 | |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 176 | 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] | 177 | printf("testMapMap(%d)\n", hello); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 178 | |
| 179 | map<int32_t,int32_t> pos; |
| 180 | map<int32_t,int32_t> neg; |
| 181 | for (int i = 1; i < 5; i++) { |
| 182 | pos.insert(make_pair(i,i)); |
| 183 | neg.insert(make_pair(-i,-i)); |
| 184 | } |
| 185 | |
| 186 | mapmap.insert(make_pair(4, pos)); |
| 187 | mapmap.insert(make_pair(-4, neg)); |
| 188 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 191 | void testInsanity(map<UserId, map<Numberz::type,Insanity> > &insane, const Insanity &argument) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 192 | printf("testInsanity()\n"); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 193 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 194 | Xtruct hello; |
| 195 | hello.string_thing = "Hello2"; |
| 196 | hello.byte_thing = 2; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 197 | hello.i32_thing = 2; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 198 | hello.i64_thing = 2; |
| 199 | |
| 200 | Xtruct goodbye; |
| 201 | goodbye.string_thing = "Goodbye4"; |
| 202 | goodbye.byte_thing = 4; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 203 | goodbye.i32_thing = 4; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 204 | goodbye.i64_thing = 4; |
| 205 | |
| 206 | Insanity crazy; |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 207 | crazy.userMap.insert(make_pair(Numberz::EIGHT, 8)); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 208 | crazy.xtructs.push_back(goodbye); |
| 209 | |
| 210 | Insanity looney; |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 211 | crazy.userMap.insert(make_pair(Numberz::FIVE, 5)); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 212 | crazy.xtructs.push_back(hello); |
| 213 | |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 214 | map<Numberz::type, Insanity> first_map; |
| 215 | map<Numberz::type, Insanity> second_map; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 216 | |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 217 | first_map.insert(make_pair(Numberz::TWO, crazy)); |
| 218 | first_map.insert(make_pair(Numberz::THREE, crazy)); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 219 | |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 220 | second_map.insert(make_pair(Numberz::SIX, looney)); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 221 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 222 | insane.insert(make_pair(1, first_map)); |
| 223 | insane.insert(make_pair(2, second_map)); |
| 224 | |
| 225 | printf("return"); |
| 226 | printf(" = {"); |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 227 | map<UserId, map<Numberz::type,Insanity> >::const_iterator i_iter; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 228 | for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 229 | printf("%"PRId64" => {", i_iter->first); |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 230 | map<Numberz::type,Insanity>::const_iterator i2_iter; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 231 | for (i2_iter = i_iter->second.begin(); |
| 232 | i2_iter != i_iter->second.end(); |
| 233 | ++i2_iter) { |
| 234 | printf("%d => {", i2_iter->first); |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 235 | map<Numberz::type, UserId> userMap = i2_iter->second.userMap; |
| 236 | map<Numberz::type, UserId>::const_iterator um; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 237 | printf("{"); |
| 238 | for (um = userMap.begin(); um != userMap.end(); ++um) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 239 | printf("%d => %"PRId64", ", um->first, um->second); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 240 | } |
| 241 | printf("}, "); |
| 242 | |
Mark Slee | b9acf98 | 2006-10-10 01:57:32 +0000 | [diff] [blame] | 243 | vector<Xtruct> xtructs = i2_iter->second.xtructs; |
| 244 | vector<Xtruct>::const_iterator x; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 245 | printf("{"); |
| 246 | for (x = xtructs.begin(); x != xtructs.end(); ++x) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 247 | 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] | 248 | } |
| 249 | printf("}"); |
| 250 | |
| 251 | printf("}, "); |
| 252 | } |
| 253 | printf("}, "); |
| 254 | } |
| 255 | printf("}\n"); |
| 256 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 257 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 258 | } |
Marc Slemko | e6889de | 2006-08-12 00:32:53 +0000 | [diff] [blame] | 259 | |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 260 | 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) { |
Marc Slemko | e6889de | 2006-08-12 00:32:53 +0000 | [diff] [blame] | 261 | printf("testMulti()\n"); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 262 | |
Marc Slemko | e6889de | 2006-08-12 00:32:53 +0000 | [diff] [blame] | 263 | hello.string_thing = "Hello2"; |
| 264 | hello.byte_thing = arg0; |
| 265 | hello.i32_thing = arg1; |
| 266 | hello.i64_thing = (int64_t)arg2; |
Marc Slemko | e6889de | 2006-08-12 00:32:53 +0000 | [diff] [blame] | 267 | } |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 268 | |
David Reiss | 55ff70f | 2008-06-11 00:58:25 +0000 | [diff] [blame] | 269 | void testException(const std::string &arg) |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 270 | throw(Xception, apache::thrift::TException) |
David Reiss | 55ff70f | 2008-06-11 00:58:25 +0000 | [diff] [blame] | 271 | { |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 272 | printf("testException(%s)\n", arg.c_str()); |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 273 | if (arg.compare("Xception") == 0) { |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 274 | Xception e; |
| 275 | e.errorCode = 1001; |
David Reiss | 9813cbe | 2009-01-31 21:39:11 +0000 | [diff] [blame] | 276 | e.message = arg; |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 277 | throw e; |
David Reiss | 55ff70f | 2008-06-11 00:58:25 +0000 | [diff] [blame] | 278 | } else if (arg.compare("ApplicationException") == 0) { |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 279 | apache::thrift::TException e; |
David Reiss | 55ff70f | 2008-06-11 00:58:25 +0000 | [diff] [blame] | 280 | throw e; |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 281 | } else { |
| 282 | Xtruct result; |
| 283 | result.string_thing = arg; |
| 284 | return; |
| 285 | } |
| 286 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 287 | |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 288 | 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] | 289 | |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 290 | printf("testMultiException(%s, %s)\n", arg0.c_str(), arg1.c_str()); |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 291 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 292 | if (arg0.compare("Xception") == 0) { |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 293 | Xception e; |
| 294 | e.errorCode = 1001; |
| 295 | e.message = "This is an Xception"; |
| 296 | throw e; |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 297 | } else if (arg0.compare("Xception2") == 0) { |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 298 | Xception2 e; |
| 299 | e.errorCode = 2002; |
| 300 | e.struct_thing.string_thing = "This is an Xception2"; |
| 301 | throw e; |
| 302 | } else { |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 303 | result.string_thing = arg1; |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 304 | return; |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 305 | } |
| 306 | } |
David Reiss | 2ab6fe8 | 2008-02-18 02:11:44 +0000 | [diff] [blame] | 307 | |
David Reiss | 6ce401d | 2009-03-24 20:01:58 +0000 | [diff] [blame] | 308 | void testOneway(int sleepFor) { |
| 309 | printf("testOneway(%d): Sleeping...\n", sleepFor); |
David Reiss | 2ab6fe8 | 2008-02-18 02:11:44 +0000 | [diff] [blame] | 310 | sleep(sleepFor); |
David Reiss | 6ce401d | 2009-03-24 20:01:58 +0000 | [diff] [blame] | 311 | printf("testOneway(%d): done sleeping!\n", sleepFor); |
David Reiss | 2ab6fe8 | 2008-02-18 02:11:44 +0000 | [diff] [blame] | 312 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 313 | }; |
| 314 | |
David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 315 | |
| 316 | class TestProcessorEventHandler : public TProcessorEventHandler { |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 317 | virtual void* getContext(const char* fn_name, void* serverContext) { |
David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 318 | return new std::string(fn_name); |
| 319 | } |
| 320 | virtual void freeContext(void* ctx, const char* fn_name) { |
| 321 | delete static_cast<std::string*>(ctx); |
| 322 | } |
| 323 | virtual void preRead(void* ctx, const char* fn_name) { |
| 324 | communicate("preRead", ctx, fn_name); |
| 325 | } |
David Reiss | ef7200f | 2010-10-06 17:09:42 +0000 | [diff] [blame] | 326 | virtual void postRead(void* ctx, const char* fn_name, uint32_t bytes) { |
David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 327 | communicate("postRead", ctx, fn_name); |
| 328 | } |
| 329 | virtual void preWrite(void* ctx, const char* fn_name) { |
| 330 | communicate("preWrite", ctx, fn_name); |
| 331 | } |
David Reiss | ef7200f | 2010-10-06 17:09:42 +0000 | [diff] [blame] | 332 | virtual void postWrite(void* ctx, const char* fn_name, uint32_t bytes) { |
David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 333 | communicate("postWrite", ctx, fn_name); |
| 334 | } |
| 335 | virtual void asyncComplete(void* ctx, const char* fn_name) { |
| 336 | communicate("asyncComplete", ctx, fn_name); |
| 337 | } |
| 338 | virtual void handlerError(void* ctx, const char* fn_name) { |
| 339 | communicate("handlerError", ctx, fn_name); |
| 340 | } |
| 341 | |
| 342 | void communicate(const char* event, void* ctx, const char* fn_name) { |
| 343 | std::cout << event << ": " << *static_cast<std::string*>(ctx) << " = " << fn_name << std::endl; |
| 344 | } |
| 345 | }; |
| 346 | |
| 347 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 348 | int main(int argc, char **argv) { |
| 349 | int port = 9090; |
Bryan Duxbury | cd9aea1 | 2011-02-22 18:12:06 +0000 | [diff] [blame] | 350 | bool ssl = false; |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 351 | string transport_type = "buffered"; |
| 352 | string protocol_type = "binary"; |
| 353 | string server_type = "simple"; |
| 354 | string domain_socket = ""; |
| 355 | size_t workers = 4; |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 356 | |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 357 | |
| 358 | program_options::options_description desc("Allowed options"); |
| 359 | desc.add_options() |
| 360 | ("help,h", "produce help message") |
| 361 | ("port", program_options::value<int>(&port)->default_value(port), "Port number to listen") |
| 362 | ("domain-socket", program_options::value<string>(&domain_socket)->default_value(domain_socket), |
| 363 | "Unix Domain Socket (e.g. /tmp/ThriftTest.thrift)") |
| 364 | ("server-type", program_options::value<string>(&server_type)->default_value(server_type), |
| 365 | "type of server, \"simple\", \"thread-pool\", \"threaded\", or \"nonblocking\"") |
| 366 | ("transport", program_options::value<string>(&transport_type)->default_value(transport_type), |
| 367 | "transport: buffered, framed, http") |
| 368 | ("protocol", program_options::value<string>(&protocol_type)->default_value(protocol_type), |
| 369 | "protocol: binary, json") |
| 370 | ("ssl", "Encrypted Transport using SSL") |
| 371 | ("processor-events", "processor-events") |
| 372 | ("workers,n", program_options::value<size_t>(&workers)->default_value(workers), |
| 373 | "Number of thread pools workers. Only valid for thread-pool server type") |
| 374 | ; |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 375 | |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 376 | program_options::variables_map vm; |
| 377 | program_options::store(program_options::parse_command_line(argc, argv, desc), vm); |
| 378 | program_options::notify(vm); |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 379 | |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 380 | if (vm.count("help")) { |
| 381 | cout << desc << "\n"; |
| 382 | return 1; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 383 | } |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 384 | |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 385 | try { |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 386 | if (!server_type.empty()) { |
| 387 | if (server_type == "simple") { |
| 388 | } else if (server_type == "thread-pool") { |
| 389 | } else if (server_type == "threaded") { |
| 390 | } else if (server_type == "nonblocking") { |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 391 | } else { |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 392 | throw invalid_argument("Unknown server type "+server_type); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | if (!protocol_type.empty()) { |
| 397 | if (protocol_type == "binary") { |
| 398 | } else if (protocol_type == "json") { |
| 399 | } else { |
| 400 | throw invalid_argument("Unknown protocol type "+protocol_type); |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 404 | if (!transport_type.empty()) { |
| 405 | if (transport_type == "buffered") { |
| 406 | } else if (transport_type == "framed") { |
| 407 | } else if (transport_type == "http") { |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 408 | } else { |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 409 | throw invalid_argument("Unknown transport type "+transport_type); |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 410 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 411 | } |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 412 | |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 413 | } catch (std::exception& e) { |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 414 | cerr << e.what() << endl; |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 415 | cout << desc << "\n"; |
| 416 | return 1; |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 419 | if (vm.count("ssl")) { |
Bryan Duxbury | cd9aea1 | 2011-02-22 18:12:06 +0000 | [diff] [blame] | 420 | ssl = true; |
| 421 | signal(SIGPIPE, SIG_IGN); |
| 422 | } |
| 423 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 424 | // Dispatcher |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 425 | shared_ptr<TProtocolFactory> protocolFactory; |
| 426 | if (protocol_type == "json") { |
| 427 | shared_ptr<TProtocolFactory> jsonProtocolFactory(new TJSONProtocolFactory()); |
| 428 | protocolFactory = jsonProtocolFactory; |
| 429 | } else { |
| 430 | shared_ptr<TProtocolFactory> binaryProtocolFactory(new TBinaryProtocolFactoryT<TBufferBase>()); |
| 431 | protocolFactory = binaryProtocolFactory; |
| 432 | } |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 433 | |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 434 | // Processor |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 435 | shared_ptr<TestHandler> testHandler(new TestHandler()); |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 436 | shared_ptr<ThriftTestProcessor> testProcessor(new ThriftTestProcessor(testHandler)); |
| 437 | |
| 438 | if (vm.count("processor-events")) { |
David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 439 | testProcessor->setEventHandler(shared_ptr<TProcessorEventHandler>( |
| 440 | new TestProcessorEventHandler())); |
| 441 | } |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 442 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 443 | // Transport |
Bryan Duxbury | cd9aea1 | 2011-02-22 18:12:06 +0000 | [diff] [blame] | 444 | shared_ptr<TSSLSocketFactory> sslSocketFactory; |
| 445 | shared_ptr<TServerSocket> serverSocket; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 446 | |
Bryan Duxbury | cd9aea1 | 2011-02-22 18:12:06 +0000 | [diff] [blame] | 447 | if (ssl) { |
| 448 | sslSocketFactory = shared_ptr<TSSLSocketFactory>(new TSSLSocketFactory()); |
| 449 | sslSocketFactory->loadCertificate("./server-certificate.pem"); |
| 450 | sslSocketFactory->loadPrivateKey("./server-private-key.pem"); |
| 451 | sslSocketFactory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH"); |
| 452 | serverSocket = shared_ptr<TServerSocket>(new TSSLServerSocket(port, sslSocketFactory)); |
| 453 | } else { |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 454 | if (domain_socket != "") { |
| 455 | unlink(domain_socket.c_str()); |
| 456 | serverSocket = shared_ptr<TServerSocket>(new TServerSocket(domain_socket)); |
| 457 | port = 0; |
| 458 | } |
| 459 | else { |
| 460 | serverSocket = shared_ptr<TServerSocket>(new TServerSocket(port)); |
| 461 | } |
Bryan Duxbury | cd9aea1 | 2011-02-22 18:12:06 +0000 | [diff] [blame] | 462 | } |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 463 | |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 464 | // Factory |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 465 | shared_ptr<TTransportFactory> transportFactory; |
| 466 | |
| 467 | if (transport_type == "http") { |
| 468 | shared_ptr<TTransportFactory> httpTransportFactory(new THttpServerTransportFactory()); |
| 469 | transportFactory = httpTransportFactory; |
| 470 | } else if (transport_type == "framed") { |
| 471 | shared_ptr<TTransportFactory> framedTransportFactory(new TFramedTransportFactory()); |
| 472 | transportFactory = framedTransportFactory; |
| 473 | } else { |
| 474 | shared_ptr<TTransportFactory> bufferedTransportFactory(new TBufferedTransportFactory()); |
| 475 | transportFactory = bufferedTransportFactory; |
| 476 | } |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 477 | |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 478 | // Server Info |
| 479 | cout << "Starting \"" << server_type << "\" server (" |
| 480 | << transport_type << "/" << protocol_type << ") listen on: " << domain_socket; |
| 481 | if (port != 0) { |
| 482 | cout << port; |
| 483 | } |
| 484 | cout << endl; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 485 | |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 486 | // Server |
| 487 | if (server_type == "simple") { |
Mark Slee | 018b699 | 2006-09-07 21:31:12 +0000 | [diff] [blame] | 488 | TSimpleServer simpleServer(testProcessor, |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 489 | serverSocket, |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 490 | transportFactory, |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 491 | protocolFactory); |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 492 | |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 493 | simpleServer.serve(); |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 494 | |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 495 | } else if (server_type == "thread-pool") { |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 496 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 497 | shared_ptr<ThreadManager> threadManager = |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 498 | ThreadManager::newSimpleThreadManager(workers); |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 499 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 500 | shared_ptr<PosixThreadFactory> threadFactory = |
| 501 | shared_ptr<PosixThreadFactory>(new PosixThreadFactory()); |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 502 | |
| 503 | threadManager->threadFactory(threadFactory); |
| 504 | |
| 505 | threadManager->start(); |
| 506 | |
Mark Slee | 018b699 | 2006-09-07 21:31:12 +0000 | [diff] [blame] | 507 | TThreadPoolServer threadPoolServer(testProcessor, |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 508 | serverSocket, |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 509 | transportFactory, |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 510 | protocolFactory, |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 511 | threadManager); |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 512 | |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 513 | threadPoolServer.serve(); |
Mark Slee | b9acf98 | 2006-10-10 01:57:32 +0000 | [diff] [blame] | 514 | |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 515 | } else if (server_type == "threaded") { |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 516 | |
Mark Slee | 0c2dff3 | 2007-03-09 19:26:57 +0000 | [diff] [blame] | 517 | TThreadedServer threadedServer(testProcessor, |
| 518 | serverSocket, |
| 519 | transportFactory, |
| 520 | protocolFactory); |
| 521 | |
Mark Slee | 0c2dff3 | 2007-03-09 19:26:57 +0000 | [diff] [blame] | 522 | threadedServer.serve(); |
| 523 | |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 524 | } else if (server_type == "nonblocking") { |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 525 | TNonblockingServer nonblockingServer(testProcessor, port); |
Mark Slee | b9acf98 | 2006-10-10 01:57:32 +0000 | [diff] [blame] | 526 | nonblockingServer.serve(); |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 529 | cout << "done." << endl; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 530 | return 0; |
| 531 | } |