blob: 0e9fe05d8e0221aab10a53a2c31c984274854e51 [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{
Jens Geyerd5436f52014-10-03 19:50:38 +020035 public class TestServer
36 {
Randy Abernethy0e86f1f2014-07-13 09:50:19 -070037 public class TradeServerEventHandler : TServerEventHandler
38 {
39 public int callCount = 0;
40 public void preServe()
41 {
42 callCount++;
43 }
44 public Object createContext(Thrift.Protocol.TProtocol input, Thrift.Protocol.TProtocol output)
45 {
46 callCount++;
47 return null;
48 }
49 public void deleteContext(Object serverContext, Thrift.Protocol.TProtocol input, Thrift.Protocol.TProtocol output)
50 {
51 callCount++;
52 }
53 public void processContext(Object serverContext, Thrift.Transport.TTransport transport)
54 {
55 callCount++;
56 }
57 };
58
Jens Geyerd5436f52014-10-03 19:50:38 +020059 public class TestHandler : ThriftTest.Iface
60 {
61 public TServer server;
David Reiss63191332009-01-06 19:49:22 +000062
Jens Geyerd5436f52014-10-03 19:50:38 +020063 public TestHandler() { }
David Reiss63191332009-01-06 19:49:22 +000064
Jens Geyerd5436f52014-10-03 19:50:38 +020065 public void testVoid()
66 {
67 Console.WriteLine("testVoid()");
68 }
David Reiss63191332009-01-06 19:49:22 +000069
Jens Geyerd5436f52014-10-03 19:50:38 +020070 public string testString(string thing)
71 {
72 Console.WriteLine("teststring(\"" + thing + "\")");
73 return thing;
74 }
David Reiss63191332009-01-06 19:49:22 +000075
Jens Geyerd5436f52014-10-03 19:50:38 +020076 public sbyte testByte(sbyte thing)
77 {
78 Console.WriteLine("testByte(" + thing + ")");
79 return thing;
80 }
David Reiss63191332009-01-06 19:49:22 +000081
Jens Geyerd5436f52014-10-03 19:50:38 +020082 public int testI32(int thing)
83 {
84 Console.WriteLine("testI32(" + thing + ")");
85 return thing;
86 }
David Reiss63191332009-01-06 19:49:22 +000087
Jens Geyerd5436f52014-10-03 19:50:38 +020088 public long testI64(long thing)
89 {
90 Console.WriteLine("testI64(" + thing + ")");
91 return thing;
92 }
David Reiss63191332009-01-06 19:49:22 +000093
Jens Geyerd5436f52014-10-03 19:50:38 +020094 public double testDouble(double thing)
95 {
96 Console.WriteLine("testDouble(" + thing + ")");
97 return thing;
98 }
David Reiss63191332009-01-06 19:49:22 +000099
Jens Geyer71e814a2014-12-13 23:40:35 +0100100 public byte[] testBinary(byte[] thing)
101 {
102 string hex = BitConverter.ToString(thing).Replace("-", string.Empty);
103 Console.WriteLine("testBinary(" + hex + ")");
104 return thing;
105 }
106
Jens Geyerd5436f52014-10-03 19:50:38 +0200107 public Xtruct testStruct(Xtruct thing)
108 {
109 Console.WriteLine("testStruct({" +
110 "\"" + thing.String_thing + "\", " +
111 thing.Byte_thing + ", " +
112 thing.I32_thing + ", " +
113 thing.I64_thing + "})");
114 return thing;
115 }
David Reiss63191332009-01-06 19:49:22 +0000116
Jens Geyerd5436f52014-10-03 19:50:38 +0200117 public Xtruct2 testNest(Xtruct2 nest)
118 {
119 Xtruct thing = nest.Struct_thing;
120 Console.WriteLine("testNest({" +
121 nest.Byte_thing + ", {" +
122 "\"" + thing.String_thing + "\", " +
123 thing.Byte_thing + ", " +
124 thing.I32_thing + ", " +
125 thing.I64_thing + "}, " +
126 nest.I32_thing + "})");
127 return nest;
128 }
David Reiss63191332009-01-06 19:49:22 +0000129
Jens Geyerd5436f52014-10-03 19:50:38 +0200130 public Dictionary<int, int> testMap(Dictionary<int, int> thing)
131 {
132 Console.WriteLine("testMap({");
133 bool first = true;
134 foreach (int key in thing.Keys)
135 {
136 if (first)
137 {
138 first = false;
139 }
140 else
141 {
142 Console.WriteLine(", ");
143 }
144 Console.WriteLine(key + " => " + thing[key]);
145 }
146 Console.WriteLine("})");
147 return thing;
148 }
Jens Geyer1c99e702014-03-17 22:50:39 +0200149
Jens Geyerd5436f52014-10-03 19:50:38 +0200150 public Dictionary<string, string> testStringMap(Dictionary<string, string> thing)
151 {
152 Console.WriteLine("testStringMap({");
153 bool first = true;
154 foreach (string key in thing.Keys)
155 {
156 if (first)
157 {
158 first = false;
159 }
160 else
161 {
162 Console.WriteLine(", ");
163 }
164 Console.WriteLine(key + " => " + thing[key]);
165 }
166 Console.WriteLine("})");
167 return thing;
168 }
David Reiss63191332009-01-06 19:49:22 +0000169
Jens Geyerd5436f52014-10-03 19:50:38 +0200170 public THashSet<int> testSet(THashSet<int> thing)
171 {
172 Console.WriteLine("testSet({");
173 bool first = true;
174 foreach (int elem in thing)
175 {
176 if (first)
177 {
178 first = false;
179 }
180 else
181 {
182 Console.WriteLine(", ");
183 }
184 Console.WriteLine(elem);
185 }
186 Console.WriteLine("})");
187 return thing;
188 }
David Reiss63191332009-01-06 19:49:22 +0000189
Jens Geyerd5436f52014-10-03 19:50:38 +0200190 public List<int> testList(List<int> thing)
191 {
192 Console.WriteLine("testList({");
193 bool first = true;
194 foreach (int elem in thing)
195 {
196 if (first)
197 {
198 first = false;
199 }
200 else
201 {
202 Console.WriteLine(", ");
203 }
204 Console.WriteLine(elem);
205 }
206 Console.WriteLine("})");
207 return thing;
208 }
David Reiss63191332009-01-06 19:49:22 +0000209
Jens Geyerd5436f52014-10-03 19:50:38 +0200210 public Numberz testEnum(Numberz thing)
211 {
212 Console.WriteLine("testEnum(" + thing + ")");
213 return thing;
214 }
David Reiss63191332009-01-06 19:49:22 +0000215
Jens Geyerd5436f52014-10-03 19:50:38 +0200216 public long testTypedef(long thing)
217 {
218 Console.WriteLine("testTypedef(" + thing + ")");
219 return thing;
220 }
David Reiss63191332009-01-06 19:49:22 +0000221
Jens Geyerd5436f52014-10-03 19:50:38 +0200222 public Dictionary<int, Dictionary<int, int>> testMapMap(int hello)
223 {
224 Console.WriteLine("testMapMap(" + hello + ")");
225 Dictionary<int, Dictionary<int, int>> mapmap =
226 new Dictionary<int, Dictionary<int, int>>();
David Reiss63191332009-01-06 19:49:22 +0000227
Jens Geyerd5436f52014-10-03 19:50:38 +0200228 Dictionary<int, int> pos = new Dictionary<int, int>();
229 Dictionary<int, int> neg = new Dictionary<int, int>();
230 for (int i = 1; i < 5; i++)
231 {
232 pos[i] = i;
233 neg[-i] = -i;
234 }
David Reiss63191332009-01-06 19:49:22 +0000235
Jens Geyerd5436f52014-10-03 19:50:38 +0200236 mapmap[4] = pos;
237 mapmap[-4] = neg;
David Reiss63191332009-01-06 19:49:22 +0000238
Jens Geyerd5436f52014-10-03 19:50:38 +0200239 return mapmap;
240 }
David Reiss63191332009-01-06 19:49:22 +0000241
Jens Geyerd5436f52014-10-03 19:50:38 +0200242 public Dictionary<long, Dictionary<Numberz, Insanity>> testInsanity(Insanity argument)
243 {
244 Console.WriteLine("testInsanity()");
David Reiss63191332009-01-06 19:49:22 +0000245
Jens Geyerd5436f52014-10-03 19:50:38 +0200246 Xtruct hello = new Xtruct();
247 hello.String_thing = "Hello2";
248 hello.Byte_thing = 2;
249 hello.I32_thing = 2;
250 hello.I64_thing = 2;
David Reiss63191332009-01-06 19:49:22 +0000251
Jens Geyerd5436f52014-10-03 19:50:38 +0200252 Xtruct goodbye = new Xtruct();
253 goodbye.String_thing = "Goodbye4";
254 goodbye.Byte_thing = (sbyte)4;
255 goodbye.I32_thing = 4;
256 goodbye.I64_thing = (long)4;
David Reiss63191332009-01-06 19:49:22 +0000257
Jens Geyerd5436f52014-10-03 19:50:38 +0200258 Insanity crazy = new Insanity();
259 crazy.UserMap = new Dictionary<Numberz, long>();
260 crazy.UserMap[Numberz.EIGHT] = (long)8;
261 crazy.Xtructs = new List<Xtruct>();
262 crazy.Xtructs.Add(goodbye);
David Reiss63191332009-01-06 19:49:22 +0000263
Jens Geyerd5436f52014-10-03 19:50:38 +0200264 Insanity looney = new Insanity();
265 crazy.UserMap[Numberz.FIVE] = (long)5;
266 crazy.Xtructs.Add(hello);
David Reiss63191332009-01-06 19:49:22 +0000267
Jens Geyerd5436f52014-10-03 19:50:38 +0200268 Dictionary<Numberz, Insanity> first_map = new Dictionary<Numberz, Insanity>();
269 Dictionary<Numberz, Insanity> second_map = new Dictionary<Numberz, Insanity>(); ;
David Reiss63191332009-01-06 19:49:22 +0000270
Jens Geyerd5436f52014-10-03 19:50:38 +0200271 first_map[Numberz.TWO] = crazy;
272 first_map[Numberz.THREE] = crazy;
David Reiss63191332009-01-06 19:49:22 +0000273
Jens Geyerd5436f52014-10-03 19:50:38 +0200274 second_map[Numberz.SIX] = looney;
David Reiss63191332009-01-06 19:49:22 +0000275
Jens Geyerd5436f52014-10-03 19:50:38 +0200276 Dictionary<long, Dictionary<Numberz, Insanity>> insane =
277 new Dictionary<long, Dictionary<Numberz, Insanity>>();
278 insane[(long)1] = first_map;
279 insane[(long)2] = second_map;
David Reiss63191332009-01-06 19:49:22 +0000280
Jens Geyerd5436f52014-10-03 19:50:38 +0200281 return insane;
282 }
David Reiss63191332009-01-06 19:49:22 +0000283
Jens Geyerd5436f52014-10-03 19:50:38 +0200284 public Xtruct testMulti(sbyte arg0, int arg1, long arg2, Dictionary<short, string> arg3, Numberz arg4, long arg5)
285 {
286 Console.WriteLine("testMulti()");
David Reiss63191332009-01-06 19:49:22 +0000287
Jens Geyerd5436f52014-10-03 19:50:38 +0200288 Xtruct hello = new Xtruct(); ;
289 hello.String_thing = "Hello2";
290 hello.Byte_thing = arg0;
291 hello.I32_thing = arg1;
292 hello.I64_thing = arg2;
293 return hello;
294 }
David Reiss63191332009-01-06 19:49:22 +0000295
Jens Geyer8b14d172015-02-26 19:36:28 +0100296 /**
297 * Print 'testException(%s)' with arg as '%s'
298 * @param string arg - a string indication what type of exception to throw
299 * if arg == "Xception" throw Xception with errorCode = 1001 and message = arg
300 * elsen if arg == "TException" throw TException
301 * else do not throw anything
302 */
Jens Geyerd5436f52014-10-03 19:50:38 +0200303 public void testException(string arg)
304 {
305 Console.WriteLine("testException(" + arg + ")");
306 if (arg == "Xception")
307 {
308 Xception x = new Xception();
309 x.ErrorCode = 1001;
Jens Geyer8b14d172015-02-26 19:36:28 +0100310 x.Message = arg;
Jens Geyerd5436f52014-10-03 19:50:38 +0200311 throw x;
312 }
Jens Geyer8b14d172015-02-26 19:36:28 +0100313 if (arg == "TException")
314 {
315 throw new Thrift.TException();
316 }
Jens Geyerd5436f52014-10-03 19:50:38 +0200317 return;
318 }
David Reiss63191332009-01-06 19:49:22 +0000319
Jens Geyerd5436f52014-10-03 19:50:38 +0200320 public Xtruct testMultiException(string arg0, string arg1)
321 {
322 Console.WriteLine("testMultiException(" + arg0 + ", " + arg1 + ")");
323 if (arg0 == "Xception")
324 {
325 Xception x = new Xception();
326 x.ErrorCode = 1001;
327 x.Message = "This is an Xception";
328 throw x;
329 }
330 else if (arg0 == "Xception2")
331 {
332 Xception2 x = new Xception2();
333 x.ErrorCode = 2002;
334 x.Struct_thing = new Xtruct();
335 x.Struct_thing.String_thing = "This is an Xception2";
336 throw x;
337 }
David Reiss63191332009-01-06 19:49:22 +0000338
Jens Geyerd5436f52014-10-03 19:50:38 +0200339 Xtruct result = new Xtruct();
340 result.String_thing = arg1;
341 return result;
342 }
David Reiss63191332009-01-06 19:49:22 +0000343
Jens Geyerd5436f52014-10-03 19:50:38 +0200344 public void testStop()
345 {
346 if (server != null)
347 {
348 server.Stop();
349 }
350 }
David Reiss63191332009-01-06 19:49:22 +0000351
Jens Geyerd5436f52014-10-03 19:50:38 +0200352 public void testOneway(int arg)
353 {
354 Console.WriteLine("testOneway(" + arg + "), sleeping...");
355 System.Threading.Thread.Sleep(arg * 1000);
356 Console.WriteLine("testOneway finished");
357 }
David Reiss63191332009-01-06 19:49:22 +0000358
Jens Geyerd5436f52014-10-03 19:50:38 +0200359 } // class TestHandler
David Reiss63191332009-01-06 19:49:22 +0000360
Roger Meier41ad4342015-03-24 22:30:40 +0100361 public static bool Execute(string[] args)
Jens Geyerd5436f52014-10-03 19:50:38 +0200362 {
363 try
364 {
365 bool useBufferedSockets = false, useFramed = false, useEncryption = false, compact = false, json = false;
366 int port = 9090;
367 string pipe = null;
Roger Meier41ad4342015-03-24 22:30:40 +0100368 string certPath = "../../../../../keys/server.pem";
Jens Geyerd5436f52014-10-03 19:50:38 +0200369 for (int i = 0; i < args.Length; i++)
370 {
371 if (args[i] == "-pipe") // -pipe name
372 {
373 pipe = args[++i];
374 }
375 else if (args[i].Contains("--port="))
376 {
377 port = int.Parse(args[i].Substring(args[i].IndexOf("=")+1));
378 }
379 else if (args[i] == "-b" || args[i] == "--buffered" || args[i] == "--transport=buffered")
380 {
381 useBufferedSockets = true;
382 }
383 else if (args[i] == "-f" || args[i] == "--framed" || args[i] == "--transport=framed")
384 {
385 useFramed = true;
386 }
387 else if (args[i] == "--compact" || args[i] == "--protocol=compact")
388 {
389 compact = true;
390 }
391 else if (args[i] == "--json" || args[i] == "--protocol=json")
392 {
393 json = true;
394 }
395 else if (args[i] == "--ssl")
396 {
397 useEncryption = true;
398 }
Roger Meier41ad4342015-03-24 22:30:40 +0100399 else if (args[i].StartsWith("--cert="))
400 {
401 certPath = args[i].Substring("--cert=".Length);
402 }
Jens Geyerd5436f52014-10-03 19:50:38 +0200403 }
David Reiss63191332009-01-06 19:49:22 +0000404
Jens Geyerd5436f52014-10-03 19:50:38 +0200405 // Processor
406 TestHandler testHandler = new TestHandler();
407 ThriftTest.Processor testProcessor = new ThriftTest.Processor(testHandler);
David Reiss63191332009-01-06 19:49:22 +0000408
Jens Geyerd5436f52014-10-03 19:50:38 +0200409 // Transport
410 TServerTransport trans;
411 if( pipe != null)
412 {
413 trans = new TNamedPipeServerTransport(pipe);
414 }
415 else
416 {
417 if (useEncryption)
418 {
Roger Meier41ad4342015-03-24 22:30:40 +0100419 trans = new TTLSServerSocket(port, 0, useBufferedSockets, new X509Certificate2(certPath));
Jens Geyerd5436f52014-10-03 19:50:38 +0200420 }
421 else
422 {
423 trans = new TServerSocket(port, 0, useBufferedSockets);
424 }
425 }
David Reiss63191332009-01-06 19:49:22 +0000426
Jens Geyerd5436f52014-10-03 19:50:38 +0200427 TProtocolFactory proto;
428 if ( compact )
429 proto = new TCompactProtocol.Factory();
430 else if ( json )
431 proto = new TJSONProtocol.Factory();
432 else
433 proto = new TBinaryProtocol.Factory();
David Reiss63191332009-01-06 19:49:22 +0000434
Jens Geyerd5436f52014-10-03 19:50:38 +0200435 // Simple Server
436 TServer serverEngine;
437 if ( useFramed )
438 serverEngine = new TSimpleServer(testProcessor, trans, new TFramedTransport.Factory(), proto);
439 else
440 serverEngine = new TSimpleServer(testProcessor, trans, new TTransportFactory(), proto);
David Reissd831a212009-02-13 03:09:52 +0000441
Jens Geyerd5436f52014-10-03 19:50:38 +0200442 // ThreadPool Server
443 // serverEngine = new TThreadPoolServer(testProcessor, tServerSocket);
444
445 // Threaded Server
446 // serverEngine = new TThreadedServer(testProcessor, tServerSocket);
David Reiss63191332009-01-06 19:49:22 +0000447
Randy Abernethy0e86f1f2014-07-13 09:50:19 -0700448 //Server event handler
449 TradeServerEventHandler serverEvents = new TradeServerEventHandler();
450 serverEngine.setEventHandler(serverEvents);
451
Jens Geyerd5436f52014-10-03 19:50:38 +0200452 testHandler.server = serverEngine;
David Reiss63191332009-01-06 19:49:22 +0000453
Jens Geyerd5436f52014-10-03 19:50:38 +0200454 // Run it
455 string where = ( pipe != null ? "on pipe "+pipe : "on port " + port);
456 Console.WriteLine("Starting the server " + where +
457 (useBufferedSockets ? " with buffered socket" : "") +
458 (useFramed ? " with framed transport" : "") +
459 (useEncryption ? " with encryption" : "") +
460 (compact ? " with compact protocol" : "") +
461 (json ? " with json protocol" : "") +
462 "...");
463 serverEngine.Serve();
David Reiss63191332009-01-06 19:49:22 +0000464
Jens Geyerd5436f52014-10-03 19:50:38 +0200465 }
466 catch (Exception x)
467 {
468 Console.Error.Write(x);
Roger Meier41ad4342015-03-24 22:30:40 +0100469 return false;
Jens Geyerd5436f52014-10-03 19:50:38 +0200470 }
471 Console.WriteLine("done.");
Roger Meier41ad4342015-03-24 22:30:40 +0100472 return true;
Jens Geyerd5436f52014-10-03 19:50:38 +0200473 }
474 }
David Reiss63191332009-01-06 19:49:22 +0000475}