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