blob: f0e9abbf7d8b742bee4fd80b01a6ec196a5265ed [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;
Jens Geyerc1d79432014-04-22 22:52:43 +020026using System.Security.Cryptography.X509Certificates;
David Reissd831a212009-02-13 03:09:52 +000027using Thrift.Collections;
David Reiss63191332009-01-06 19:49:22 +000028using Thrift.Test; //generated code
David Reiss63191332009-01-06 19:49:22 +000029using Thrift.Transport;
30using Thrift.Protocol;
31using Thrift.Server;
32
33namespace Test
34{
35 public class TestServer
36 {
37 public class TestHandler : ThriftTest.Iface
38 {
39 public TServer server;
40
41 public TestHandler() { }
42
43 public void testVoid()
44 {
45 Console.WriteLine("testVoid()");
46 }
47
48 public string testString(string thing)
49 {
50 Console.WriteLine("teststring(\"" + thing + "\")");
51 return thing;
52 }
53
Jens Geyer1b4c9b92013-04-26 23:38:58 +020054 public sbyte testByte(sbyte thing)
David Reiss63191332009-01-06 19:49:22 +000055 {
56 Console.WriteLine("testByte(" + thing + ")");
57 return thing;
58 }
59
60 public int testI32(int thing)
61 {
62 Console.WriteLine("testI32(" + thing + ")");
63 return thing;
64 }
65
66 public long testI64(long thing)
67 {
68 Console.WriteLine("testI64(" + thing + ")");
69 return thing;
70 }
71
72 public double testDouble(double thing)
73 {
74 Console.WriteLine("testDouble(" + thing + ")");
75 return thing;
76 }
77
78 public Xtruct testStruct(Xtruct thing)
79 {
80 Console.WriteLine("testStruct({" +
81 "\"" + thing.String_thing + "\", " +
82 thing.Byte_thing + ", " +
83 thing.I32_thing + ", " +
84 thing.I64_thing + "})");
85 return thing;
86 }
87
88 public Xtruct2 testNest(Xtruct2 nest)
89 {
90 Xtruct thing = nest.Struct_thing;
91 Console.WriteLine("testNest({" +
92 nest.Byte_thing + ", {" +
93 "\"" + thing.String_thing + "\", " +
94 thing.Byte_thing + ", " +
95 thing.I32_thing + ", " +
96 thing.I64_thing + "}, " +
97 nest.I32_thing + "})");
98 return nest;
99 }
100
101 public Dictionary<int, int> testMap(Dictionary<int, int> thing)
102 {
103 Console.WriteLine("testMap({");
104 bool first = true;
105 foreach (int key in thing.Keys)
106 {
107 if (first)
108 {
109 first = false;
110 }
111 else
112 {
113 Console.WriteLine(", ");
114 }
115 Console.WriteLine(key + " => " + thing[key]);
116 }
117 Console.WriteLine("})");
118 return thing;
Jens Geyer1c99e702014-03-17 22:50:39 +0200119 }
120
Jens Geyerfd62df72014-03-20 00:52:18 +0200121 public Dictionary<string, string> testStringMap(Dictionary<string, string> thing)
122 {
123 Console.WriteLine("testStringMap({");
124 bool first = true;
125 foreach (string key in thing.Keys)
126 {
127 if (first)
128 {
129 first = false;
130 }
131 else
132 {
133 Console.WriteLine(", ");
134 }
135 Console.WriteLine(key + " => " + thing[key]);
136 }
137 Console.WriteLine("})");
138 return thing;
139 }
David Reiss63191332009-01-06 19:49:22 +0000140
David Reissd831a212009-02-13 03:09:52 +0000141 public THashSet<int> testSet(THashSet<int> thing)
David Reiss63191332009-01-06 19:49:22 +0000142 {
143 Console.WriteLine("testSet({");
144 bool first = true;
145 foreach (int elem in thing)
146 {
147 if (first)
148 {
149 first = false;
150 }
151 else
152 {
153 Console.WriteLine(", ");
154 }
155 Console.WriteLine(elem);
156 }
157 Console.WriteLine("})");
158 return thing;
159 }
160
161 public List<int> testList(List<int> thing)
162 {
163 Console.WriteLine("testList({");
164 bool first = true;
165 foreach (int elem in thing)
166 {
167 if (first)
168 {
169 first = false;
170 }
171 else
172 {
173 Console.WriteLine(", ");
174 }
175 Console.WriteLine(elem);
176 }
177 Console.WriteLine("})");
178 return thing;
179 }
180
181 public Numberz testEnum(Numberz thing)
182 {
183 Console.WriteLine("testEnum(" + thing + ")");
184 return thing;
185 }
186
187 public long testTypedef(long thing)
188 {
189 Console.WriteLine("testTypedef(" + thing + ")");
190 return thing;
191 }
192
193 public Dictionary<int, Dictionary<int, int>> testMapMap(int hello)
194 {
195 Console.WriteLine("testMapMap(" + hello + ")");
196 Dictionary<int, Dictionary<int, int>> mapmap =
197 new Dictionary<int, Dictionary<int, int>>();
198
199 Dictionary<int, int> pos = new Dictionary<int, int>();
200 Dictionary<int, int> neg = new Dictionary<int, int>();
201 for (int i = 1; i < 5; i++)
202 {
203 pos[i] = i;
204 neg[-i] = -i;
205 }
206
207 mapmap[4] = pos;
208 mapmap[-4] = neg;
209
210 return mapmap;
211 }
212
213 public Dictionary<long, Dictionary<Numberz, Insanity>> testInsanity(Insanity argument)
214 {
215 Console.WriteLine("testInsanity()");
216
217 Xtruct hello = new Xtruct();
218 hello.String_thing = "Hello2";
219 hello.Byte_thing = 2;
220 hello.I32_thing = 2;
221 hello.I64_thing = 2;
222
223 Xtruct goodbye = new Xtruct();
224 goodbye.String_thing = "Goodbye4";
Jens Geyer1b4c9b92013-04-26 23:38:58 +0200225 goodbye.Byte_thing = (sbyte)4;
David Reiss63191332009-01-06 19:49:22 +0000226 goodbye.I32_thing = 4;
227 goodbye.I64_thing = (long)4;
228
229 Insanity crazy = new Insanity();
230 crazy.UserMap = new Dictionary<Numberz, long>();
231 crazy.UserMap[Numberz.EIGHT] = (long)8;
232 crazy.Xtructs = new List<Xtruct>();
233 crazy.Xtructs.Add(goodbye);
234
235 Insanity looney = new Insanity();
236 crazy.UserMap[Numberz.FIVE] = (long)5;
237 crazy.Xtructs.Add(hello);
238
239 Dictionary<Numberz, Insanity> first_map = new Dictionary<Numberz, Insanity>();
240 Dictionary<Numberz, Insanity> second_map = new Dictionary<Numberz, Insanity>(); ;
241
242 first_map[Numberz.TWO] = crazy;
243 first_map[Numberz.THREE] = crazy;
244
245 second_map[Numberz.SIX] = looney;
246
247 Dictionary<long, Dictionary<Numberz, Insanity>> insane =
248 new Dictionary<long, Dictionary<Numberz, Insanity>>();
249 insane[(long)1] = first_map;
250 insane[(long)2] = second_map;
251
252 return insane;
253 }
254
Jens Geyer1b4c9b92013-04-26 23:38:58 +0200255 public Xtruct testMulti(sbyte arg0, int arg1, long arg2, Dictionary<short, string> arg3, Numberz arg4, long arg5)
David Reiss63191332009-01-06 19:49:22 +0000256 {
257 Console.WriteLine("testMulti()");
258
259 Xtruct hello = new Xtruct(); ;
260 hello.String_thing = "Hello2";
261 hello.Byte_thing = arg0;
262 hello.I32_thing = arg1;
263 hello.I64_thing = arg2;
264 return hello;
265 }
266
267 public void testException(string arg)
268 {
269 Console.WriteLine("testException(" + arg + ")");
270 if (arg == "Xception")
271 {
272 Xception x = new Xception();
273 x.ErrorCode = 1001;
274 x.Message = "This is an Xception";
275 throw x;
276 }
277 return;
278 }
279
280 public Xtruct testMultiException(string arg0, string arg1)
281 {
282 Console.WriteLine("testMultiException(" + arg0 + ", " + arg1 + ")");
283 if (arg0 == "Xception")
284 {
285 Xception x = new Xception();
286 x.ErrorCode = 1001;
287 x.Message = "This is an Xception";
288 throw x;
289 }
290 else if (arg0 == "Xception2")
291 {
292 Xception2 x = new Xception2();
293 x.ErrorCode = 2002;
294 x.Struct_thing = new Xtruct();
295 x.Struct_thing.String_thing = "This is an Xception2";
296 throw x;
297 }
298
299 Xtruct result = new Xtruct();
300 result.String_thing = arg1;
301 return result;
302 }
303
304 public void testStop()
305 {
306 if (server != null)
307 {
308 server.Stop();
309 }
310 }
311
David Reiss6ce401d2009-03-24 20:01:58 +0000312 public void testOneway(int arg)
David Reiss63191332009-01-06 19:49:22 +0000313 {
David Reiss6ce401d2009-03-24 20:01:58 +0000314 Console.WriteLine("testOneway(" + arg + "), sleeping...");
David Reiss63191332009-01-06 19:49:22 +0000315 System.Threading.Thread.Sleep(arg * 1000);
David Reiss6ce401d2009-03-24 20:01:58 +0000316 Console.WriteLine("testOneway finished");
David Reiss63191332009-01-06 19:49:22 +0000317 }
318
319 } // class TestHandler
320
321 public static void Execute(string[] args)
322 {
323 try
324 {
Jens Geyerc1d79432014-04-22 22:52:43 +0200325 bool useBufferedSockets = false, useFramed = false, useEncryption = false, compact = false, json = false;
Jens Geyerfd62df72014-03-20 00:52:18 +0200326 int port = 9090, i = 0;
327 string pipe = null;
David Reiss63191332009-01-06 19:49:22 +0000328 if (args.Length > 0)
329 {
Jens Geyerfd62df72014-03-20 00:52:18 +0200330 i = 0;
331 if (args[i] == "-pipe") // -pipe name
David Reiss63191332009-01-06 19:49:22 +0000332 {
Jens Geyerfd62df72014-03-20 00:52:18 +0200333 pipe = args[++i];
334 }
335 else // default to port number (compatibility)
336 {
337 port = int.Parse(args[i]);
338 }
339
340 ++i;
341 if (args.Length > i)
342 {
343 if ( args[i] == "raw" )
T Jake Luciani7070aaa2011-01-27 02:51:51 +0000344 {
345 // as default
346 }
Jens Geyerc1d79432014-04-22 22:52:43 +0200347 else if (args[i] == "buffered")
T Jake Luciani7070aaa2011-01-27 02:51:51 +0000348 {
349 useBufferedSockets = true;
350 }
Jens Geyerfd62df72014-03-20 00:52:18 +0200351 else if (args[i] == "framed")
T Jake Luciani7070aaa2011-01-27 02:51:51 +0000352 {
353 useFramed = true;
354 }
Jens Geyerc1d79432014-04-22 22:52:43 +0200355 else if (args[i] == "ssl")
356 {
357 useEncryption = true;
358 }
359 else if (args[i] == "compact" )
360 {
361 compact = true;
362 }
363 else if (args[i] == "json" )
364 {
365 json = true;
366 }
T Jake Luciani7070aaa2011-01-27 02:51:51 +0000367 else
368 {
369 // Fall back to the older boolean syntax
Jens Geyerfd62df72014-03-20 00:52:18 +0200370 bool.TryParse(args[i], out useBufferedSockets);
T Jake Luciani7070aaa2011-01-27 02:51:51 +0000371 }
David Reiss63191332009-01-06 19:49:22 +0000372 }
373 }
374
375 // Processor
376 TestHandler testHandler = new TestHandler();
377 ThriftTest.Processor testProcessor = new ThriftTest.Processor(testHandler);
378
379 // Transport
Jens Geyerfd62df72014-03-20 00:52:18 +0200380 TServerTransport trans;
381 if( pipe != null)
382 {
383 trans = new TNamedPipeServerTransport(pipe);
384 }
385 else
386 {
Jens Geyerc1d79432014-04-22 22:52:43 +0200387 if (useEncryption)
388 {
389 trans = new TTLSServerSocket(port, 0, useBufferedSockets, new X509Certificate2("../../../../../keys/server.pem"));
390 }
391 else
392 {
393 trans = new TServerSocket(port, 0, useBufferedSockets);
394 }
Jens Geyerfd62df72014-03-20 00:52:18 +0200395 }
David Reiss63191332009-01-06 19:49:22 +0000396
Jens Geyerc1d79432014-04-22 22:52:43 +0200397 TProtocolFactory proto;
398 if ( compact )
399 proto = new TCompactProtocol.Factory();
400 else if ( json )
401 proto = new TJSONProtocol.Factory();
402 else
403 proto = new TBinaryProtocol.Factory();
404
David Reiss63191332009-01-06 19:49:22 +0000405 // Simple Server
T Jake Luciani7070aaa2011-01-27 02:51:51 +0000406 TServer serverEngine;
407 if ( useFramed )
Jens Geyerc1d79432014-04-22 22:52:43 +0200408 serverEngine = new TSimpleServer(testProcessor, trans, new TFramedTransport.Factory(), proto);
T Jake Luciani7070aaa2011-01-27 02:51:51 +0000409 else
Jens Geyerc1d79432014-04-22 22:52:43 +0200410 serverEngine = new TSimpleServer(testProcessor, trans, new TTransportFactory(), proto);
David Reiss63191332009-01-06 19:49:22 +0000411
412 // ThreadPool Server
David Reissd831a212009-02-13 03:09:52 +0000413 // serverEngine = new TThreadPoolServer(testProcessor, tServerSocket);
414
415 // Threaded Server
416 // serverEngine = new TThreadedServer(testProcessor, tServerSocket);
David Reiss63191332009-01-06 19:49:22 +0000417
418 testHandler.server = serverEngine;
419
420 // Run it
Jens Geyerfd62df72014-03-20 00:52:18 +0200421 string where = ( pipe != null ? "on pipe "+pipe : "on port " + port);
Jens Geyerc1d79432014-04-22 22:52:43 +0200422 Console.WriteLine("Starting the server " + where +
T Jake Luciani7070aaa2011-01-27 02:51:51 +0000423 (useBufferedSockets ? " with buffered socket" : "") +
424 (useFramed ? " with framed transport" : "") +
Jens Geyerc1d79432014-04-22 22:52:43 +0200425 (useEncryption ? " with encryption" : "") +
426 (compact ? " with compact protocol" : "") +
T Jake Luciani7070aaa2011-01-27 02:51:51 +0000427 "...");
David Reiss63191332009-01-06 19:49:22 +0000428 serverEngine.Serve();
429
430 }
431 catch (Exception x)
432 {
433 Console.Error.Write(x);
434 }
435 Console.WriteLine("done.");
436 }
437 }
438}