blob: 4e415ae1d350f6f88c820be9131962ff48684ee4 [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 Slee78f58e22006-09-02 04:17:07 +000019public class TestServer extends ThriftTest.Server {
Mark Slee95771002006-06-07 06:53:25 +000020 public TestServer(TProtocol prot) {
21 super(prot);
22 }
23
24 public void testVoid() {
25 System.out.print("testVoid()\n");
26 }
27
Mark Slee78f58e22006-09-02 04:17:07 +000028 public String testString(String thing) {
29 System.out.print("testString(\"" + thing + "\")\n");
Mark Slee95771002006-06-07 06:53:25 +000030 return thing;
31 }
32
Mark Slee78f58e22006-09-02 04:17:07 +000033 public byte testByte(byte thing) {
34 System.out.print("testByte(" + thing + ")\n");
Mark Slee95771002006-06-07 06:53:25 +000035 return thing;
36 }
37
Mark Slee78f58e22006-09-02 04:17:07 +000038 public int testI32(int thing) {
39 System.out.print("testI32(" + thing + ")\n");
Mark Slee95771002006-06-07 06:53:25 +000040 return thing;
41 }
42
Mark Slee78f58e22006-09-02 04:17:07 +000043 public long testI64(long thing) {
44 System.out.print("testI64(" + thing + ")\n");
Mark Slee95771002006-06-07 06:53:25 +000045 return thing;
46 }
47
48 public Xtruct testStruct(Xtruct thing) {
49 System.out.print("testStruct({" +
Mark Slee78f58e22006-09-02 04:17:07 +000050 "\"" + thing.string_thing + "\", " +
51 thing.byte_thing + ", " +
52 thing.i32_thing + ", " +
53 thing.i64_thing + "})\n");
Mark Slee95771002006-06-07 06:53:25 +000054 return thing;
55 }
56
57 public Xtruct2 testNest(Xtruct2 nest) {
58 Xtruct thing = nest.struct_thing;
59 System.out.print("testNest({" +
Mark Slee78f58e22006-09-02 04:17:07 +000060 nest.byte_thing + ", {" +
61 "\"" + thing.string_thing + "\", " +
62 thing.byte_thing + ", " +
63 thing.i32_thing + ", " +
64 thing.i64_thing + "}, " +
65 nest.i32_thing + "})\n");
Mark Slee95771002006-06-07 06:53:25 +000066 return nest;
67 }
68
Mark Slee78f58e22006-09-02 04:17:07 +000069 public HashMap<Integer,Integer> testMap(HashMap<Integer,Integer> thing) {
Mark Slee95771002006-06-07 06:53:25 +000070 System.out.print("testMap({");
71 boolean first = true;
Mark Slee78f58e22006-09-02 04:17:07 +000072 for (int key : thing.keySet()) {
Mark Slee95771002006-06-07 06:53:25 +000073 if (first) {
74 first = false;
75 } else {
76 System.out.print(", ");
77 }
Mark Slee78f58e22006-09-02 04:17:07 +000078 System.out.print(key + " => " + thing.get(key));
Mark Slee95771002006-06-07 06:53:25 +000079 }
80 System.out.print("})\n");
81 return thing;
82 }
83
Mark Slee78f58e22006-09-02 04:17:07 +000084 public HashSet<Integer> testSet(HashSet<Integer> thing) {
Mark Slee95771002006-06-07 06:53:25 +000085 System.out.print("testSet({");
86 boolean first = true;
Mark Slee78f58e22006-09-02 04:17:07 +000087 for (int elem : thing) {
Mark Slee95771002006-06-07 06:53:25 +000088 if (first) {
89 first = false;
90 } else {
91 System.out.print(", ");
92 }
Mark Slee78f58e22006-09-02 04:17:07 +000093 System.out.print(elem);
Mark Slee95771002006-06-07 06:53:25 +000094 }
95 System.out.print("})\n");
96 return thing;
97 }
98
Mark Slee78f58e22006-09-02 04:17:07 +000099 public ArrayList<Integer> testList(ArrayList<Integer> thing) {
Mark Slee95771002006-06-07 06:53:25 +0000100 System.out.print("testList({");
101 boolean first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000102 for (int elem : thing) {
Mark Slee95771002006-06-07 06:53:25 +0000103 if (first) {
104 first = false;
105 } else {
106 System.out.print(", ");
107 }
Mark Slee78f58e22006-09-02 04:17:07 +0000108 System.out.print(elem);
Mark Slee95771002006-06-07 06:53:25 +0000109 }
110 System.out.print("})\n");
111 return thing;
112 }
113
Mark Slee78f58e22006-09-02 04:17:07 +0000114 public int testEnum(int thing) {
115 System.out.print("testEnum(" + thing + ")\n");
Mark Slee95771002006-06-07 06:53:25 +0000116 return thing;
117 }
118
Mark Slee78f58e22006-09-02 04:17:07 +0000119 public long testTypedef(long thing) {
120 System.out.print("testTypedef(" + thing + ")\n");
Mark Slee95771002006-06-07 06:53:25 +0000121 return thing;
122 }
123
Mark Slee78f58e22006-09-02 04:17:07 +0000124 public HashMap<Integer,HashMap<Integer,Integer>> testMapMap(int hello) {
125 System.out.print("testMapMap(" + hello + ")\n");
126 HashMap<Integer,HashMap<Integer,Integer>> mapmap =
127 new HashMap<Integer,HashMap<Integer,Integer>>();
Mark Slee95771002006-06-07 06:53:25 +0000128
Mark Slee78f58e22006-09-02 04:17:07 +0000129 HashMap<Integer,Integer> pos = new HashMap<Integer,Integer>();
130 HashMap<Integer,Integer> neg = new HashMap<Integer,Integer>();
Mark Slee95771002006-06-07 06:53:25 +0000131 for (int i = 1; i < 5; i++) {
Mark Slee78f58e22006-09-02 04:17:07 +0000132 pos.put(i, i);
133 neg.put(-i, -i);
Mark Slee95771002006-06-07 06:53:25 +0000134 }
135
Mark Slee78f58e22006-09-02 04:17:07 +0000136 mapmap.put(4, pos);
137 mapmap.put(-4, neg);
Mark Slee95771002006-06-07 06:53:25 +0000138
139 return mapmap;
140 }
141
Mark Slee78f58e22006-09-02 04:17:07 +0000142 public HashMap<Long, HashMap<Integer,Insanity>> testInsanity(Insanity argument) {
Mark Slee95771002006-06-07 06:53:25 +0000143 System.out.print("testInsanity()\n");
144
145 Xtruct hello = new Xtruct();
Mark Slee78f58e22006-09-02 04:17:07 +0000146 hello.string_thing = "Hello2";
147 hello.byte_thing = 2;
148 hello.i32_thing = 2;
149 hello.i64_thing = 2;
Mark Slee95771002006-06-07 06:53:25 +0000150
151 Xtruct goodbye = new Xtruct();
Mark Slee78f58e22006-09-02 04:17:07 +0000152 goodbye.string_thing = "Goodbye4";
153 goodbye.byte_thing = (byte)4;
154 goodbye.i32_thing = 4;
155 goodbye.i64_thing = (long)4;
Mark Slee95771002006-06-07 06:53:25 +0000156
157 Insanity crazy = new Insanity();
Mark Slee78f58e22006-09-02 04:17:07 +0000158 crazy.userMap.put(Numberz.EIGHT, (long)8);
Mark Slee95771002006-06-07 06:53:25 +0000159 crazy.xtructs.add(goodbye);
160
161 Insanity looney = new Insanity();
Mark Slee78f58e22006-09-02 04:17:07 +0000162 crazy.userMap.put(Numberz.FIVE, (long)5);
Mark Slee95771002006-06-07 06:53:25 +0000163 crazy.xtructs.add(hello);
164
Mark Slee78f58e22006-09-02 04:17:07 +0000165 HashMap<Integer,Insanity> first_map = new HashMap<Integer, Insanity>();
166 HashMap<Integer,Insanity> second_map = new HashMap<Integer, Insanity>();;
Mark Slee95771002006-06-07 06:53:25 +0000167
168 first_map.put(Numberz.TWO, crazy);
169 first_map.put(Numberz.THREE, crazy);
170
171 second_map.put(Numberz.SIX, looney);
172
Mark Slee78f58e22006-09-02 04:17:07 +0000173 HashMap<Long,HashMap<Integer,Insanity>> insane =
174 new HashMap<Long, HashMap<Integer,Insanity>>();
175 insane.put((long)1, first_map);
176 insane.put((long)2, second_map);
Mark Slee95771002006-06-07 06:53:25 +0000177
178 return insane;
179 }
180
Mark Slee78f58e22006-09-02 04:17:07 +0000181 public Xtruct testMulti(byte arg0, int arg1, long arg2, HashMap<Short,String> arg3, int arg4, long arg5) {
182 System.out.print("testMulti()\n");
183
184 Xtruct hello = new Xtruct();;
185 hello.string_thing = "Hello2";
186 hello.byte_thing = arg0;
187 hello.i32_thing = arg1;
188 hello.i64_thing = arg2;
189 return hello;
190 }
191
192 public void testException(String arg) throws Xception {
193 System.out.print("testException("+arg+")\n");
194 if (arg.equals("Xception")) {
195 Xception x = new Xception();
196 x.errorCode = 1001;
197 x.message = "This is an Xception";
198 throw x;
199 }
200 return;
201 }
202
203 public Xtruct testMultiException(String arg0, String arg1) throws Xception, Xception2 {
204 System.out.print("testMultiException(" + arg0 + ", " + arg1 + ")\n");
205 if (arg0.equals("Xception")) {
206 Xception x = new Xception();
207 x.errorCode = 1001;
208 x.message = "This is an Xception";
209 } else if (arg0.equals("Xception2")) {
210 Xception2 x = new Xception2();
211 x.errorCode = 2002;
212 x.struct_thing.string_thing = "This is an Xception2";
213 }
214
215 Xtruct result = new Xtruct();
216 result.string_thing = arg1;
217 return result;
218 }
219
220
Mark Slee95771002006-06-07 06:53:25 +0000221 public static void main(String [] args) {
222 try {
223 int port = 9090;
224 if (args.length > 1) {
225 port = Integer.valueOf(args[0]);
226 }
227
228 // Processor
229 TBinaryProtocol binaryProtocol = new TBinaryProtocol();
230 TestServer testServer = new TestServer(binaryProtocol);
231
232 // Options
233 TServer.Options serverOptions = new TServer.Options();
234
235 // Transport
236 ServerSocket serverSocket = new ServerSocket(port);
237 TServerSocket tServerSocket = new TServerSocket(serverSocket);
238
239 // Server
240 TSimpleServer simpleServer = new TSimpleServer(testServer,
241 serverOptions,
242 tServerSocket);
243
244 // Run it
245 System.out.println("Starting the server on port " + port + "...");
246 simpleServer.run();
247 } catch (Exception x) {
248 x.printStackTrace();
249 }
250 System.out.println("done.");
251 }
252}