blob: 894ec9c236edd8535d719f7d0697811bf5d5d17d [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;
118 }
119
David Reissd831a212009-02-13 03:09:52 +0000120 public THashSet<int> testSet(THashSet<int> thing)
David Reiss63191332009-01-06 19:49:22 +0000121 {
122 Console.WriteLine("testSet({");
123 bool first = true;
124 foreach (int elem in thing)
125 {
126 if (first)
127 {
128 first = false;
129 }
130 else
131 {
132 Console.WriteLine(", ");
133 }
134 Console.WriteLine(elem);
135 }
136 Console.WriteLine("})");
137 return thing;
138 }
139
140 public List<int> testList(List<int> thing)
141 {
142 Console.WriteLine("testList({");
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 Numberz testEnum(Numberz thing)
161 {
162 Console.WriteLine("testEnum(" + thing + ")");
163 return thing;
164 }
165
166 public long testTypedef(long thing)
167 {
168 Console.WriteLine("testTypedef(" + thing + ")");
169 return thing;
170 }
171
172 public Dictionary<int, Dictionary<int, int>> testMapMap(int hello)
173 {
174 Console.WriteLine("testMapMap(" + hello + ")");
175 Dictionary<int, Dictionary<int, int>> mapmap =
176 new Dictionary<int, Dictionary<int, int>>();
177
178 Dictionary<int, int> pos = new Dictionary<int, int>();
179 Dictionary<int, int> neg = new Dictionary<int, int>();
180 for (int i = 1; i < 5; i++)
181 {
182 pos[i] = i;
183 neg[-i] = -i;
184 }
185
186 mapmap[4] = pos;
187 mapmap[-4] = neg;
188
189 return mapmap;
190 }
191
192 public Dictionary<long, Dictionary<Numberz, Insanity>> testInsanity(Insanity argument)
193 {
194 Console.WriteLine("testInsanity()");
195
196 Xtruct hello = new Xtruct();
197 hello.String_thing = "Hello2";
198 hello.Byte_thing = 2;
199 hello.I32_thing = 2;
200 hello.I64_thing = 2;
201
202 Xtruct goodbye = new Xtruct();
203 goodbye.String_thing = "Goodbye4";
204 goodbye.Byte_thing = (byte)4;
205 goodbye.I32_thing = 4;
206 goodbye.I64_thing = (long)4;
207
208 Insanity crazy = new Insanity();
209 crazy.UserMap = new Dictionary<Numberz, long>();
210 crazy.UserMap[Numberz.EIGHT] = (long)8;
211 crazy.Xtructs = new List<Xtruct>();
212 crazy.Xtructs.Add(goodbye);
213
214 Insanity looney = new Insanity();
215 crazy.UserMap[Numberz.FIVE] = (long)5;
216 crazy.Xtructs.Add(hello);
217
218 Dictionary<Numberz, Insanity> first_map = new Dictionary<Numberz, Insanity>();
219 Dictionary<Numberz, Insanity> second_map = new Dictionary<Numberz, Insanity>(); ;
220
221 first_map[Numberz.TWO] = crazy;
222 first_map[Numberz.THREE] = crazy;
223
224 second_map[Numberz.SIX] = looney;
225
226 Dictionary<long, Dictionary<Numberz, Insanity>> insane =
227 new Dictionary<long, Dictionary<Numberz, Insanity>>();
228 insane[(long)1] = first_map;
229 insane[(long)2] = second_map;
230
231 return insane;
232 }
233
234 public Xtruct testMulti(byte arg0, int arg1, long arg2, Dictionary<short, string> arg3, Numberz arg4, long arg5)
235 {
236 Console.WriteLine("testMulti()");
237
238 Xtruct hello = new Xtruct(); ;
239 hello.String_thing = "Hello2";
240 hello.Byte_thing = arg0;
241 hello.I32_thing = arg1;
242 hello.I64_thing = arg2;
243 return hello;
244 }
245
246 public void testException(string arg)
247 {
248 Console.WriteLine("testException(" + arg + ")");
249 if (arg == "Xception")
250 {
251 Xception x = new Xception();
252 x.ErrorCode = 1001;
253 x.Message = "This is an Xception";
254 throw x;
255 }
256 return;
257 }
258
259 public Xtruct testMultiException(string arg0, string arg1)
260 {
261 Console.WriteLine("testMultiException(" + arg0 + ", " + arg1 + ")");
262 if (arg0 == "Xception")
263 {
264 Xception x = new Xception();
265 x.ErrorCode = 1001;
266 x.Message = "This is an Xception";
267 throw x;
268 }
269 else if (arg0 == "Xception2")
270 {
271 Xception2 x = new Xception2();
272 x.ErrorCode = 2002;
273 x.Struct_thing = new Xtruct();
274 x.Struct_thing.String_thing = "This is an Xception2";
275 throw x;
276 }
277
278 Xtruct result = new Xtruct();
279 result.String_thing = arg1;
280 return result;
281 }
282
283 public void testStop()
284 {
285 if (server != null)
286 {
287 server.Stop();
288 }
289 }
290
David Reiss6ce401d2009-03-24 20:01:58 +0000291 public void testOneway(int arg)
David Reiss63191332009-01-06 19:49:22 +0000292 {
David Reiss6ce401d2009-03-24 20:01:58 +0000293 Console.WriteLine("testOneway(" + arg + "), sleeping...");
David Reiss63191332009-01-06 19:49:22 +0000294 System.Threading.Thread.Sleep(arg * 1000);
David Reiss6ce401d2009-03-24 20:01:58 +0000295 Console.WriteLine("testOneway finished");
David Reiss63191332009-01-06 19:49:22 +0000296 }
297
298 } // class TestHandler
299
300 public static void Execute(string[] args)
301 {
302 try
303 {
T Jake Luciani7070aaa2011-01-27 02:51:51 +0000304 bool useBufferedSockets = false, useFramed = false;
David Reiss63191332009-01-06 19:49:22 +0000305 int port = 9090;
306 if (args.Length > 0)
307 {
308 port = int.Parse(args[0]);
309
310 if (args.Length > 1)
311 {
T Jake Luciani7070aaa2011-01-27 02:51:51 +0000312 if ( args[1] == "raw" )
313 {
314 // as default
315 }
316 else if ( args[1] == "buffered" )
317 {
318 useBufferedSockets = true;
319 }
320 else if ( args[1] == "framed" )
321 {
322 useFramed = true;
323 }
324 else
325 {
326 // Fall back to the older boolean syntax
327 bool.TryParse(args[1], out useBufferedSockets);
328 }
David Reiss63191332009-01-06 19:49:22 +0000329 }
330 }
331
332 // Processor
333 TestHandler testHandler = new TestHandler();
334 ThriftTest.Processor testProcessor = new ThriftTest.Processor(testHandler);
335
336 // Transport
337 TServerSocket tServerSocket = new TServerSocket(port, 0, useBufferedSockets);
338
David Reiss63191332009-01-06 19:49:22 +0000339 // Simple Server
T Jake Luciani7070aaa2011-01-27 02:51:51 +0000340 TServer serverEngine;
341 if ( useFramed )
342 serverEngine = new TSimpleServer(testProcessor, tServerSocket, new TFramedTransport.Factory());
343 else
344 serverEngine = new TSimpleServer(testProcessor, tServerSocket);
David Reiss63191332009-01-06 19:49:22 +0000345
346 // ThreadPool Server
David Reissd831a212009-02-13 03:09:52 +0000347 // serverEngine = new TThreadPoolServer(testProcessor, tServerSocket);
348
349 // Threaded Server
350 // serverEngine = new TThreadedServer(testProcessor, tServerSocket);
David Reiss63191332009-01-06 19:49:22 +0000351
352 testHandler.server = serverEngine;
353
354 // Run it
T Jake Luciani7070aaa2011-01-27 02:51:51 +0000355 Console.WriteLine("Starting the server on port " + port +
356 (useBufferedSockets ? " with buffered socket" : "") +
357 (useFramed ? " with framed transport" : "") +
358 "...");
David Reiss63191332009-01-06 19:49:22 +0000359 serverEngine.Serve();
360
361 }
362 catch (Exception x)
363 {
364 Console.Error.Write(x);
365 }
366 Console.WriteLine("done.");
367 }
368 }
369}