blob: b5277770a2760321255575fac21692c1502d5976 [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 Slee95771002006-06-07 06:53:25 +00006import com.facebook.thrift.server.TServer;
7import com.facebook.thrift.server.TSimpleServer;
Mark Sleeffcddd62006-09-06 20:37:03 +00008import com.facebook.thrift.server.TThreadPoolServer;
Mark Slee95771002006-06-07 06:53:25 +00009import com.facebook.thrift.transport.TServerSocket;
10import com.facebook.thrift.transport.TServerTransport;
11
Mark Slee78f58e22006-09-02 04:17:07 +000012// Generated code
13import thrift.test.*;
Mark Slee95771002006-06-07 06:53:25 +000014
15import java.net.ServerSocket;
16import java.util.ArrayList;
17import java.util.HashMap;
18import java.util.HashSet;
19
Mark Sleed2655522006-09-05 22:09:57 +000020public class TestServer {
Mark Slee95771002006-06-07 06:53:25 +000021
Mark Sleed2655522006-09-05 22:09:57 +000022 public static class TestHandler implements ThriftTest.Iface {
Mark Slee95771002006-06-07 06:53:25 +000023
Mark Sleed2655522006-09-05 22:09:57 +000024 public TestHandler() {}
25
26 public void testVoid() {
27 System.out.print("testVoid()\n");
28 }
Mark Slee95771002006-06-07 06:53:25 +000029
Mark Sleed2655522006-09-05 22:09:57 +000030 public String testString(String thing) {
31 System.out.print("testString(\"" + thing + "\")\n");
32 return thing;
33 }
Mark Slee95771002006-06-07 06:53:25 +000034
Mark Sleed2655522006-09-05 22:09:57 +000035 public byte testByte(byte thing) {
36 System.out.print("testByte(" + thing + ")\n");
37 return thing;
38 }
39
40 public int testI32(int thing) {
41 System.out.print("testI32(" + thing + ")\n");
42 return thing;
43 }
44
45 public long testI64(long thing) {
46 System.out.print("testI64(" + thing + ")\n");
47 return thing;
48 }
Mark Sleec98d0502006-09-06 02:42:25 +000049
50 public double testDouble(double thing) {
51 System.out.print("testDouble(" + thing + ")\n");
52 return thing;
53 }
Mark Slee95771002006-06-07 06:53:25 +000054
Mark Sleed2655522006-09-05 22:09:57 +000055 public Xtruct testStruct(Xtruct thing) {
56 System.out.print("testStruct({" +
57 "\"" + thing.string_thing + "\", " +
58 thing.byte_thing + ", " +
59 thing.i32_thing + ", " +
60 thing.i64_thing + "})\n");
61 return thing;
62 }
Mark Slee95771002006-06-07 06:53:25 +000063
Mark Sleed2655522006-09-05 22:09:57 +000064 public Xtruct2 testNest(Xtruct2 nest) {
65 Xtruct thing = nest.struct_thing;
66 System.out.print("testNest({" +
67 nest.byte_thing + ", {" +
68 "\"" + thing.string_thing + "\", " +
69 thing.byte_thing + ", " +
70 thing.i32_thing + ", " +
71 thing.i64_thing + "}, " +
72 nest.i32_thing + "})\n");
73 return nest;
74 }
Mark Slee95771002006-06-07 06:53:25 +000075
Mark Sleed2655522006-09-05 22:09:57 +000076 public HashMap<Integer,Integer> testMap(HashMap<Integer,Integer> thing) {
77 System.out.print("testMap({");
78 boolean first = true;
79 for (int key : thing.keySet()) {
80 if (first) {
81 first = false;
82 } else {
83 System.out.print(", ");
84 }
85 System.out.print(key + " => " + thing.get(key));
Mark Slee95771002006-06-07 06:53:25 +000086 }
Mark Sleed2655522006-09-05 22:09:57 +000087 System.out.print("})\n");
88 return thing;
Mark Slee95771002006-06-07 06:53:25 +000089 }
Mark Slee95771002006-06-07 06:53:25 +000090
Mark Sleed2655522006-09-05 22:09:57 +000091 public HashSet<Integer> testSet(HashSet<Integer> thing) {
92 System.out.print("testSet({");
93 boolean first = true;
94 for (int elem : thing) {
95 if (first) {
96 first = false;
97 } else {
98 System.out.print(", ");
99 }
100 System.out.print(elem);
Mark Slee95771002006-06-07 06:53:25 +0000101 }
Mark Sleed2655522006-09-05 22:09:57 +0000102 System.out.print("})\n");
103 return thing;
Mark Slee95771002006-06-07 06:53:25 +0000104 }
Mark Slee95771002006-06-07 06:53:25 +0000105
Mark Sleed2655522006-09-05 22:09:57 +0000106 public ArrayList<Integer> testList(ArrayList<Integer> thing) {
107 System.out.print("testList({");
108 boolean first = true;
109 for (int elem : thing) {
110 if (first) {
111 first = false;
112 } else {
113 System.out.print(", ");
114 }
115 System.out.print(elem);
Mark Slee95771002006-06-07 06:53:25 +0000116 }
Mark Sleed2655522006-09-05 22:09:57 +0000117 System.out.print("})\n");
118 return thing;
Mark Slee95771002006-06-07 06:53:25 +0000119 }
120
Mark Sleed2655522006-09-05 22:09:57 +0000121 public int testEnum(int thing) {
122 System.out.print("testEnum(" + thing + ")\n");
123 return thing;
124 }
Mark Slee95771002006-06-07 06:53:25 +0000125
Mark Sleed2655522006-09-05 22:09:57 +0000126 public long testTypedef(long thing) {
127 System.out.print("testTypedef(" + thing + ")\n");
128 return thing;
129 }
Mark Slee95771002006-06-07 06:53:25 +0000130
Mark Sleed2655522006-09-05 22:09:57 +0000131 public HashMap<Integer,HashMap<Integer,Integer>> testMapMap(int hello) {
132 System.out.print("testMapMap(" + hello + ")\n");
133 HashMap<Integer,HashMap<Integer,Integer>> mapmap =
134 new HashMap<Integer,HashMap<Integer,Integer>>();
135
136 HashMap<Integer,Integer> pos = new HashMap<Integer,Integer>();
137 HashMap<Integer,Integer> neg = new HashMap<Integer,Integer>();
138 for (int i = 1; i < 5; i++) {
139 pos.put(i, i);
140 neg.put(-i, -i);
141 }
142
143 mapmap.put(4, pos);
144 mapmap.put(-4, neg);
145
146 return mapmap;
147 }
148
149 public HashMap<Long, HashMap<Integer,Insanity>> testInsanity(Insanity argument) {
150 System.out.print("testInsanity()\n");
Mark Slee95771002006-06-07 06:53:25 +0000151
Mark Sleed2655522006-09-05 22:09:57 +0000152 Xtruct hello = new Xtruct();
153 hello.string_thing = "Hello2";
154 hello.byte_thing = 2;
155 hello.i32_thing = 2;
156 hello.i64_thing = 2;
Mark Slee95771002006-06-07 06:53:25 +0000157
Mark Sleed2655522006-09-05 22:09:57 +0000158 Xtruct goodbye = new Xtruct();
159 goodbye.string_thing = "Goodbye4";
160 goodbye.byte_thing = (byte)4;
161 goodbye.i32_thing = 4;
162 goodbye.i64_thing = (long)4;
Mark Slee95771002006-06-07 06:53:25 +0000163
Mark Sleed2655522006-09-05 22:09:57 +0000164 Insanity crazy = new Insanity();
165 crazy.userMap.put(Numberz.EIGHT, (long)8);
166 crazy.xtructs.add(goodbye);
Mark Slee95771002006-06-07 06:53:25 +0000167
Mark Sleed2655522006-09-05 22:09:57 +0000168 Insanity looney = new Insanity();
169 crazy.userMap.put(Numberz.FIVE, (long)5);
170 crazy.xtructs.add(hello);
Mark Slee95771002006-06-07 06:53:25 +0000171
Mark Sleed2655522006-09-05 22:09:57 +0000172 HashMap<Integer,Insanity> first_map = new HashMap<Integer, Insanity>();
173 HashMap<Integer,Insanity> second_map = new HashMap<Integer, Insanity>();;
Mark Slee95771002006-06-07 06:53:25 +0000174
Mark Sleed2655522006-09-05 22:09:57 +0000175 first_map.put(Numberz.TWO, crazy);
176 first_map.put(Numberz.THREE, crazy);
Mark Slee95771002006-06-07 06:53:25 +0000177
Mark Sleed2655522006-09-05 22:09:57 +0000178 second_map.put(Numberz.SIX, looney);
Mark Slee95771002006-06-07 06:53:25 +0000179
Mark Sleed2655522006-09-05 22:09:57 +0000180 HashMap<Long,HashMap<Integer,Insanity>> insane =
181 new HashMap<Long, HashMap<Integer,Insanity>>();
182 insane.put((long)1, first_map);
183 insane.put((long)2, second_map);
Mark Slee95771002006-06-07 06:53:25 +0000184
Mark Sleed2655522006-09-05 22:09:57 +0000185 return insane;
186 }
Mark Slee95771002006-06-07 06:53:25 +0000187
Mark Sleed2655522006-09-05 22:09:57 +0000188 public Xtruct testMulti(byte arg0, int arg1, long arg2, HashMap<Short,String> arg3, int arg4, long arg5) {
189 System.out.print("testMulti()\n");
Mark Slee78f58e22006-09-02 04:17:07 +0000190
Mark Sleed2655522006-09-05 22:09:57 +0000191 Xtruct hello = new Xtruct();;
192 hello.string_thing = "Hello2";
193 hello.byte_thing = arg0;
194 hello.i32_thing = arg1;
195 hello.i64_thing = arg2;
196 return hello;
Mark Slee78f58e22006-09-02 04:17:07 +0000197 }
Mark Slee78f58e22006-09-02 04:17:07 +0000198
Mark Sleed2655522006-09-05 22:09:57 +0000199 public void testException(String arg) throws Xception {
200 System.out.print("testException("+arg+")\n");
201 if (arg.equals("Xception")) {
202 Xception x = new Xception();
203 x.errorCode = 1001;
204 x.message = "This is an Xception";
205 throw x;
206 }
207 return;
Mark Slee78f58e22006-09-02 04:17:07 +0000208 }
Mark Sleed2655522006-09-05 22:09:57 +0000209
210 public Xtruct testMultiException(String arg0, String arg1) throws Xception, Xception2 {
211 System.out.print("testMultiException(" + arg0 + ", " + arg1 + ")\n");
212 if (arg0.equals("Xception")) {
213 Xception x = new Xception();
214 x.errorCode = 1001;
215 x.message = "This is an Xception";
216 throw x;
217 } else if (arg0.equals("Xception2")) {
218 Xception2 x = new Xception2();
219 x.errorCode = 2002;
220 x.struct_thing = new Xtruct();
221 x.struct_thing.string_thing = "This is an Xception2";
222 throw x;
223 }
Mark Slee78f58e22006-09-02 04:17:07 +0000224
Mark Sleed2655522006-09-05 22:09:57 +0000225 Xtruct result = new Xtruct();
226 result.string_thing = arg1;
227 return result;
228 }
Mark Slee78f58e22006-09-02 04:17:07 +0000229
Mark Sleed2655522006-09-05 22:09:57 +0000230 } // class TestHandler
Mark Slee78f58e22006-09-02 04:17:07 +0000231
Mark Slee95771002006-06-07 06:53:25 +0000232 public static void main(String [] args) {
233 try {
234 int port = 9090;
235 if (args.length > 1) {
236 port = Integer.valueOf(args[0]);
237 }
238
239 // Processor
Mark Sleed2655522006-09-05 22:09:57 +0000240 TBinaryProtocol binaryProtocol =
241 new TBinaryProtocol();
242 TestHandler testHandler =
243 new TestHandler();
244 ThriftTest.Server testServer =
245 new ThriftTest.Server(testHandler, binaryProtocol);
Mark Slee95771002006-06-07 06:53:25 +0000246
Mark Slee95771002006-06-07 06:53:25 +0000247 // Transport
Mark Sleed2655522006-09-05 22:09:57 +0000248 TServerSocket tServerSocket =
Mark Sleeffcddd62006-09-06 20:37:03 +0000249 new TServerSocket(port);
Mark Slee95771002006-06-07 06:53:25 +0000250
Mark Sleeffcddd62006-09-06 20:37:03 +0000251 TServer serverEngine;
252
253 // Simple Server
254 // serverEngine = new TSimpleServer(testServer, tServerSocket);
255
256 // ThreadPool Server
257 serverEngine = new TThreadPoolServer(testServer, tServerSocket);
Mark Slee95771002006-06-07 06:53:25 +0000258
259 // Run it
260 System.out.println("Starting the server on port " + port + "...");
Mark Sleeffcddd62006-09-06 20:37:03 +0000261 serverEngine.run();
262
Mark Slee95771002006-06-07 06:53:25 +0000263 } catch (Exception x) {
264 x.printStackTrace();
265 }
266 System.out.println("done.");
267 }
268}