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