blob: a4c4a376d7305542108541ca3d20db82c829d006 [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;
17import java.util.ArrayList;
Mark Sleea7747c22007-01-29 17:35:54 +000018import java.util.AbstractMap;
Mark Slee95771002006-06-07 06:53:25 +000019import java.util.HashMap;
20import java.util.HashSet;
21
Mark Sleed2655522006-09-05 22:09:57 +000022public class TestServer {
Mark Slee95771002006-06-07 06:53:25 +000023
Mark Sleed2655522006-09-05 22:09:57 +000024 public static class TestHandler implements ThriftTest.Iface {
Mark Slee95771002006-06-07 06:53:25 +000025
Mark Sleed2655522006-09-05 22:09:57 +000026 public TestHandler() {}
27
28 public void testVoid() {
29 System.out.print("testVoid()\n");
30 }
Mark Slee95771002006-06-07 06:53:25 +000031
Mark Sleed2655522006-09-05 22:09:57 +000032 public String testString(String thing) {
33 System.out.print("testString(\"" + thing + "\")\n");
34 return thing;
35 }
Mark Slee95771002006-06-07 06:53:25 +000036
Mark Sleed2655522006-09-05 22:09:57 +000037 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 Sleec98d0502006-09-06 02:42:25 +000051
52 public double testDouble(double thing) {
53 System.out.print("testDouble(" + thing + ")\n");
54 return thing;
55 }
Mark Slee95771002006-06-07 06:53:25 +000056
Mark Sleed2655522006-09-05 22:09:57 +000057 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 Slee95771002006-06-07 06:53:25 +000065
Mark Sleed2655522006-09-05 22:09:57 +000066 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 Slee95771002006-06-07 06:53:25 +000077
Mark Sleea7747c22007-01-29 17:35:54 +000078 public AbstractMap<Integer,Integer> testMap(AbstractMap<Integer,Integer> thing) {
Mark Sleed2655522006-09-05 22:09:57 +000079 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 Slee95771002006-06-07 06:53:25 +000088 }
Mark Sleed2655522006-09-05 22:09:57 +000089 System.out.print("})\n");
90 return thing;
Mark Slee95771002006-06-07 06:53:25 +000091 }
Mark Slee95771002006-06-07 06:53:25 +000092
Mark Sleed2655522006-09-05 22:09:57 +000093 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 Slee95771002006-06-07 06:53:25 +0000103 }
Mark Sleed2655522006-09-05 22:09:57 +0000104 System.out.print("})\n");
105 return thing;
Mark Slee95771002006-06-07 06:53:25 +0000106 }
Mark Slee95771002006-06-07 06:53:25 +0000107
Mark Sleed2655522006-09-05 22:09:57 +0000108 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 Slee95771002006-06-07 06:53:25 +0000118 }
Mark Sleed2655522006-09-05 22:09:57 +0000119 System.out.print("})\n");
120 return thing;
Mark Slee95771002006-06-07 06:53:25 +0000121 }
122
Mark Sleed2655522006-09-05 22:09:57 +0000123 public int testEnum(int thing) {
124 System.out.print("testEnum(" + thing + ")\n");
125 return thing;
126 }
Mark Slee95771002006-06-07 06:53:25 +0000127
Mark Sleed2655522006-09-05 22:09:57 +0000128 public long testTypedef(long thing) {
129 System.out.print("testTypedef(" + thing + ")\n");
130 return thing;
131 }
Mark Slee95771002006-06-07 06:53:25 +0000132
Mark Sleea7747c22007-01-29 17:35:54 +0000133 public AbstractMap<Integer,AbstractMap<Integer,Integer>> testMapMap(int hello) {
Mark Sleed2655522006-09-05 22:09:57 +0000134 System.out.print("testMapMap(" + hello + ")\n");
Mark Sleea7747c22007-01-29 17:35:54 +0000135 AbstractMap<Integer,AbstractMap<Integer,Integer>> mapmap =
136 new HashMap<Integer,AbstractMap<Integer,Integer>>();
Mark Sleed2655522006-09-05 22:09:57 +0000137
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 Sleea7747c22007-01-29 17:35:54 +0000151 public AbstractMap<Long, AbstractMap<Integer,Insanity>> testInsanity(Insanity argument) {
Mark Sleed2655522006-09-05 22:09:57 +0000152 System.out.print("testInsanity()\n");
Mark Slee95771002006-06-07 06:53:25 +0000153
Mark Sleed2655522006-09-05 22:09:57 +0000154 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 Slee95771002006-06-07 06:53:25 +0000159
Mark Sleed2655522006-09-05 22:09:57 +0000160 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 Slee95771002006-06-07 06:53:25 +0000165
Mark Sleed2655522006-09-05 22:09:57 +0000166 Insanity crazy = new Insanity();
Mark Sleecbc51602007-04-11 08:57:07 +0000167 crazy.userMap = new HashMap<Integer, Long>();
168 crazy.xtructs = new ArrayList<Xtruct>();
169
Mark Sleed2655522006-09-05 22:09:57 +0000170 crazy.userMap.put(Numberz.EIGHT, (long)8);
171 crazy.xtructs.add(goodbye);
Mark Slee95771002006-06-07 06:53:25 +0000172
Mark Sleed2655522006-09-05 22:09:57 +0000173 Insanity looney = new Insanity();
174 crazy.userMap.put(Numberz.FIVE, (long)5);
175 crazy.xtructs.add(hello);
Mark Slee95771002006-06-07 06:53:25 +0000176
Mark Sleed2655522006-09-05 22:09:57 +0000177 HashMap<Integer,Insanity> first_map = new HashMap<Integer, Insanity>();
178 HashMap<Integer,Insanity> second_map = new HashMap<Integer, Insanity>();;
Mark Slee95771002006-06-07 06:53:25 +0000179
Mark Sleed2655522006-09-05 22:09:57 +0000180 first_map.put(Numberz.TWO, crazy);
181 first_map.put(Numberz.THREE, crazy);
Mark Slee95771002006-06-07 06:53:25 +0000182
Mark Sleed2655522006-09-05 22:09:57 +0000183 second_map.put(Numberz.SIX, looney);
Mark Slee95771002006-06-07 06:53:25 +0000184
Mark Sleea7747c22007-01-29 17:35:54 +0000185 AbstractMap<Long,AbstractMap<Integer,Insanity>> insane =
186 new HashMap<Long, AbstractMap<Integer,Insanity>>();
Mark Sleed2655522006-09-05 22:09:57 +0000187 insane.put((long)1, first_map);
188 insane.put((long)2, second_map);
Mark Slee95771002006-06-07 06:53:25 +0000189
Mark Sleed2655522006-09-05 22:09:57 +0000190 return insane;
191 }
Mark Slee95771002006-06-07 06:53:25 +0000192
Mark Sleea7747c22007-01-29 17:35:54 +0000193 public Xtruct testMulti(byte arg0, int arg1, long arg2, AbstractMap<Short,String> arg3, int arg4, long arg5) {
Mark Sleed2655522006-09-05 22:09:57 +0000194 System.out.print("testMulti()\n");
Mark Slee78f58e22006-09-02 04:17:07 +0000195
Mark Sleed2655522006-09-05 22:09:57 +0000196 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 Slee78f58e22006-09-02 04:17:07 +0000202 }
Mark Slee78f58e22006-09-02 04:17:07 +0000203
Mark Sleed2655522006-09-05 22:09:57 +0000204 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 Slee78f58e22006-09-02 04:17:07 +0000213 }
Mark Sleed2655522006-09-05 22:09:57 +0000214
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 Slee78f58e22006-09-02 04:17:07 +0000229
Mark Sleed2655522006-09-05 22:09:57 +0000230 Xtruct result = new Xtruct();
231 result.string_thing = arg1;
232 return result;
233 }
Mark Slee78f58e22006-09-02 04:17:07 +0000234
Mark Sleed2655522006-09-05 22:09:57 +0000235 } // class TestHandler
Mark Slee78f58e22006-09-02 04:17:07 +0000236
Mark Slee95771002006-06-07 06:53:25 +0000237 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 Sleed2655522006-09-05 22:09:57 +0000245 TestHandler testHandler =
246 new TestHandler();
Mark Slee018b6992006-09-07 21:31:12 +0000247 ThriftTest.Processor testProcessor =
Mark Slee1dd819c2006-10-26 04:56:18 +0000248 new ThriftTest.Processor(testHandler);
Mark Slee95771002006-06-07 06:53:25 +0000249
Mark Slee95771002006-06-07 06:53:25 +0000250 // Transport
Mark Sleed2655522006-09-05 22:09:57 +0000251 TServerSocket tServerSocket =
Mark Sleeffcddd62006-09-06 20:37:03 +0000252 new TServerSocket(port);
Mark Slee95771002006-06-07 06:53:25 +0000253
Mark Sleefa6ac9f2007-05-04 18:49:56 +0000254 // Protocol factory
255 TProtocolFactory tProtocolFactory =
256 new TBinaryProtocol.Factory(true, true);
257
Mark Sleeffcddd62006-09-06 20:37:03 +0000258 TServer serverEngine;
259
260 // Simple Server
Mark Slee018b6992006-09-07 21:31:12 +0000261 // serverEngine = new TSimpleServer(testProcessor, tServerSocket);
Mark Sleeffcddd62006-09-06 20:37:03 +0000262
263 // ThreadPool Server
Mark Sleefa6ac9f2007-05-04 18:49:56 +0000264 serverEngine = new TThreadPoolServer(testProcessor, tServerSocket, tProtocolFactory);
Mark Slee95771002006-06-07 06:53:25 +0000265
266 // Run it
267 System.out.println("Starting the server on port " + port + "...");
Mark Slee4e755ca2006-09-12 00:46:08 +0000268 serverEngine.serve();
Mark Sleeffcddd62006-09-06 20:37:03 +0000269
Mark Slee95771002006-06-07 06:53:25 +0000270 } catch (Exception x) {
271 x.printStackTrace();
272 }
273 System.out.println("done.");
274 }
275}