blob: 7418cbb23771352a148e342cc0c8831ed07f521a [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 Slee95771002006-06-07 06:53:25 +000048
Mark Sleed2655522006-09-05 22:09:57 +000049 public Xtruct testStruct(Xtruct thing) {
50 System.out.print("testStruct({" +
51 "\"" + thing.string_thing + "\", " +
52 thing.byte_thing + ", " +
53 thing.i32_thing + ", " +
54 thing.i64_thing + "})\n");
55 return thing;
56 }
Mark Slee95771002006-06-07 06:53:25 +000057
Mark Sleed2655522006-09-05 22:09:57 +000058 public Xtruct2 testNest(Xtruct2 nest) {
59 Xtruct thing = nest.struct_thing;
60 System.out.print("testNest({" +
61 nest.byte_thing + ", {" +
62 "\"" + thing.string_thing + "\", " +
63 thing.byte_thing + ", " +
64 thing.i32_thing + ", " +
65 thing.i64_thing + "}, " +
66 nest.i32_thing + "})\n");
67 return nest;
68 }
Mark Slee95771002006-06-07 06:53:25 +000069
Mark Sleed2655522006-09-05 22:09:57 +000070 public HashMap<Integer,Integer> testMap(HashMap<Integer,Integer> thing) {
71 System.out.print("testMap({");
72 boolean first = true;
73 for (int key : thing.keySet()) {
74 if (first) {
75 first = false;
76 } else {
77 System.out.print(", ");
78 }
79 System.out.print(key + " => " + thing.get(key));
Mark Slee95771002006-06-07 06:53:25 +000080 }
Mark Sleed2655522006-09-05 22:09:57 +000081 System.out.print("})\n");
82 return thing;
Mark Slee95771002006-06-07 06:53:25 +000083 }
Mark Slee95771002006-06-07 06:53:25 +000084
Mark Sleed2655522006-09-05 22:09:57 +000085 public HashSet<Integer> testSet(HashSet<Integer> thing) {
86 System.out.print("testSet({");
87 boolean first = true;
88 for (int elem : thing) {
89 if (first) {
90 first = false;
91 } else {
92 System.out.print(", ");
93 }
94 System.out.print(elem);
Mark Slee95771002006-06-07 06:53:25 +000095 }
Mark Sleed2655522006-09-05 22:09:57 +000096 System.out.print("})\n");
97 return thing;
Mark Slee95771002006-06-07 06:53:25 +000098 }
Mark Slee95771002006-06-07 06:53:25 +000099
Mark Sleed2655522006-09-05 22:09:57 +0000100 public ArrayList<Integer> testList(ArrayList<Integer> thing) {
101 System.out.print("testList({");
102 boolean first = true;
103 for (int elem : thing) {
104 if (first) {
105 first = false;
106 } else {
107 System.out.print(", ");
108 }
109 System.out.print(elem);
Mark Slee95771002006-06-07 06:53:25 +0000110 }
Mark Sleed2655522006-09-05 22:09:57 +0000111 System.out.print("})\n");
112 return thing;
Mark Slee95771002006-06-07 06:53:25 +0000113 }
114
Mark Sleed2655522006-09-05 22:09:57 +0000115 public int testEnum(int thing) {
116 System.out.print("testEnum(" + thing + ")\n");
117 return thing;
118 }
Mark Slee95771002006-06-07 06:53:25 +0000119
Mark Sleed2655522006-09-05 22:09:57 +0000120 public long testTypedef(long thing) {
121 System.out.print("testTypedef(" + thing + ")\n");
122 return thing;
123 }
Mark Slee95771002006-06-07 06:53:25 +0000124
Mark Sleed2655522006-09-05 22:09:57 +0000125 public HashMap<Integer,HashMap<Integer,Integer>> testMapMap(int hello) {
126 System.out.print("testMapMap(" + hello + ")\n");
127 HashMap<Integer,HashMap<Integer,Integer>> mapmap =
128 new HashMap<Integer,HashMap<Integer,Integer>>();
129
130 HashMap<Integer,Integer> pos = new HashMap<Integer,Integer>();
131 HashMap<Integer,Integer> neg = new HashMap<Integer,Integer>();
132 for (int i = 1; i < 5; i++) {
133 pos.put(i, i);
134 neg.put(-i, -i);
135 }
136
137 mapmap.put(4, pos);
138 mapmap.put(-4, neg);
139
140 return mapmap;
141 }
142
143 public HashMap<Long, HashMap<Integer,Insanity>> testInsanity(Insanity argument) {
144 System.out.print("testInsanity()\n");
Mark Slee95771002006-06-07 06:53:25 +0000145
Mark Sleed2655522006-09-05 22:09:57 +0000146 Xtruct hello = new Xtruct();
147 hello.string_thing = "Hello2";
148 hello.byte_thing = 2;
149 hello.i32_thing = 2;
150 hello.i64_thing = 2;
Mark Slee95771002006-06-07 06:53:25 +0000151
Mark Sleed2655522006-09-05 22:09:57 +0000152 Xtruct goodbye = new Xtruct();
153 goodbye.string_thing = "Goodbye4";
154 goodbye.byte_thing = (byte)4;
155 goodbye.i32_thing = 4;
156 goodbye.i64_thing = (long)4;
Mark Slee95771002006-06-07 06:53:25 +0000157
Mark Sleed2655522006-09-05 22:09:57 +0000158 Insanity crazy = new Insanity();
159 crazy.userMap.put(Numberz.EIGHT, (long)8);
160 crazy.xtructs.add(goodbye);
Mark Slee95771002006-06-07 06:53:25 +0000161
Mark Sleed2655522006-09-05 22:09:57 +0000162 Insanity looney = new Insanity();
163 crazy.userMap.put(Numberz.FIVE, (long)5);
164 crazy.xtructs.add(hello);
Mark Slee95771002006-06-07 06:53:25 +0000165
Mark Sleed2655522006-09-05 22:09:57 +0000166 HashMap<Integer,Insanity> first_map = new HashMap<Integer, Insanity>();
167 HashMap<Integer,Insanity> second_map = new HashMap<Integer, Insanity>();;
Mark Slee95771002006-06-07 06:53:25 +0000168
Mark Sleed2655522006-09-05 22:09:57 +0000169 first_map.put(Numberz.TWO, crazy);
170 first_map.put(Numberz.THREE, crazy);
Mark Slee95771002006-06-07 06:53:25 +0000171
Mark Sleed2655522006-09-05 22:09:57 +0000172 second_map.put(Numberz.SIX, looney);
Mark Slee95771002006-06-07 06:53:25 +0000173
Mark Sleed2655522006-09-05 22:09:57 +0000174 HashMap<Long,HashMap<Integer,Insanity>> insane =
175 new HashMap<Long, HashMap<Integer,Insanity>>();
176 insane.put((long)1, first_map);
177 insane.put((long)2, second_map);
Mark Slee95771002006-06-07 06:53:25 +0000178
Mark Sleed2655522006-09-05 22:09:57 +0000179 return insane;
180 }
Mark Slee95771002006-06-07 06:53:25 +0000181
Mark Sleed2655522006-09-05 22:09:57 +0000182 public Xtruct testMulti(byte arg0, int arg1, long arg2, HashMap<Short,String> arg3, int arg4, long arg5) {
183 System.out.print("testMulti()\n");
Mark Slee78f58e22006-09-02 04:17:07 +0000184
Mark Sleed2655522006-09-05 22:09:57 +0000185 Xtruct hello = new Xtruct();;
186 hello.string_thing = "Hello2";
187 hello.byte_thing = arg0;
188 hello.i32_thing = arg1;
189 hello.i64_thing = arg2;
190 return hello;
Mark Slee78f58e22006-09-02 04:17:07 +0000191 }
Mark Slee78f58e22006-09-02 04:17:07 +0000192
Mark Sleed2655522006-09-05 22:09:57 +0000193 public void testException(String arg) throws Xception {
194 System.out.print("testException("+arg+")\n");
195 if (arg.equals("Xception")) {
196 Xception x = new Xception();
197 x.errorCode = 1001;
198 x.message = "This is an Xception";
199 throw x;
200 }
201 return;
Mark Slee78f58e22006-09-02 04:17:07 +0000202 }
Mark Sleed2655522006-09-05 22:09:57 +0000203
204 public Xtruct testMultiException(String arg0, String arg1) throws Xception, Xception2 {
205 System.out.print("testMultiException(" + arg0 + ", " + arg1 + ")\n");
206 if (arg0.equals("Xception")) {
207 Xception x = new Xception();
208 x.errorCode = 1001;
209 x.message = "This is an Xception";
210 throw x;
211 } else if (arg0.equals("Xception2")) {
212 Xception2 x = new Xception2();
213 x.errorCode = 2002;
214 x.struct_thing = new Xtruct();
215 x.struct_thing.string_thing = "This is an Xception2";
216 throw x;
217 }
Mark Slee78f58e22006-09-02 04:17:07 +0000218
Mark Sleed2655522006-09-05 22:09:57 +0000219 Xtruct result = new Xtruct();
220 result.string_thing = arg1;
221 return result;
222 }
Mark Slee78f58e22006-09-02 04:17:07 +0000223
Mark Sleed2655522006-09-05 22:09:57 +0000224 } // class TestHandler
Mark Slee78f58e22006-09-02 04:17:07 +0000225
Mark Slee95771002006-06-07 06:53:25 +0000226 public static void main(String [] args) {
227 try {
228 int port = 9090;
229 if (args.length > 1) {
230 port = Integer.valueOf(args[0]);
231 }
232
233 // Processor
Mark Sleed2655522006-09-05 22:09:57 +0000234 TBinaryProtocol binaryProtocol =
235 new TBinaryProtocol();
236 TestHandler testHandler =
237 new TestHandler();
238 ThriftTest.Server testServer =
239 new ThriftTest.Server(testHandler, binaryProtocol);
Mark Slee95771002006-06-07 06:53:25 +0000240
241 // Options
Mark Sleed2655522006-09-05 22:09:57 +0000242 TServer.Options serverOptions =
243 new TServer.Options();
Mark Slee95771002006-06-07 06:53:25 +0000244
245 // Transport
Mark Sleed2655522006-09-05 22:09:57 +0000246 ServerSocket serverSocket =
247 new ServerSocket(port);
248 TServerSocket tServerSocket =
249 new TServerSocket(serverSocket);
Mark Slee95771002006-06-07 06:53:25 +0000250
251 // Server
Mark Sleed2655522006-09-05 22:09:57 +0000252 TSimpleServer simpleServer =
253 new TSimpleServer(testServer, serverOptions, tServerSocket);
Mark Slee95771002006-06-07 06:53:25 +0000254
255 // Run it
256 System.out.println("Starting the server on port " + port + "...");
257 simpleServer.run();
258 } catch (Exception x) {
259 x.printStackTrace();
260 }
261 System.out.println("done.");
262 }
263}