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