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