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