blob: d89ea210155fe2f85aed32cb46a9292173ecb2f3 [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();
166 crazy.userMap.put(Numberz.EIGHT, (long)8);
167 crazy.xtructs.add(goodbye);
Mark Slee95771002006-06-07 06:53:25 +0000168
Mark Sleed2655522006-09-05 22:09:57 +0000169 Insanity looney = new Insanity();
170 crazy.userMap.put(Numberz.FIVE, (long)5);
171 crazy.xtructs.add(hello);
Mark Slee95771002006-06-07 06:53:25 +0000172
Mark Sleed2655522006-09-05 22:09:57 +0000173 HashMap<Integer,Insanity> first_map = new HashMap<Integer, Insanity>();
174 HashMap<Integer,Insanity> second_map = new HashMap<Integer, Insanity>();;
Mark Slee95771002006-06-07 06:53:25 +0000175
Mark Sleed2655522006-09-05 22:09:57 +0000176 first_map.put(Numberz.TWO, crazy);
177 first_map.put(Numberz.THREE, crazy);
Mark Slee95771002006-06-07 06:53:25 +0000178
Mark Sleed2655522006-09-05 22:09:57 +0000179 second_map.put(Numberz.SIX, looney);
Mark Slee95771002006-06-07 06:53:25 +0000180
Mark Sleea7747c22007-01-29 17:35:54 +0000181 AbstractMap<Long,AbstractMap<Integer,Insanity>> insane =
182 new HashMap<Long, AbstractMap<Integer,Insanity>>();
Mark Sleed2655522006-09-05 22:09:57 +0000183 insane.put((long)1, first_map);
184 insane.put((long)2, second_map);
Mark Slee95771002006-06-07 06:53:25 +0000185
Mark Sleed2655522006-09-05 22:09:57 +0000186 return insane;
187 }
Mark Slee95771002006-06-07 06:53:25 +0000188
Mark Sleea7747c22007-01-29 17:35:54 +0000189 public Xtruct testMulti(byte arg0, int arg1, long arg2, AbstractMap<Short,String> arg3, int arg4, long arg5) {
Mark Sleed2655522006-09-05 22:09:57 +0000190 System.out.print("testMulti()\n");
Mark Slee78f58e22006-09-02 04:17:07 +0000191
Mark Sleed2655522006-09-05 22:09:57 +0000192 Xtruct hello = new Xtruct();;
193 hello.string_thing = "Hello2";
194 hello.byte_thing = arg0;
195 hello.i32_thing = arg1;
196 hello.i64_thing = arg2;
197 return hello;
Mark Slee78f58e22006-09-02 04:17:07 +0000198 }
Mark Slee78f58e22006-09-02 04:17:07 +0000199
Mark Sleed2655522006-09-05 22:09:57 +0000200 public void testException(String arg) throws Xception {
201 System.out.print("testException("+arg+")\n");
202 if (arg.equals("Xception")) {
203 Xception x = new Xception();
204 x.errorCode = 1001;
205 x.message = "This is an Xception";
206 throw x;
207 }
208 return;
Mark Slee78f58e22006-09-02 04:17:07 +0000209 }
Mark Sleed2655522006-09-05 22:09:57 +0000210
211 public Xtruct testMultiException(String arg0, String arg1) throws Xception, Xception2 {
212 System.out.print("testMultiException(" + arg0 + ", " + arg1 + ")\n");
213 if (arg0.equals("Xception")) {
214 Xception x = new Xception();
215 x.errorCode = 1001;
216 x.message = "This is an Xception";
217 throw x;
218 } else if (arg0.equals("Xception2")) {
219 Xception2 x = new Xception2();
220 x.errorCode = 2002;
221 x.struct_thing = new Xtruct();
222 x.struct_thing.string_thing = "This is an Xception2";
223 throw x;
224 }
Mark Slee78f58e22006-09-02 04:17:07 +0000225
Mark Sleed2655522006-09-05 22:09:57 +0000226 Xtruct result = new Xtruct();
227 result.string_thing = arg1;
228 return result;
229 }
Mark Slee78f58e22006-09-02 04:17:07 +0000230
Mark Sleed2655522006-09-05 22:09:57 +0000231 } // class TestHandler
Mark Slee78f58e22006-09-02 04:17:07 +0000232
Mark Slee95771002006-06-07 06:53:25 +0000233 public static void main(String [] args) {
234 try {
235 int port = 9090;
236 if (args.length > 1) {
237 port = Integer.valueOf(args[0]);
238 }
239
240 // Processor
Mark Sleed2655522006-09-05 22:09:57 +0000241 TestHandler testHandler =
242 new TestHandler();
Mark Slee018b6992006-09-07 21:31:12 +0000243 ThriftTest.Processor testProcessor =
Mark Slee1dd819c2006-10-26 04:56:18 +0000244 new ThriftTest.Processor(testHandler);
Mark Slee95771002006-06-07 06:53:25 +0000245
Mark Slee95771002006-06-07 06:53:25 +0000246 // Transport
Mark Sleed2655522006-09-05 22:09:57 +0000247 TServerSocket tServerSocket =
Mark Sleeffcddd62006-09-06 20:37:03 +0000248 new TServerSocket(port);
Mark Slee95771002006-06-07 06:53:25 +0000249
Mark Sleeffcddd62006-09-06 20:37:03 +0000250 TServer serverEngine;
251
252 // Simple Server
Mark Slee018b6992006-09-07 21:31:12 +0000253 // serverEngine = new TSimpleServer(testProcessor, tServerSocket);
Mark Sleeffcddd62006-09-06 20:37:03 +0000254
255 // ThreadPool Server
Mark Slee1dd819c2006-10-26 04:56:18 +0000256 serverEngine = new TThreadPoolServer(testProcessor, tServerSocket);
Mark Slee95771002006-06-07 06:53:25 +0000257
258 // Run it
259 System.out.println("Starting the server on port " + port + "...");
Mark Slee4e755ca2006-09-12 00:46:08 +0000260 serverEngine.serve();
Mark Sleeffcddd62006-09-06 20:37:03 +0000261
Mark Slee95771002006-06-07 06:53:25 +0000262 } catch (Exception x) {
263 x.printStackTrace();
264 }
265 System.out.println("done.");
266 }
267}