blob: 0acc8a21ec4f0f23f663aab0f2b3f63baa888752 [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
Mark Slee95771002006-06-07 06:53:25 +000038 public Int32 testI32(Int32 thing) {
39 System.out.print("testI32(" + thing.get() + ")\n");
40 return thing;
41 }
42
Mark Slee95771002006-06-07 06:53:25 +000043 public Int64 testI64(Int64 thing) {
44 System.out.print("testI64(" + thing.get() + ")\n");
45 return thing;
46 }
47
48 public Xtruct testStruct(Xtruct thing) {
49 System.out.print("testStruct({" +
50 "\"" + thing.string_thing.value + "\", " +
51 thing.byte_thing.get() + ", " +
Mark Slee95771002006-06-07 06:53:25 +000052 thing.i32_thing.get() + ", " +
Mark Slee95771002006-06-07 06:53:25 +000053 thing.i64_thing.get() + "})\n");
54 return thing;
55 }
56
57 public Xtruct2 testNest(Xtruct2 nest) {
58 Xtruct thing = nest.struct_thing;
59 System.out.print("testNest({" +
60 nest.byte_thing.get() + ", {" +
61 "\"" + thing.string_thing.value + "\", " +
62 thing.byte_thing.get() + ", " +
Mark Slee95771002006-06-07 06:53:25 +000063 thing.i32_thing.get() + ", " +
Mark Slee95771002006-06-07 06:53:25 +000064 thing.i64_thing.get() + "}, " +
65 nest.i32_thing.get() + "})\n");
66 return nest;
67 }
68
69 public HashMap<Int32,Int32> testMap(HashMap<Int32,Int32> thing) {
70 System.out.print("testMap({");
71 boolean first = true;
72 for (Int32 key : thing.keySet()) {
73 if (first) {
74 first = false;
75 } else {
76 System.out.print(", ");
77 }
78 System.out.print(key.get() + " => " + thing.get(key).get());
79 }
80 System.out.print("})\n");
81 return thing;
82 }
83
84 public HashSet<Int32> testSet(HashSet<Int32> thing) {
85 System.out.print("testSet({");
86 boolean first = true;
87 for (Int32 elem : thing) {
88 if (first) {
89 first = false;
90 } else {
91 System.out.print(", ");
92 }
93 System.out.print(elem.get());
94 }
95 System.out.print("})\n");
96 return thing;
97 }
98
99 public ArrayList<Int32> testList(ArrayList<Int32> thing) {
100 System.out.print("testList({");
101 boolean first = true;
102 for (Int32 elem : thing) {
103 if (first) {
104 first = false;
105 } else {
106 System.out.print(", ");
107 }
108 System.out.print(elem.get());
109 }
110 System.out.print("})\n");
111 return thing;
112 }
113
114 public Int32 testEnum(Int32 thing) {
115 System.out.print("testEnum(" + thing.get() + ")\n");
116 return thing;
117 }
118
Mark Slee6e536442006-06-30 18:28:50 +0000119 public Int64 testTypedef(Int64 thing) {
120 System.out.print("testTypedef(" + thing.get() + ")\n");
Mark Slee95771002006-06-07 06:53:25 +0000121 return thing;
122 }
123
124 public HashMap<Int32,HashMap<Int32,Int32>> testMapMap(Int32 hello) {
125 System.out.print("testMapMap(" + hello.get() + ")\n");
126 HashMap<Int32,HashMap<Int32,Int32>> mapmap =
127 new HashMap<Int32,HashMap<Int32,Int32>>();
128
129 HashMap<Int32,Int32> pos = new HashMap<Int32,Int32>();
130 HashMap<Int32,Int32> neg = new HashMap<Int32,Int32>();
131 for (int i = 1; i < 5; i++) {
132 pos.put(new Int32(i), new Int32(i));
133 neg.put(new Int32(-i), new Int32(-i));
134 }
135
136 mapmap.put(new Int32(4), pos);
137 mapmap.put(new Int32(-4), neg);
138
139 return mapmap;
140 }
141
Mark Slee6e536442006-06-30 18:28:50 +0000142 public HashMap<Int64, HashMap<Int32,Insanity>> testInsanity(Insanity argument) {
Mark Slee95771002006-06-07 06:53:25 +0000143 System.out.print("testInsanity()\n");
144
145 Xtruct hello = new Xtruct();
146 hello.string_thing.value = "Hello2";
147 hello.byte_thing.set((short)2);
Mark Slee95771002006-06-07 06:53:25 +0000148 hello.i32_thing.set(2);
Mark Slee95771002006-06-07 06:53:25 +0000149 hello.i64_thing.set(2);
150
151 Xtruct goodbye = new Xtruct();
152 goodbye.string_thing.value = "Goodbye4";
153 goodbye.byte_thing.set((short)4);
Mark Slee95771002006-06-07 06:53:25 +0000154 goodbye.i32_thing.set(4);
Mark Slee95771002006-06-07 06:53:25 +0000155 goodbye.i64_thing.set(4);
156
157 Insanity crazy = new Insanity();
Mark Slee6e536442006-06-30 18:28:50 +0000158 crazy.userMap.put(Numberz.EIGHT, new Int64(8));
Mark Slee95771002006-06-07 06:53:25 +0000159 crazy.xtructs.add(goodbye);
160
161 Insanity looney = new Insanity();
Mark Slee6e536442006-06-30 18:28:50 +0000162 crazy.userMap.put(Numberz.FIVE, new Int64(5));
Mark Slee95771002006-06-07 06:53:25 +0000163 crazy.xtructs.add(hello);
164
165 HashMap<Int32,Insanity> first_map = new HashMap<Int32, Insanity>();
166 HashMap<Int32,Insanity> second_map = new HashMap<Int32, Insanity>();;
167
168 first_map.put(Numberz.TWO, crazy);
169 first_map.put(Numberz.THREE, crazy);
170
171 second_map.put(Numberz.SIX, looney);
172
Mark Slee6e536442006-06-30 18:28:50 +0000173 HashMap<Int64,HashMap<Int32,Insanity>> insane =
174 new HashMap<Int64, HashMap<Int32,Insanity>>();
175 insane.put(new Int64(1), first_map);
176 insane.put(new Int64(2), second_map);
Mark Slee95771002006-06-07 06:53:25 +0000177
178 return insane;
179 }
180
181 public static void main(String [] args) {
182 try {
183 int port = 9090;
184 if (args.length > 1) {
185 port = Integer.valueOf(args[0]);
186 }
187
188 // Processor
189 TBinaryProtocol binaryProtocol = new TBinaryProtocol();
190 TestServer testServer = new TestServer(binaryProtocol);
191
192 // Options
193 TServer.Options serverOptions = new TServer.Options();
194
195 // Transport
196 ServerSocket serverSocket = new ServerSocket(port);
197 TServerSocket tServerSocket = new TServerSocket(serverSocket);
198
199 // Server
200 TSimpleServer simpleServer = new TSimpleServer(testServer,
201 serverOptions,
202 tServerSocket);
203
204 // Run it
205 System.out.println("Starting the server on port " + port + "...");
206 simpleServer.run();
207 } catch (Exception x) {
208 x.printStackTrace();
209 }
210 System.out.println("done.");
211 }
212}