Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include "protocol/TBinaryProtocol.h" |
| 3 | #include "server/TSimpleServer.h" |
| 4 | #include "transport/TServerSocket.h" |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame^] | 5 | #include "ThriftTest.h" |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 6 | using namespace std; |
| 7 | |
| 8 | class TestServer : public ThriftTestServerIf { |
| 9 | public: |
| 10 | TestServer(TProtocol* protocol) : |
| 11 | ThriftTestServerIf(protocol) {} |
| 12 | |
| 13 | void testVoid() { |
| 14 | printf("testVoid()\n"); |
| 15 | } |
| 16 | |
| 17 | string testString(string thing) { |
| 18 | printf("testString(\"%s\")\n", thing.c_str()); |
| 19 | return thing; |
| 20 | } |
| 21 | |
| 22 | uint8_t testByte(uint8_t thing) { |
| 23 | printf("testByte(%d)\n", (int)thing); |
| 24 | return thing; |
| 25 | } |
| 26 | |
| 27 | uint32_t testU32(uint32_t thing) { |
| 28 | printf("testU32(%u)\n", thing); |
| 29 | return thing; |
| 30 | } |
| 31 | |
| 32 | int32_t testI32(int32_t thing) { |
| 33 | printf("testI32(%d)\n", thing); |
| 34 | return thing; |
| 35 | } |
| 36 | |
| 37 | uint64_t testU64(uint64_t thing) { |
| 38 | printf("testU64(%lu)\n", thing); |
| 39 | return thing; |
| 40 | } |
| 41 | |
| 42 | int64_t testI64(int64_t thing) { |
| 43 | printf("testI64(%ld)\n", thing); |
| 44 | return thing; |
| 45 | } |
| 46 | |
| 47 | Xtruct testStruct(Xtruct thing) { |
| 48 | printf("testStruct({\"%s\", %d, %u, %d, %lu, %ld})\n", |
| 49 | thing.string_thing.c_str(), |
| 50 | (int)thing.byte_thing, |
| 51 | thing.u32_thing, |
| 52 | thing.i32_thing, |
| 53 | thing.u64_thing, |
| 54 | thing.i64_thing); |
| 55 | return thing; |
| 56 | } |
| 57 | |
| 58 | Xtruct2 testNest(Xtruct2 nest) { |
| 59 | Xtruct thing = nest.struct_thing; |
| 60 | printf("testNest({%d, {\"%s\", %d, %u, %d, %lu, %ld}, %d})\n", |
| 61 | (int)nest.byte_thing, |
| 62 | thing.string_thing.c_str(), |
| 63 | (int)thing.byte_thing, |
| 64 | thing.u32_thing, |
| 65 | thing.i32_thing, |
| 66 | thing.u64_thing, |
| 67 | thing.i64_thing, |
| 68 | nest.i32_thing); |
| 69 | return nest; |
| 70 | } |
| 71 | |
| 72 | map<int32_t, int32_t> testMap(map<int32_t, int32_t> thing) { |
| 73 | printf("testMap({"); |
| 74 | map<int32_t, int32_t>::const_iterator m_iter; |
| 75 | bool first = true; |
| 76 | for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) { |
| 77 | if (first) { |
| 78 | first = false; |
| 79 | } else { |
| 80 | printf(", "); |
| 81 | } |
| 82 | printf("%d => %d", m_iter->first, m_iter->second); |
| 83 | } |
| 84 | printf("})\n"); |
| 85 | return thing; |
| 86 | } |
| 87 | |
| 88 | set<int32_t> testSet(set<int32_t> thing) { |
| 89 | printf("testSet({"); |
| 90 | set<int32_t>::const_iterator s_iter; |
| 91 | bool first = true; |
| 92 | for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) { |
| 93 | if (first) { |
| 94 | first = false; |
| 95 | } else { |
| 96 | printf(", "); |
| 97 | } |
| 98 | printf("%d", *s_iter); |
| 99 | } |
| 100 | printf("})\n"); |
| 101 | return thing; |
| 102 | } |
| 103 | |
| 104 | list<int32_t> testList(list<int32_t> thing) { |
| 105 | printf("testList({"); |
| 106 | list<int32_t>::const_iterator l_iter; |
| 107 | bool first = true; |
| 108 | for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) { |
| 109 | if (first) { |
| 110 | first = false; |
| 111 | } else { |
| 112 | printf(", "); |
| 113 | } |
| 114 | printf("%d", *l_iter); |
| 115 | } |
| 116 | printf("})\n"); |
| 117 | return thing; |
| 118 | } |
| 119 | |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame^] | 120 | Numberz testEnum(Numberz thing) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 121 | printf("testEnum(%d)\n", thing); |
| 122 | return thing; |
| 123 | } |
| 124 | |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame^] | 125 | UserId testTypedef(UserId thing) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 126 | printf("testTypedef(%lu)\n", thing); |
| 127 | return thing; |
| 128 | } |
| 129 | |
| 130 | map<int32_t, map<int32_t,int32_t> > testMapMap(int32_t hello) { |
| 131 | printf("testMapMap(%d)\n", hello); |
| 132 | map<int32_t, map<int32_t,int32_t> > mapmap; |
| 133 | |
| 134 | map<int32_t,int32_t> pos; |
| 135 | map<int32_t,int32_t> neg; |
| 136 | for (int i = 1; i < 5; i++) { |
| 137 | pos.insert(make_pair(i,i)); |
| 138 | neg.insert(make_pair(-i,-i)); |
| 139 | } |
| 140 | |
| 141 | mapmap.insert(make_pair(4, pos)); |
| 142 | mapmap.insert(make_pair(-4, neg)); |
| 143 | |
| 144 | return mapmap; |
| 145 | } |
| 146 | |
| 147 | map<UserId, map<Numberz,Insanity> > testInsanity(Insanity argument) { |
| 148 | printf("testInsanity()\n"); |
| 149 | |
| 150 | Xtruct hello; |
| 151 | hello.string_thing = "Hello2"; |
| 152 | hello.byte_thing = 2; |
| 153 | hello.u32_thing = 2; |
| 154 | hello.i32_thing = 2; |
| 155 | hello.u64_thing = 2; |
| 156 | hello.i64_thing = 2; |
| 157 | |
| 158 | Xtruct goodbye; |
| 159 | goodbye.string_thing = "Goodbye4"; |
| 160 | goodbye.byte_thing = 4; |
| 161 | goodbye.u32_thing = 4; |
| 162 | goodbye.i32_thing = 4; |
| 163 | goodbye.u64_thing = 4; |
| 164 | goodbye.i64_thing = 4; |
| 165 | |
| 166 | Insanity crazy; |
| 167 | crazy.userMap.insert(make_pair(EIGHT, 8)); |
| 168 | crazy.xtructs.push_back(goodbye); |
| 169 | |
| 170 | Insanity looney; |
| 171 | crazy.userMap.insert(make_pair(FIVE, 5)); |
| 172 | crazy.xtructs.push_back(hello); |
| 173 | |
| 174 | map<Numberz, Insanity> first_map; |
| 175 | map<Numberz, Insanity> second_map; |
| 176 | |
| 177 | first_map.insert(make_pair(TWO, crazy)); |
| 178 | first_map.insert(make_pair(THREE, crazy)); |
| 179 | |
| 180 | second_map.insert(make_pair(SIX, looney)); |
| 181 | |
| 182 | map<UserId, map<Numberz,Insanity> > insane; |
| 183 | insane.insert(make_pair(1, first_map)); |
| 184 | insane.insert(make_pair(2, second_map)); |
| 185 | |
| 186 | printf("return"); |
| 187 | printf(" = {"); |
| 188 | map<UserId, map<Numberz,Insanity> >::const_iterator i_iter; |
| 189 | for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) { |
| 190 | printf("%lu => {", i_iter->first); |
| 191 | map<Numberz,Insanity>::const_iterator i2_iter; |
| 192 | for (i2_iter = i_iter->second.begin(); |
| 193 | i2_iter != i_iter->second.end(); |
| 194 | ++i2_iter) { |
| 195 | printf("%d => {", i2_iter->first); |
| 196 | map<Numberz, UserId> userMap = i2_iter->second.userMap; |
| 197 | map<Numberz, UserId>::const_iterator um; |
| 198 | printf("{"); |
| 199 | for (um = userMap.begin(); um != userMap.end(); ++um) { |
| 200 | printf("%d => %lu, ", um->first, um->second); |
| 201 | } |
| 202 | printf("}, "); |
| 203 | |
| 204 | list<Xtruct> xtructs = i2_iter->second.xtructs; |
| 205 | list<Xtruct>::const_iterator x; |
| 206 | printf("{"); |
| 207 | for (x = xtructs.begin(); x != xtructs.end(); ++x) { |
| 208 | printf("{\"%s\", %d, %u, %d, %lu, %ld}, ", |
| 209 | x->string_thing.c_str(), |
| 210 | (int)x->byte_thing, |
| 211 | x->u32_thing, |
| 212 | x->i32_thing, |
| 213 | x->u64_thing, |
| 214 | x->i64_thing); |
| 215 | } |
| 216 | printf("}"); |
| 217 | |
| 218 | printf("}, "); |
| 219 | } |
| 220 | printf("}, "); |
| 221 | } |
| 222 | printf("}\n"); |
| 223 | |
| 224 | return insane; |
| 225 | } |
| 226 | |
| 227 | }; |
| 228 | |
| 229 | int main(int argc, char **argv) { |
| 230 | int port = 9090; |
| 231 | if (argc > 1) { |
| 232 | port = atoi(argv[1]); |
| 233 | } |
| 234 | |
| 235 | // Dispatcher |
| 236 | TBinaryProtocol binaryProtocol; |
| 237 | TestServer testServer(&binaryProtocol); |
| 238 | |
| 239 | // Options |
| 240 | TServerOptions serverOptions; |
| 241 | |
| 242 | // Transport |
| 243 | TServerSocket serverSocket(port); |
| 244 | |
| 245 | // Server |
| 246 | TSimpleServer simpleServer(&testServer, |
| 247 | &serverOptions, |
| 248 | &serverSocket); |
| 249 | |
| 250 | printf("Starting the server on port %d...\n", port); |
| 251 | simpleServer.run(); |
| 252 | printf("done.\n"); |
| 253 | return 0; |
| 254 | } |