blob: c5edca474b9a2354521fee9cfb4bf4338a79dabb [file] [log] [blame]
Mark Slee95771002006-06-07 06:53:25 +00001package com.facebook.thrift.test;
2
3import com.facebook.thrift.types.*;
4import com.facebook.thrift.protocol.TBinaryProtocol;
5import com.facebook.thrift.protocol.TProtocol;
6import com.facebook.thrift.protocol.TString;
7import com.facebook.thrift.server.TServer;
8import com.facebook.thrift.server.TSimpleServer;
9import com.facebook.thrift.transport.TServerSocket;
10import com.facebook.thrift.transport.TServerTransport;
11
12import ThriftTest.*;
13
14import java.net.ServerSocket;
15import java.util.ArrayList;
16import java.util.HashMap;
17import java.util.HashSet;
18
19public class TestServer extends ThriftTestServerIf {
20 public TestServer(TProtocol prot) {
21 super(prot);
22 }
23
24 public void testVoid() {
25 System.out.print("testVoid()\n");
26 }
27
28 public TString testString(TString thing) {
29 System.out.print("testString(\"" + thing.value + "\")\n");
30 return thing;
31 }
32
33 public UInt8 testByte(UInt8 thing) {
34 System.out.print("testByte(" + thing.get() + ")\n");
35 return thing;
36 }
37
38 public UInt32 testU32(UInt32 thing) {
39 System.out.print("testU32(" + thing.get() + ")\n");
40 return thing;
41 }
42
43 public Int32 testI32(Int32 thing) {
44 System.out.print("testI32(" + thing.get() + ")\n");
45 return thing;
46 }
47
48 public UInt64 testU64(UInt64 thing) {
49 System.out.print("testU64(" + thing.toLong() + ")\n");
50 return thing;
51 }
52
53 public Int64 testI64(Int64 thing) {
54 System.out.print("testI64(" + thing.get() + ")\n");
55 return thing;
56 }
57
58 public Xtruct testStruct(Xtruct thing) {
59 System.out.print("testStruct({" +
60 "\"" + thing.string_thing.value + "\", " +
61 thing.byte_thing.get() + ", " +
62 thing.u32_thing.get() + ", " +
63 thing.i32_thing.get() + ", " +
64 thing.u64_thing.toLong() + ", " +
65 thing.i64_thing.get() + "})\n");
66 return thing;
67 }
68
69 public Xtruct2 testNest(Xtruct2 nest) {
70 Xtruct thing = nest.struct_thing;
71 System.out.print("testNest({" +
72 nest.byte_thing.get() + ", {" +
73 "\"" + thing.string_thing.value + "\", " +
74 thing.byte_thing.get() + ", " +
75 thing.u32_thing.get() + ", " +
76 thing.i32_thing.get() + ", " +
77 thing.u64_thing.toLong() + ", " +
78 thing.i64_thing.get() + "}, " +
79 nest.i32_thing.get() + "})\n");
80 return nest;
81 }
82
83 public HashMap<Int32,Int32> testMap(HashMap<Int32,Int32> thing) {
84 System.out.print("testMap({");
85 boolean first = true;
86 for (Int32 key : thing.keySet()) {
87 if (first) {
88 first = false;
89 } else {
90 System.out.print(", ");
91 }
92 System.out.print(key.get() + " => " + thing.get(key).get());
93 }
94 System.out.print("})\n");
95 return thing;
96 }
97
98 public HashSet<Int32> testSet(HashSet<Int32> thing) {
99 System.out.print("testSet({");
100 boolean first = true;
101 for (Int32 elem : thing) {
102 if (first) {
103 first = false;
104 } else {
105 System.out.print(", ");
106 }
107 System.out.print(elem.get());
108 }
109 System.out.print("})\n");
110 return thing;
111 }
112
113 public ArrayList<Int32> testList(ArrayList<Int32> thing) {
114 System.out.print("testList({");
115 boolean first = true;
116 for (Int32 elem : thing) {
117 if (first) {
118 first = false;
119 } else {
120 System.out.print(", ");
121 }
122 System.out.print(elem.get());
123 }
124 System.out.print("})\n");
125 return thing;
126 }
127
128 public Int32 testEnum(Int32 thing) {
129 System.out.print("testEnum(" + thing.get() + ")\n");
130 return thing;
131 }
132
133 public UInt64 testTypedef(UInt64 thing) {
134 System.out.print("testTypedef(" + thing.toLong() + ")\n");
135 return thing;
136 }
137
138 public HashMap<Int32,HashMap<Int32,Int32>> testMapMap(Int32 hello) {
139 System.out.print("testMapMap(" + hello.get() + ")\n");
140 HashMap<Int32,HashMap<Int32,Int32>> mapmap =
141 new HashMap<Int32,HashMap<Int32,Int32>>();
142
143 HashMap<Int32,Int32> pos = new HashMap<Int32,Int32>();
144 HashMap<Int32,Int32> neg = new HashMap<Int32,Int32>();
145 for (int i = 1; i < 5; i++) {
146 pos.put(new Int32(i), new Int32(i));
147 neg.put(new Int32(-i), new Int32(-i));
148 }
149
150 mapmap.put(new Int32(4), pos);
151 mapmap.put(new Int32(-4), neg);
152
153 return mapmap;
154 }
155
156 public HashMap<UInt64, HashMap<Int32,Insanity>> testInsanity(Insanity argument) {
157 System.out.print("testInsanity()\n");
158
159 Xtruct hello = new Xtruct();
160 hello.string_thing.value = "Hello2";
161 hello.byte_thing.set((short)2);
162 hello.u32_thing.set(2);
163 hello.i32_thing.set(2);
164 hello.u64_thing.set(2);
165 hello.i64_thing.set(2);
166
167 Xtruct goodbye = new Xtruct();
168 goodbye.string_thing.value = "Goodbye4";
169 goodbye.byte_thing.set((short)4);
170 goodbye.u32_thing.set(4);
171 goodbye.i32_thing.set(4);
172 goodbye.u64_thing.set(4);
173 goodbye.i64_thing.set(4);
174
175 Insanity crazy = new Insanity();
176 crazy.userMap.put(Numberz.EIGHT, new UInt64(8));
177 crazy.xtructs.add(goodbye);
178
179 Insanity looney = new Insanity();
180 crazy.userMap.put(Numberz.FIVE, new UInt64(5));
181 crazy.xtructs.add(hello);
182
183 HashMap<Int32,Insanity> first_map = new HashMap<Int32, Insanity>();
184 HashMap<Int32,Insanity> second_map = new HashMap<Int32, Insanity>();;
185
186 first_map.put(Numberz.TWO, crazy);
187 first_map.put(Numberz.THREE, crazy);
188
189 second_map.put(Numberz.SIX, looney);
190
191 HashMap<UInt64,HashMap<Int32,Insanity>> insane =
192 new HashMap<UInt64, HashMap<Int32,Insanity>>();
193 insane.put(new UInt64(1), first_map);
194 insane.put(new UInt64(2), second_map);
195
196 return insane;
197 }
198
199 public static void main(String [] args) {
200 try {
201 int port = 9090;
202 if (args.length > 1) {
203 port = Integer.valueOf(args[0]);
204 }
205
206 // Processor
207 TBinaryProtocol binaryProtocol = new TBinaryProtocol();
208 TestServer testServer = new TestServer(binaryProtocol);
209
210 // Options
211 TServer.Options serverOptions = new TServer.Options();
212
213 // Transport
214 ServerSocket serverSocket = new ServerSocket(port);
215 TServerSocket tServerSocket = new TServerSocket(serverSocket);
216
217 // Server
218 TSimpleServer simpleServer = new TSimpleServer(testServer,
219 serverOptions,
220 tServerSocket);
221
222 // Run it
223 System.out.println("Starting the server on port " + port + "...");
224 simpleServer.run();
225 } catch (Exception x) {
226 x.printStackTrace();
227 }
228 System.out.println("done.");
229 }
230}