Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 1 | package com.facebook.thrift.test; |
| 2 | |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 3 | import com.facebook.thrift.TException; |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 4 | import com.facebook.thrift.protocol.TBinaryProtocol; |
| 5 | import com.facebook.thrift.protocol.TProtocol; |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 6 | import com.facebook.thrift.server.TServer; |
| 7 | import com.facebook.thrift.server.TSimpleServer; |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame^] | 8 | import com.facebook.thrift.server.TThreadPoolServer; |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 9 | import com.facebook.thrift.transport.TServerSocket; |
| 10 | import com.facebook.thrift.transport.TServerTransport; |
| 11 | |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 12 | // Generated code |
| 13 | import thrift.test.*; |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 14 | |
| 15 | import java.net.ServerSocket; |
| 16 | import java.util.ArrayList; |
| 17 | import java.util.HashMap; |
| 18 | import java.util.HashSet; |
| 19 | |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 20 | public class TestServer { |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 21 | |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 22 | public static class TestHandler implements ThriftTest.Iface { |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 23 | |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 24 | public TestHandler() {} |
| 25 | |
| 26 | public void testVoid() { |
| 27 | System.out.print("testVoid()\n"); |
| 28 | } |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 29 | |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 30 | public String testString(String thing) { |
| 31 | System.out.print("testString(\"" + thing + "\")\n"); |
| 32 | return thing; |
| 33 | } |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 34 | |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 35 | public byte testByte(byte thing) { |
| 36 | System.out.print("testByte(" + thing + ")\n"); |
| 37 | return thing; |
| 38 | } |
| 39 | |
| 40 | public int testI32(int thing) { |
| 41 | System.out.print("testI32(" + thing + ")\n"); |
| 42 | return thing; |
| 43 | } |
| 44 | |
| 45 | public long testI64(long thing) { |
| 46 | System.out.print("testI64(" + thing + ")\n"); |
| 47 | return thing; |
| 48 | } |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 49 | |
| 50 | public double testDouble(double thing) { |
| 51 | System.out.print("testDouble(" + thing + ")\n"); |
| 52 | return thing; |
| 53 | } |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 54 | |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 55 | public Xtruct testStruct(Xtruct thing) { |
| 56 | System.out.print("testStruct({" + |
| 57 | "\"" + thing.string_thing + "\", " + |
| 58 | thing.byte_thing + ", " + |
| 59 | thing.i32_thing + ", " + |
| 60 | thing.i64_thing + "})\n"); |
| 61 | return thing; |
| 62 | } |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 63 | |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 64 | public Xtruct2 testNest(Xtruct2 nest) { |
| 65 | Xtruct thing = nest.struct_thing; |
| 66 | System.out.print("testNest({" + |
| 67 | nest.byte_thing + ", {" + |
| 68 | "\"" + thing.string_thing + "\", " + |
| 69 | thing.byte_thing + ", " + |
| 70 | thing.i32_thing + ", " + |
| 71 | thing.i64_thing + "}, " + |
| 72 | nest.i32_thing + "})\n"); |
| 73 | return nest; |
| 74 | } |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 75 | |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 76 | public HashMap<Integer,Integer> testMap(HashMap<Integer,Integer> thing) { |
| 77 | System.out.print("testMap({"); |
| 78 | boolean first = true; |
| 79 | for (int key : thing.keySet()) { |
| 80 | if (first) { |
| 81 | first = false; |
| 82 | } else { |
| 83 | System.out.print(", "); |
| 84 | } |
| 85 | System.out.print(key + " => " + thing.get(key)); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 86 | } |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 87 | System.out.print("})\n"); |
| 88 | return thing; |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 89 | } |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 90 | |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 91 | public HashSet<Integer> testSet(HashSet<Integer> thing) { |
| 92 | System.out.print("testSet({"); |
| 93 | boolean first = true; |
| 94 | for (int elem : thing) { |
| 95 | if (first) { |
| 96 | first = false; |
| 97 | } else { |
| 98 | System.out.print(", "); |
| 99 | } |
| 100 | System.out.print(elem); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 101 | } |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 102 | System.out.print("})\n"); |
| 103 | return thing; |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 104 | } |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 105 | |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 106 | public ArrayList<Integer> testList(ArrayList<Integer> thing) { |
| 107 | System.out.print("testList({"); |
| 108 | boolean first = true; |
| 109 | for (int elem : thing) { |
| 110 | if (first) { |
| 111 | first = false; |
| 112 | } else { |
| 113 | System.out.print(", "); |
| 114 | } |
| 115 | System.out.print(elem); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 116 | } |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 117 | System.out.print("})\n"); |
| 118 | return thing; |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 121 | public int testEnum(int thing) { |
| 122 | System.out.print("testEnum(" + thing + ")\n"); |
| 123 | return thing; |
| 124 | } |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 125 | |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 126 | public long testTypedef(long thing) { |
| 127 | System.out.print("testTypedef(" + thing + ")\n"); |
| 128 | return thing; |
| 129 | } |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 130 | |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 131 | public HashMap<Integer,HashMap<Integer,Integer>> testMapMap(int hello) { |
| 132 | System.out.print("testMapMap(" + hello + ")\n"); |
| 133 | HashMap<Integer,HashMap<Integer,Integer>> mapmap = |
| 134 | new HashMap<Integer,HashMap<Integer,Integer>>(); |
| 135 | |
| 136 | HashMap<Integer,Integer> pos = new HashMap<Integer,Integer>(); |
| 137 | HashMap<Integer,Integer> neg = new HashMap<Integer,Integer>(); |
| 138 | for (int i = 1; i < 5; i++) { |
| 139 | pos.put(i, i); |
| 140 | neg.put(-i, -i); |
| 141 | } |
| 142 | |
| 143 | mapmap.put(4, pos); |
| 144 | mapmap.put(-4, neg); |
| 145 | |
| 146 | return mapmap; |
| 147 | } |
| 148 | |
| 149 | public HashMap<Long, HashMap<Integer,Insanity>> testInsanity(Insanity argument) { |
| 150 | System.out.print("testInsanity()\n"); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 151 | |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 152 | Xtruct hello = new Xtruct(); |
| 153 | hello.string_thing = "Hello2"; |
| 154 | hello.byte_thing = 2; |
| 155 | hello.i32_thing = 2; |
| 156 | hello.i64_thing = 2; |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 157 | |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 158 | Xtruct goodbye = new Xtruct(); |
| 159 | goodbye.string_thing = "Goodbye4"; |
| 160 | goodbye.byte_thing = (byte)4; |
| 161 | goodbye.i32_thing = 4; |
| 162 | goodbye.i64_thing = (long)4; |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 163 | |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 164 | Insanity crazy = new Insanity(); |
| 165 | crazy.userMap.put(Numberz.EIGHT, (long)8); |
| 166 | crazy.xtructs.add(goodbye); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 167 | |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 168 | Insanity looney = new Insanity(); |
| 169 | crazy.userMap.put(Numberz.FIVE, (long)5); |
| 170 | crazy.xtructs.add(hello); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 171 | |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 172 | HashMap<Integer,Insanity> first_map = new HashMap<Integer, Insanity>(); |
| 173 | HashMap<Integer,Insanity> second_map = new HashMap<Integer, Insanity>();; |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 174 | |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 175 | first_map.put(Numberz.TWO, crazy); |
| 176 | first_map.put(Numberz.THREE, crazy); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 177 | |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 178 | second_map.put(Numberz.SIX, looney); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 179 | |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 180 | HashMap<Long,HashMap<Integer,Insanity>> insane = |
| 181 | new HashMap<Long, HashMap<Integer,Insanity>>(); |
| 182 | insane.put((long)1, first_map); |
| 183 | insane.put((long)2, second_map); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 184 | |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 185 | return insane; |
| 186 | } |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 187 | |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 188 | public Xtruct testMulti(byte arg0, int arg1, long arg2, HashMap<Short,String> arg3, int arg4, long arg5) { |
| 189 | System.out.print("testMulti()\n"); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 190 | |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 191 | Xtruct hello = new Xtruct();; |
| 192 | hello.string_thing = "Hello2"; |
| 193 | hello.byte_thing = arg0; |
| 194 | hello.i32_thing = arg1; |
| 195 | hello.i64_thing = arg2; |
| 196 | return hello; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 197 | } |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 198 | |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 199 | public void testException(String arg) throws Xception { |
| 200 | System.out.print("testException("+arg+")\n"); |
| 201 | if (arg.equals("Xception")) { |
| 202 | Xception x = new Xception(); |
| 203 | x.errorCode = 1001; |
| 204 | x.message = "This is an Xception"; |
| 205 | throw x; |
| 206 | } |
| 207 | return; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 208 | } |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 209 | |
| 210 | public Xtruct testMultiException(String arg0, String arg1) throws Xception, Xception2 { |
| 211 | System.out.print("testMultiException(" + arg0 + ", " + arg1 + ")\n"); |
| 212 | if (arg0.equals("Xception")) { |
| 213 | Xception x = new Xception(); |
| 214 | x.errorCode = 1001; |
| 215 | x.message = "This is an Xception"; |
| 216 | throw x; |
| 217 | } else if (arg0.equals("Xception2")) { |
| 218 | Xception2 x = new Xception2(); |
| 219 | x.errorCode = 2002; |
| 220 | x.struct_thing = new Xtruct(); |
| 221 | x.struct_thing.string_thing = "This is an Xception2"; |
| 222 | throw x; |
| 223 | } |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 224 | |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 225 | Xtruct result = new Xtruct(); |
| 226 | result.string_thing = arg1; |
| 227 | return result; |
| 228 | } |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 229 | |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 230 | } // class TestHandler |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 231 | |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 232 | public static void main(String [] args) { |
| 233 | try { |
| 234 | int port = 9090; |
| 235 | if (args.length > 1) { |
| 236 | port = Integer.valueOf(args[0]); |
| 237 | } |
| 238 | |
| 239 | // Processor |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 240 | TBinaryProtocol binaryProtocol = |
| 241 | new TBinaryProtocol(); |
| 242 | TestHandler testHandler = |
| 243 | new TestHandler(); |
| 244 | ThriftTest.Server testServer = |
| 245 | new ThriftTest.Server(testHandler, binaryProtocol); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 246 | |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 247 | // Transport |
Mark Slee | d265552 | 2006-09-05 22:09:57 +0000 | [diff] [blame] | 248 | TServerSocket tServerSocket = |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame^] | 249 | new TServerSocket(port); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 250 | |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame^] | 251 | TServer serverEngine; |
| 252 | |
| 253 | // Simple Server |
| 254 | // serverEngine = new TSimpleServer(testServer, tServerSocket); |
| 255 | |
| 256 | // ThreadPool Server |
| 257 | serverEngine = new TThreadPoolServer(testServer, tServerSocket); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 258 | |
| 259 | // Run it |
| 260 | System.out.println("Starting the server on port " + port + "..."); |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame^] | 261 | serverEngine.run(); |
| 262 | |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 263 | } catch (Exception x) { |
| 264 | x.printStackTrace(); |
| 265 | } |
| 266 | System.out.println("done."); |
| 267 | } |
| 268 | } |