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 | |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 20 | #include <concurrency/ThreadManager.h> |
| 21 | #include <concurrency/PosixThreadFactory.h> |
| 22 | #include <protocol/TBinaryProtocol.h> |
| 23 | #include <server/TSimpleServer.h> |
Mark Slee | 0c2dff3 | 2007-03-09 19:26:57 +0000 | [diff] [blame] | 24 | #include <server/TThreadedServer.h> |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 25 | #include <server/TThreadPoolServer.h> |
Mark Slee | b9acf98 | 2006-10-10 01:57:32 +0000 | [diff] [blame] | 26 | #include <server/TNonblockingServer.h> |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 27 | #include <transport/TServerSocket.h> |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 28 | #include <transport/TTransportUtils.h> |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 29 | #include "ThriftTest.h" |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 30 | |
| 31 | #include <iostream> |
| 32 | #include <stdexcept> |
| 33 | #include <sstream> |
| 34 | |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 35 | #define __STDC_FORMAT_MACROS |
| 36 | #include <inttypes.h> |
| 37 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 38 | using namespace std; |
Mark Slee | 0c2dff3 | 2007-03-09 19:26:57 +0000 | [diff] [blame] | 39 | using namespace boost; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 40 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 41 | using namespace apache::thrift; |
| 42 | using namespace apache::thrift::concurrency; |
| 43 | using namespace apache::thrift::protocol; |
| 44 | using namespace apache::thrift::transport; |
| 45 | using namespace apache::thrift::server; |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 46 | |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 47 | using namespace thrift::test; |
| 48 | |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 49 | class TestHandler : public ThriftTestIf { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 50 | public: |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 51 | TestHandler() {} |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 52 | |
| 53 | void testVoid() { |
| 54 | printf("testVoid()\n"); |
| 55 | } |
| 56 | |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 57 | void testString(string& out, const string &thing) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 58 | printf("testString(\"%s\")\n", thing.c_str()); |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 59 | out = thing; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 62 | int8_t testByte(const int8_t thing) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 63 | printf("testByte(%d)\n", (int)thing); |
| 64 | return thing; |
| 65 | } |
| 66 | |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 67 | int32_t testI32(const int32_t thing) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 68 | printf("testI32(%d)\n", thing); |
| 69 | return thing; |
| 70 | } |
| 71 | |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 72 | int64_t testI64(const int64_t thing) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 73 | printf("testI64(%"PRId64")\n", thing); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 74 | return thing; |
| 75 | } |
| 76 | |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 77 | double testDouble(const double thing) { |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 78 | printf("testDouble(%lf)\n", thing); |
| 79 | return thing; |
| 80 | } |
| 81 | |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 82 | void testStruct(Xtruct& out, const Xtruct &thing) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 83 | 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] | 84 | out = thing; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 87 | void testNest(Xtruct2& out, const Xtruct2& nest) { |
| 88 | const Xtruct &thing = nest.struct_thing; |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 89 | 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] | 90 | out = nest; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 93 | 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] | 94 | printf("testMap({"); |
| 95 | map<int32_t, int32_t>::const_iterator m_iter; |
| 96 | bool first = true; |
| 97 | for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) { |
| 98 | if (first) { |
| 99 | first = false; |
| 100 | } else { |
| 101 | printf(", "); |
| 102 | } |
| 103 | printf("%d => %d", m_iter->first, m_iter->second); |
| 104 | } |
| 105 | printf("})\n"); |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 106 | out = thing; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 109 | void testSet(set<int32_t> &out, const set<int32_t> &thing) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 110 | printf("testSet({"); |
| 111 | set<int32_t>::const_iterator s_iter; |
| 112 | bool first = true; |
| 113 | for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) { |
| 114 | if (first) { |
| 115 | first = false; |
| 116 | } else { |
| 117 | printf(", "); |
| 118 | } |
| 119 | printf("%d", *s_iter); |
| 120 | } |
| 121 | printf("})\n"); |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 122 | out = thing; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 125 | void testList(vector<int32_t> &out, const vector<int32_t> &thing) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 126 | printf("testList({"); |
Mark Slee | b9acf98 | 2006-10-10 01:57:32 +0000 | [diff] [blame] | 127 | vector<int32_t>::const_iterator l_iter; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 128 | bool first = true; |
| 129 | for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) { |
| 130 | if (first) { |
| 131 | first = false; |
| 132 | } else { |
| 133 | printf(", "); |
| 134 | } |
| 135 | printf("%d", *l_iter); |
| 136 | } |
| 137 | printf("})\n"); |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 138 | out = thing; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 141 | Numberz::type testEnum(const Numberz::type thing) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 142 | printf("testEnum(%d)\n", thing); |
| 143 | return thing; |
| 144 | } |
| 145 | |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 146 | UserId testTypedef(const UserId thing) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 147 | printf("testTypedef(%"PRId64")\n", thing); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 148 | return thing; |
| 149 | } |
| 150 | |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 151 | 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] | 152 | printf("testMapMap(%d)\n", hello); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 153 | |
| 154 | map<int32_t,int32_t> pos; |
| 155 | map<int32_t,int32_t> neg; |
| 156 | for (int i = 1; i < 5; i++) { |
| 157 | pos.insert(make_pair(i,i)); |
| 158 | neg.insert(make_pair(-i,-i)); |
| 159 | } |
| 160 | |
| 161 | mapmap.insert(make_pair(4, pos)); |
| 162 | mapmap.insert(make_pair(-4, neg)); |
| 163 | |
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 | void testInsanity(map<UserId, map<Numberz::type,Insanity> > &insane, const Insanity &argument) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 167 | printf("testInsanity()\n"); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 168 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 169 | Xtruct hello; |
| 170 | hello.string_thing = "Hello2"; |
| 171 | hello.byte_thing = 2; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 172 | hello.i32_thing = 2; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 173 | hello.i64_thing = 2; |
| 174 | |
| 175 | Xtruct goodbye; |
| 176 | goodbye.string_thing = "Goodbye4"; |
| 177 | goodbye.byte_thing = 4; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 178 | goodbye.i32_thing = 4; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 179 | goodbye.i64_thing = 4; |
| 180 | |
| 181 | Insanity crazy; |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 182 | crazy.userMap.insert(make_pair(Numberz::EIGHT, 8)); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 183 | crazy.xtructs.push_back(goodbye); |
| 184 | |
| 185 | Insanity looney; |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 186 | crazy.userMap.insert(make_pair(Numberz::FIVE, 5)); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 187 | crazy.xtructs.push_back(hello); |
| 188 | |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 189 | map<Numberz::type, Insanity> first_map; |
| 190 | map<Numberz::type, Insanity> second_map; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 191 | |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 192 | first_map.insert(make_pair(Numberz::TWO, crazy)); |
| 193 | first_map.insert(make_pair(Numberz::THREE, crazy)); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 194 | |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 195 | second_map.insert(make_pair(Numberz::SIX, looney)); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 196 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 197 | insane.insert(make_pair(1, first_map)); |
| 198 | insane.insert(make_pair(2, second_map)); |
| 199 | |
| 200 | printf("return"); |
| 201 | printf(" = {"); |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 202 | map<UserId, map<Numberz::type,Insanity> >::const_iterator i_iter; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 203 | for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 204 | printf("%"PRId64" => {", i_iter->first); |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 205 | map<Numberz::type,Insanity>::const_iterator i2_iter; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 206 | for (i2_iter = i_iter->second.begin(); |
| 207 | i2_iter != i_iter->second.end(); |
| 208 | ++i2_iter) { |
| 209 | printf("%d => {", i2_iter->first); |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 210 | map<Numberz::type, UserId> userMap = i2_iter->second.userMap; |
| 211 | map<Numberz::type, UserId>::const_iterator um; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 212 | printf("{"); |
| 213 | for (um = userMap.begin(); um != userMap.end(); ++um) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 214 | printf("%d => %"PRId64", ", um->first, um->second); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 215 | } |
| 216 | printf("}, "); |
| 217 | |
Mark Slee | b9acf98 | 2006-10-10 01:57:32 +0000 | [diff] [blame] | 218 | vector<Xtruct> xtructs = i2_iter->second.xtructs; |
| 219 | vector<Xtruct>::const_iterator x; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 220 | printf("{"); |
| 221 | for (x = xtructs.begin(); x != xtructs.end(); ++x) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 222 | 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] | 223 | } |
| 224 | printf("}"); |
| 225 | |
| 226 | printf("}, "); |
| 227 | } |
| 228 | printf("}, "); |
| 229 | } |
| 230 | printf("}\n"); |
| 231 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 232 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 233 | } |
Marc Slemko | e6889de | 2006-08-12 00:32:53 +0000 | [diff] [blame] | 234 | |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 235 | 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] | 236 | printf("testMulti()\n"); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 237 | |
Marc Slemko | e6889de | 2006-08-12 00:32:53 +0000 | [diff] [blame] | 238 | hello.string_thing = "Hello2"; |
| 239 | hello.byte_thing = arg0; |
| 240 | hello.i32_thing = arg1; |
| 241 | hello.i64_thing = (int64_t)arg2; |
Marc Slemko | e6889de | 2006-08-12 00:32:53 +0000 | [diff] [blame] | 242 | } |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 243 | |
David Reiss | 55ff70f | 2008-06-11 00:58:25 +0000 | [diff] [blame] | 244 | void testException(const std::string &arg) |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 245 | throw(Xception, apache::thrift::TException) |
David Reiss | 55ff70f | 2008-06-11 00:58:25 +0000 | [diff] [blame] | 246 | { |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 247 | printf("testException(%s)\n", arg.c_str()); |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 248 | if (arg.compare("Xception") == 0) { |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 249 | Xception e; |
| 250 | e.errorCode = 1001; |
David Reiss | 9813cbe | 2009-01-31 21:39:11 +0000 | [diff] [blame] | 251 | e.message = arg; |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 252 | throw e; |
David Reiss | 55ff70f | 2008-06-11 00:58:25 +0000 | [diff] [blame] | 253 | } else if (arg.compare("ApplicationException") == 0) { |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 254 | apache::thrift::TException e; |
David Reiss | 55ff70f | 2008-06-11 00:58:25 +0000 | [diff] [blame] | 255 | throw e; |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 256 | } else { |
| 257 | Xtruct result; |
| 258 | result.string_thing = arg; |
| 259 | return; |
| 260 | } |
| 261 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 262 | |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 263 | 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] | 264 | |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 265 | printf("testMultiException(%s, %s)\n", arg0.c_str(), arg1.c_str()); |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 266 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 267 | if (arg0.compare("Xception") == 0) { |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 268 | Xception e; |
| 269 | e.errorCode = 1001; |
| 270 | e.message = "This is an Xception"; |
| 271 | throw e; |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 272 | } else if (arg0.compare("Xception2") == 0) { |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 273 | Xception2 e; |
| 274 | e.errorCode = 2002; |
| 275 | e.struct_thing.string_thing = "This is an Xception2"; |
| 276 | throw e; |
| 277 | } else { |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 278 | result.string_thing = arg1; |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 279 | return; |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 280 | } |
| 281 | } |
David Reiss | 2ab6fe8 | 2008-02-18 02:11:44 +0000 | [diff] [blame] | 282 | |
David Reiss | 6ce401d | 2009-03-24 20:01:58 +0000 | [diff] [blame] | 283 | void testOneway(int sleepFor) { |
| 284 | printf("testOneway(%d): Sleeping...\n", sleepFor); |
David Reiss | 2ab6fe8 | 2008-02-18 02:11:44 +0000 | [diff] [blame] | 285 | sleep(sleepFor); |
David Reiss | 6ce401d | 2009-03-24 20:01:58 +0000 | [diff] [blame] | 286 | printf("testOneway(%d): done sleeping!\n", sleepFor); |
David Reiss | 2ab6fe8 | 2008-02-18 02:11:44 +0000 | [diff] [blame] | 287 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 288 | }; |
| 289 | |
David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame^] | 290 | |
| 291 | class TestProcessorEventHandler : public TProcessorEventHandler { |
| 292 | virtual void* getContext(const char* fn_name) { |
| 293 | return new std::string(fn_name); |
| 294 | } |
| 295 | virtual void freeContext(void* ctx, const char* fn_name) { |
| 296 | delete static_cast<std::string*>(ctx); |
| 297 | } |
| 298 | virtual void preRead(void* ctx, const char* fn_name) { |
| 299 | communicate("preRead", ctx, fn_name); |
| 300 | } |
| 301 | virtual void postRead(void* ctx, const char* fn_name) { |
| 302 | communicate("postRead", ctx, fn_name); |
| 303 | } |
| 304 | virtual void preWrite(void* ctx, const char* fn_name) { |
| 305 | communicate("preWrite", ctx, fn_name); |
| 306 | } |
| 307 | virtual void postWrite(void* ctx, const char* fn_name) { |
| 308 | communicate("postWrite", ctx, fn_name); |
| 309 | } |
| 310 | virtual void asyncComplete(void* ctx, const char* fn_name) { |
| 311 | communicate("asyncComplete", ctx, fn_name); |
| 312 | } |
| 313 | virtual void handlerError(void* ctx, const char* fn_name) { |
| 314 | communicate("handlerError", ctx, fn_name); |
| 315 | } |
| 316 | |
| 317 | void communicate(const char* event, void* ctx, const char* fn_name) { |
| 318 | std::cout << event << ": " << *static_cast<std::string*>(ctx) << " = " << fn_name << std::endl; |
| 319 | } |
| 320 | }; |
| 321 | |
| 322 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 323 | int main(int argc, char **argv) { |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 324 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 325 | int port = 9090; |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 326 | string serverType = "simple"; |
| 327 | string protocolType = "binary"; |
| 328 | size_t workerCount = 4; |
| 329 | |
| 330 | ostringstream usage; |
| 331 | |
| 332 | usage << |
David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame^] | 333 | argv[0] << " [--port=<port number>] [--server-type=<server-type>] [--protocol-type=<protocol-type>] [--workers=<worker-count>] [--processor-events]" << endl << |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 334 | |
Mark Slee | 0c2dff3 | 2007-03-09 19:26:57 +0000 | [diff] [blame] | 335 | "\t\tserver-type\t\ttype of server, \"simple\", \"thread-pool\", \"threaded\", or \"nonblocking\". Default is " << serverType << endl << |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 336 | |
| 337 | "\t\tprotocol-type\t\ttype of protocol, \"binary\", \"ascii\", or \"xml\". Default is " << protocolType << endl << |
| 338 | |
| 339 | "\t\tworkers\t\tNumber of thread pools workers. Only valid for thread-pool server type. Default is " << workerCount << endl; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 340 | |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 341 | map<string, string> args; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 342 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 343 | for (int ix = 1; ix < argc; ix++) { |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 344 | string arg(argv[ix]); |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 345 | if (arg.compare(0,2, "--") == 0) { |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 346 | size_t end = arg.find_first_of("=", 2); |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 347 | if (end != string::npos) { |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 348 | args[string(arg, 2, end - 2)] = string(arg, end + 1); |
| 349 | } else { |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 350 | args[string(arg, 2)] = "true"; |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 351 | } |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 352 | } else { |
| 353 | throw invalid_argument("Unexcepted command line token: "+arg); |
| 354 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 355 | } |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 356 | |
| 357 | try { |
| 358 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 359 | if (!args["port"].empty()) { |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 360 | port = atoi(args["port"].c_str()); |
| 361 | } |
| 362 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 363 | if (!args["server-type"].empty()) { |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 364 | serverType = args["server-type"]; |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 365 | if (serverType == "simple") { |
| 366 | } else if (serverType == "thread-pool") { |
Mark Slee | 0c2dff3 | 2007-03-09 19:26:57 +0000 | [diff] [blame] | 367 | } else if (serverType == "threaded") { |
Mark Slee | b9acf98 | 2006-10-10 01:57:32 +0000 | [diff] [blame] | 368 | } else if (serverType == "nonblocking") { |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 369 | } else { |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 370 | throw invalid_argument("Unknown server type "+serverType); |
| 371 | } |
| 372 | } |
| 373 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 374 | if (!args["protocol-type"].empty()) { |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 375 | protocolType = args["protocol-type"]; |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 376 | if (protocolType == "binary") { |
| 377 | } else if (protocolType == "ascii") { |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 378 | throw invalid_argument("ASCII protocol not supported"); |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 379 | } else if (protocolType == "xml") { |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 380 | throw invalid_argument("XML protocol not supported"); |
| 381 | } else { |
| 382 | throw invalid_argument("Unknown protocol type "+protocolType); |
| 383 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 384 | } |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 385 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 386 | if (!args["workers"].empty()) { |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 387 | workerCount = atoi(args["workers"].c_str()); |
| 388 | } |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 389 | } catch (std::exception& e) { |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 390 | cerr << e.what() << endl; |
| 391 | cerr << usage; |
| 392 | } |
| 393 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 394 | // Dispatcher |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 395 | shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory()); |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 396 | |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 397 | shared_ptr<TestHandler> testHandler(new TestHandler()); |
| 398 | |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 399 | shared_ptr<ThriftTestProcessor> testProcessor(new ThriftTestProcessor(testHandler)); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 400 | |
David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame^] | 401 | |
| 402 | if (!args["processor-events"].empty()) { |
| 403 | testProcessor->setEventHandler(shared_ptr<TProcessorEventHandler>( |
| 404 | new TestProcessorEventHandler())); |
| 405 | } |
| 406 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 407 | // Transport |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 408 | shared_ptr<TServerSocket> serverSocket(new TServerSocket(port)); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 409 | |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 410 | // Factory |
| 411 | shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory()); |
| 412 | |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 413 | if (serverType == "simple") { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 414 | |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 415 | // Server |
Mark Slee | 018b699 | 2006-09-07 21:31:12 +0000 | [diff] [blame] | 416 | TSimpleServer simpleServer(testProcessor, |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 417 | serverSocket, |
| 418 | transportFactory, |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 419 | protocolFactory); |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 420 | |
| 421 | printf("Starting the server on port %d...\n", port); |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 422 | simpleServer.serve(); |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 423 | |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 424 | } else if (serverType == "thread-pool") { |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 425 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 426 | shared_ptr<ThreadManager> threadManager = |
| 427 | ThreadManager::newSimpleThreadManager(workerCount); |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 428 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 429 | shared_ptr<PosixThreadFactory> threadFactory = |
| 430 | shared_ptr<PosixThreadFactory>(new PosixThreadFactory()); |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 431 | |
| 432 | threadManager->threadFactory(threadFactory); |
| 433 | |
| 434 | threadManager->start(); |
| 435 | |
Mark Slee | 018b699 | 2006-09-07 21:31:12 +0000 | [diff] [blame] | 436 | TThreadPoolServer threadPoolServer(testProcessor, |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 437 | serverSocket, |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 438 | transportFactory, |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 439 | protocolFactory, |
| 440 | threadManager); |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 441 | |
| 442 | printf("Starting the server on port %d...\n", port); |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 443 | threadPoolServer.serve(); |
Mark Slee | b9acf98 | 2006-10-10 01:57:32 +0000 | [diff] [blame] | 444 | |
Mark Slee | 0c2dff3 | 2007-03-09 19:26:57 +0000 | [diff] [blame] | 445 | } else if (serverType == "threaded") { |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 446 | |
Mark Slee | 0c2dff3 | 2007-03-09 19:26:57 +0000 | [diff] [blame] | 447 | TThreadedServer threadedServer(testProcessor, |
| 448 | serverSocket, |
| 449 | transportFactory, |
| 450 | protocolFactory); |
| 451 | |
| 452 | printf("Starting the server on port %d...\n", port); |
| 453 | threadedServer.serve(); |
| 454 | |
Mark Slee | b9acf98 | 2006-10-10 01:57:32 +0000 | [diff] [blame] | 455 | } else if (serverType == "nonblocking") { |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 456 | TNonblockingServer nonblockingServer(testProcessor, port); |
Mark Slee | b9acf98 | 2006-10-10 01:57:32 +0000 | [diff] [blame] | 457 | printf("Starting the nonblocking server on port %d...\n", port); |
| 458 | nonblockingServer.serve(); |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 461 | printf("done.\n"); |
| 462 | return 0; |
| 463 | } |