blob: a9af715ad3e33bb0ac38eb62621b9f857225e8b5 [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
Jens Geyerd5436f52014-10-03 19:50:38 +0200276 public Dictionary<long, Dictionary<Numberz, Insanity>> testInsanity(Insanity argument)
277 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000278 testLogDelegate.Invoke("testInsanity()");
David Reiss63191332009-01-06 19:49:22 +0000279
Jens Geyerd5436f52014-10-03 19:50:38 +0200280 Xtruct hello = new Xtruct();
281 hello.String_thing = "Hello2";
282 hello.Byte_thing = 2;
283 hello.I32_thing = 2;
284 hello.I64_thing = 2;
David Reiss63191332009-01-06 19:49:22 +0000285
Jens Geyerd5436f52014-10-03 19:50:38 +0200286 Xtruct goodbye = new Xtruct();
287 goodbye.String_thing = "Goodbye4";
288 goodbye.Byte_thing = (sbyte)4;
289 goodbye.I32_thing = 4;
290 goodbye.I64_thing = (long)4;
David Reiss63191332009-01-06 19:49:22 +0000291
Jens Geyerd5436f52014-10-03 19:50:38 +0200292 Insanity crazy = new Insanity();
293 crazy.UserMap = new Dictionary<Numberz, long>();
294 crazy.UserMap[Numberz.EIGHT] = (long)8;
295 crazy.Xtructs = new List<Xtruct>();
296 crazy.Xtructs.Add(goodbye);
David Reiss63191332009-01-06 19:49:22 +0000297
Jens Geyerd5436f52014-10-03 19:50:38 +0200298 Insanity looney = new Insanity();
299 crazy.UserMap[Numberz.FIVE] = (long)5;
300 crazy.Xtructs.Add(hello);
David Reiss63191332009-01-06 19:49:22 +0000301
Jens Geyerd5436f52014-10-03 19:50:38 +0200302 Dictionary<Numberz, Insanity> first_map = new Dictionary<Numberz, Insanity>();
303 Dictionary<Numberz, Insanity> second_map = new Dictionary<Numberz, Insanity>(); ;
David Reiss63191332009-01-06 19:49:22 +0000304
Jens Geyerd5436f52014-10-03 19:50:38 +0200305 first_map[Numberz.TWO] = crazy;
306 first_map[Numberz.THREE] = crazy;
David Reiss63191332009-01-06 19:49:22 +0000307
Jens Geyerd5436f52014-10-03 19:50:38 +0200308 second_map[Numberz.SIX] = looney;
David Reiss63191332009-01-06 19:49:22 +0000309
Jens Geyerd5436f52014-10-03 19:50:38 +0200310 Dictionary<long, Dictionary<Numberz, Insanity>> insane =
311 new Dictionary<long, Dictionary<Numberz, Insanity>>();
312 insane[(long)1] = first_map;
313 insane[(long)2] = second_map;
David Reiss63191332009-01-06 19:49:22 +0000314
Jens Geyerd5436f52014-10-03 19:50:38 +0200315 return insane;
316 }
David Reiss63191332009-01-06 19:49:22 +0000317
Jens Geyerd5436f52014-10-03 19:50:38 +0200318 public Xtruct testMulti(sbyte arg0, int arg1, long arg2, Dictionary<short, string> arg3, Numberz arg4, long arg5)
319 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000320 testLogDelegate.Invoke("testMulti()");
David Reiss63191332009-01-06 19:49:22 +0000321
Jens Geyerd5436f52014-10-03 19:50:38 +0200322 Xtruct hello = new Xtruct(); ;
323 hello.String_thing = "Hello2";
324 hello.Byte_thing = arg0;
325 hello.I32_thing = arg1;
326 hello.I64_thing = arg2;
327 return hello;
328 }
David Reiss63191332009-01-06 19:49:22 +0000329
Jens Geyer8b14d172015-02-26 19:36:28 +0100330 /**
331 * Print 'testException(%s)' with arg as '%s'
332 * @param string arg - a string indication what type of exception to throw
333 * if arg == "Xception" throw Xception with errorCode = 1001 and message = arg
334 * elsen if arg == "TException" throw TException
335 * else do not throw anything
336 */
Jens Geyerd5436f52014-10-03 19:50:38 +0200337 public void testException(string arg)
338 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000339 testLogDelegate.Invoke("testException({0})", arg);
Jens Geyerd5436f52014-10-03 19:50:38 +0200340 if (arg == "Xception")
341 {
342 Xception x = new Xception();
343 x.ErrorCode = 1001;
Jens Geyer8b14d172015-02-26 19:36:28 +0100344 x.Message = arg;
Jens Geyerd5436f52014-10-03 19:50:38 +0200345 throw x;
346 }
Jens Geyer8b14d172015-02-26 19:36:28 +0100347 if (arg == "TException")
348 {
349 throw new Thrift.TException();
350 }
Jens Geyerd5436f52014-10-03 19:50:38 +0200351 return;
352 }
David Reiss63191332009-01-06 19:49:22 +0000353
Jens Geyerd5436f52014-10-03 19:50:38 +0200354 public Xtruct testMultiException(string arg0, string arg1)
355 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000356 testLogDelegate.Invoke("testMultiException({0}, {1})", arg0,arg1);
Jens Geyerd5436f52014-10-03 19:50:38 +0200357 if (arg0 == "Xception")
358 {
359 Xception x = new Xception();
360 x.ErrorCode = 1001;
361 x.Message = "This is an Xception";
362 throw x;
363 }
364 else if (arg0 == "Xception2")
365 {
366 Xception2 x = new Xception2();
367 x.ErrorCode = 2002;
368 x.Struct_thing = new Xtruct();
369 x.Struct_thing.String_thing = "This is an Xception2";
370 throw x;
371 }
David Reiss63191332009-01-06 19:49:22 +0000372
Jens Geyerd5436f52014-10-03 19:50:38 +0200373 Xtruct result = new Xtruct();
374 result.String_thing = arg1;
375 return result;
376 }
David Reiss63191332009-01-06 19:49:22 +0000377
Jens Geyerd5436f52014-10-03 19:50:38 +0200378 public void testStop()
379 {
380 if (server != null)
381 {
382 server.Stop();
383 }
384 }
David Reiss63191332009-01-06 19:49:22 +0000385
Jens Geyerd5436f52014-10-03 19:50:38 +0200386 public void testOneway(int arg)
387 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000388 testLogDelegate.Invoke("testOneway({0}), sleeping...", arg);
Jens Geyerd5436f52014-10-03 19:50:38 +0200389 System.Threading.Thread.Sleep(arg * 1000);
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000390 testLogDelegate.Invoke("testOneway finished");
Jens Geyerd5436f52014-10-03 19:50:38 +0200391 }
David Reiss63191332009-01-06 19:49:22 +0000392
Jens Geyerd5436f52014-10-03 19:50:38 +0200393 } // class TestHandler
David Reiss63191332009-01-06 19:49:22 +0000394
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000395 private enum ServerType
396 {
397 TSimpleServer,
398 TThreadedServer,
399 TThreadPoolServer,
400 }
401
402 private enum ProcessorFactoryType
403 {
404 TSingletonProcessorFactory,
405 TPrototypeProcessorFactory,
406 }
407
Roger Meier41ad4342015-03-24 22:30:40 +0100408 public static bool Execute(string[] args)
Jens Geyerd5436f52014-10-03 19:50:38 +0200409 {
410 try
411 {
412 bool useBufferedSockets = false, useFramed = false, useEncryption = false, compact = false, json = false;
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000413 ServerType serverType = ServerType.TSimpleServer;
414 ProcessorFactoryType processorFactoryType = ProcessorFactoryType.TSingletonProcessorFactory;
Jens Geyerd5436f52014-10-03 19:50:38 +0200415 int port = 9090;
416 string pipe = null;
417 for (int i = 0; i < args.Length; i++)
418 {
419 if (args[i] == "-pipe") // -pipe name
420 {
421 pipe = args[++i];
422 }
423 else if (args[i].Contains("--port="))
424 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000425 port = int.Parse(args[i].Substring(args[i].IndexOf("=") + 1));
Jens Geyerd5436f52014-10-03 19:50:38 +0200426 }
427 else if (args[i] == "-b" || args[i] == "--buffered" || args[i] == "--transport=buffered")
428 {
429 useBufferedSockets = true;
430 }
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000431 else if (args[i] == "-f" || args[i] == "--framed" || args[i] == "--transport=framed")
Jens Geyerd5436f52014-10-03 19:50:38 +0200432 {
433 useFramed = true;
434 }
435 else if (args[i] == "--compact" || args[i] == "--protocol=compact")
436 {
437 compact = true;
438 }
439 else if (args[i] == "--json" || args[i] == "--protocol=json")
440 {
441 json = true;
442 }
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000443 else if (args[i] == "--threaded" || args[i] == "--server-type=threaded")
444 {
445 serverType = ServerType.TThreadedServer;
446 }
447 else if (args[i] == "--threadpool" || args[i] == "--server-type=threadpool")
448 {
449 serverType = ServerType.TThreadPoolServer;
450 }
451 else if (args[i] == "--prototype" || args[i] == "--processor=prototype")
452 {
453 processorFactoryType = ProcessorFactoryType.TPrototypeProcessorFactory;
454 }
Jens Geyerd5436f52014-10-03 19:50:38 +0200455 else if (args[i] == "--ssl")
456 {
457 useEncryption = true;
458 }
459 }
David Reiss63191332009-01-06 19:49:22 +0000460
Jens Geyerd5436f52014-10-03 19:50:38 +0200461 // Transport
462 TServerTransport trans;
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000463 if (pipe != null)
Jens Geyerd5436f52014-10-03 19:50:38 +0200464 {
465 trans = new TNamedPipeServerTransport(pipe);
466 }
467 else
468 {
469 if (useEncryption)
470 {
Nobuaki Sukegawa88c5ee72016-09-04 18:49:18 +0900471 string certPath = "../keys/server.p12";
Nobuaki Sukegawa474ddbd2016-02-17 23:44:27 +0900472 trans = new TTLSServerSocket(port, 0, useBufferedSockets, new X509Certificate2(certPath, "thrift"), null, null, SslProtocols.Tls);
Jens Geyerd5436f52014-10-03 19:50:38 +0200473 }
474 else
475 {
476 trans = new TServerSocket(port, 0, useBufferedSockets);
477 }
478 }
David Reiss63191332009-01-06 19:49:22 +0000479
Jens Geyerd5436f52014-10-03 19:50:38 +0200480 TProtocolFactory proto;
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000481 if (compact)
Jens Geyerd5436f52014-10-03 19:50:38 +0200482 proto = new TCompactProtocol.Factory();
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000483 else if (json)
Jens Geyerd5436f52014-10-03 19:50:38 +0200484 proto = new TJSONProtocol.Factory();
485 else
486 proto = new TBinaryProtocol.Factory();
David Reiss63191332009-01-06 19:49:22 +0000487
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000488 TProcessorFactory processorFactory;
489 if (processorFactoryType == ProcessorFactoryType.TPrototypeProcessorFactory)
490 {
491 processorFactory = new TPrototypeProcessorFactory<ThriftTest.Processor, TestHandler>();
492 }
Jens Geyerd5436f52014-10-03 19:50:38 +0200493 else
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000494 {
495 // Processor
496 TestHandler testHandler = new TestHandler();
497 ThriftTest.Processor testProcessor = new ThriftTest.Processor(testHandler);
498 processorFactory = new TSingletonProcessorFactory(testProcessor);
499 }
David Reissd831a212009-02-13 03:09:52 +0000500
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000501 TTransportFactory transFactory;
502 if (useFramed)
503 transFactory = new TFramedTransport.Factory();
504 else
505 transFactory = new TTransportFactory();
Jens Geyerd5436f52014-10-03 19:50:38 +0200506
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000507 TServer serverEngine;
508 switch (serverType)
509 {
510 case ServerType.TThreadPoolServer:
511 serverEngine = new TThreadPoolServer(processorFactory, trans, transFactory, proto);
512 break;
513 case ServerType.TThreadedServer:
514 serverEngine = new TThreadedServer(processorFactory, trans, transFactory, proto);
515 break;
516 default:
517 serverEngine = new TSimpleServer(processorFactory, trans, transFactory, proto);
518 break;
519 }
David Reiss63191332009-01-06 19:49:22 +0000520
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000521 //Server event handler
522 TradeServerEventHandler serverEvents = new TradeServerEventHandler();
523 serverEngine.setEventHandler(serverEvents);
David Reiss63191332009-01-06 19:49:22 +0000524
Jens Geyerd5436f52014-10-03 19:50:38 +0200525 // Run it
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000526 string where = (pipe != null ? "on pipe " + pipe : "on port " + port);
527 Console.WriteLine("Starting the " + serverType.ToString() + " " + where +
528 (processorFactoryType == ProcessorFactoryType.TPrototypeProcessorFactory ? " with processor prototype factory " : "") +
Jens Geyerd5436f52014-10-03 19:50:38 +0200529 (useBufferedSockets ? " with buffered socket" : "") +
530 (useFramed ? " with framed transport" : "") +
531 (useEncryption ? " with encryption" : "") +
532 (compact ? " with compact protocol" : "") +
533 (json ? " with json protocol" : "") +
534 "...");
535 serverEngine.Serve();
David Reiss63191332009-01-06 19:49:22 +0000536
Jens Geyerd5436f52014-10-03 19:50:38 +0200537 }
538 catch (Exception x)
539 {
540 Console.Error.Write(x);
Roger Meier41ad4342015-03-24 22:30:40 +0100541 return false;
Jens Geyerd5436f52014-10-03 19:50:38 +0200542 }
543 Console.WriteLine("done.");
Roger Meier41ad4342015-03-24 22:30:40 +0100544 return true;
Jens Geyerd5436f52014-10-03 19:50:38 +0200545 }
546 }
David Reiss63191332009-01-06 19:49:22 +0000547}