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