blob: b5cc73c792ad8d0ba3a651f63ef88af67d7fe056 [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
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +090076 public bool testBool(bool thing)
77 {
78 Console.WriteLine("testBool(" + thing + ")");
79 return thing;
80 }
81
Jens Geyerd5436f52014-10-03 19:50:38 +020082 public sbyte testByte(sbyte thing)
83 {
84 Console.WriteLine("testByte(" + thing + ")");
85 return thing;
86 }
David Reiss63191332009-01-06 19:49:22 +000087
Jens Geyerd5436f52014-10-03 19:50:38 +020088 public int testI32(int thing)
89 {
90 Console.WriteLine("testI32(" + thing + ")");
91 return thing;
92 }
David Reiss63191332009-01-06 19:49:22 +000093
Jens Geyerd5436f52014-10-03 19:50:38 +020094 public long testI64(long thing)
95 {
96 Console.WriteLine("testI64(" + thing + ")");
97 return thing;
98 }
David Reiss63191332009-01-06 19:49:22 +000099
Jens Geyerd5436f52014-10-03 19:50:38 +0200100 public double testDouble(double thing)
101 {
102 Console.WriteLine("testDouble(" + thing + ")");
103 return thing;
104 }
David Reiss63191332009-01-06 19:49:22 +0000105
Jens Geyer71e814a2014-12-13 23:40:35 +0100106 public byte[] testBinary(byte[] thing)
107 {
108 string hex = BitConverter.ToString(thing).Replace("-", string.Empty);
109 Console.WriteLine("testBinary(" + hex + ")");
110 return thing;
111 }
112
Jens Geyerd5436f52014-10-03 19:50:38 +0200113 public Xtruct testStruct(Xtruct thing)
114 {
115 Console.WriteLine("testStruct({" +
116 "\"" + thing.String_thing + "\", " +
117 thing.Byte_thing + ", " +
118 thing.I32_thing + ", " +
119 thing.I64_thing + "})");
120 return thing;
121 }
David Reiss63191332009-01-06 19:49:22 +0000122
Jens Geyerd5436f52014-10-03 19:50:38 +0200123 public Xtruct2 testNest(Xtruct2 nest)
124 {
125 Xtruct thing = nest.Struct_thing;
126 Console.WriteLine("testNest({" +
127 nest.Byte_thing + ", {" +
128 "\"" + thing.String_thing + "\", " +
129 thing.Byte_thing + ", " +
130 thing.I32_thing + ", " +
131 thing.I64_thing + "}, " +
132 nest.I32_thing + "})");
133 return nest;
134 }
David Reiss63191332009-01-06 19:49:22 +0000135
Jens Geyerd5436f52014-10-03 19:50:38 +0200136 public Dictionary<int, int> testMap(Dictionary<int, int> thing)
137 {
138 Console.WriteLine("testMap({");
139 bool first = true;
140 foreach (int key in thing.Keys)
141 {
142 if (first)
143 {
144 first = false;
145 }
146 else
147 {
148 Console.WriteLine(", ");
149 }
150 Console.WriteLine(key + " => " + thing[key]);
151 }
152 Console.WriteLine("})");
153 return thing;
154 }
Jens Geyer1c99e702014-03-17 22:50:39 +0200155
Jens Geyerd5436f52014-10-03 19:50:38 +0200156 public Dictionary<string, string> testStringMap(Dictionary<string, string> thing)
157 {
158 Console.WriteLine("testStringMap({");
159 bool first = true;
160 foreach (string key in thing.Keys)
161 {
162 if (first)
163 {
164 first = false;
165 }
166 else
167 {
168 Console.WriteLine(", ");
169 }
170 Console.WriteLine(key + " => " + thing[key]);
171 }
172 Console.WriteLine("})");
173 return thing;
174 }
David Reiss63191332009-01-06 19:49:22 +0000175
Jens Geyerd5436f52014-10-03 19:50:38 +0200176 public THashSet<int> testSet(THashSet<int> thing)
177 {
178 Console.WriteLine("testSet({");
179 bool first = true;
180 foreach (int elem in thing)
181 {
182 if (first)
183 {
184 first = false;
185 }
186 else
187 {
188 Console.WriteLine(", ");
189 }
190 Console.WriteLine(elem);
191 }
192 Console.WriteLine("})");
193 return thing;
194 }
David Reiss63191332009-01-06 19:49:22 +0000195
Jens Geyerd5436f52014-10-03 19:50:38 +0200196 public List<int> testList(List<int> thing)
197 {
198 Console.WriteLine("testList({");
199 bool first = true;
200 foreach (int elem in thing)
201 {
202 if (first)
203 {
204 first = false;
205 }
206 else
207 {
208 Console.WriteLine(", ");
209 }
210 Console.WriteLine(elem);
211 }
212 Console.WriteLine("})");
213 return thing;
214 }
David Reiss63191332009-01-06 19:49:22 +0000215
Jens Geyerd5436f52014-10-03 19:50:38 +0200216 public Numberz testEnum(Numberz thing)
217 {
218 Console.WriteLine("testEnum(" + thing + ")");
219 return thing;
220 }
David Reiss63191332009-01-06 19:49:22 +0000221
Jens Geyerd5436f52014-10-03 19:50:38 +0200222 public long testTypedef(long thing)
223 {
224 Console.WriteLine("testTypedef(" + thing + ")");
225 return thing;
226 }
David Reiss63191332009-01-06 19:49:22 +0000227
Jens Geyerd5436f52014-10-03 19:50:38 +0200228 public Dictionary<int, Dictionary<int, int>> testMapMap(int hello)
229 {
230 Console.WriteLine("testMapMap(" + hello + ")");
231 Dictionary<int, Dictionary<int, int>> mapmap =
232 new Dictionary<int, Dictionary<int, int>>();
David Reiss63191332009-01-06 19:49:22 +0000233
Jens Geyerd5436f52014-10-03 19:50:38 +0200234 Dictionary<int, int> pos = new Dictionary<int, int>();
235 Dictionary<int, int> neg = new Dictionary<int, int>();
236 for (int i = 1; i < 5; i++)
237 {
238 pos[i] = i;
239 neg[-i] = -i;
240 }
David Reiss63191332009-01-06 19:49:22 +0000241
Jens Geyerd5436f52014-10-03 19:50:38 +0200242 mapmap[4] = pos;
243 mapmap[-4] = neg;
David Reiss63191332009-01-06 19:49:22 +0000244
Jens Geyerd5436f52014-10-03 19:50:38 +0200245 return mapmap;
246 }
David Reiss63191332009-01-06 19:49:22 +0000247
Jens Geyerd5436f52014-10-03 19:50:38 +0200248 public Dictionary<long, Dictionary<Numberz, Insanity>> testInsanity(Insanity argument)
249 {
250 Console.WriteLine("testInsanity()");
David Reiss63191332009-01-06 19:49:22 +0000251
Jens Geyerd5436f52014-10-03 19:50:38 +0200252 Xtruct hello = new Xtruct();
253 hello.String_thing = "Hello2";
254 hello.Byte_thing = 2;
255 hello.I32_thing = 2;
256 hello.I64_thing = 2;
David Reiss63191332009-01-06 19:49:22 +0000257
Jens Geyerd5436f52014-10-03 19:50:38 +0200258 Xtruct goodbye = new Xtruct();
259 goodbye.String_thing = "Goodbye4";
260 goodbye.Byte_thing = (sbyte)4;
261 goodbye.I32_thing = 4;
262 goodbye.I64_thing = (long)4;
David Reiss63191332009-01-06 19:49:22 +0000263
Jens Geyerd5436f52014-10-03 19:50:38 +0200264 Insanity crazy = new Insanity();
265 crazy.UserMap = new Dictionary<Numberz, long>();
266 crazy.UserMap[Numberz.EIGHT] = (long)8;
267 crazy.Xtructs = new List<Xtruct>();
268 crazy.Xtructs.Add(goodbye);
David Reiss63191332009-01-06 19:49:22 +0000269
Jens Geyerd5436f52014-10-03 19:50:38 +0200270 Insanity looney = new Insanity();
271 crazy.UserMap[Numberz.FIVE] = (long)5;
272 crazy.Xtructs.Add(hello);
David Reiss63191332009-01-06 19:49:22 +0000273
Jens Geyerd5436f52014-10-03 19:50:38 +0200274 Dictionary<Numberz, Insanity> first_map = new Dictionary<Numberz, Insanity>();
275 Dictionary<Numberz, Insanity> second_map = new Dictionary<Numberz, Insanity>(); ;
David Reiss63191332009-01-06 19:49:22 +0000276
Jens Geyerd5436f52014-10-03 19:50:38 +0200277 first_map[Numberz.TWO] = crazy;
278 first_map[Numberz.THREE] = crazy;
David Reiss63191332009-01-06 19:49:22 +0000279
Jens Geyerd5436f52014-10-03 19:50:38 +0200280 second_map[Numberz.SIX] = looney;
David Reiss63191332009-01-06 19:49:22 +0000281
Jens Geyerd5436f52014-10-03 19:50:38 +0200282 Dictionary<long, Dictionary<Numberz, Insanity>> insane =
283 new Dictionary<long, Dictionary<Numberz, Insanity>>();
284 insane[(long)1] = first_map;
285 insane[(long)2] = second_map;
David Reiss63191332009-01-06 19:49:22 +0000286
Jens Geyerd5436f52014-10-03 19:50:38 +0200287 return insane;
288 }
David Reiss63191332009-01-06 19:49:22 +0000289
Jens Geyerd5436f52014-10-03 19:50:38 +0200290 public Xtruct testMulti(sbyte arg0, int arg1, long arg2, Dictionary<short, string> arg3, Numberz arg4, long arg5)
291 {
292 Console.WriteLine("testMulti()");
David Reiss63191332009-01-06 19:49:22 +0000293
Jens Geyerd5436f52014-10-03 19:50:38 +0200294 Xtruct hello = new Xtruct(); ;
295 hello.String_thing = "Hello2";
296 hello.Byte_thing = arg0;
297 hello.I32_thing = arg1;
298 hello.I64_thing = arg2;
299 return hello;
300 }
David Reiss63191332009-01-06 19:49:22 +0000301
Jens Geyer8b14d172015-02-26 19:36:28 +0100302 /**
303 * Print 'testException(%s)' with arg as '%s'
304 * @param string arg - a string indication what type of exception to throw
305 * if arg == "Xception" throw Xception with errorCode = 1001 and message = arg
306 * elsen if arg == "TException" throw TException
307 * else do not throw anything
308 */
Jens Geyerd5436f52014-10-03 19:50:38 +0200309 public void testException(string arg)
310 {
311 Console.WriteLine("testException(" + arg + ")");
312 if (arg == "Xception")
313 {
314 Xception x = new Xception();
315 x.ErrorCode = 1001;
Jens Geyer8b14d172015-02-26 19:36:28 +0100316 x.Message = arg;
Jens Geyerd5436f52014-10-03 19:50:38 +0200317 throw x;
318 }
Jens Geyer8b14d172015-02-26 19:36:28 +0100319 if (arg == "TException")
320 {
321 throw new Thrift.TException();
322 }
Jens Geyerd5436f52014-10-03 19:50:38 +0200323 return;
324 }
David Reiss63191332009-01-06 19:49:22 +0000325
Jens Geyerd5436f52014-10-03 19:50:38 +0200326 public Xtruct testMultiException(string arg0, string arg1)
327 {
328 Console.WriteLine("testMultiException(" + arg0 + ", " + arg1 + ")");
329 if (arg0 == "Xception")
330 {
331 Xception x = new Xception();
332 x.ErrorCode = 1001;
333 x.Message = "This is an Xception";
334 throw x;
335 }
336 else if (arg0 == "Xception2")
337 {
338 Xception2 x = new Xception2();
339 x.ErrorCode = 2002;
340 x.Struct_thing = new Xtruct();
341 x.Struct_thing.String_thing = "This is an Xception2";
342 throw x;
343 }
David Reiss63191332009-01-06 19:49:22 +0000344
Jens Geyerd5436f52014-10-03 19:50:38 +0200345 Xtruct result = new Xtruct();
346 result.String_thing = arg1;
347 return result;
348 }
David Reiss63191332009-01-06 19:49:22 +0000349
Jens Geyerd5436f52014-10-03 19:50:38 +0200350 public void testStop()
351 {
352 if (server != null)
353 {
354 server.Stop();
355 }
356 }
David Reiss63191332009-01-06 19:49:22 +0000357
Jens Geyerd5436f52014-10-03 19:50:38 +0200358 public void testOneway(int arg)
359 {
360 Console.WriteLine("testOneway(" + arg + "), sleeping...");
361 System.Threading.Thread.Sleep(arg * 1000);
362 Console.WriteLine("testOneway finished");
363 }
David Reiss63191332009-01-06 19:49:22 +0000364
Jens Geyerd5436f52014-10-03 19:50:38 +0200365 } // class TestHandler
David Reiss63191332009-01-06 19:49:22 +0000366
Roger Meier41ad4342015-03-24 22:30:40 +0100367 public static bool Execute(string[] args)
Jens Geyerd5436f52014-10-03 19:50:38 +0200368 {
369 try
370 {
371 bool useBufferedSockets = false, useFramed = false, useEncryption = false, compact = false, json = false;
372 int port = 9090;
373 string pipe = null;
Roger Meier41ad4342015-03-24 22:30:40 +0100374 string certPath = "../../../../../keys/server.pem";
Jens Geyerd5436f52014-10-03 19:50:38 +0200375 for (int i = 0; i < args.Length; i++)
376 {
377 if (args[i] == "-pipe") // -pipe name
378 {
379 pipe = args[++i];
380 }
381 else if (args[i].Contains("--port="))
382 {
383 port = int.Parse(args[i].Substring(args[i].IndexOf("=")+1));
384 }
385 else if (args[i] == "-b" || args[i] == "--buffered" || args[i] == "--transport=buffered")
386 {
387 useBufferedSockets = true;
388 }
389 else if (args[i] == "-f" || args[i] == "--framed" || args[i] == "--transport=framed")
390 {
391 useFramed = true;
392 }
393 else if (args[i] == "--compact" || args[i] == "--protocol=compact")
394 {
395 compact = true;
396 }
397 else if (args[i] == "--json" || args[i] == "--protocol=json")
398 {
399 json = true;
400 }
401 else if (args[i] == "--ssl")
402 {
403 useEncryption = true;
404 }
Roger Meier41ad4342015-03-24 22:30:40 +0100405 else if (args[i].StartsWith("--cert="))
406 {
407 certPath = args[i].Substring("--cert=".Length);
408 }
Jens Geyerd5436f52014-10-03 19:50:38 +0200409 }
David Reiss63191332009-01-06 19:49:22 +0000410
Jens Geyerd5436f52014-10-03 19:50:38 +0200411 // Processor
412 TestHandler testHandler = new TestHandler();
413 ThriftTest.Processor testProcessor = new ThriftTest.Processor(testHandler);
David Reiss63191332009-01-06 19:49:22 +0000414
Jens Geyerd5436f52014-10-03 19:50:38 +0200415 // Transport
416 TServerTransport trans;
417 if( pipe != null)
418 {
419 trans = new TNamedPipeServerTransport(pipe);
420 }
421 else
422 {
423 if (useEncryption)
424 {
Roger Meier41ad4342015-03-24 22:30:40 +0100425 trans = new TTLSServerSocket(port, 0, useBufferedSockets, new X509Certificate2(certPath));
Jens Geyerd5436f52014-10-03 19:50:38 +0200426 }
427 else
428 {
429 trans = new TServerSocket(port, 0, useBufferedSockets);
430 }
431 }
David Reiss63191332009-01-06 19:49:22 +0000432
Jens Geyerd5436f52014-10-03 19:50:38 +0200433 TProtocolFactory proto;
434 if ( compact )
435 proto = new TCompactProtocol.Factory();
436 else if ( json )
437 proto = new TJSONProtocol.Factory();
438 else
439 proto = new TBinaryProtocol.Factory();
David Reiss63191332009-01-06 19:49:22 +0000440
Jens Geyerd5436f52014-10-03 19:50:38 +0200441 // Simple Server
442 TServer serverEngine;
443 if ( useFramed )
444 serverEngine = new TSimpleServer(testProcessor, trans, new TFramedTransport.Factory(), proto);
445 else
446 serverEngine = new TSimpleServer(testProcessor, trans, new TTransportFactory(), proto);
David Reissd831a212009-02-13 03:09:52 +0000447
Jens Geyerd5436f52014-10-03 19:50:38 +0200448 // ThreadPool Server
449 // serverEngine = new TThreadPoolServer(testProcessor, tServerSocket);
450
451 // Threaded Server
452 // serverEngine = new TThreadedServer(testProcessor, tServerSocket);
David Reiss63191332009-01-06 19:49:22 +0000453
Randy Abernethy0e86f1f2014-07-13 09:50:19 -0700454 //Server event handler
455 TradeServerEventHandler serverEvents = new TradeServerEventHandler();
456 serverEngine.setEventHandler(serverEvents);
457
Jens Geyerd5436f52014-10-03 19:50:38 +0200458 testHandler.server = serverEngine;
David Reiss63191332009-01-06 19:49:22 +0000459
Jens Geyerd5436f52014-10-03 19:50:38 +0200460 // Run it
461 string where = ( pipe != null ? "on pipe "+pipe : "on port " + port);
462 Console.WriteLine("Starting the server " + where +
463 (useBufferedSockets ? " with buffered socket" : "") +
464 (useFramed ? " with framed transport" : "") +
465 (useEncryption ? " with encryption" : "") +
466 (compact ? " with compact protocol" : "") +
467 (json ? " with json protocol" : "") +
468 "...");
469 serverEngine.Serve();
David Reiss63191332009-01-06 19:49:22 +0000470
Jens Geyerd5436f52014-10-03 19:50:38 +0200471 }
472 catch (Exception x)
473 {
474 Console.Error.Write(x);
Roger Meier41ad4342015-03-24 22:30:40 +0100475 return false;
Jens Geyerd5436f52014-10-03 19:50:38 +0200476 }
477 Console.WriteLine("done.");
Roger Meier41ad4342015-03-24 22:30:40 +0100478 return true;
Jens Geyerd5436f52014-10-03 19:50:38 +0200479 }
480 }
David Reiss63191332009-01-06 19:49:22 +0000481}