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