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 | |
| 32 | string testString(string thing) { |
| 33 | printf("testString(\"%s\")\n", thing.c_str()); |
| 34 | return thing; |
| 35 | } |
| 36 | |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 37 | int8_t testByte(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 | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 42 | int32_t testI32(int32_t thing) { |
| 43 | printf("testI32(%d)\n", thing); |
| 44 | return thing; |
| 45 | } |
| 46 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 47 | int64_t testI64(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 | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 52 | double testDouble(double thing) { |
| 53 | printf("testDouble(%lf)\n", thing); |
| 54 | return thing; |
| 55 | } |
| 56 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 57 | Xtruct testStruct(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 | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 59 | return thing; |
| 60 | } |
| 61 | |
| 62 | Xtruct2 testNest(Xtruct2 nest) { |
| 63 | 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 | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 65 | return nest; |
| 66 | } |
| 67 | |
| 68 | map<int32_t, int32_t> testMap(map<int32_t, int32_t> thing) { |
| 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"); |
| 81 | return thing; |
| 82 | } |
| 83 | |
| 84 | set<int32_t> testSet(set<int32_t> thing) { |
| 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"); |
| 97 | return thing; |
| 98 | } |
| 99 | |
Mark Slee | b9acf98 | 2006-10-10 01:57:32 +0000 | [diff] [blame] | 100 | vector<int32_t> testList(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"); |
| 113 | return thing; |
| 114 | } |
| 115 | |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 116 | Numberz testEnum(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 | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 121 | UserId testTypedef(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 | |
| 126 | map<int32_t, map<int32_t,int32_t> > testMapMap(int32_t hello) { |
| 127 | printf("testMapMap(%d)\n", hello); |
| 128 | map<int32_t, map<int32_t,int32_t> > mapmap; |
| 129 | |
| 130 | map<int32_t,int32_t> pos; |
| 131 | map<int32_t,int32_t> neg; |
| 132 | for (int i = 1; i < 5; i++) { |
| 133 | pos.insert(make_pair(i,i)); |
| 134 | neg.insert(make_pair(-i,-i)); |
| 135 | } |
| 136 | |
| 137 | mapmap.insert(make_pair(4, pos)); |
| 138 | mapmap.insert(make_pair(-4, neg)); |
| 139 | |
| 140 | return mapmap; |
| 141 | } |
| 142 | |
| 143 | map<UserId, map<Numberz,Insanity> > testInsanity(Insanity argument) { |
| 144 | printf("testInsanity()\n"); |
| 145 | |
| 146 | Xtruct hello; |
| 147 | hello.string_thing = "Hello2"; |
| 148 | hello.byte_thing = 2; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 149 | hello.i32_thing = 2; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 150 | hello.i64_thing = 2; |
| 151 | |
| 152 | Xtruct goodbye; |
| 153 | goodbye.string_thing = "Goodbye4"; |
| 154 | goodbye.byte_thing = 4; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 155 | goodbye.i32_thing = 4; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 156 | goodbye.i64_thing = 4; |
| 157 | |
| 158 | Insanity crazy; |
| 159 | crazy.userMap.insert(make_pair(EIGHT, 8)); |
| 160 | crazy.xtructs.push_back(goodbye); |
| 161 | |
| 162 | Insanity looney; |
| 163 | crazy.userMap.insert(make_pair(FIVE, 5)); |
| 164 | crazy.xtructs.push_back(hello); |
| 165 | |
| 166 | map<Numberz, Insanity> first_map; |
| 167 | map<Numberz, Insanity> second_map; |
| 168 | |
| 169 | first_map.insert(make_pair(TWO, crazy)); |
| 170 | first_map.insert(make_pair(THREE, crazy)); |
| 171 | |
| 172 | second_map.insert(make_pair(SIX, looney)); |
| 173 | |
| 174 | map<UserId, map<Numberz,Insanity> > insane; |
| 175 | insane.insert(make_pair(1, first_map)); |
| 176 | insane.insert(make_pair(2, second_map)); |
| 177 | |
| 178 | printf("return"); |
| 179 | printf(" = {"); |
| 180 | map<UserId, map<Numberz,Insanity> >::const_iterator i_iter; |
| 181 | for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) { |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 182 | printf("%ld => {", i_iter->first); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 183 | map<Numberz,Insanity>::const_iterator i2_iter; |
| 184 | for (i2_iter = i_iter->second.begin(); |
| 185 | i2_iter != i_iter->second.end(); |
| 186 | ++i2_iter) { |
| 187 | printf("%d => {", i2_iter->first); |
| 188 | map<Numberz, UserId> userMap = i2_iter->second.userMap; |
| 189 | map<Numberz, UserId>::const_iterator um; |
| 190 | printf("{"); |
| 191 | for (um = userMap.begin(); um != userMap.end(); ++um) { |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 192 | printf("%d => %ld, ", um->first, um->second); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 193 | } |
| 194 | printf("}, "); |
| 195 | |
Mark Slee | b9acf98 | 2006-10-10 01:57:32 +0000 | [diff] [blame] | 196 | vector<Xtruct> xtructs = i2_iter->second.xtructs; |
| 197 | vector<Xtruct>::const_iterator x; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 198 | printf("{"); |
| 199 | for (x = xtructs.begin(); x != xtructs.end(); ++x) { |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 200 | 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] | 201 | } |
| 202 | printf("}"); |
| 203 | |
| 204 | printf("}, "); |
| 205 | } |
| 206 | printf("}, "); |
| 207 | } |
| 208 | printf("}\n"); |
| 209 | |
| 210 | return insane; |
| 211 | } |
Marc Slemko | e6889de | 2006-08-12 00:32:53 +0000 | [diff] [blame] | 212 | |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 213 | Xtruct testMulti(int8_t arg0, int32_t arg1, int64_t arg2, std::map<int16_t, std::string> arg3, Numberz arg4, UserId arg5) { |
Marc Slemko | e6889de | 2006-08-12 00:32:53 +0000 | [diff] [blame] | 214 | printf("testMulti()\n"); |
| 215 | |
| 216 | Xtruct hello; |
| 217 | hello.string_thing = "Hello2"; |
| 218 | hello.byte_thing = arg0; |
| 219 | hello.i32_thing = arg1; |
| 220 | hello.i64_thing = (int64_t)arg2; |
| 221 | |
| 222 | return hello; |
| 223 | } |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 224 | |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 225 | void testException(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 | } |
| 238 | |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 239 | Xtruct testMultiException(std::string arg0, 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 { |
| 254 | Xtruct result; |
| 255 | result.string_thing = arg1; |
| 256 | return result; |
| 257 | } |
| 258 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 259 | }; |
| 260 | |
| 261 | int main(int argc, char **argv) { |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 262 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 263 | int port = 9090; |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 264 | string serverType = "simple"; |
| 265 | string protocolType = "binary"; |
| 266 | size_t workerCount = 4; |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame^] | 267 | bool frameOutput = true; |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 268 | |
| 269 | ostringstream usage; |
| 270 | |
| 271 | usage << |
| 272 | argv[0] << " [--port=<port number>] [--server-type=<server-type>] [--protocol-type=<protocol-type>] [--workers=<worker-count>]" << endl << |
| 273 | |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame^] | 274 | "\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] | 275 | |
| 276 | "\t\tprotocol-type\t\ttype of protocol, \"binary\", \"ascii\", or \"xml\". Default is " << protocolType << endl << |
| 277 | |
| 278 | "\t\tworkers\t\tNumber of thread pools workers. Only valid for thread-pool server type. Default is " << workerCount << endl; |
| 279 | |
| 280 | map<string, string> args; |
| 281 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 282 | for (int ix = 1; ix < argc; ix++) { |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 283 | string arg(argv[ix]); |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 284 | if (arg.compare(0,2, "--") == 0) { |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 285 | size_t end = arg.find_first_of("=", 2); |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 286 | if (end != string::npos) { |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 287 | args[string(arg, 2, end - 2)] = string(arg, end + 1); |
| 288 | } else { |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame^] | 289 | args[string(arg, 2)] = "true"; |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 290 | } |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 291 | } else { |
| 292 | throw invalid_argument("Unexcepted command line token: "+arg); |
| 293 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 294 | } |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 295 | |
| 296 | try { |
| 297 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 298 | if (!args["port"].empty()) { |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 299 | port = atoi(args["port"].c_str()); |
| 300 | } |
| 301 | |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame^] | 302 | if (!args["noframe"].empty()) { |
| 303 | frameOutput = false; |
| 304 | } |
| 305 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 306 | if (!args["server-type"].empty()) { |
| 307 | serverType = args["server-type"]; |
| 308 | if (serverType == "simple") { |
| 309 | } else if (serverType == "thread-pool") { |
Mark Slee | b9acf98 | 2006-10-10 01:57:32 +0000 | [diff] [blame] | 310 | } else if (serverType == "nonblocking") { |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 311 | } else { |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 312 | throw invalid_argument("Unknown server type "+serverType); |
| 313 | } |
| 314 | } |
| 315 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 316 | if (!args["protocol-type"].empty()) { |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 317 | protocolType = args["protocol-type"]; |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 318 | if (protocolType == "binary") { |
| 319 | } else if (protocolType == "ascii") { |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 320 | throw invalid_argument("ASCII protocol not supported"); |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 321 | } else if (protocolType == "xml") { |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 322 | throw invalid_argument("XML protocol not supported"); |
| 323 | } else { |
| 324 | throw invalid_argument("Unknown protocol type "+protocolType); |
| 325 | } |
| 326 | } |
| 327 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 328 | if (!args["workers"].empty()) { |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 329 | workerCount = atoi(args["workers"].c_str()); |
| 330 | } |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 331 | } catch (exception& e) { |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 332 | cerr << e.what() << endl; |
| 333 | cerr << usage; |
| 334 | } |
| 335 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 336 | // Dispatcher |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame^] | 337 | shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory()); |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 338 | |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 339 | shared_ptr<TestHandler> testHandler(new TestHandler()); |
| 340 | |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame^] | 341 | shared_ptr<ThriftTestProcessor> testProcessor(new ThriftTestProcessor(testHandler)); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 342 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 343 | // Transport |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 344 | shared_ptr<TServerSocket> serverSocket(new TServerSocket(port)); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 345 | |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 346 | // Factory |
| 347 | shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory()); |
| 348 | |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 349 | if (serverType == "simple") { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 350 | |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 351 | // Server |
Mark Slee | 018b699 | 2006-09-07 21:31:12 +0000 | [diff] [blame] | 352 | TSimpleServer simpleServer(testProcessor, |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 353 | serverSocket, |
| 354 | transportFactory, |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame^] | 355 | protocolFactory); |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 356 | |
| 357 | printf("Starting the server on port %d...\n", port); |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 358 | simpleServer.serve(); |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 359 | |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 360 | } else if (serverType == "thread-pool") { |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 361 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 362 | shared_ptr<ThreadManager> threadManager = |
| 363 | ThreadManager::newSimpleThreadManager(workerCount); |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 364 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 365 | shared_ptr<PosixThreadFactory> threadFactory = |
| 366 | shared_ptr<PosixThreadFactory>(new PosixThreadFactory()); |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 367 | |
| 368 | threadManager->threadFactory(threadFactory); |
| 369 | |
| 370 | threadManager->start(); |
| 371 | |
Mark Slee | 018b699 | 2006-09-07 21:31:12 +0000 | [diff] [blame] | 372 | TThreadPoolServer threadPoolServer(testProcessor, |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 373 | serverSocket, |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 374 | transportFactory, |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame^] | 375 | protocolFactory, |
| 376 | threadManager); |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 377 | |
| 378 | printf("Starting the server on port %d...\n", port); |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 379 | threadPoolServer.serve(); |
Mark Slee | b9acf98 | 2006-10-10 01:57:32 +0000 | [diff] [blame] | 380 | |
| 381 | } else if (serverType == "nonblocking") { |
| 382 | |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame^] | 383 | TNonblockingServer nonblockingServer(testProcessor, port); |
| 384 | nonblockingServer.setFrameResponses(frameOutput); |
| 385 | if (frameOutput) { |
| 386 | printf("Using framed output mode\n"); |
| 387 | } else { |
| 388 | printf("Using non-framed output mode\n"); |
| 389 | } |
| 390 | |
Mark Slee | b9acf98 | 2006-10-10 01:57:32 +0000 | [diff] [blame] | 391 | printf("Starting the nonblocking server on port %d...\n", port); |
| 392 | nonblockingServer.serve(); |
| 393 | |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 396 | printf("done.\n"); |
| 397 | return 0; |
| 398 | } |