blob: 6f1643e233d5cad4c1ffd99623abf925d2864295 [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;
Mark Sleea7747c22007-01-29 17:35:54 +000017import java.util.AbstractMap;
Mark Slee95771002006-06-07 06:53:25 +000018import java.util.HashMap;
19import java.util.HashSet;
20
Mark Sleed2655522006-09-05 22:09:57 +000021public class TestServer {
Mark Slee95771002006-06-07 06:53:25 +000022
Mark Sleed2655522006-09-05 22:09:57 +000023 public static class TestHandler implements ThriftTest.Iface {
Mark Slee95771002006-06-07 06:53:25 +000024
Mark Sleed2655522006-09-05 22:09:57 +000025 public TestHandler() {}
26
27 public void testVoid() {
28 System.out.print("testVoid()\n");
29 }
Mark Slee95771002006-06-07 06:53:25 +000030
Mark Sleed2655522006-09-05 22:09:57 +000031 public String testString(String thing) {
32 System.out.print("testString(\"" + thing + "\")\n");
33 return thing;
34 }
Mark Slee95771002006-06-07 06:53:25 +000035
Mark Sleed2655522006-09-05 22:09:57 +000036 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 Sleec98d0502006-09-06 02:42:25 +000050
51 public double testDouble(double thing) {
52 System.out.print("testDouble(" + thing + ")\n");
53 return thing;
54 }
Mark Slee95771002006-06-07 06:53:25 +000055
Mark Sleed2655522006-09-05 22:09:57 +000056 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 Slee95771002006-06-07 06:53:25 +000064
Mark Sleed2655522006-09-05 22:09:57 +000065 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 Slee95771002006-06-07 06:53:25 +000076
Mark Sleea7747c22007-01-29 17:35:54 +000077 public AbstractMap<Integer,Integer> testMap(AbstractMap<Integer,Integer> thing) {
Mark Sleed2655522006-09-05 22:09:57 +000078 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 Slee95771002006-06-07 06:53:25 +000087 }
Mark Sleed2655522006-09-05 22:09:57 +000088 System.out.print("})\n");
89 return thing;
Mark Slee95771002006-06-07 06:53:25 +000090 }
Mark Slee95771002006-06-07 06:53:25 +000091
Mark Sleed2655522006-09-05 22:09:57 +000092 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 Slee95771002006-06-07 06:53:25 +0000102 }
Mark Sleed2655522006-09-05 22:09:57 +0000103 System.out.print("})\n");
104 return thing;
Mark Slee95771002006-06-07 06:53:25 +0000105 }
Mark Slee95771002006-06-07 06:53:25 +0000106
Mark Sleed2655522006-09-05 22:09:57 +0000107 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 Slee95771002006-06-07 06:53:25 +0000117 }
Mark Sleed2655522006-09-05 22:09:57 +0000118 System.out.print("})\n");
119 return thing;
Mark Slee95771002006-06-07 06:53:25 +0000120 }
121
Mark Sleed2655522006-09-05 22:09:57 +0000122 public int testEnum(int thing) {
123 System.out.print("testEnum(" + thing + ")\n");
124 return thing;
125 }
Mark Slee95771002006-06-07 06:53:25 +0000126
Mark Sleed2655522006-09-05 22:09:57 +0000127 public long testTypedef(long thing) {
128 System.out.print("testTypedef(" + thing + ")\n");
129 return thing;
130 }
Mark Slee95771002006-06-07 06:53:25 +0000131
Mark Sleea7747c22007-01-29 17:35:54 +0000132 public AbstractMap<Integer,AbstractMap<Integer,Integer>> testMapMap(int hello) {
Mark Sleed2655522006-09-05 22:09:57 +0000133 System.out.print("testMapMap(" + hello + ")\n");
Mark Sleea7747c22007-01-29 17:35:54 +0000134 AbstractMap<Integer,AbstractMap<Integer,Integer>> mapmap =
135 new HashMap<Integer,AbstractMap<Integer,Integer>>();
Mark Sleed2655522006-09-05 22:09:57 +0000136
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 Sleea7747c22007-01-29 17:35:54 +0000150 public AbstractMap<Long, AbstractMap<Integer,Insanity>> testInsanity(Insanity argument) {
Mark Sleed2655522006-09-05 22:09:57 +0000151 System.out.print("testInsanity()\n");
Mark Slee95771002006-06-07 06:53:25 +0000152
Mark Sleed2655522006-09-05 22:09:57 +0000153 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 Slee95771002006-06-07 06:53:25 +0000158
Mark Sleed2655522006-09-05 22:09:57 +0000159 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 Slee95771002006-06-07 06:53:25 +0000164
Mark Sleed2655522006-09-05 22:09:57 +0000165 Insanity crazy = new Insanity();
Mark Sleecbc51602007-04-11 08:57:07 +0000166 crazy.userMap = new HashMap<Integer, Long>();
167 crazy.xtructs = new ArrayList<Xtruct>();
168
Mark Sleed2655522006-09-05 22:09:57 +0000169 crazy.userMap.put(Numberz.EIGHT, (long)8);
170 crazy.xtructs.add(goodbye);
Mark Slee95771002006-06-07 06:53:25 +0000171
Mark Sleed2655522006-09-05 22:09:57 +0000172 Insanity looney = new Insanity();
173 crazy.userMap.put(Numberz.FIVE, (long)5);
174 crazy.xtructs.add(hello);
Mark Slee95771002006-06-07 06:53:25 +0000175
Mark Sleed2655522006-09-05 22:09:57 +0000176 HashMap<Integer,Insanity> first_map = new HashMap<Integer, Insanity>();
177 HashMap<Integer,Insanity> second_map = new HashMap<Integer, Insanity>();;
Mark Slee95771002006-06-07 06:53:25 +0000178
Mark Sleed2655522006-09-05 22:09:57 +0000179 first_map.put(Numberz.TWO, crazy);
180 first_map.put(Numberz.THREE, crazy);
Mark Slee95771002006-06-07 06:53:25 +0000181
Mark Sleed2655522006-09-05 22:09:57 +0000182 second_map.put(Numberz.SIX, looney);
Mark Slee95771002006-06-07 06:53:25 +0000183
Mark Sleea7747c22007-01-29 17:35:54 +0000184 AbstractMap<Long,AbstractMap<Integer,Insanity>> insane =
185 new HashMap<Long, AbstractMap<Integer,Insanity>>();
Mark Sleed2655522006-09-05 22:09:57 +0000186 insane.put((long)1, first_map);
187 insane.put((long)2, second_map);
Mark Slee95771002006-06-07 06:53:25 +0000188
Mark Sleed2655522006-09-05 22:09:57 +0000189 return insane;
190 }
Mark Slee95771002006-06-07 06:53:25 +0000191
Mark Sleea7747c22007-01-29 17:35:54 +0000192 public Xtruct testMulti(byte arg0, int arg1, long arg2, AbstractMap<Short,String> arg3, int arg4, long arg5) {
Mark Sleed2655522006-09-05 22:09:57 +0000193 System.out.print("testMulti()\n");
Mark Slee78f58e22006-09-02 04:17:07 +0000194
Mark Sleed2655522006-09-05 22:09:57 +0000195 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 Slee78f58e22006-09-02 04:17:07 +0000201 }
Mark Slee78f58e22006-09-02 04:17:07 +0000202
Mark Sleed2655522006-09-05 22:09:57 +0000203 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 Slee78f58e22006-09-02 04:17:07 +0000212 }
Mark Sleed2655522006-09-05 22:09:57 +0000213
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 Slee78f58e22006-09-02 04:17:07 +0000228
Mark Sleed2655522006-09-05 22:09:57 +0000229 Xtruct result = new Xtruct();
230 result.string_thing = arg1;
231 return result;
232 }
Mark Slee78f58e22006-09-02 04:17:07 +0000233
Mark Sleed2655522006-09-05 22:09:57 +0000234 } // class TestHandler
Mark Slee78f58e22006-09-02 04:17:07 +0000235
Mark Slee95771002006-06-07 06:53:25 +0000236 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 Sleed2655522006-09-05 22:09:57 +0000244 TestHandler testHandler =
245 new TestHandler();
Mark Slee018b6992006-09-07 21:31:12 +0000246 ThriftTest.Processor testProcessor =
Mark Slee1dd819c2006-10-26 04:56:18 +0000247 new ThriftTest.Processor(testHandler);
Mark Slee95771002006-06-07 06:53:25 +0000248
Mark Slee95771002006-06-07 06:53:25 +0000249 // Transport
Mark Sleed2655522006-09-05 22:09:57 +0000250 TServerSocket tServerSocket =
Mark Sleeffcddd62006-09-06 20:37:03 +0000251 new TServerSocket(port);
Mark Slee95771002006-06-07 06:53:25 +0000252
Mark Sleeffcddd62006-09-06 20:37:03 +0000253 TServer serverEngine;
254
255 // Simple Server
Mark Slee018b6992006-09-07 21:31:12 +0000256 // serverEngine = new TSimpleServer(testProcessor, tServerSocket);
Mark Sleeffcddd62006-09-06 20:37:03 +0000257
258 // ThreadPool Server
Mark Slee1dd819c2006-10-26 04:56:18 +0000259 serverEngine = new TThreadPoolServer(testProcessor, tServerSocket);
Mark Slee95771002006-06-07 06:53:25 +0000260
261 // Run it
262 System.out.println("Starting the server on port " + port + "...");
Mark Slee4e755ca2006-09-12 00:46:08 +0000263 serverEngine.serve();
Mark Sleeffcddd62006-09-06 20:37:03 +0000264
Mark Slee95771002006-06-07 06:53:25 +0000265 } catch (Exception x) {
266 x.printStackTrace();
267 }
268 System.out.println("done.");
269 }
270}