blob: 6fb75f44c264fa50f8f1fb15fa0d140fd93ed7be [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
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
David Reiss63191332009-01-06 19:49:22 +000020// Distributed under the Thrift Software License
21//
22// See accompanying file LICENSE or visit the Thrift site at:
23// http://developers.facebook.com/thrift/
24using System;
25using System.Collections.Generic;
David Reissd831a212009-02-13 03:09:52 +000026using Thrift.Collections;
David Reiss63191332009-01-06 19:49:22 +000027using Thrift.Test; //generated code
David Reiss63191332009-01-06 19:49:22 +000028using Thrift.Transport;
29using Thrift.Protocol;
30using Thrift.Server;
31
32namespace Test
33{
34 public class TestServer
35 {
36 public class TestHandler : ThriftTest.Iface
37 {
38 public TServer server;
39
40 public TestHandler() { }
41
42 public void testVoid()
43 {
44 Console.WriteLine("testVoid()");
45 }
46
47 public string testString(string thing)
48 {
49 Console.WriteLine("teststring(\"" + thing + "\")");
50 return thing;
51 }
52
53 public byte testByte(byte thing)
54 {
55 Console.WriteLine("testByte(" + thing + ")");
56 return thing;
57 }
58
59 public int testI32(int thing)
60 {
61 Console.WriteLine("testI32(" + thing + ")");
62 return thing;
63 }
64
65 public long testI64(long thing)
66 {
67 Console.WriteLine("testI64(" + thing + ")");
68 return thing;
69 }
70
71 public double testDouble(double thing)
72 {
73 Console.WriteLine("testDouble(" + thing + ")");
74 return thing;
75 }
76
77 public Xtruct testStruct(Xtruct thing)
78 {
79 Console.WriteLine("testStruct({" +
80 "\"" + thing.String_thing + "\", " +
81 thing.Byte_thing + ", " +
82 thing.I32_thing + ", " +
83 thing.I64_thing + "})");
84 return thing;
85 }
86
87 public Xtruct2 testNest(Xtruct2 nest)
88 {
89 Xtruct thing = nest.Struct_thing;
90 Console.WriteLine("testNest({" +
91 nest.Byte_thing + ", {" +
92 "\"" + thing.String_thing + "\", " +
93 thing.Byte_thing + ", " +
94 thing.I32_thing + ", " +
95 thing.I64_thing + "}, " +
96 nest.I32_thing + "})");
97 return nest;
98 }
99
100 public Dictionary<int, int> testMap(Dictionary<int, int> thing)
101 {
102 Console.WriteLine("testMap({");
103 bool first = true;
104 foreach (int key in thing.Keys)
105 {
106 if (first)
107 {
108 first = false;
109 }
110 else
111 {
112 Console.WriteLine(", ");
113 }
114 Console.WriteLine(key + " => " + thing[key]);
115 }
116 Console.WriteLine("})");
117 return thing;
Roger Meierf0e517d2012-01-17 21:20:56 +0000118 }
119
120 public Dictionary<string, string> testStringMap(Dictionary<string, string> thing)
121 {
122 Console.WriteLine("testStringMap({");
123 bool first = true;
124 foreach (string key in thing.Keys)
125 {
126 if (first)
127 {
128 first = false;
129 }
130 else
131 {
132 Console.WriteLine(", ");
133 }
134 Console.WriteLine(key + " => " + thing[key]);
135 }
136 Console.WriteLine("})");
137 return thing;
138 }
David Reiss63191332009-01-06 19:49:22 +0000139
David Reissd831a212009-02-13 03:09:52 +0000140 public THashSet<int> testSet(THashSet<int> thing)
David Reiss63191332009-01-06 19:49:22 +0000141 {
142 Console.WriteLine("testSet({");
143 bool first = true;
144 foreach (int elem in thing)
145 {
146 if (first)
147 {
148 first = false;
149 }
150 else
151 {
152 Console.WriteLine(", ");
153 }
154 Console.WriteLine(elem);
155 }
156 Console.WriteLine("})");
157 return thing;
158 }
159
160 public List<int> testList(List<int> thing)
161 {
162 Console.WriteLine("testList({");
163 bool first = true;
164 foreach (int elem in thing)
165 {
166 if (first)
167 {
168 first = false;
169 }
170 else
171 {
172 Console.WriteLine(", ");
173 }
174 Console.WriteLine(elem);
175 }
176 Console.WriteLine("})");
177 return thing;
178 }
179
180 public Numberz testEnum(Numberz thing)
181 {
182 Console.WriteLine("testEnum(" + thing + ")");
183 return thing;
184 }
185
186 public long testTypedef(long thing)
187 {
188 Console.WriteLine("testTypedef(" + thing + ")");
189 return thing;
190 }
191
192 public Dictionary<int, Dictionary<int, int>> testMapMap(int hello)
193 {
194 Console.WriteLine("testMapMap(" + hello + ")");
195 Dictionary<int, Dictionary<int, int>> mapmap =
196 new Dictionary<int, Dictionary<int, int>>();
197
198 Dictionary<int, int> pos = new Dictionary<int, int>();
199 Dictionary<int, int> neg = new Dictionary<int, int>();
200 for (int i = 1; i < 5; i++)
201 {
202 pos[i] = i;
203 neg[-i] = -i;
204 }
205
206 mapmap[4] = pos;
207 mapmap[-4] = neg;
208
209 return mapmap;
210 }
211
212 public Dictionary<long, Dictionary<Numberz, Insanity>> testInsanity(Insanity argument)
213 {
214 Console.WriteLine("testInsanity()");
215
216 Xtruct hello = new Xtruct();
217 hello.String_thing = "Hello2";
218 hello.Byte_thing = 2;
219 hello.I32_thing = 2;
220 hello.I64_thing = 2;
221
222 Xtruct goodbye = new Xtruct();
223 goodbye.String_thing = "Goodbye4";
224 goodbye.Byte_thing = (byte)4;
225 goodbye.I32_thing = 4;
226 goodbye.I64_thing = (long)4;
227
228 Insanity crazy = new Insanity();
229 crazy.UserMap = new Dictionary<Numberz, long>();
230 crazy.UserMap[Numberz.EIGHT] = (long)8;
231 crazy.Xtructs = new List<Xtruct>();
232 crazy.Xtructs.Add(goodbye);
233
234 Insanity looney = new Insanity();
235 crazy.UserMap[Numberz.FIVE] = (long)5;
236 crazy.Xtructs.Add(hello);
237
238 Dictionary<Numberz, Insanity> first_map = new Dictionary<Numberz, Insanity>();
239 Dictionary<Numberz, Insanity> second_map = new Dictionary<Numberz, Insanity>(); ;
240
241 first_map[Numberz.TWO] = crazy;
242 first_map[Numberz.THREE] = crazy;
243
244 second_map[Numberz.SIX] = looney;
245
246 Dictionary<long, Dictionary<Numberz, Insanity>> insane =
247 new Dictionary<long, Dictionary<Numberz, Insanity>>();
248 insane[(long)1] = first_map;
249 insane[(long)2] = second_map;
250
251 return insane;
252 }
253
254 public Xtruct testMulti(byte arg0, int arg1, long arg2, Dictionary<short, string> arg3, Numberz arg4, long arg5)
255 {
256 Console.WriteLine("testMulti()");
257
258 Xtruct hello = new Xtruct(); ;
259 hello.String_thing = "Hello2";
260 hello.Byte_thing = arg0;
261 hello.I32_thing = arg1;
262 hello.I64_thing = arg2;
263 return hello;
264 }
265
266 public void testException(string arg)
267 {
268 Console.WriteLine("testException(" + arg + ")");
269 if (arg == "Xception")
270 {
271 Xception x = new Xception();
272 x.ErrorCode = 1001;
273 x.Message = "This is an Xception";
274 throw x;
275 }
276 return;
277 }
278
279 public Xtruct testMultiException(string arg0, string arg1)
280 {
281 Console.WriteLine("testMultiException(" + arg0 + ", " + arg1 + ")");
282 if (arg0 == "Xception")
283 {
284 Xception x = new Xception();
285 x.ErrorCode = 1001;
286 x.Message = "This is an Xception";
287 throw x;
288 }
289 else if (arg0 == "Xception2")
290 {
291 Xception2 x = new Xception2();
292 x.ErrorCode = 2002;
293 x.Struct_thing = new Xtruct();
294 x.Struct_thing.String_thing = "This is an Xception2";
295 throw x;
296 }
297
298 Xtruct result = new Xtruct();
299 result.String_thing = arg1;
300 return result;
301 }
302
303 public void testStop()
304 {
305 if (server != null)
306 {
307 server.Stop();
308 }
309 }
310
David Reiss6ce401d2009-03-24 20:01:58 +0000311 public void testOneway(int arg)
David Reiss63191332009-01-06 19:49:22 +0000312 {
David Reiss6ce401d2009-03-24 20:01:58 +0000313 Console.WriteLine("testOneway(" + arg + "), sleeping...");
David Reiss63191332009-01-06 19:49:22 +0000314 System.Threading.Thread.Sleep(arg * 1000);
David Reiss6ce401d2009-03-24 20:01:58 +0000315 Console.WriteLine("testOneway finished");
David Reiss63191332009-01-06 19:49:22 +0000316 }
317
318 } // class TestHandler
319
320 public static void Execute(string[] args)
321 {
322 try
323 {
T Jake Luciani7070aaa2011-01-27 02:51:51 +0000324 bool useBufferedSockets = false, useFramed = false;
David Reiss63191332009-01-06 19:49:22 +0000325 int port = 9090;
326 if (args.Length > 0)
327 {
328 port = int.Parse(args[0]);
329
330 if (args.Length > 1)
331 {
T Jake Luciani7070aaa2011-01-27 02:51:51 +0000332 if ( args[1] == "raw" )
333 {
334 // as default
335 }
336 else if ( args[1] == "buffered" )
337 {
338 useBufferedSockets = true;
339 }
340 else if ( args[1] == "framed" )
341 {
342 useFramed = true;
343 }
344 else
345 {
346 // Fall back to the older boolean syntax
347 bool.TryParse(args[1], out useBufferedSockets);
348 }
David Reiss63191332009-01-06 19:49:22 +0000349 }
350 }
351
352 // Processor
353 TestHandler testHandler = new TestHandler();
354 ThriftTest.Processor testProcessor = new ThriftTest.Processor(testHandler);
355
356 // Transport
357 TServerSocket tServerSocket = new TServerSocket(port, 0, useBufferedSockets);
358
David Reiss63191332009-01-06 19:49:22 +0000359 // Simple Server
T Jake Luciani7070aaa2011-01-27 02:51:51 +0000360 TServer serverEngine;
361 if ( useFramed )
362 serverEngine = new TSimpleServer(testProcessor, tServerSocket, new TFramedTransport.Factory());
363 else
364 serverEngine = new TSimpleServer(testProcessor, tServerSocket);
David Reiss63191332009-01-06 19:49:22 +0000365
366 // ThreadPool Server
David Reissd831a212009-02-13 03:09:52 +0000367 // serverEngine = new TThreadPoolServer(testProcessor, tServerSocket);
368
369 // Threaded Server
370 // serverEngine = new TThreadedServer(testProcessor, tServerSocket);
David Reiss63191332009-01-06 19:49:22 +0000371
372 testHandler.server = serverEngine;
373
374 // Run it
T Jake Luciani7070aaa2011-01-27 02:51:51 +0000375 Console.WriteLine("Starting the server on port " + port +
376 (useBufferedSockets ? " with buffered socket" : "") +
377 (useFramed ? " with framed transport" : "") +
378 "...");
David Reiss63191332009-01-06 19:49:22 +0000379 serverEngine.Serve();
380
381 }
382 catch (Exception x)
383 {
384 Console.Error.Write(x);
385 }
386 Console.WriteLine("done.");
387 }
388 }
389}