blob: 7404ca2ffa16daeff072eea9f85525777fed7a69 [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;
Jonathan Heard2bfd7df2015-10-28 17:34:27 +000032using Thrift;
33using System.Threading;
34using System.Text;
Nobuaki Sukegawa474ddbd2016-02-17 23:44:27 +090035using System.Security.Authentication;
David Reiss63191332009-01-06 19:49:22 +000036
37namespace Test
38{
Jens Geyerd5436f52014-10-03 19:50:38 +020039 public class TestServer
40 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +000041 public static int _clientID = -1;
42 public delegate void TestLogDelegate(string msg, params object[] values);
43
Randy Abernethy0e86f1f2014-07-13 09:50:19 -070044 public class TradeServerEventHandler : TServerEventHandler
45 {
46 public int callCount = 0;
47 public void preServe()
48 {
49 callCount++;
50 }
51 public Object createContext(Thrift.Protocol.TProtocol input, Thrift.Protocol.TProtocol output)
52 {
53 callCount++;
54 return null;
55 }
56 public void deleteContext(Object serverContext, Thrift.Protocol.TProtocol input, Thrift.Protocol.TProtocol output)
57 {
58 callCount++;
59 }
60 public void processContext(Object serverContext, Thrift.Transport.TTransport transport)
61 {
62 callCount++;
63 }
64 };
65
Jonathan Heard2bfd7df2015-10-28 17:34:27 +000066 public class TestHandler : ThriftTest.Iface, Thrift.TControllingHandler
Jens Geyerd5436f52014-10-03 19:50:38 +020067 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +000068 public TServer server { get; set; }
69 private int handlerID;
70 private StringBuilder reusableStringBuilder = new StringBuilder();
71 private TestLogDelegate testLogDelegate;
David Reiss63191332009-01-06 19:49:22 +000072
Jonathan Heard2bfd7df2015-10-28 17:34:27 +000073 public TestHandler()
74 {
75 handlerID = Interlocked.Increment(ref _clientID);
76 testLogDelegate += testConsoleLogger;
77 testLogDelegate.Invoke("New TestHandler instance created");
78 }
79
80 public void testConsoleLogger(string msg, params object[] values)
81 {
82 reusableStringBuilder.Clear();
83 reusableStringBuilder.AppendFormat("handler{0:D3}:",handlerID);
84 reusableStringBuilder.AppendFormat(msg, values);
85 reusableStringBuilder.AppendLine();
86 Console.Write( reusableStringBuilder.ToString() );
87 }
David Reiss63191332009-01-06 19:49:22 +000088
Jens Geyerd5436f52014-10-03 19:50:38 +020089 public void testVoid()
90 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +000091 testLogDelegate.Invoke("testVoid()");
Jens Geyerd5436f52014-10-03 19:50:38 +020092 }
David Reiss63191332009-01-06 19:49:22 +000093
Jens Geyerd5436f52014-10-03 19:50:38 +020094 public string testString(string thing)
95 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +000096 testLogDelegate.Invoke("testString({0})", thing);
Jens Geyerd5436f52014-10-03 19:50:38 +020097 return thing;
98 }
David Reiss63191332009-01-06 19:49:22 +000099
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900100 public bool testBool(bool thing)
101 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000102 testLogDelegate.Invoke("testBool({0})", thing);
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900103 return thing;
104 }
105
Jens Geyerd5436f52014-10-03 19:50:38 +0200106 public sbyte testByte(sbyte thing)
107 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000108 testLogDelegate.Invoke("testByte({0})", thing);
Jens Geyerd5436f52014-10-03 19:50:38 +0200109 return thing;
110 }
David Reiss63191332009-01-06 19:49:22 +0000111
Jens Geyerd5436f52014-10-03 19:50:38 +0200112 public int testI32(int thing)
113 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000114 testLogDelegate.Invoke("testI32({0})", thing);
Jens Geyerd5436f52014-10-03 19:50:38 +0200115 return thing;
116 }
David Reiss63191332009-01-06 19:49:22 +0000117
Jens Geyerd5436f52014-10-03 19:50:38 +0200118 public long testI64(long thing)
119 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000120 testLogDelegate.Invoke("testI64({0})", thing);
Jens Geyerd5436f52014-10-03 19:50:38 +0200121 return thing;
122 }
David Reiss63191332009-01-06 19:49:22 +0000123
Jens Geyerd5436f52014-10-03 19:50:38 +0200124 public double testDouble(double thing)
125 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000126 testLogDelegate.Invoke("testDouble({0})", thing);
Jens Geyerd5436f52014-10-03 19:50:38 +0200127 return thing;
128 }
David Reiss63191332009-01-06 19:49:22 +0000129
Jens Geyer71e814a2014-12-13 23:40:35 +0100130 public byte[] testBinary(byte[] thing)
131 {
132 string hex = BitConverter.ToString(thing).Replace("-", string.Empty);
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000133 testLogDelegate.Invoke("testBinary({0:X})", hex);
Jens Geyer71e814a2014-12-13 23:40:35 +0100134 return thing;
135 }
136
Jens Geyerd5436f52014-10-03 19:50:38 +0200137 public Xtruct testStruct(Xtruct thing)
138 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000139 testLogDelegate.Invoke("testStruct({{\"{0}\", {1}, {2}, {3}}})", thing.String_thing, thing.Byte_thing, thing.I32_thing, thing.I64_thing);
Jens Geyerd5436f52014-10-03 19:50:38 +0200140 return thing;
141 }
David Reiss63191332009-01-06 19:49:22 +0000142
Jens Geyerd5436f52014-10-03 19:50:38 +0200143 public Xtruct2 testNest(Xtruct2 nest)
144 {
145 Xtruct thing = nest.Struct_thing;
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000146 testLogDelegate.Invoke("testNest({{{0}, {{\"{1}\", {2}, {3}, {4}, {5}}}}})",
147 nest.Byte_thing,
148 thing.String_thing,
149 thing.Byte_thing,
150 thing.I32_thing,
151 thing.I64_thing,
152 nest.I32_thing);
Jens Geyerd5436f52014-10-03 19:50:38 +0200153 return nest;
154 }
David Reiss63191332009-01-06 19:49:22 +0000155
Jens Geyerd5436f52014-10-03 19:50:38 +0200156 public Dictionary<int, int> testMap(Dictionary<int, int> thing)
157 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000158 reusableStringBuilder.Clear();
159 reusableStringBuilder.Append("testMap({{");
Jens Geyerd5436f52014-10-03 19:50:38 +0200160 bool first = true;
161 foreach (int key in thing.Keys)
162 {
163 if (first)
164 {
165 first = false;
166 }
167 else
168 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000169 reusableStringBuilder.Append(", ");
Jens Geyerd5436f52014-10-03 19:50:38 +0200170 }
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000171 reusableStringBuilder.AppendFormat("{0} => {1}", key, thing[key]);
Jens Geyerd5436f52014-10-03 19:50:38 +0200172 }
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000173 reusableStringBuilder.Append("}})");
174 testLogDelegate.Invoke(reusableStringBuilder.ToString());
Jens Geyerd5436f52014-10-03 19:50:38 +0200175 return thing;
176 }
Jens Geyer1c99e702014-03-17 22:50:39 +0200177
Jens Geyerd5436f52014-10-03 19:50:38 +0200178 public Dictionary<string, string> testStringMap(Dictionary<string, string> thing)
179 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000180 reusableStringBuilder.Clear();
181 reusableStringBuilder.Append("testStringMap({{");
Jens Geyerd5436f52014-10-03 19:50:38 +0200182 bool first = true;
183 foreach (string key in thing.Keys)
184 {
185 if (first)
186 {
187 first = false;
188 }
189 else
190 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000191 reusableStringBuilder.Append(", ");
Jens Geyerd5436f52014-10-03 19:50:38 +0200192 }
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000193 reusableStringBuilder.AppendFormat("{0} => {1}", key, thing[key]);
Jens Geyerd5436f52014-10-03 19:50:38 +0200194 }
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000195 reusableStringBuilder.Append("}})");
196 testLogDelegate.Invoke(reusableStringBuilder.ToString());
Jens Geyerd5436f52014-10-03 19:50:38 +0200197 return thing;
198 }
David Reiss63191332009-01-06 19:49:22 +0000199
Jens Geyerd5436f52014-10-03 19:50:38 +0200200 public THashSet<int> testSet(THashSet<int> thing)
201 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000202 reusableStringBuilder.Clear();
203 reusableStringBuilder.Append("testSet({{");
Jens Geyerd5436f52014-10-03 19:50:38 +0200204 bool first = true;
205 foreach (int elem in thing)
206 {
207 if (first)
208 {
209 first = false;
210 }
211 else
212 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000213 reusableStringBuilder.Append(", ");
Jens Geyerd5436f52014-10-03 19:50:38 +0200214 }
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000215 reusableStringBuilder.AppendFormat("{0}", elem);
Jens Geyerd5436f52014-10-03 19:50:38 +0200216 }
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000217 reusableStringBuilder.Append("}})");
218 testLogDelegate.Invoke(reusableStringBuilder.ToString());
Jens Geyerd5436f52014-10-03 19:50:38 +0200219 return thing;
220 }
David Reiss63191332009-01-06 19:49:22 +0000221
Jens Geyerd5436f52014-10-03 19:50:38 +0200222 public List<int> testList(List<int> thing)
223 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000224 reusableStringBuilder.Clear();
225 reusableStringBuilder.Append("testList({{");
Jens Geyerd5436f52014-10-03 19:50:38 +0200226 bool first = true;
227 foreach (int elem in thing)
228 {
229 if (first)
230 {
231 first = false;
232 }
233 else
234 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000235 reusableStringBuilder.Append(", ");
Jens Geyerd5436f52014-10-03 19:50:38 +0200236 }
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000237 reusableStringBuilder.AppendFormat("{0}", elem);
Jens Geyerd5436f52014-10-03 19:50:38 +0200238 }
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000239 reusableStringBuilder.Append("}})");
240 testLogDelegate.Invoke(reusableStringBuilder.ToString());
Jens Geyerd5436f52014-10-03 19:50:38 +0200241 return thing;
242 }
David Reiss63191332009-01-06 19:49:22 +0000243
Jens Geyerd5436f52014-10-03 19:50:38 +0200244 public Numberz testEnum(Numberz thing)
245 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000246 testLogDelegate.Invoke("testEnum({0})", thing);
Jens Geyerd5436f52014-10-03 19:50:38 +0200247 return thing;
248 }
David Reiss63191332009-01-06 19:49:22 +0000249
Jens Geyerd5436f52014-10-03 19:50:38 +0200250 public long testTypedef(long thing)
251 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000252 testLogDelegate.Invoke("testTypedef({0})", thing);
Jens Geyerd5436f52014-10-03 19:50:38 +0200253 return thing;
254 }
David Reiss63191332009-01-06 19:49:22 +0000255
Jens Geyerd5436f52014-10-03 19:50:38 +0200256 public Dictionary<int, Dictionary<int, int>> testMapMap(int hello)
257 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000258 testLogDelegate.Invoke("testMapMap({0})", hello);
Jens Geyerd5436f52014-10-03 19:50:38 +0200259 Dictionary<int, Dictionary<int, int>> mapmap =
260 new Dictionary<int, Dictionary<int, int>>();
David Reiss63191332009-01-06 19:49:22 +0000261
Jens Geyerd5436f52014-10-03 19:50:38 +0200262 Dictionary<int, int> pos = new Dictionary<int, int>();
263 Dictionary<int, int> neg = new Dictionary<int, int>();
264 for (int i = 1; i < 5; i++)
265 {
266 pos[i] = i;
267 neg[-i] = -i;
268 }
David Reiss63191332009-01-06 19:49:22 +0000269
Jens Geyerd5436f52014-10-03 19:50:38 +0200270 mapmap[4] = pos;
271 mapmap[-4] = neg;
David Reiss63191332009-01-06 19:49:22 +0000272
Jens Geyerd5436f52014-10-03 19:50:38 +0200273 return mapmap;
274 }
David Reiss63191332009-01-06 19:49:22 +0000275
Allen Georged8bb0e32017-01-02 10:43:37 +0100276 // Insanity
277 // returns:
278 // { 1 => { 2 => argument,
279 // 3 => argument,
280 // },
281 // 2 => { 6 => <empty Insanity struct>, },
282 // }
Jens Geyerd5436f52014-10-03 19:50:38 +0200283 public Dictionary<long, Dictionary<Numberz, Insanity>> testInsanity(Insanity argument)
284 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000285 testLogDelegate.Invoke("testInsanity()");
David Reiss63191332009-01-06 19:49:22 +0000286
Jens Geyerd5436f52014-10-03 19:50:38 +0200287 Dictionary<Numberz, Insanity> first_map = new Dictionary<Numberz, Insanity>();
288 Dictionary<Numberz, Insanity> second_map = new Dictionary<Numberz, Insanity>(); ;
David Reiss63191332009-01-06 19:49:22 +0000289
Allen Georged8bb0e32017-01-02 10:43:37 +0100290 first_map[Numberz.TWO] = argument;
291 first_map[Numberz.THREE] = argument;
David Reiss63191332009-01-06 19:49:22 +0000292
Allen Georged8bb0e32017-01-02 10:43:37 +0100293 second_map[Numberz.SIX] = new Insanity();
David Reiss63191332009-01-06 19:49:22 +0000294
Jens Geyerd5436f52014-10-03 19:50:38 +0200295 Dictionary<long, Dictionary<Numberz, Insanity>> insane =
296 new Dictionary<long, Dictionary<Numberz, Insanity>>();
297 insane[(long)1] = first_map;
298 insane[(long)2] = second_map;
David Reiss63191332009-01-06 19:49:22 +0000299
Jens Geyerd5436f52014-10-03 19:50:38 +0200300 return insane;
301 }
David Reiss63191332009-01-06 19:49:22 +0000302
Jens Geyerd5436f52014-10-03 19:50:38 +0200303 public Xtruct testMulti(sbyte arg0, int arg1, long arg2, Dictionary<short, string> arg3, Numberz arg4, long arg5)
304 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000305 testLogDelegate.Invoke("testMulti()");
David Reiss63191332009-01-06 19:49:22 +0000306
Jens Geyerd5436f52014-10-03 19:50:38 +0200307 Xtruct hello = new Xtruct(); ;
308 hello.String_thing = "Hello2";
309 hello.Byte_thing = arg0;
310 hello.I32_thing = arg1;
311 hello.I64_thing = arg2;
312 return hello;
313 }
David Reiss63191332009-01-06 19:49:22 +0000314
Jens Geyer8b14d172015-02-26 19:36:28 +0100315 /**
316 * Print 'testException(%s)' with arg as '%s'
317 * @param string arg - a string indication what type of exception to throw
318 * if arg == "Xception" throw Xception with errorCode = 1001 and message = arg
319 * elsen if arg == "TException" throw TException
320 * else do not throw anything
321 */
Jens Geyerd5436f52014-10-03 19:50:38 +0200322 public void testException(string arg)
323 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000324 testLogDelegate.Invoke("testException({0})", arg);
Jens Geyerd5436f52014-10-03 19:50:38 +0200325 if (arg == "Xception")
326 {
327 Xception x = new Xception();
328 x.ErrorCode = 1001;
Jens Geyer8b14d172015-02-26 19:36:28 +0100329 x.Message = arg;
Jens Geyerd5436f52014-10-03 19:50:38 +0200330 throw x;
331 }
Jens Geyer8b14d172015-02-26 19:36:28 +0100332 if (arg == "TException")
333 {
334 throw new Thrift.TException();
335 }
Jens Geyerd5436f52014-10-03 19:50:38 +0200336 return;
337 }
David Reiss63191332009-01-06 19:49:22 +0000338
Jens Geyerd5436f52014-10-03 19:50:38 +0200339 public Xtruct testMultiException(string arg0, string arg1)
340 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000341 testLogDelegate.Invoke("testMultiException({0}, {1})", arg0,arg1);
Jens Geyerd5436f52014-10-03 19:50:38 +0200342 if (arg0 == "Xception")
343 {
344 Xception x = new Xception();
345 x.ErrorCode = 1001;
346 x.Message = "This is an Xception";
347 throw x;
348 }
349 else if (arg0 == "Xception2")
350 {
351 Xception2 x = new Xception2();
352 x.ErrorCode = 2002;
353 x.Struct_thing = new Xtruct();
354 x.Struct_thing.String_thing = "This is an Xception2";
355 throw x;
356 }
David Reiss63191332009-01-06 19:49:22 +0000357
Jens Geyerd5436f52014-10-03 19:50:38 +0200358 Xtruct result = new Xtruct();
359 result.String_thing = arg1;
360 return result;
361 }
David Reiss63191332009-01-06 19:49:22 +0000362
Jens Geyerd5436f52014-10-03 19:50:38 +0200363 public void testStop()
364 {
365 if (server != null)
366 {
367 server.Stop();
368 }
369 }
David Reiss63191332009-01-06 19:49:22 +0000370
Jens Geyerd5436f52014-10-03 19:50:38 +0200371 public void testOneway(int arg)
372 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000373 testLogDelegate.Invoke("testOneway({0}), sleeping...", arg);
Jens Geyerd5436f52014-10-03 19:50:38 +0200374 System.Threading.Thread.Sleep(arg * 1000);
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000375 testLogDelegate.Invoke("testOneway finished");
Jens Geyerd5436f52014-10-03 19:50:38 +0200376 }
David Reiss63191332009-01-06 19:49:22 +0000377
Jens Geyerd5436f52014-10-03 19:50:38 +0200378 } // class TestHandler
David Reiss63191332009-01-06 19:49:22 +0000379
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000380 private enum ServerType
381 {
382 TSimpleServer,
383 TThreadedServer,
384 TThreadPoolServer,
385 }
386
387 private enum ProcessorFactoryType
388 {
389 TSingletonProcessorFactory,
390 TPrototypeProcessorFactory,
391 }
392
Roger Meier41ad4342015-03-24 22:30:40 +0100393 public static bool Execute(string[] args)
Jens Geyerd5436f52014-10-03 19:50:38 +0200394 {
395 try
396 {
397 bool useBufferedSockets = false, useFramed = false, useEncryption = false, compact = false, json = false;
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000398 ServerType serverType = ServerType.TSimpleServer;
399 ProcessorFactoryType processorFactoryType = ProcessorFactoryType.TSingletonProcessorFactory;
Jens Geyerd5436f52014-10-03 19:50:38 +0200400 int port = 9090;
401 string pipe = null;
402 for (int i = 0; i < args.Length; i++)
403 {
404 if (args[i] == "-pipe") // -pipe name
405 {
406 pipe = args[++i];
407 }
408 else if (args[i].Contains("--port="))
409 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000410 port = int.Parse(args[i].Substring(args[i].IndexOf("=") + 1));
Jens Geyerd5436f52014-10-03 19:50:38 +0200411 }
412 else if (args[i] == "-b" || args[i] == "--buffered" || args[i] == "--transport=buffered")
413 {
414 useBufferedSockets = true;
415 }
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000416 else if (args[i] == "-f" || args[i] == "--framed" || args[i] == "--transport=framed")
Jens Geyerd5436f52014-10-03 19:50:38 +0200417 {
418 useFramed = true;
419 }
420 else if (args[i] == "--compact" || args[i] == "--protocol=compact")
421 {
422 compact = true;
423 }
424 else if (args[i] == "--json" || args[i] == "--protocol=json")
425 {
426 json = true;
427 }
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000428 else if (args[i] == "--threaded" || args[i] == "--server-type=threaded")
429 {
430 serverType = ServerType.TThreadedServer;
431 }
432 else if (args[i] == "--threadpool" || args[i] == "--server-type=threadpool")
433 {
434 serverType = ServerType.TThreadPoolServer;
435 }
436 else if (args[i] == "--prototype" || args[i] == "--processor=prototype")
437 {
438 processorFactoryType = ProcessorFactoryType.TPrototypeProcessorFactory;
439 }
Jens Geyerd5436f52014-10-03 19:50:38 +0200440 else if (args[i] == "--ssl")
441 {
442 useEncryption = true;
443 }
444 }
David Reiss63191332009-01-06 19:49:22 +0000445
Jens Geyerd5436f52014-10-03 19:50:38 +0200446 // Transport
447 TServerTransport trans;
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000448 if (pipe != null)
Jens Geyerd5436f52014-10-03 19:50:38 +0200449 {
450 trans = new TNamedPipeServerTransport(pipe);
451 }
452 else
453 {
454 if (useEncryption)
455 {
Nobuaki Sukegawa88c5ee72016-09-04 18:49:18 +0900456 string certPath = "../keys/server.p12";
Nobuaki Sukegawa474ddbd2016-02-17 23:44:27 +0900457 trans = new TTLSServerSocket(port, 0, useBufferedSockets, new X509Certificate2(certPath, "thrift"), null, null, SslProtocols.Tls);
Jens Geyerd5436f52014-10-03 19:50:38 +0200458 }
459 else
460 {
461 trans = new TServerSocket(port, 0, useBufferedSockets);
462 }
463 }
David Reiss63191332009-01-06 19:49:22 +0000464
Jens Geyerd5436f52014-10-03 19:50:38 +0200465 TProtocolFactory proto;
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000466 if (compact)
Jens Geyerd5436f52014-10-03 19:50:38 +0200467 proto = new TCompactProtocol.Factory();
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000468 else if (json)
Jens Geyerd5436f52014-10-03 19:50:38 +0200469 proto = new TJSONProtocol.Factory();
470 else
471 proto = new TBinaryProtocol.Factory();
David Reiss63191332009-01-06 19:49:22 +0000472
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000473 TProcessorFactory processorFactory;
474 if (processorFactoryType == ProcessorFactoryType.TPrototypeProcessorFactory)
475 {
476 processorFactory = new TPrototypeProcessorFactory<ThriftTest.Processor, TestHandler>();
477 }
Jens Geyerd5436f52014-10-03 19:50:38 +0200478 else
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000479 {
480 // Processor
481 TestHandler testHandler = new TestHandler();
482 ThriftTest.Processor testProcessor = new ThriftTest.Processor(testHandler);
483 processorFactory = new TSingletonProcessorFactory(testProcessor);
484 }
David Reissd831a212009-02-13 03:09:52 +0000485
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000486 TTransportFactory transFactory;
487 if (useFramed)
488 transFactory = new TFramedTransport.Factory();
489 else
490 transFactory = new TTransportFactory();
Jens Geyerd5436f52014-10-03 19:50:38 +0200491
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000492 TServer serverEngine;
493 switch (serverType)
494 {
495 case ServerType.TThreadPoolServer:
496 serverEngine = new TThreadPoolServer(processorFactory, trans, transFactory, proto);
497 break;
498 case ServerType.TThreadedServer:
499 serverEngine = new TThreadedServer(processorFactory, trans, transFactory, proto);
500 break;
501 default:
502 serverEngine = new TSimpleServer(processorFactory, trans, transFactory, proto);
503 break;
504 }
David Reiss63191332009-01-06 19:49:22 +0000505
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000506 //Server event handler
507 TradeServerEventHandler serverEvents = new TradeServerEventHandler();
508 serverEngine.setEventHandler(serverEvents);
David Reiss63191332009-01-06 19:49:22 +0000509
Jens Geyerd5436f52014-10-03 19:50:38 +0200510 // Run it
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000511 string where = (pipe != null ? "on pipe " + pipe : "on port " + port);
512 Console.WriteLine("Starting the " + serverType.ToString() + " " + where +
513 (processorFactoryType == ProcessorFactoryType.TPrototypeProcessorFactory ? " with processor prototype factory " : "") +
Jens Geyerd5436f52014-10-03 19:50:38 +0200514 (useBufferedSockets ? " with buffered socket" : "") +
515 (useFramed ? " with framed transport" : "") +
516 (useEncryption ? " with encryption" : "") +
517 (compact ? " with compact protocol" : "") +
518 (json ? " with json protocol" : "") +
519 "...");
520 serverEngine.Serve();
David Reiss63191332009-01-06 19:49:22 +0000521
Jens Geyerd5436f52014-10-03 19:50:38 +0200522 }
523 catch (Exception x)
524 {
525 Console.Error.Write(x);
Roger Meier41ad4342015-03-24 22:30:40 +0100526 return false;
Jens Geyerd5436f52014-10-03 19:50:38 +0200527 }
528 Console.WriteLine("done.");
Roger Meier41ad4342015-03-24 22:30:40 +0100529 return true;
Jens Geyerd5436f52014-10-03 19:50:38 +0200530 }
531 }
David Reiss63191332009-01-06 19:49:22 +0000532}