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