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