blob: e9c7168eb75a44f62670efd67fe86c5755815972 [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
Jens Geyerd4df9172017-10-25 22:30:23 +020044 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
Randy Abernethy0e86f1f2014-07-13 09:50:19 -070066
Jonathan Heard2bfd7df2015-10-28 17:34:27 +000067 public class TestHandler : ThriftTest.Iface, Thrift.TControllingHandler
Jens Geyerd5436f52014-10-03 19:50:38 +020068 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +000069 public TServer server { get; set; }
70 private int handlerID;
71 private StringBuilder reusableStringBuilder = new StringBuilder();
72 private TestLogDelegate testLogDelegate;
David Reiss63191332009-01-06 19:49:22 +000073
Jonathan Heard2bfd7df2015-10-28 17:34:27 +000074 public TestHandler()
75 {
76 handlerID = Interlocked.Increment(ref _clientID);
77 testLogDelegate += testConsoleLogger;
78 testLogDelegate.Invoke("New TestHandler instance created");
79 }
80
81 public void testConsoleLogger(string msg, params object[] values)
82 {
83 reusableStringBuilder.Clear();
84 reusableStringBuilder.AppendFormat("handler{0:D3}:",handlerID);
85 reusableStringBuilder.AppendFormat(msg, values);
86 reusableStringBuilder.AppendLine();
87 Console.Write( reusableStringBuilder.ToString() );
88 }
David Reiss63191332009-01-06 19:49:22 +000089
Jens Geyerd5436f52014-10-03 19:50:38 +020090 public void testVoid()
91 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +000092 testLogDelegate.Invoke("testVoid()");
Jens Geyerd5436f52014-10-03 19:50:38 +020093 }
David Reiss63191332009-01-06 19:49:22 +000094
Jens Geyerd5436f52014-10-03 19:50:38 +020095 public string testString(string thing)
96 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +000097 testLogDelegate.Invoke("testString({0})", thing);
Jens Geyerd5436f52014-10-03 19:50:38 +020098 return thing;
99 }
David Reiss63191332009-01-06 19:49:22 +0000100
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900101 public bool testBool(bool thing)
102 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000103 testLogDelegate.Invoke("testBool({0})", thing);
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900104 return thing;
105 }
106
Jens Geyerd5436f52014-10-03 19:50:38 +0200107 public sbyte testByte(sbyte thing)
108 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000109 testLogDelegate.Invoke("testByte({0})", thing);
Jens Geyerd5436f52014-10-03 19:50:38 +0200110 return thing;
111 }
David Reiss63191332009-01-06 19:49:22 +0000112
Jens Geyerd5436f52014-10-03 19:50:38 +0200113 public int testI32(int thing)
114 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000115 testLogDelegate.Invoke("testI32({0})", thing);
Jens Geyerd5436f52014-10-03 19:50:38 +0200116 return thing;
117 }
David Reiss63191332009-01-06 19:49:22 +0000118
Jens Geyerd5436f52014-10-03 19:50:38 +0200119 public long testI64(long thing)
120 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000121 testLogDelegate.Invoke("testI64({0})", thing);
Jens Geyerd5436f52014-10-03 19:50:38 +0200122 return thing;
123 }
David Reiss63191332009-01-06 19:49:22 +0000124
Jens Geyerd5436f52014-10-03 19:50:38 +0200125 public double testDouble(double thing)
126 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000127 testLogDelegate.Invoke("testDouble({0})", thing);
Jens Geyerd5436f52014-10-03 19:50:38 +0200128 return thing;
129 }
David Reiss63191332009-01-06 19:49:22 +0000130
Jens Geyer71e814a2014-12-13 23:40:35 +0100131 public byte[] testBinary(byte[] thing)
132 {
133 string hex = BitConverter.ToString(thing).Replace("-", string.Empty);
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000134 testLogDelegate.Invoke("testBinary({0:X})", hex);
Jens Geyer71e814a2014-12-13 23:40:35 +0100135 return thing;
136 }
137
Jens Geyerd5436f52014-10-03 19:50:38 +0200138 public Xtruct testStruct(Xtruct thing)
139 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000140 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 +0200141 return thing;
142 }
David Reiss63191332009-01-06 19:49:22 +0000143
Jens Geyerd5436f52014-10-03 19:50:38 +0200144 public Xtruct2 testNest(Xtruct2 nest)
145 {
146 Xtruct thing = nest.Struct_thing;
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000147 testLogDelegate.Invoke("testNest({{{0}, {{\"{1}\", {2}, {3}, {4}, {5}}}}})",
148 nest.Byte_thing,
149 thing.String_thing,
150 thing.Byte_thing,
151 thing.I32_thing,
152 thing.I64_thing,
153 nest.I32_thing);
Jens Geyerd5436f52014-10-03 19:50:38 +0200154 return nest;
155 }
David Reiss63191332009-01-06 19:49:22 +0000156
Jens Geyerd5436f52014-10-03 19:50:38 +0200157 public Dictionary<int, int> testMap(Dictionary<int, int> thing)
158 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000159 reusableStringBuilder.Clear();
160 reusableStringBuilder.Append("testMap({{");
Jens Geyerd5436f52014-10-03 19:50:38 +0200161 bool first = true;
162 foreach (int key in thing.Keys)
163 {
164 if (first)
165 {
166 first = false;
167 }
168 else
169 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000170 reusableStringBuilder.Append(", ");
Jens Geyerd5436f52014-10-03 19:50:38 +0200171 }
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000172 reusableStringBuilder.AppendFormat("{0} => {1}", key, thing[key]);
Jens Geyerd5436f52014-10-03 19:50:38 +0200173 }
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000174 reusableStringBuilder.Append("}})");
175 testLogDelegate.Invoke(reusableStringBuilder.ToString());
Jens Geyerd5436f52014-10-03 19:50:38 +0200176 return thing;
177 }
Jens Geyer1c99e702014-03-17 22:50:39 +0200178
Jens Geyerd5436f52014-10-03 19:50:38 +0200179 public Dictionary<string, string> testStringMap(Dictionary<string, string> thing)
180 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000181 reusableStringBuilder.Clear();
182 reusableStringBuilder.Append("testStringMap({{");
Jens Geyerd5436f52014-10-03 19:50:38 +0200183 bool first = true;
184 foreach (string key in thing.Keys)
185 {
186 if (first)
187 {
188 first = false;
189 }
190 else
191 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000192 reusableStringBuilder.Append(", ");
Jens Geyerd5436f52014-10-03 19:50:38 +0200193 }
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000194 reusableStringBuilder.AppendFormat("{0} => {1}", key, thing[key]);
Jens Geyerd5436f52014-10-03 19:50:38 +0200195 }
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000196 reusableStringBuilder.Append("}})");
197 testLogDelegate.Invoke(reusableStringBuilder.ToString());
Jens Geyerd5436f52014-10-03 19:50:38 +0200198 return thing;
199 }
David Reiss63191332009-01-06 19:49:22 +0000200
Jens Geyerd5436f52014-10-03 19:50:38 +0200201 public THashSet<int> testSet(THashSet<int> thing)
202 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000203 reusableStringBuilder.Clear();
204 reusableStringBuilder.Append("testSet({{");
Jens Geyerd5436f52014-10-03 19:50:38 +0200205 bool first = true;
206 foreach (int elem in thing)
207 {
208 if (first)
209 {
210 first = false;
211 }
212 else
213 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000214 reusableStringBuilder.Append(", ");
Jens Geyerd5436f52014-10-03 19:50:38 +0200215 }
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000216 reusableStringBuilder.AppendFormat("{0}", elem);
Jens Geyerd5436f52014-10-03 19:50:38 +0200217 }
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000218 reusableStringBuilder.Append("}})");
219 testLogDelegate.Invoke(reusableStringBuilder.ToString());
Jens Geyerd5436f52014-10-03 19:50:38 +0200220 return thing;
221 }
David Reiss63191332009-01-06 19:49:22 +0000222
Jens Geyerd5436f52014-10-03 19:50:38 +0200223 public List<int> testList(List<int> thing)
224 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000225 reusableStringBuilder.Clear();
226 reusableStringBuilder.Append("testList({{");
Jens Geyerd5436f52014-10-03 19:50:38 +0200227 bool first = true;
228 foreach (int elem in thing)
229 {
230 if (first)
231 {
232 first = false;
233 }
234 else
235 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000236 reusableStringBuilder.Append(", ");
Jens Geyerd5436f52014-10-03 19:50:38 +0200237 }
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000238 reusableStringBuilder.AppendFormat("{0}", elem);
Jens Geyerd5436f52014-10-03 19:50:38 +0200239 }
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000240 reusableStringBuilder.Append("}})");
241 testLogDelegate.Invoke(reusableStringBuilder.ToString());
Jens Geyerd5436f52014-10-03 19:50:38 +0200242 return thing;
243 }
David Reiss63191332009-01-06 19:49:22 +0000244
Jens Geyerd5436f52014-10-03 19:50:38 +0200245 public Numberz testEnum(Numberz thing)
246 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000247 testLogDelegate.Invoke("testEnum({0})", thing);
Jens Geyerd5436f52014-10-03 19:50:38 +0200248 return thing;
249 }
David Reiss63191332009-01-06 19:49:22 +0000250
Jens Geyerd5436f52014-10-03 19:50:38 +0200251 public long testTypedef(long thing)
252 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000253 testLogDelegate.Invoke("testTypedef({0})", thing);
Jens Geyerd5436f52014-10-03 19:50:38 +0200254 return thing;
255 }
David Reiss63191332009-01-06 19:49:22 +0000256
Jens Geyerd5436f52014-10-03 19:50:38 +0200257 public Dictionary<int, Dictionary<int, int>> testMapMap(int hello)
258 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000259 testLogDelegate.Invoke("testMapMap({0})", hello);
Jens Geyerd5436f52014-10-03 19:50:38 +0200260 Dictionary<int, Dictionary<int, int>> mapmap =
261 new Dictionary<int, Dictionary<int, int>>();
David Reiss63191332009-01-06 19:49:22 +0000262
Jens Geyerd5436f52014-10-03 19:50:38 +0200263 Dictionary<int, int> pos = new Dictionary<int, int>();
264 Dictionary<int, int> neg = new Dictionary<int, int>();
265 for (int i = 1; i < 5; i++)
266 {
267 pos[i] = i;
268 neg[-i] = -i;
269 }
David Reiss63191332009-01-06 19:49:22 +0000270
Jens Geyerd5436f52014-10-03 19:50:38 +0200271 mapmap[4] = pos;
272 mapmap[-4] = neg;
David Reiss63191332009-01-06 19:49:22 +0000273
Jens Geyerd5436f52014-10-03 19:50:38 +0200274 return mapmap;
275 }
David Reiss63191332009-01-06 19:49:22 +0000276
Allen Georged8bb0e32017-01-02 10:43:37 +0100277 // Insanity
278 // returns:
279 // { 1 => { 2 => argument,
280 // 3 => argument,
281 // },
282 // 2 => { 6 => <empty Insanity struct>, },
283 // }
Jens Geyerd5436f52014-10-03 19:50:38 +0200284 public Dictionary<long, Dictionary<Numberz, Insanity>> testInsanity(Insanity argument)
285 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000286 testLogDelegate.Invoke("testInsanity()");
David Reiss63191332009-01-06 19:49:22 +0000287
Jens Geyerd5436f52014-10-03 19:50:38 +0200288 Dictionary<Numberz, Insanity> first_map = new Dictionary<Numberz, Insanity>();
289 Dictionary<Numberz, Insanity> second_map = new Dictionary<Numberz, Insanity>(); ;
David Reiss63191332009-01-06 19:49:22 +0000290
Allen Georged8bb0e32017-01-02 10:43:37 +0100291 first_map[Numberz.TWO] = argument;
292 first_map[Numberz.THREE] = argument;
David Reiss63191332009-01-06 19:49:22 +0000293
Allen Georged8bb0e32017-01-02 10:43:37 +0100294 second_map[Numberz.SIX] = new Insanity();
David Reiss63191332009-01-06 19:49:22 +0000295
Jens Geyerd5436f52014-10-03 19:50:38 +0200296 Dictionary<long, Dictionary<Numberz, Insanity>> insane =
297 new Dictionary<long, Dictionary<Numberz, Insanity>>();
298 insane[(long)1] = first_map;
299 insane[(long)2] = second_map;
David Reiss63191332009-01-06 19:49:22 +0000300
Jens Geyerd5436f52014-10-03 19:50:38 +0200301 return insane;
302 }
David Reiss63191332009-01-06 19:49:22 +0000303
Jens Geyerd5436f52014-10-03 19:50:38 +0200304 public Xtruct testMulti(sbyte arg0, int arg1, long arg2, Dictionary<short, string> arg3, Numberz arg4, long arg5)
305 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000306 testLogDelegate.Invoke("testMulti()");
David Reiss63191332009-01-06 19:49:22 +0000307
Jens Geyerd5436f52014-10-03 19:50:38 +0200308 Xtruct hello = new Xtruct(); ;
309 hello.String_thing = "Hello2";
310 hello.Byte_thing = arg0;
311 hello.I32_thing = arg1;
312 hello.I64_thing = arg2;
313 return hello;
314 }
David Reiss63191332009-01-06 19:49:22 +0000315
Jens Geyer8b14d172015-02-26 19:36:28 +0100316 /**
317 * Print 'testException(%s)' with arg as '%s'
318 * @param string arg - a string indication what type of exception to throw
319 * if arg == "Xception" throw Xception with errorCode = 1001 and message = arg
320 * elsen if arg == "TException" throw TException
321 * else do not throw anything
322 */
Jens Geyerd5436f52014-10-03 19:50:38 +0200323 public void testException(string arg)
324 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000325 testLogDelegate.Invoke("testException({0})", arg);
Jens Geyerd5436f52014-10-03 19:50:38 +0200326 if (arg == "Xception")
327 {
328 Xception x = new Xception();
329 x.ErrorCode = 1001;
Jens Geyer8b14d172015-02-26 19:36:28 +0100330 x.Message = arg;
Jens Geyerd5436f52014-10-03 19:50:38 +0200331 throw x;
332 }
Jens Geyer8b14d172015-02-26 19:36:28 +0100333 if (arg == "TException")
334 {
335 throw new Thrift.TException();
336 }
Jens Geyerd5436f52014-10-03 19:50:38 +0200337 return;
338 }
David Reiss63191332009-01-06 19:49:22 +0000339
Jens Geyerd5436f52014-10-03 19:50:38 +0200340 public Xtruct testMultiException(string arg0, string arg1)
341 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000342 testLogDelegate.Invoke("testMultiException({0}, {1})", arg0,arg1);
Jens Geyerd5436f52014-10-03 19:50:38 +0200343 if (arg0 == "Xception")
344 {
345 Xception x = new Xception();
346 x.ErrorCode = 1001;
347 x.Message = "This is an Xception";
348 throw x;
349 }
350 else if (arg0 == "Xception2")
351 {
352 Xception2 x = new Xception2();
353 x.ErrorCode = 2002;
354 x.Struct_thing = new Xtruct();
355 x.Struct_thing.String_thing = "This is an Xception2";
356 throw x;
357 }
David Reiss63191332009-01-06 19:49:22 +0000358
Jens Geyerd5436f52014-10-03 19:50:38 +0200359 Xtruct result = new Xtruct();
360 result.String_thing = arg1;
361 return result;
362 }
David Reiss63191332009-01-06 19:49:22 +0000363
Jens Geyerd5436f52014-10-03 19:50:38 +0200364 public void testStop()
365 {
366 if (server != null)
367 {
368 server.Stop();
369 }
370 }
David Reiss63191332009-01-06 19:49:22 +0000371
Jens Geyerd5436f52014-10-03 19:50:38 +0200372 public void testOneway(int arg)
373 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000374 testLogDelegate.Invoke("testOneway({0}), sleeping...", arg);
Jens Geyerd5436f52014-10-03 19:50:38 +0200375 System.Threading.Thread.Sleep(arg * 1000);
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000376 testLogDelegate.Invoke("testOneway finished");
Jens Geyerd5436f52014-10-03 19:50:38 +0200377 }
David Reiss63191332009-01-06 19:49:22 +0000378
Jens Geyerd5436f52014-10-03 19:50:38 +0200379 } // class TestHandler
David Reiss63191332009-01-06 19:49:22 +0000380
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000381 private enum ServerType
382 {
383 TSimpleServer,
384 TThreadedServer,
385 TThreadPoolServer,
386 }
387
388 private enum ProcessorFactoryType
389 {
390 TSingletonProcessorFactory,
391 TPrototypeProcessorFactory,
392 }
393
Roger Meier41ad4342015-03-24 22:30:40 +0100394 public static bool Execute(string[] args)
Jens Geyerd5436f52014-10-03 19:50:38 +0200395 {
396 try
397 {
398 bool useBufferedSockets = false, useFramed = false, useEncryption = false, compact = false, json = false;
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000399 ServerType serverType = ServerType.TSimpleServer;
400 ProcessorFactoryType processorFactoryType = ProcessorFactoryType.TSingletonProcessorFactory;
Jens Geyerd5436f52014-10-03 19:50:38 +0200401 int port = 9090;
402 string pipe = null;
403 for (int i = 0; i < args.Length; i++)
404 {
405 if (args[i] == "-pipe") // -pipe name
406 {
407 pipe = args[++i];
408 }
409 else if (args[i].Contains("--port="))
410 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000411 port = int.Parse(args[i].Substring(args[i].IndexOf("=") + 1));
Jens Geyerd5436f52014-10-03 19:50:38 +0200412 }
413 else if (args[i] == "-b" || args[i] == "--buffered" || args[i] == "--transport=buffered")
414 {
415 useBufferedSockets = true;
416 }
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000417 else if (args[i] == "-f" || args[i] == "--framed" || args[i] == "--transport=framed")
Jens Geyerd5436f52014-10-03 19:50:38 +0200418 {
419 useFramed = true;
420 }
421 else if (args[i] == "--compact" || args[i] == "--protocol=compact")
422 {
423 compact = true;
424 }
425 else if (args[i] == "--json" || args[i] == "--protocol=json")
426 {
427 json = true;
428 }
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000429 else if (args[i] == "--threaded" || args[i] == "--server-type=threaded")
430 {
431 serverType = ServerType.TThreadedServer;
432 }
433 else if (args[i] == "--threadpool" || args[i] == "--server-type=threadpool")
434 {
435 serverType = ServerType.TThreadPoolServer;
436 }
437 else if (args[i] == "--prototype" || args[i] == "--processor=prototype")
438 {
439 processorFactoryType = ProcessorFactoryType.TPrototypeProcessorFactory;
440 }
Jens Geyerd5436f52014-10-03 19:50:38 +0200441 else if (args[i] == "--ssl")
442 {
443 useEncryption = true;
444 }
445 }
David Reiss63191332009-01-06 19:49:22 +0000446
Jens Geyerd5436f52014-10-03 19:50:38 +0200447 // Transport
448 TServerTransport trans;
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000449 if (pipe != null)
Jens Geyerd5436f52014-10-03 19:50:38 +0200450 {
451 trans = new TNamedPipeServerTransport(pipe);
452 }
453 else
454 {
455 if (useEncryption)
456 {
Nobuaki Sukegawa88c5ee72016-09-04 18:49:18 +0900457 string certPath = "../keys/server.p12";
Nobuaki Sukegawa474ddbd2016-02-17 23:44:27 +0900458 trans = new TTLSServerSocket(port, 0, useBufferedSockets, new X509Certificate2(certPath, "thrift"), null, null, SslProtocols.Tls);
Jens Geyerd5436f52014-10-03 19:50:38 +0200459 }
460 else
461 {
462 trans = new TServerSocket(port, 0, useBufferedSockets);
463 }
464 }
David Reiss63191332009-01-06 19:49:22 +0000465
Jens Geyerd5436f52014-10-03 19:50:38 +0200466 TProtocolFactory proto;
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000467 if (compact)
Jens Geyerd5436f52014-10-03 19:50:38 +0200468 proto = new TCompactProtocol.Factory();
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000469 else if (json)
Jens Geyerd5436f52014-10-03 19:50:38 +0200470 proto = new TJSONProtocol.Factory();
471 else
472 proto = new TBinaryProtocol.Factory();
David Reiss63191332009-01-06 19:49:22 +0000473
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000474 TProcessorFactory processorFactory;
475 if (processorFactoryType == ProcessorFactoryType.TPrototypeProcessorFactory)
476 {
477 processorFactory = new TPrototypeProcessorFactory<ThriftTest.Processor, TestHandler>();
478 }
Jens Geyerd5436f52014-10-03 19:50:38 +0200479 else
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000480 {
481 // Processor
482 TestHandler testHandler = new TestHandler();
483 ThriftTest.Processor testProcessor = new ThriftTest.Processor(testHandler);
484 processorFactory = new TSingletonProcessorFactory(testProcessor);
485 }
David Reissd831a212009-02-13 03:09:52 +0000486
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000487 TTransportFactory transFactory;
488 if (useFramed)
489 transFactory = new TFramedTransport.Factory();
490 else
491 transFactory = new TTransportFactory();
Jens Geyerd5436f52014-10-03 19:50:38 +0200492
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000493 TServer serverEngine;
494 switch (serverType)
495 {
496 case ServerType.TThreadPoolServer:
497 serverEngine = new TThreadPoolServer(processorFactory, trans, transFactory, proto);
498 break;
499 case ServerType.TThreadedServer:
500 serverEngine = new TThreadedServer(processorFactory, trans, transFactory, proto);
501 break;
502 default:
503 serverEngine = new TSimpleServer(processorFactory, trans, transFactory, proto);
504 break;
505 }
David Reiss63191332009-01-06 19:49:22 +0000506
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000507 //Server event handler
508 TradeServerEventHandler serverEvents = new TradeServerEventHandler();
509 serverEngine.setEventHandler(serverEvents);
David Reiss63191332009-01-06 19:49:22 +0000510
Jens Geyerd5436f52014-10-03 19:50:38 +0200511 // Run it
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000512 string where = (pipe != null ? "on pipe " + pipe : "on port " + port);
513 Console.WriteLine("Starting the " + serverType.ToString() + " " + where +
514 (processorFactoryType == ProcessorFactoryType.TPrototypeProcessorFactory ? " with processor prototype factory " : "") +
Jens Geyerd5436f52014-10-03 19:50:38 +0200515 (useBufferedSockets ? " with buffered socket" : "") +
516 (useFramed ? " with framed transport" : "") +
517 (useEncryption ? " with encryption" : "") +
518 (compact ? " with compact protocol" : "") +
519 (json ? " with json protocol" : "") +
520 "...");
521 serverEngine.Serve();
David Reiss63191332009-01-06 19:49:22 +0000522
Jens Geyerd5436f52014-10-03 19:50:38 +0200523 }
524 catch (Exception x)
525 {
526 Console.Error.Write(x);
Roger Meier41ad4342015-03-24 22:30:40 +0100527 return false;
Jens Geyerd5436f52014-10-03 19:50:38 +0200528 }
529 Console.WriteLine("done.");
Roger Meier41ad4342015-03-24 22:30:40 +0100530 return true;
Jens Geyerd5436f52014-10-03 19:50:38 +0200531 }
532 }
David Reiss63191332009-01-06 19:49:22 +0000533}