blob: 8e3e4ed392e6d0ded53d07465d316141555fd38e [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;
8import com.facebook.thrift.transport.TServerSocket;
9import com.facebook.thrift.transport.TServerTransport;
10
Mark Slee78f58e22006-09-02 04:17:07 +000011// Generated code
12import thrift.test.*;
Mark Slee95771002006-06-07 06:53:25 +000013
14import java.net.ServerSocket;
15import java.util.ArrayList;
16import java.util.HashMap;
17import java.util.HashSet;
18
Mark Sleed2655522006-09-05 22:09:57 +000019public class TestServer {
Mark Slee95771002006-06-07 06:53:25 +000020
Mark Sleed2655522006-09-05 22:09:57 +000021 public static class TestHandler implements ThriftTest.Iface {
Mark Slee95771002006-06-07 06:53:25 +000022
Mark Sleed2655522006-09-05 22:09:57 +000023 public TestHandler() {}
24
25 public void testVoid() {
26 System.out.print("testVoid()\n");
27 }
Mark Slee95771002006-06-07 06:53:25 +000028
Mark Sleed2655522006-09-05 22:09:57 +000029 public String testString(String thing) {
30 System.out.print("testString(\"" + thing + "\")\n");
31 return thing;
32 }
Mark Slee95771002006-06-07 06:53:25 +000033
Mark Sleed2655522006-09-05 22:09:57 +000034 public byte testByte(byte thing) {
35 System.out.print("testByte(" + thing + ")\n");
36 return thing;
37 }
38
39 public int testI32(int thing) {
40 System.out.print("testI32(" + thing + ")\n");
41 return thing;
42 }
43
44 public long testI64(long thing) {
45 System.out.print("testI64(" + thing + ")\n");
46 return thing;
47 }
Mark Sleec98d0502006-09-06 02:42:25 +000048
49 public double testDouble(double thing) {
50 System.out.print("testDouble(" + thing + ")\n");
51 return thing;
52 }
Mark Slee95771002006-06-07 06:53:25 +000053
Mark Sleed2655522006-09-05 22:09:57 +000054 public Xtruct testStruct(Xtruct thing) {
55 System.out.print("testStruct({" +
56 "\"" + thing.string_thing + "\", " +
57 thing.byte_thing + ", " +
58 thing.i32_thing + ", " +
59 thing.i64_thing + "})\n");
60 return thing;
61 }
Mark Slee95771002006-06-07 06:53:25 +000062
Mark Sleed2655522006-09-05 22:09:57 +000063 public Xtruct2 testNest(Xtruct2 nest) {
64 Xtruct thing = nest.struct_thing;
65 System.out.print("testNest({" +
66 nest.byte_thing + ", {" +
67 "\"" + thing.string_thing + "\", " +
68 thing.byte_thing + ", " +
69 thing.i32_thing + ", " +
70 thing.i64_thing + "}, " +
71 nest.i32_thing + "})\n");
72 return nest;
73 }
Mark Slee95771002006-06-07 06:53:25 +000074
Mark Sleed2655522006-09-05 22:09:57 +000075 public HashMap<Integer,Integer> testMap(HashMap<Integer,Integer> thing) {
76 System.out.print("testMap({");
77 boolean first = true;
78 for (int key : thing.keySet()) {
79 if (first) {
80 first = false;
81 } else {
82 System.out.print(", ");
83 }
84 System.out.print(key + " => " + thing.get(key));
Mark Slee95771002006-06-07 06:53:25 +000085 }
Mark Sleed2655522006-09-05 22:09:57 +000086 System.out.print("})\n");
87 return thing;
Mark Slee95771002006-06-07 06:53:25 +000088 }
Mark Slee95771002006-06-07 06:53:25 +000089
Mark Sleed2655522006-09-05 22:09:57 +000090 public HashSet<Integer> testSet(HashSet<Integer> thing) {
91 System.out.print("testSet({");
92 boolean first = true;
93 for (int elem : thing) {
94 if (first) {
95 first = false;
96 } else {
97 System.out.print(", ");
98 }
99 System.out.print(elem);
Mark Slee95771002006-06-07 06:53:25 +0000100 }
Mark Sleed2655522006-09-05 22:09:57 +0000101 System.out.print("})\n");
102 return thing;
Mark Slee95771002006-06-07 06:53:25 +0000103 }
Mark Slee95771002006-06-07 06:53:25 +0000104
Mark Sleed2655522006-09-05 22:09:57 +0000105 public ArrayList<Integer> testList(ArrayList<Integer> thing) {
106 System.out.print("testList({");
107 boolean first = true;
108 for (int elem : thing) {
109 if (first) {
110 first = false;
111 } else {
112 System.out.print(", ");
113 }
114 System.out.print(elem);
Mark Slee95771002006-06-07 06:53:25 +0000115 }
Mark Sleed2655522006-09-05 22:09:57 +0000116 System.out.print("})\n");
117 return thing;
Mark Slee95771002006-06-07 06:53:25 +0000118 }
119
Mark Sleed2655522006-09-05 22:09:57 +0000120 public int testEnum(int thing) {
121 System.out.print("testEnum(" + thing + ")\n");
122 return thing;
123 }
Mark Slee95771002006-06-07 06:53:25 +0000124
Mark Sleed2655522006-09-05 22:09:57 +0000125 public long testTypedef(long thing) {
126 System.out.print("testTypedef(" + thing + ")\n");
127 return thing;
128 }
Mark Slee95771002006-06-07 06:53:25 +0000129
Mark Sleed2655522006-09-05 22:09:57 +0000130 public HashMap<Integer,HashMap<Integer,Integer>> testMapMap(int hello) {
131 System.out.print("testMapMap(" + hello + ")\n");
132 HashMap<Integer,HashMap<Integer,Integer>> mapmap =
133 new HashMap<Integer,HashMap<Integer,Integer>>();
134
135 HashMap<Integer,Integer> pos = new HashMap<Integer,Integer>();
136 HashMap<Integer,Integer> neg = new HashMap<Integer,Integer>();
137 for (int i = 1; i < 5; i++) {
138 pos.put(i, i);
139 neg.put(-i, -i);
140 }
141
142 mapmap.put(4, pos);
143 mapmap.put(-4, neg);
144
145 return mapmap;
146 }
147
148 public HashMap<Long, HashMap<Integer,Insanity>> testInsanity(Insanity argument) {
149 System.out.print("testInsanity()\n");
Mark Slee95771002006-06-07 06:53:25 +0000150
Mark Sleed2655522006-09-05 22:09:57 +0000151 Xtruct hello = new Xtruct();
152 hello.string_thing = "Hello2";
153 hello.byte_thing = 2;
154 hello.i32_thing = 2;
155 hello.i64_thing = 2;
Mark Slee95771002006-06-07 06:53:25 +0000156
Mark Sleed2655522006-09-05 22:09:57 +0000157 Xtruct goodbye = new Xtruct();
158 goodbye.string_thing = "Goodbye4";
159 goodbye.byte_thing = (byte)4;
160 goodbye.i32_thing = 4;
161 goodbye.i64_thing = (long)4;
Mark Slee95771002006-06-07 06:53:25 +0000162
Mark Sleed2655522006-09-05 22:09:57 +0000163 Insanity crazy = new Insanity();
164 crazy.userMap.put(Numberz.EIGHT, (long)8);
165 crazy.xtructs.add(goodbye);
Mark Slee95771002006-06-07 06:53:25 +0000166
Mark Sleed2655522006-09-05 22:09:57 +0000167 Insanity looney = new Insanity();
168 crazy.userMap.put(Numberz.FIVE, (long)5);
169 crazy.xtructs.add(hello);
Mark Slee95771002006-06-07 06:53:25 +0000170
Mark Sleed2655522006-09-05 22:09:57 +0000171 HashMap<Integer,Insanity> first_map = new HashMap<Integer, Insanity>();
172 HashMap<Integer,Insanity> second_map = new HashMap<Integer, Insanity>();;
Mark Slee95771002006-06-07 06:53:25 +0000173
Mark Sleed2655522006-09-05 22:09:57 +0000174 first_map.put(Numberz.TWO, crazy);
175 first_map.put(Numberz.THREE, crazy);
Mark Slee95771002006-06-07 06:53:25 +0000176
Mark Sleed2655522006-09-05 22:09:57 +0000177 second_map.put(Numberz.SIX, looney);
Mark Slee95771002006-06-07 06:53:25 +0000178
Mark Sleed2655522006-09-05 22:09:57 +0000179 HashMap<Long,HashMap<Integer,Insanity>> insane =
180 new HashMap<Long, HashMap<Integer,Insanity>>();
181 insane.put((long)1, first_map);
182 insane.put((long)2, second_map);
Mark Slee95771002006-06-07 06:53:25 +0000183
Mark Sleed2655522006-09-05 22:09:57 +0000184 return insane;
185 }
Mark Slee95771002006-06-07 06:53:25 +0000186
Mark Sleed2655522006-09-05 22:09:57 +0000187 public Xtruct testMulti(byte arg0, int arg1, long arg2, HashMap<Short,String> arg3, int arg4, long arg5) {
188 System.out.print("testMulti()\n");
Mark Slee78f58e22006-09-02 04:17:07 +0000189
Mark Sleed2655522006-09-05 22:09:57 +0000190 Xtruct hello = new Xtruct();;
191 hello.string_thing = "Hello2";
192 hello.byte_thing = arg0;
193 hello.i32_thing = arg1;
194 hello.i64_thing = arg2;
195 return hello;
Mark Slee78f58e22006-09-02 04:17:07 +0000196 }
Mark Slee78f58e22006-09-02 04:17:07 +0000197
Mark Sleed2655522006-09-05 22:09:57 +0000198 public void testException(String arg) throws Xception {
199 System.out.print("testException("+arg+")\n");
200 if (arg.equals("Xception")) {
201 Xception x = new Xception();
202 x.errorCode = 1001;
203 x.message = "This is an Xception";
204 throw x;
205 }
206 return;
Mark Slee78f58e22006-09-02 04:17:07 +0000207 }
Mark Sleed2655522006-09-05 22:09:57 +0000208
209 public Xtruct testMultiException(String arg0, String arg1) throws Xception, Xception2 {
210 System.out.print("testMultiException(" + arg0 + ", " + arg1 + ")\n");
211 if (arg0.equals("Xception")) {
212 Xception x = new Xception();
213 x.errorCode = 1001;
214 x.message = "This is an Xception";
215 throw x;
216 } else if (arg0.equals("Xception2")) {
217 Xception2 x = new Xception2();
218 x.errorCode = 2002;
219 x.struct_thing = new Xtruct();
220 x.struct_thing.string_thing = "This is an Xception2";
221 throw x;
222 }
Mark Slee78f58e22006-09-02 04:17:07 +0000223
Mark Sleed2655522006-09-05 22:09:57 +0000224 Xtruct result = new Xtruct();
225 result.string_thing = arg1;
226 return result;
227 }
Mark Slee78f58e22006-09-02 04:17:07 +0000228
Mark Sleed2655522006-09-05 22:09:57 +0000229 } // class TestHandler
Mark Slee78f58e22006-09-02 04:17:07 +0000230
Mark Slee95771002006-06-07 06:53:25 +0000231 public static void main(String [] args) {
232 try {
233 int port = 9090;
234 if (args.length > 1) {
235 port = Integer.valueOf(args[0]);
236 }
237
238 // Processor
Mark Sleed2655522006-09-05 22:09:57 +0000239 TBinaryProtocol binaryProtocol =
240 new TBinaryProtocol();
241 TestHandler testHandler =
242 new TestHandler();
243 ThriftTest.Server testServer =
244 new ThriftTest.Server(testHandler, binaryProtocol);
Mark Slee95771002006-06-07 06:53:25 +0000245
246 // Options
Mark Sleed2655522006-09-05 22:09:57 +0000247 TServer.Options serverOptions =
248 new TServer.Options();
Mark Slee95771002006-06-07 06:53:25 +0000249
250 // Transport
Mark Sleed2655522006-09-05 22:09:57 +0000251 ServerSocket serverSocket =
252 new ServerSocket(port);
253 TServerSocket tServerSocket =
254 new TServerSocket(serverSocket);
Mark Slee95771002006-06-07 06:53:25 +0000255
256 // Server
Mark Sleed2655522006-09-05 22:09:57 +0000257 TSimpleServer simpleServer =
258 new TSimpleServer(testServer, serverOptions, tServerSocket);
Mark Slee95771002006-06-07 06:53:25 +0000259
260 // Run it
261 System.out.println("Starting the server on port " + port + "...");
262 simpleServer.run();
263 } catch (Exception x) {
264 x.printStackTrace();
265 }
266 System.out.println("done.");
267 }
268}