blob: 21003b10d4222a33a397b98a125e9cb171b6854e [file] [log] [blame]
Mark Slee95771002006-06-07 06:53:25 +00001package com.facebook.thrift.test;
2
Mark Slee78f58e22006-09-02 04:17:07 +00003import com.facebook.thrift.TException;
Mark Slee95771002006-06-07 06:53:25 +00004import com.facebook.thrift.protocol.TBinaryProtocol;
5import com.facebook.thrift.protocol.TProtocol;
Mark Sleefa6ac9f2007-05-04 18:49:56 +00006import com.facebook.thrift.protocol.TProtocolFactory;
Mark Slee95771002006-06-07 06:53:25 +00007import com.facebook.thrift.server.TServer;
8import com.facebook.thrift.server.TSimpleServer;
Mark Sleeffcddd62006-09-06 20:37:03 +00009import com.facebook.thrift.server.TThreadPoolServer;
Mark Slee95771002006-06-07 06:53:25 +000010import com.facebook.thrift.transport.TServerSocket;
11import com.facebook.thrift.transport.TServerTransport;
12
Mark Slee78f58e22006-09-02 04:17:07 +000013// Generated code
14import thrift.test.*;
Mark Slee95771002006-06-07 06:53:25 +000015
16import java.net.ServerSocket;
Mark Slee22360b22008-02-09 00:18:32 +000017import java.util.List;
Mark Slee95771002006-06-07 06:53:25 +000018import java.util.ArrayList;
Mark Slee22360b22008-02-09 00:18:32 +000019import java.util.Map;
Mark Slee95771002006-06-07 06:53:25 +000020import java.util.HashMap;
Mark Slee22360b22008-02-09 00:18:32 +000021import java.util.Set;
Mark Slee95771002006-06-07 06:53:25 +000022import java.util.HashSet;
23
Mark Sleed2655522006-09-05 22:09:57 +000024public class TestServer {
Mark Slee95771002006-06-07 06:53:25 +000025
Mark Sleed2655522006-09-05 22:09:57 +000026 public static class TestHandler implements ThriftTest.Iface {
Mark Slee95771002006-06-07 06:53:25 +000027
Mark Sleed2655522006-09-05 22:09:57 +000028 public TestHandler() {}
David Reiss0c90f6f2008-02-06 22:18:40 +000029
Mark Sleed2655522006-09-05 22:09:57 +000030 public void testVoid() {
31 System.out.print("testVoid()\n");
32 }
Mark Slee95771002006-06-07 06:53:25 +000033
Mark Sleed2655522006-09-05 22:09:57 +000034 public String testString(String thing) {
35 System.out.print("testString(\"" + thing + "\")\n");
36 return thing;
37 }
Mark Slee95771002006-06-07 06:53:25 +000038
Mark Sleed2655522006-09-05 22:09:57 +000039 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 Sleec98d0502006-09-06 02:42:25 +000053
54 public double testDouble(double thing) {
55 System.out.print("testDouble(" + thing + ")\n");
56 return thing;
57 }
David Reiss0c90f6f2008-02-06 22:18:40 +000058
Mark Sleed2655522006-09-05 22:09:57 +000059 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 Reiss0c90f6f2008-02-06 22:18:40 +000067
Mark Sleed2655522006-09-05 22:09:57 +000068 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 Reiss0c90f6f2008-02-06 22:18:40 +000079
Mark Slee22360b22008-02-09 00:18:32 +000080 public Map<Integer,Integer> testMap(Map<Integer,Integer> thing) {
Mark Sleed2655522006-09-05 22:09:57 +000081 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 Slee95771002006-06-07 06:53:25 +000090 }
Mark Sleed2655522006-09-05 22:09:57 +000091 System.out.print("})\n");
92 return thing;
Mark Slee95771002006-06-07 06:53:25 +000093 }
Mark Slee95771002006-06-07 06:53:25 +000094
Mark Slee22360b22008-02-09 00:18:32 +000095 public Set<Integer> testSet(Set<Integer> thing) {
Mark Sleed2655522006-09-05 22:09:57 +000096 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 Slee95771002006-06-07 06:53:25 +0000105 }
Mark Sleed2655522006-09-05 22:09:57 +0000106 System.out.print("})\n");
107 return thing;
Mark Slee95771002006-06-07 06:53:25 +0000108 }
Mark Slee95771002006-06-07 06:53:25 +0000109
Mark Slee22360b22008-02-09 00:18:32 +0000110 public List<Integer> testList(List<Integer> thing) {
Mark Sleed2655522006-09-05 22:09:57 +0000111 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 Slee95771002006-06-07 06:53:25 +0000120 }
Mark Sleed2655522006-09-05 22:09:57 +0000121 System.out.print("})\n");
122 return thing;
Mark Slee95771002006-06-07 06:53:25 +0000123 }
124
Mark Sleed2655522006-09-05 22:09:57 +0000125 public int testEnum(int thing) {
126 System.out.print("testEnum(" + thing + ")\n");
127 return thing;
128 }
Mark Slee95771002006-06-07 06:53:25 +0000129
Mark Sleed2655522006-09-05 22:09:57 +0000130 public long testTypedef(long thing) {
131 System.out.print("testTypedef(" + thing + ")\n");
132 return thing;
133 }
Mark Slee95771002006-06-07 06:53:25 +0000134
Mark Slee22360b22008-02-09 00:18:32 +0000135 public Map<Integer,Map<Integer,Integer>> testMapMap(int hello) {
Mark Sleed2655522006-09-05 22:09:57 +0000136 System.out.print("testMapMap(" + hello + ")\n");
Mark Slee22360b22008-02-09 00:18:32 +0000137 Map<Integer,Map<Integer,Integer>> mapmap =
138 new HashMap<Integer,Map<Integer,Integer>>();
Mark Sleed2655522006-09-05 22:09:57 +0000139
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 Slee22360b22008-02-09 00:18:32 +0000153 public Map<Long, Map<Integer,Insanity>> testInsanity(Insanity argument) {
Mark Sleed2655522006-09-05 22:09:57 +0000154 System.out.print("testInsanity()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000155
Mark Sleed2655522006-09-05 22:09:57 +0000156 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 Slee95771002006-06-07 06:53:25 +0000161
Mark Sleed2655522006-09-05 22:09:57 +0000162 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 Slee95771002006-06-07 06:53:25 +0000167
Mark Sleed2655522006-09-05 22:09:57 +0000168 Insanity crazy = new Insanity();
Mark Sleecbc51602007-04-11 08:57:07 +0000169 crazy.userMap = new HashMap<Integer, Long>();
170 crazy.xtructs = new ArrayList<Xtruct>();
171
Mark Sleed2655522006-09-05 22:09:57 +0000172 crazy.userMap.put(Numberz.EIGHT, (long)8);
173 crazy.xtructs.add(goodbye);
Mark Slee95771002006-06-07 06:53:25 +0000174
Mark Sleed2655522006-09-05 22:09:57 +0000175 Insanity looney = new Insanity();
176 crazy.userMap.put(Numberz.FIVE, (long)5);
177 crazy.xtructs.add(hello);
Mark Slee95771002006-06-07 06:53:25 +0000178
Mark Sleed2655522006-09-05 22:09:57 +0000179 HashMap<Integer,Insanity> first_map = new HashMap<Integer, Insanity>();
180 HashMap<Integer,Insanity> second_map = new HashMap<Integer, Insanity>();;
Mark Slee95771002006-06-07 06:53:25 +0000181
Mark Sleed2655522006-09-05 22:09:57 +0000182 first_map.put(Numberz.TWO, crazy);
183 first_map.put(Numberz.THREE, crazy);
Mark Slee95771002006-06-07 06:53:25 +0000184
Mark Sleed2655522006-09-05 22:09:57 +0000185 second_map.put(Numberz.SIX, looney);
Mark Slee95771002006-06-07 06:53:25 +0000186
Mark Slee22360b22008-02-09 00:18:32 +0000187 Map<Long,Map<Integer,Insanity>> insane =
188 new HashMap<Long, Map<Integer,Insanity>>();
Mark Sleed2655522006-09-05 22:09:57 +0000189 insane.put((long)1, first_map);
190 insane.put((long)2, second_map);
Mark Slee95771002006-06-07 06:53:25 +0000191
Mark Sleed2655522006-09-05 22:09:57 +0000192 return insane;
193 }
Mark Slee95771002006-06-07 06:53:25 +0000194
Mark Slee22360b22008-02-09 00:18:32 +0000195 public Xtruct testMulti(byte arg0, int arg1, long arg2, Map<Short,String> arg3, int arg4, long arg5) {
Mark Sleed2655522006-09-05 22:09:57 +0000196 System.out.print("testMulti()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000197
Mark Sleed2655522006-09-05 22:09:57 +0000198 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 Slee78f58e22006-09-02 04:17:07 +0000204 }
Mark Slee78f58e22006-09-02 04:17:07 +0000205
Mark Sleed2655522006-09-05 22:09:57 +0000206 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 Slee78f58e22006-09-02 04:17:07 +0000215 }
Mark Sleed2655522006-09-05 22:09:57 +0000216
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 Reiss0c90f6f2008-02-06 22:18:40 +0000231
Mark Sleed2655522006-09-05 22:09:57 +0000232 Xtruct result = new Xtruct();
233 result.string_thing = arg1;
234 return result;
235 }
Mark Slee78f58e22006-09-02 04:17:07 +0000236
Mark Sleed2655522006-09-05 22:09:57 +0000237 } // class TestHandler
Mark Slee78f58e22006-09-02 04:17:07 +0000238
Mark Slee95771002006-06-07 06:53:25 +0000239 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 Reiss0c90f6f2008-02-06 22:18:40 +0000245
Mark Slee95771002006-06-07 06:53:25 +0000246 // Processor
Mark Sleed2655522006-09-05 22:09:57 +0000247 TestHandler testHandler =
248 new TestHandler();
Mark Slee018b6992006-09-07 21:31:12 +0000249 ThriftTest.Processor testProcessor =
Mark Slee1dd819c2006-10-26 04:56:18 +0000250 new ThriftTest.Processor(testHandler);
Mark Slee95771002006-06-07 06:53:25 +0000251
Mark Slee95771002006-06-07 06:53:25 +0000252 // Transport
Mark Sleed2655522006-09-05 22:09:57 +0000253 TServerSocket tServerSocket =
Mark Sleeffcddd62006-09-06 20:37:03 +0000254 new TServerSocket(port);
Mark Slee95771002006-06-07 06:53:25 +0000255
Mark Sleefa6ac9f2007-05-04 18:49:56 +0000256 // Protocol factory
257 TProtocolFactory tProtocolFactory =
Mark Slee7dafc942007-08-09 03:40:15 +0000258 new TBinaryProtocol.Factory();
Mark Sleefa6ac9f2007-05-04 18:49:56 +0000259
Mark Sleeffcddd62006-09-06 20:37:03 +0000260 TServer serverEngine;
261
262 // Simple Server
Mark Slee018b6992006-09-07 21:31:12 +0000263 // serverEngine = new TSimpleServer(testProcessor, tServerSocket);
Mark Sleeffcddd62006-09-06 20:37:03 +0000264
265 // ThreadPool Server
Mark Sleefa6ac9f2007-05-04 18:49:56 +0000266 serverEngine = new TThreadPoolServer(testProcessor, tServerSocket, tProtocolFactory);
Mark Slee95771002006-06-07 06:53:25 +0000267
268 // Run it
269 System.out.println("Starting the server on port " + port + "...");
Mark Slee4e755ca2006-09-12 00:46:08 +0000270 serverEngine.serve();
Mark Sleeffcddd62006-09-06 20:37:03 +0000271
Mark Slee95771002006-06-07 06:53:25 +0000272 } catch (Exception x) {
273 x.printStackTrace();
274 }
275 System.out.println("done.");
276 }
277}