blob: 4efe4b4dfb680c29c1448152998ca1dcc96ceae4 [file] [log] [blame]
David Reiss63191332009-01-06 19:49:22 +00001// Distributed under the Thrift Software License
2//
3// See accompanying file LICENSE or visit the Thrift site at:
4// http://developers.facebook.com/thrift/
5using System;
6using System.Collections.Generic;
7using System.Text;
8using Thrift.Test; //generated code
9
10using Thrift.Transport;
11using Thrift.Protocol;
12using Thrift.Server;
13
14namespace Test
15{
16 public class TestServer
17 {
18 public class TestHandler : ThriftTest.Iface
19 {
20 public TServer server;
21
22 public TestHandler() { }
23
24 public void testVoid()
25 {
26 Console.WriteLine("testVoid()");
27 }
28
29 public string testString(string thing)
30 {
31 Console.WriteLine("teststring(\"" + thing + "\")");
32 return thing;
33 }
34
35 public byte testByte(byte thing)
36 {
37 Console.WriteLine("testByte(" + thing + ")");
38 return thing;
39 }
40
41 public int testI32(int thing)
42 {
43 Console.WriteLine("testI32(" + thing + ")");
44 return thing;
45 }
46
47 public long testI64(long thing)
48 {
49 Console.WriteLine("testI64(" + thing + ")");
50 return thing;
51 }
52
53 public double testDouble(double thing)
54 {
55 Console.WriteLine("testDouble(" + thing + ")");
56 return thing;
57 }
58
59 public Xtruct testStruct(Xtruct thing)
60 {
61 Console.WriteLine("testStruct({" +
62 "\"" + thing.String_thing + "\", " +
63 thing.Byte_thing + ", " +
64 thing.I32_thing + ", " +
65 thing.I64_thing + "})");
66 return thing;
67 }
68
69 public Xtruct2 testNest(Xtruct2 nest)
70 {
71 Xtruct thing = nest.Struct_thing;
72 Console.WriteLine("testNest({" +
73 nest.Byte_thing + ", {" +
74 "\"" + thing.String_thing + "\", " +
75 thing.Byte_thing + ", " +
76 thing.I32_thing + ", " +
77 thing.I64_thing + "}, " +
78 nest.I32_thing + "})");
79 return nest;
80 }
81
82 public Dictionary<int, int> testMap(Dictionary<int, int> thing)
83 {
84 Console.WriteLine("testMap({");
85 bool first = true;
86 foreach (int key in thing.Keys)
87 {
88 if (first)
89 {
90 first = false;
91 }
92 else
93 {
94 Console.WriteLine(", ");
95 }
96 Console.WriteLine(key + " => " + thing[key]);
97 }
98 Console.WriteLine("})");
99 return thing;
100 }
101
102 public HashSet<int> testSet(HashSet<int> thing)
103 {
104 Console.WriteLine("testSet({");
105 bool first = true;
106 foreach (int elem in thing)
107 {
108 if (first)
109 {
110 first = false;
111 }
112 else
113 {
114 Console.WriteLine(", ");
115 }
116 Console.WriteLine(elem);
117 }
118 Console.WriteLine("})");
119 return thing;
120 }
121
122 public List<int> testList(List<int> thing)
123 {
124 Console.WriteLine("testList({");
125 bool first = true;
126 foreach (int elem in thing)
127 {
128 if (first)
129 {
130 first = false;
131 }
132 else
133 {
134 Console.WriteLine(", ");
135 }
136 Console.WriteLine(elem);
137 }
138 Console.WriteLine("})");
139 return thing;
140 }
141
142 public Numberz testEnum(Numberz thing)
143 {
144 Console.WriteLine("testEnum(" + thing + ")");
145 return thing;
146 }
147
148 public long testTypedef(long thing)
149 {
150 Console.WriteLine("testTypedef(" + thing + ")");
151 return thing;
152 }
153
154 public Dictionary<int, Dictionary<int, int>> testMapMap(int hello)
155 {
156 Console.WriteLine("testMapMap(" + hello + ")");
157 Dictionary<int, Dictionary<int, int>> mapmap =
158 new Dictionary<int, Dictionary<int, int>>();
159
160 Dictionary<int, int> pos = new Dictionary<int, int>();
161 Dictionary<int, int> neg = new Dictionary<int, int>();
162 for (int i = 1; i < 5; i++)
163 {
164 pos[i] = i;
165 neg[-i] = -i;
166 }
167
168 mapmap[4] = pos;
169 mapmap[-4] = neg;
170
171 return mapmap;
172 }
173
174 public Dictionary<long, Dictionary<Numberz, Insanity>> testInsanity(Insanity argument)
175 {
176 Console.WriteLine("testInsanity()");
177
178 Xtruct hello = new Xtruct();
179 hello.String_thing = "Hello2";
180 hello.Byte_thing = 2;
181 hello.I32_thing = 2;
182 hello.I64_thing = 2;
183
184 Xtruct goodbye = new Xtruct();
185 goodbye.String_thing = "Goodbye4";
186 goodbye.Byte_thing = (byte)4;
187 goodbye.I32_thing = 4;
188 goodbye.I64_thing = (long)4;
189
190 Insanity crazy = new Insanity();
191 crazy.UserMap = new Dictionary<Numberz, long>();
192 crazy.UserMap[Numberz.EIGHT] = (long)8;
193 crazy.Xtructs = new List<Xtruct>();
194 crazy.Xtructs.Add(goodbye);
195
196 Insanity looney = new Insanity();
197 crazy.UserMap[Numberz.FIVE] = (long)5;
198 crazy.Xtructs.Add(hello);
199
200 Dictionary<Numberz, Insanity> first_map = new Dictionary<Numberz, Insanity>();
201 Dictionary<Numberz, Insanity> second_map = new Dictionary<Numberz, Insanity>(); ;
202
203 first_map[Numberz.TWO] = crazy;
204 first_map[Numberz.THREE] = crazy;
205
206 second_map[Numberz.SIX] = looney;
207
208 Dictionary<long, Dictionary<Numberz, Insanity>> insane =
209 new Dictionary<long, Dictionary<Numberz, Insanity>>();
210 insane[(long)1] = first_map;
211 insane[(long)2] = second_map;
212
213 return insane;
214 }
215
216 public Xtruct testMulti(byte arg0, int arg1, long arg2, Dictionary<short, string> arg3, Numberz arg4, long arg5)
217 {
218 Console.WriteLine("testMulti()");
219
220 Xtruct hello = new Xtruct(); ;
221 hello.String_thing = "Hello2";
222 hello.Byte_thing = arg0;
223 hello.I32_thing = arg1;
224 hello.I64_thing = arg2;
225 return hello;
226 }
227
228 public void testException(string arg)
229 {
230 Console.WriteLine("testException(" + arg + ")");
231 if (arg == "Xception")
232 {
233 Xception x = new Xception();
234 x.ErrorCode = 1001;
235 x.Message = "This is an Xception";
236 throw x;
237 }
238 return;
239 }
240
241 public Xtruct testMultiException(string arg0, string arg1)
242 {
243 Console.WriteLine("testMultiException(" + arg0 + ", " + arg1 + ")");
244 if (arg0 == "Xception")
245 {
246 Xception x = new Xception();
247 x.ErrorCode = 1001;
248 x.Message = "This is an Xception";
249 throw x;
250 }
251 else if (arg0 == "Xception2")
252 {
253 Xception2 x = new Xception2();
254 x.ErrorCode = 2002;
255 x.Struct_thing = new Xtruct();
256 x.Struct_thing.String_thing = "This is an Xception2";
257 throw x;
258 }
259
260 Xtruct result = new Xtruct();
261 result.String_thing = arg1;
262 return result;
263 }
264
265 public void testStop()
266 {
267 if (server != null)
268 {
269 server.Stop();
270 }
271 }
272
273 public void testAsync(int arg)
274 {
275 Console.WriteLine("testAsync(" + arg + "), sleeping...");
276 System.Threading.Thread.Sleep(arg * 1000);
277 Console.WriteLine("testAsync finished");
278 }
279
280 } // class TestHandler
281
282 public static void Execute(string[] args)
283 {
284 try
285 {
286 bool useBufferedSockets = false;
287 int port = 9090;
288 if (args.Length > 0)
289 {
290 port = int.Parse(args[0]);
291
292 if (args.Length > 1)
293 {
294 bool.TryParse(args[1], out useBufferedSockets);
295 }
296 }
297
298 // Processor
299 TestHandler testHandler = new TestHandler();
300 ThriftTest.Processor testProcessor = new ThriftTest.Processor(testHandler);
301
302 // Transport
303 TServerSocket tServerSocket = new TServerSocket(port, 0, useBufferedSockets);
304
305 TServer serverEngine;
306
307 // Simple Server
308 // serverEngine = new TSimpleServer(testProcessor, tServerSocket);
309
310 // ThreadPool Server
311 serverEngine = new TThreadPoolServer(testProcessor, tServerSocket);
312
313 testHandler.server = serverEngine;
314
315 // Run it
316 Console.WriteLine("Starting the server on port " + port + (useBufferedSockets ? " with buffered socket" : "") + "...");
317 serverEngine.Serve();
318
319 }
320 catch (Exception x)
321 {
322 Console.Error.Write(x);
323 }
324 Console.WriteLine("done.");
325 }
326 }
327}