| 30 |
ashish |
1 |
/*
|
|
|
2 |
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
3 |
* or more contributor license agreements. See the NOTICE file
|
|
|
4 |
* distributed with this work for additional information
|
|
|
5 |
* regarding copyright ownership. The ASF licenses this file
|
|
|
6 |
* to you under the Apache License, Version 2.0 (the
|
|
|
7 |
* "License"); you may not use this file except in compliance
|
|
|
8 |
* with the License. You may obtain a copy of the License at
|
|
|
9 |
*
|
|
|
10 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
11 |
*
|
|
|
12 |
* Unless required by applicable law or agreed to in writing,
|
|
|
13 |
* software distributed under the License is distributed on an
|
|
|
14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
15 |
* KIND, either express or implied. See the License for the
|
|
|
16 |
* specific language governing permissions and limitations
|
|
|
17 |
* under the License.
|
|
|
18 |
*/
|
|
|
19 |
|
|
|
20 |
package org.apache.thrift.test;
|
|
|
21 |
|
|
|
22 |
import java.util.ArrayList;
|
|
|
23 |
import java.util.HashMap;
|
|
|
24 |
import java.util.List;
|
|
|
25 |
import java.util.Map;
|
|
|
26 |
import java.util.Set;
|
|
|
27 |
|
|
|
28 |
import org.apache.thrift.protocol.TBinaryProtocol;
|
|
|
29 |
import org.apache.thrift.protocol.TProtocolFactory;
|
|
|
30 |
import org.apache.thrift.server.TServer;
|
|
|
31 |
import org.apache.thrift.server.TThreadPoolServer;
|
|
|
32 |
import org.apache.thrift.transport.TServerSocket;
|
|
|
33 |
|
|
|
34 |
import thrift.test.Insanity;
|
|
|
35 |
import thrift.test.Numberz;
|
|
|
36 |
import thrift.test.ThriftTest;
|
|
|
37 |
import thrift.test.Xception;
|
|
|
38 |
import thrift.test.Xception2;
|
|
|
39 |
import thrift.test.Xtruct;
|
|
|
40 |
import thrift.test.Xtruct2;
|
|
|
41 |
|
|
|
42 |
public class TestServer {
|
|
|
43 |
|
|
|
44 |
public static class TestHandler implements ThriftTest.Iface {
|
|
|
45 |
|
|
|
46 |
public TestHandler() {}
|
|
|
47 |
|
|
|
48 |
public void testVoid() {
|
|
|
49 |
System.out.print("testVoid()\n");
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
public String testString(String thing) {
|
|
|
53 |
System.out.print("testString(\"" + thing + "\")\n");
|
|
|
54 |
return thing;
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
public byte testByte(byte thing) {
|
|
|
58 |
System.out.print("testByte(" + thing + ")\n");
|
|
|
59 |
return thing;
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
public int testI32(int thing) {
|
|
|
63 |
System.out.print("testI32(" + thing + ")\n");
|
|
|
64 |
return thing;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
public long testI64(long thing) {
|
|
|
68 |
System.out.print("testI64(" + thing + ")\n");
|
|
|
69 |
return thing;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
public double testDouble(double thing) {
|
|
|
73 |
System.out.print("testDouble(" + thing + ")\n");
|
|
|
74 |
return thing;
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
public Xtruct testStruct(Xtruct thing) {
|
|
|
78 |
System.out.print("testStruct({" +
|
|
|
79 |
"\"" + thing.string_thing + "\", " +
|
|
|
80 |
thing.byte_thing + ", " +
|
|
|
81 |
thing.i32_thing + ", " +
|
|
|
82 |
thing.i64_thing + "})\n");
|
|
|
83 |
return thing;
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
public Xtruct2 testNest(Xtruct2 nest) {
|
|
|
87 |
Xtruct thing = nest.struct_thing;
|
|
|
88 |
System.out.print("testNest({" +
|
|
|
89 |
nest.byte_thing + ", {" +
|
|
|
90 |
"\"" + thing.string_thing + "\", " +
|
|
|
91 |
thing.byte_thing + ", " +
|
|
|
92 |
thing.i32_thing + ", " +
|
|
|
93 |
thing.i64_thing + "}, " +
|
|
|
94 |
nest.i32_thing + "})\n");
|
|
|
95 |
return nest;
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
public Map<Integer,Integer> testMap(Map<Integer,Integer> thing) {
|
|
|
99 |
System.out.print("testMap({");
|
|
|
100 |
boolean first = true;
|
|
|
101 |
for (int key : thing.keySet()) {
|
|
|
102 |
if (first) {
|
|
|
103 |
first = false;
|
|
|
104 |
} else {
|
|
|
105 |
System.out.print(", ");
|
|
|
106 |
}
|
|
|
107 |
System.out.print(key + " => " + thing.get(key));
|
|
|
108 |
}
|
|
|
109 |
System.out.print("})\n");
|
|
|
110 |
return thing;
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
public Set<Integer> testSet(Set<Integer> thing) {
|
|
|
114 |
System.out.print("testSet({");
|
|
|
115 |
boolean first = true;
|
|
|
116 |
for (int elem : thing) {
|
|
|
117 |
if (first) {
|
|
|
118 |
first = false;
|
|
|
119 |
} else {
|
|
|
120 |
System.out.print(", ");
|
|
|
121 |
}
|
|
|
122 |
System.out.print(elem);
|
|
|
123 |
}
|
|
|
124 |
System.out.print("})\n");
|
|
|
125 |
return thing;
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
public List<Integer> testList(List<Integer> thing) {
|
|
|
129 |
System.out.print("testList({");
|
|
|
130 |
boolean first = true;
|
|
|
131 |
for (int elem : thing) {
|
|
|
132 |
if (first) {
|
|
|
133 |
first = false;
|
|
|
134 |
} else {
|
|
|
135 |
System.out.print(", ");
|
|
|
136 |
}
|
|
|
137 |
System.out.print(elem);
|
|
|
138 |
}
|
|
|
139 |
System.out.print("})\n");
|
|
|
140 |
return thing;
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
public Numberz testEnum(Numberz thing) {
|
|
|
144 |
System.out.print("testEnum(" + thing + ")\n");
|
|
|
145 |
return thing;
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
public long testTypedef(long thing) {
|
|
|
149 |
System.out.print("testTypedef(" + thing + ")\n");
|
|
|
150 |
return thing;
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
public Map<Integer,Map<Integer,Integer>> testMapMap(int hello) {
|
|
|
154 |
System.out.print("testMapMap(" + hello + ")\n");
|
|
|
155 |
Map<Integer,Map<Integer,Integer>> mapmap =
|
|
|
156 |
new HashMap<Integer,Map<Integer,Integer>>();
|
|
|
157 |
|
|
|
158 |
HashMap<Integer,Integer> pos = new HashMap<Integer,Integer>();
|
|
|
159 |
HashMap<Integer,Integer> neg = new HashMap<Integer,Integer>();
|
|
|
160 |
for (int i = 1; i < 5; i++) {
|
|
|
161 |
pos.put(i, i);
|
|
|
162 |
neg.put(-i, -i);
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
mapmap.put(4, pos);
|
|
|
166 |
mapmap.put(-4, neg);
|
|
|
167 |
|
|
|
168 |
return mapmap;
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
public Map<Long, Map<Numberz,Insanity>> testInsanity(Insanity argument) {
|
|
|
172 |
System.out.print("testInsanity()\n");
|
|
|
173 |
|
|
|
174 |
Xtruct hello = new Xtruct();
|
|
|
175 |
hello.string_thing = "Hello2";
|
|
|
176 |
hello.byte_thing = 2;
|
|
|
177 |
hello.i32_thing = 2;
|
|
|
178 |
hello.i64_thing = 2;
|
|
|
179 |
|
|
|
180 |
Xtruct goodbye = new Xtruct();
|
|
|
181 |
goodbye.string_thing = "Goodbye4";
|
|
|
182 |
goodbye.byte_thing = (byte)4;
|
|
|
183 |
goodbye.i32_thing = 4;
|
|
|
184 |
goodbye.i64_thing = (long)4;
|
|
|
185 |
|
|
|
186 |
Insanity crazy = new Insanity();
|
|
|
187 |
crazy.userMap = new HashMap<Numberz, Long>();
|
|
|
188 |
crazy.xtructs = new ArrayList<Xtruct>();
|
|
|
189 |
|
|
|
190 |
crazy.userMap.put(Numberz.EIGHT, (long)8);
|
|
|
191 |
crazy.xtructs.add(goodbye);
|
|
|
192 |
|
|
|
193 |
Insanity looney = new Insanity();
|
|
|
194 |
crazy.userMap.put(Numberz.FIVE, (long)5);
|
|
|
195 |
crazy.xtructs.add(hello);
|
|
|
196 |
|
|
|
197 |
HashMap<Numberz,Insanity> first_map = new HashMap<Numberz, Insanity>();
|
|
|
198 |
HashMap<Numberz,Insanity> second_map = new HashMap<Numberz, Insanity>();;
|
|
|
199 |
|
|
|
200 |
first_map.put(Numberz.TWO, crazy);
|
|
|
201 |
first_map.put(Numberz.THREE, crazy);
|
|
|
202 |
|
|
|
203 |
second_map.put(Numberz.SIX, looney);
|
|
|
204 |
|
|
|
205 |
Map<Long,Map<Numberz,Insanity>> insane =
|
|
|
206 |
new HashMap<Long, Map<Numberz,Insanity>>();
|
|
|
207 |
insane.put((long)1, first_map);
|
|
|
208 |
insane.put((long)2, second_map);
|
|
|
209 |
|
|
|
210 |
return insane;
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
public Xtruct testMulti(byte arg0, int arg1, long arg2, Map<Short,String> arg3, Numberz arg4, long arg5) {
|
|
|
214 |
System.out.print("testMulti()\n");
|
|
|
215 |
|
|
|
216 |
Xtruct hello = new Xtruct();;
|
|
|
217 |
hello.string_thing = "Hello2";
|
|
|
218 |
hello.byte_thing = arg0;
|
|
|
219 |
hello.i32_thing = arg1;
|
|
|
220 |
hello.i64_thing = arg2;
|
|
|
221 |
return hello;
|
|
|
222 |
}
|
|
|
223 |
|
|
|
224 |
public void testException(String arg) throws Xception {
|
|
|
225 |
System.out.print("testException("+arg+")\n");
|
|
|
226 |
if (arg.equals("Xception")) {
|
|
|
227 |
Xception x = new Xception();
|
|
|
228 |
x.errorCode = 1001;
|
|
|
229 |
x.message = "This is an Xception";
|
|
|
230 |
throw x;
|
|
|
231 |
}
|
|
|
232 |
return;
|
|
|
233 |
}
|
|
|
234 |
|
|
|
235 |
public Xtruct testMultiException(String arg0, String arg1) throws Xception, Xception2 {
|
|
|
236 |
System.out.print("testMultiException(" + arg0 + ", " + arg1 + ")\n");
|
|
|
237 |
if (arg0.equals("Xception")) {
|
|
|
238 |
Xception x = new Xception();
|
|
|
239 |
x.errorCode = 1001;
|
|
|
240 |
x.message = "This is an Xception";
|
|
|
241 |
throw x;
|
|
|
242 |
} else if (arg0.equals("Xception2")) {
|
|
|
243 |
Xception2 x = new Xception2();
|
|
|
244 |
x.errorCode = 2002;
|
|
|
245 |
x.struct_thing = new Xtruct();
|
|
|
246 |
x.struct_thing.string_thing = "This is an Xception2";
|
|
|
247 |
throw x;
|
|
|
248 |
}
|
|
|
249 |
|
|
|
250 |
Xtruct result = new Xtruct();
|
|
|
251 |
result.string_thing = arg1;
|
|
|
252 |
return result;
|
|
|
253 |
}
|
|
|
254 |
|
|
|
255 |
public void testOneway(int sleepFor) {
|
|
|
256 |
System.out.println("testOneway(" + Integer.toString(sleepFor) +
|
|
|
257 |
") => sleeping...");
|
|
|
258 |
try {
|
|
|
259 |
Thread.sleep(sleepFor * 1000);
|
|
|
260 |
System.out.println("Done sleeping!");
|
|
|
261 |
} catch (InterruptedException ie) {
|
|
|
262 |
throw new RuntimeException(ie);
|
|
|
263 |
}
|
|
|
264 |
}
|
|
|
265 |
|
|
|
266 |
} // class TestHandler
|
|
|
267 |
|
|
|
268 |
public static void main(String [] args) {
|
|
|
269 |
try {
|
|
|
270 |
int port = 9090;
|
|
|
271 |
if (args.length > 1) {
|
|
|
272 |
port = Integer.valueOf(args[0]);
|
|
|
273 |
}
|
|
|
274 |
|
|
|
275 |
// Processor
|
|
|
276 |
TestHandler testHandler =
|
|
|
277 |
new TestHandler();
|
|
|
278 |
ThriftTest.Processor testProcessor =
|
|
|
279 |
new ThriftTest.Processor(testHandler);
|
|
|
280 |
|
|
|
281 |
// Transport
|
|
|
282 |
TServerSocket tServerSocket =
|
|
|
283 |
new TServerSocket(port);
|
|
|
284 |
|
|
|
285 |
// Protocol factory
|
|
|
286 |
TProtocolFactory tProtocolFactory =
|
|
|
287 |
new TBinaryProtocol.Factory();
|
|
|
288 |
|
|
|
289 |
TServer serverEngine;
|
|
|
290 |
|
|
|
291 |
// Simple Server
|
|
|
292 |
// serverEngine = new TSimpleServer(testProcessor, tServerSocket);
|
|
|
293 |
|
|
|
294 |
// ThreadPool Server
|
|
|
295 |
serverEngine = new TThreadPoolServer(testProcessor, tServerSocket, tProtocolFactory);
|
|
|
296 |
|
|
|
297 |
// Run it
|
|
|
298 |
System.out.println("Starting the server on port " + port + "...");
|
|
|
299 |
serverEngine.serve();
|
|
|
300 |
|
|
|
301 |
} catch (Exception x) {
|
|
|
302 |
x.printStackTrace();
|
|
|
303 |
}
|
|
|
304 |
System.out.println("done.");
|
|
|
305 |
}
|
|
|
306 |
}
|