blob: 4c8fc3b0bd923dd86fa83013f02afb9291f9610f [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;
David Reiss63191332009-01-06 19:49:22 +000035
36namespace Test
37{
Jens Geyerd5436f52014-10-03 19:50:38 +020038 public class TestServer
39 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +000040 public static int _clientID = -1;
41 public delegate void TestLogDelegate(string msg, params object[] values);
42
Randy Abernethy0e86f1f2014-07-13 09:50:19 -070043 public class TradeServerEventHandler : TServerEventHandler
44 {
45 public int callCount = 0;
46 public void preServe()
47 {
48 callCount++;
49 }
50 public Object createContext(Thrift.Protocol.TProtocol input, Thrift.Protocol.TProtocol output)
51 {
52 callCount++;
53 return null;
54 }
55 public void deleteContext(Object serverContext, Thrift.Protocol.TProtocol input, Thrift.Protocol.TProtocol output)
56 {
57 callCount++;
58 }
59 public void processContext(Object serverContext, Thrift.Transport.TTransport transport)
60 {
61 callCount++;
62 }
63 };
64
Jonathan Heard2bfd7df2015-10-28 17:34:27 +000065 public class TestHandler : ThriftTest.Iface, Thrift.TControllingHandler
Jens Geyerd5436f52014-10-03 19:50:38 +020066 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +000067 public TServer server { get; set; }
68 private int handlerID;
69 private StringBuilder reusableStringBuilder = new StringBuilder();
70 private TestLogDelegate testLogDelegate;
David Reiss63191332009-01-06 19:49:22 +000071
Jonathan Heard2bfd7df2015-10-28 17:34:27 +000072 public TestHandler()
73 {
74 handlerID = Interlocked.Increment(ref _clientID);
75 testLogDelegate += testConsoleLogger;
76 testLogDelegate.Invoke("New TestHandler instance created");
77 }
78
79 public void testConsoleLogger(string msg, params object[] values)
80 {
81 reusableStringBuilder.Clear();
82 reusableStringBuilder.AppendFormat("handler{0:D3}:",handlerID);
83 reusableStringBuilder.AppendFormat(msg, values);
84 reusableStringBuilder.AppendLine();
85 Console.Write( reusableStringBuilder.ToString() );
86 }
David Reiss63191332009-01-06 19:49:22 +000087
Jens Geyerd5436f52014-10-03 19:50:38 +020088 public void testVoid()
89 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +000090 testLogDelegate.Invoke("testVoid()");
Jens Geyerd5436f52014-10-03 19:50:38 +020091 }
David Reiss63191332009-01-06 19:49:22 +000092
Jens Geyerd5436f52014-10-03 19:50:38 +020093 public string testString(string thing)
94 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +000095 testLogDelegate.Invoke("testString({0})", thing);
Jens Geyerd5436f52014-10-03 19:50:38 +020096 return thing;
97 }
David Reiss63191332009-01-06 19:49:22 +000098
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +090099 public bool testBool(bool thing)
100 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000101 testLogDelegate.Invoke("testBool({0})", thing);
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900102 return thing;
103 }
104
Jens Geyerd5436f52014-10-03 19:50:38 +0200105 public sbyte testByte(sbyte thing)
106 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000107 testLogDelegate.Invoke("testByte({0})", thing);
Jens Geyerd5436f52014-10-03 19:50:38 +0200108 return thing;
109 }
David Reiss63191332009-01-06 19:49:22 +0000110
Jens Geyerd5436f52014-10-03 19:50:38 +0200111 public int testI32(int thing)
112 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000113 testLogDelegate.Invoke("testI32({0})", thing);
Jens Geyerd5436f52014-10-03 19:50:38 +0200114 return thing;
115 }
David Reiss63191332009-01-06 19:49:22 +0000116
Jens Geyerd5436f52014-10-03 19:50:38 +0200117 public long testI64(long thing)
118 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000119 testLogDelegate.Invoke("testI64({0})", thing);
Jens Geyerd5436f52014-10-03 19:50:38 +0200120 return thing;
121 }
David Reiss63191332009-01-06 19:49:22 +0000122
Jens Geyerd5436f52014-10-03 19:50:38 +0200123 public double testDouble(double thing)
124 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000125 testLogDelegate.Invoke("testDouble({0})", thing);
Jens Geyerd5436f52014-10-03 19:50:38 +0200126 return thing;
127 }
David Reiss63191332009-01-06 19:49:22 +0000128
Jens Geyer71e814a2014-12-13 23:40:35 +0100129 public byte[] testBinary(byte[] thing)
130 {
131 string hex = BitConverter.ToString(thing).Replace("-", string.Empty);
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000132 testLogDelegate.Invoke("testBinary({0:X})", hex);
Jens Geyer71e814a2014-12-13 23:40:35 +0100133 return thing;
134 }
135
Jens Geyerd5436f52014-10-03 19:50:38 +0200136 public Xtruct testStruct(Xtruct thing)
137 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000138 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 +0200139 return thing;
140 }
David Reiss63191332009-01-06 19:49:22 +0000141
Jens Geyerd5436f52014-10-03 19:50:38 +0200142 public Xtruct2 testNest(Xtruct2 nest)
143 {
144 Xtruct thing = nest.Struct_thing;
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000145 testLogDelegate.Invoke("testNest({{{0}, {{\"{1}\", {2}, {3}, {4}, {5}}}}})",
146 nest.Byte_thing,
147 thing.String_thing,
148 thing.Byte_thing,
149 thing.I32_thing,
150 thing.I64_thing,
151 nest.I32_thing);
Jens Geyerd5436f52014-10-03 19:50:38 +0200152 return nest;
153 }
David Reiss63191332009-01-06 19:49:22 +0000154
Jens Geyerd5436f52014-10-03 19:50:38 +0200155 public Dictionary<int, int> testMap(Dictionary<int, int> thing)
156 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000157 reusableStringBuilder.Clear();
158 reusableStringBuilder.Append("testMap({{");
Jens Geyerd5436f52014-10-03 19:50:38 +0200159 bool first = true;
160 foreach (int key in thing.Keys)
161 {
162 if (first)
163 {
164 first = false;
165 }
166 else
167 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000168 reusableStringBuilder.Append(", ");
Jens Geyerd5436f52014-10-03 19:50:38 +0200169 }
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000170 reusableStringBuilder.AppendFormat("{0} => {1}", key, thing[key]);
Jens Geyerd5436f52014-10-03 19:50:38 +0200171 }
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000172 reusableStringBuilder.Append("}})");
173 testLogDelegate.Invoke(reusableStringBuilder.ToString());
Jens Geyerd5436f52014-10-03 19:50:38 +0200174 return thing;
175 }
Jens Geyer1c99e702014-03-17 22:50:39 +0200176
Jens Geyerd5436f52014-10-03 19:50:38 +0200177 public Dictionary<string, string> testStringMap(Dictionary<string, string> thing)
178 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000179 reusableStringBuilder.Clear();
180 reusableStringBuilder.Append("testStringMap({{");
Jens Geyerd5436f52014-10-03 19:50:38 +0200181 bool first = true;
182 foreach (string key in thing.Keys)
183 {
184 if (first)
185 {
186 first = false;
187 }
188 else
189 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000190 reusableStringBuilder.Append(", ");
Jens Geyerd5436f52014-10-03 19:50:38 +0200191 }
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000192 reusableStringBuilder.AppendFormat("{0} => {1}", key, thing[key]);
Jens Geyerd5436f52014-10-03 19:50:38 +0200193 }
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000194 reusableStringBuilder.Append("}})");
195 testLogDelegate.Invoke(reusableStringBuilder.ToString());
Jens Geyerd5436f52014-10-03 19:50:38 +0200196 return thing;
197 }
David Reiss63191332009-01-06 19:49:22 +0000198
Jens Geyerd5436f52014-10-03 19:50:38 +0200199 public THashSet<int> testSet(THashSet<int> thing)
200 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000201 reusableStringBuilder.Clear();
202 reusableStringBuilder.Append("testSet({{");
Jens Geyerd5436f52014-10-03 19:50:38 +0200203 bool first = true;
204 foreach (int elem in thing)
205 {
206 if (first)
207 {
208 first = false;
209 }
210 else
211 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000212 reusableStringBuilder.Append(", ");
Jens Geyerd5436f52014-10-03 19:50:38 +0200213 }
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000214 reusableStringBuilder.AppendFormat("{0}", elem);
Jens Geyerd5436f52014-10-03 19:50:38 +0200215 }
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000216 reusableStringBuilder.Append("}})");
217 testLogDelegate.Invoke(reusableStringBuilder.ToString());
Jens Geyerd5436f52014-10-03 19:50:38 +0200218 return thing;
219 }
David Reiss63191332009-01-06 19:49:22 +0000220
Jens Geyerd5436f52014-10-03 19:50:38 +0200221 public List<int> testList(List<int> thing)
222 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000223 reusableStringBuilder.Clear();
224 reusableStringBuilder.Append("testList({{");
Jens Geyerd5436f52014-10-03 19:50:38 +0200225 bool first = true;
226 foreach (int elem in thing)
227 {
228 if (first)
229 {
230 first = false;
231 }
232 else
233 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000234 reusableStringBuilder.Append(", ");
Jens Geyerd5436f52014-10-03 19:50:38 +0200235 }
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000236 reusableStringBuilder.AppendFormat("{0}", elem);
Jens Geyerd5436f52014-10-03 19:50:38 +0200237 }
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000238 reusableStringBuilder.Append("}})");
239 testLogDelegate.Invoke(reusableStringBuilder.ToString());
Jens Geyerd5436f52014-10-03 19:50:38 +0200240 return thing;
241 }
David Reiss63191332009-01-06 19:49:22 +0000242
Jens Geyerd5436f52014-10-03 19:50:38 +0200243 public Numberz testEnum(Numberz thing)
244 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000245 testLogDelegate.Invoke("testEnum({0})", thing);
Jens Geyerd5436f52014-10-03 19:50:38 +0200246 return thing;
247 }
David Reiss63191332009-01-06 19:49:22 +0000248
Jens Geyerd5436f52014-10-03 19:50:38 +0200249 public long testTypedef(long thing)
250 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000251 testLogDelegate.Invoke("testTypedef({0})", thing);
Jens Geyerd5436f52014-10-03 19:50:38 +0200252 return thing;
253 }
David Reiss63191332009-01-06 19:49:22 +0000254
Jens Geyerd5436f52014-10-03 19:50:38 +0200255 public Dictionary<int, Dictionary<int, int>> testMapMap(int hello)
256 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000257 testLogDelegate.Invoke("testMapMap({0})", hello);
Jens Geyerd5436f52014-10-03 19:50:38 +0200258 Dictionary<int, Dictionary<int, int>> mapmap =
259 new Dictionary<int, Dictionary<int, int>>();
David Reiss63191332009-01-06 19:49:22 +0000260
Jens Geyerd5436f52014-10-03 19:50:38 +0200261 Dictionary<int, int> pos = new Dictionary<int, int>();
262 Dictionary<int, int> neg = new Dictionary<int, int>();
263 for (int i = 1; i < 5; i++)
264 {
265 pos[i] = i;
266 neg[-i] = -i;
267 }
David Reiss63191332009-01-06 19:49:22 +0000268
Jens Geyerd5436f52014-10-03 19:50:38 +0200269 mapmap[4] = pos;
270 mapmap[-4] = neg;
David Reiss63191332009-01-06 19:49:22 +0000271
Jens Geyerd5436f52014-10-03 19:50:38 +0200272 return mapmap;
273 }
David Reiss63191332009-01-06 19:49:22 +0000274
Jens Geyerd5436f52014-10-03 19:50:38 +0200275 public Dictionary<long, Dictionary<Numberz, Insanity>> testInsanity(Insanity argument)
276 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000277 testLogDelegate.Invoke("testInsanity()");
David Reiss63191332009-01-06 19:49:22 +0000278
Jens Geyerd5436f52014-10-03 19:50:38 +0200279 Xtruct hello = new Xtruct();
280 hello.String_thing = "Hello2";
281 hello.Byte_thing = 2;
282 hello.I32_thing = 2;
283 hello.I64_thing = 2;
David Reiss63191332009-01-06 19:49:22 +0000284
Jens Geyerd5436f52014-10-03 19:50:38 +0200285 Xtruct goodbye = new Xtruct();
286 goodbye.String_thing = "Goodbye4";
287 goodbye.Byte_thing = (sbyte)4;
288 goodbye.I32_thing = 4;
289 goodbye.I64_thing = (long)4;
David Reiss63191332009-01-06 19:49:22 +0000290
Jens Geyerd5436f52014-10-03 19:50:38 +0200291 Insanity crazy = new Insanity();
292 crazy.UserMap = new Dictionary<Numberz, long>();
293 crazy.UserMap[Numberz.EIGHT] = (long)8;
294 crazy.Xtructs = new List<Xtruct>();
295 crazy.Xtructs.Add(goodbye);
David Reiss63191332009-01-06 19:49:22 +0000296
Jens Geyerd5436f52014-10-03 19:50:38 +0200297 Insanity looney = new Insanity();
298 crazy.UserMap[Numberz.FIVE] = (long)5;
299 crazy.Xtructs.Add(hello);
David Reiss63191332009-01-06 19:49:22 +0000300
Jens Geyerd5436f52014-10-03 19:50:38 +0200301 Dictionary<Numberz, Insanity> first_map = new Dictionary<Numberz, Insanity>();
302 Dictionary<Numberz, Insanity> second_map = new Dictionary<Numberz, Insanity>(); ;
David Reiss63191332009-01-06 19:49:22 +0000303
Jens Geyerd5436f52014-10-03 19:50:38 +0200304 first_map[Numberz.TWO] = crazy;
305 first_map[Numberz.THREE] = crazy;
David Reiss63191332009-01-06 19:49:22 +0000306
Jens Geyerd5436f52014-10-03 19:50:38 +0200307 second_map[Numberz.SIX] = looney;
David Reiss63191332009-01-06 19:49:22 +0000308
Jens Geyerd5436f52014-10-03 19:50:38 +0200309 Dictionary<long, Dictionary<Numberz, Insanity>> insane =
310 new Dictionary<long, Dictionary<Numberz, Insanity>>();
311 insane[(long)1] = first_map;
312 insane[(long)2] = second_map;
David Reiss63191332009-01-06 19:49:22 +0000313
Jens Geyerd5436f52014-10-03 19:50:38 +0200314 return insane;
315 }
David Reiss63191332009-01-06 19:49:22 +0000316
Jens Geyerd5436f52014-10-03 19:50:38 +0200317 public Xtruct testMulti(sbyte arg0, int arg1, long arg2, Dictionary<short, string> arg3, Numberz arg4, long arg5)
318 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000319 testLogDelegate.Invoke("testMulti()");
David Reiss63191332009-01-06 19:49:22 +0000320
Jens Geyerd5436f52014-10-03 19:50:38 +0200321 Xtruct hello = new Xtruct(); ;
322 hello.String_thing = "Hello2";
323 hello.Byte_thing = arg0;
324 hello.I32_thing = arg1;
325 hello.I64_thing = arg2;
326 return hello;
327 }
David Reiss63191332009-01-06 19:49:22 +0000328
Jens Geyer8b14d172015-02-26 19:36:28 +0100329 /**
330 * Print 'testException(%s)' with arg as '%s'
331 * @param string arg - a string indication what type of exception to throw
332 * if arg == "Xception" throw Xception with errorCode = 1001 and message = arg
333 * elsen if arg == "TException" throw TException
334 * else do not throw anything
335 */
Jens Geyerd5436f52014-10-03 19:50:38 +0200336 public void testException(string arg)
337 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000338 testLogDelegate.Invoke("testException({0})", arg);
Jens Geyerd5436f52014-10-03 19:50:38 +0200339 if (arg == "Xception")
340 {
341 Xception x = new Xception();
342 x.ErrorCode = 1001;
Jens Geyer8b14d172015-02-26 19:36:28 +0100343 x.Message = arg;
Jens Geyerd5436f52014-10-03 19:50:38 +0200344 throw x;
345 }
Jens Geyer8b14d172015-02-26 19:36:28 +0100346 if (arg == "TException")
347 {
348 throw new Thrift.TException();
349 }
Jens Geyerd5436f52014-10-03 19:50:38 +0200350 return;
351 }
David Reiss63191332009-01-06 19:49:22 +0000352
Jens Geyerd5436f52014-10-03 19:50:38 +0200353 public Xtruct testMultiException(string arg0, string arg1)
354 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000355 testLogDelegate.Invoke("testMultiException({0}, {1})", arg0,arg1);
Jens Geyerd5436f52014-10-03 19:50:38 +0200356 if (arg0 == "Xception")
357 {
358 Xception x = new Xception();
359 x.ErrorCode = 1001;
360 x.Message = "This is an Xception";
361 throw x;
362 }
363 else if (arg0 == "Xception2")
364 {
365 Xception2 x = new Xception2();
366 x.ErrorCode = 2002;
367 x.Struct_thing = new Xtruct();
368 x.Struct_thing.String_thing = "This is an Xception2";
369 throw x;
370 }
David Reiss63191332009-01-06 19:49:22 +0000371
Jens Geyerd5436f52014-10-03 19:50:38 +0200372 Xtruct result = new Xtruct();
373 result.String_thing = arg1;
374 return result;
375 }
David Reiss63191332009-01-06 19:49:22 +0000376
Jens Geyerd5436f52014-10-03 19:50:38 +0200377 public void testStop()
378 {
379 if (server != null)
380 {
381 server.Stop();
382 }
383 }
David Reiss63191332009-01-06 19:49:22 +0000384
Jens Geyerd5436f52014-10-03 19:50:38 +0200385 public void testOneway(int arg)
386 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000387 testLogDelegate.Invoke("testOneway({0}), sleeping...", arg);
Jens Geyerd5436f52014-10-03 19:50:38 +0200388 System.Threading.Thread.Sleep(arg * 1000);
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000389 testLogDelegate.Invoke("testOneway finished");
Jens Geyerd5436f52014-10-03 19:50:38 +0200390 }
David Reiss63191332009-01-06 19:49:22 +0000391
Jens Geyerd5436f52014-10-03 19:50:38 +0200392 } // class TestHandler
David Reiss63191332009-01-06 19:49:22 +0000393
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000394 private enum ServerType
395 {
396 TSimpleServer,
397 TThreadedServer,
398 TThreadPoolServer,
399 }
400
401 private enum ProcessorFactoryType
402 {
403 TSingletonProcessorFactory,
404 TPrototypeProcessorFactory,
405 }
406
Roger Meier41ad4342015-03-24 22:30:40 +0100407 public static bool Execute(string[] args)
Jens Geyerd5436f52014-10-03 19:50:38 +0200408 {
409 try
410 {
411 bool useBufferedSockets = false, useFramed = false, useEncryption = false, compact = false, json = false;
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000412 ServerType serverType = ServerType.TSimpleServer;
413 ProcessorFactoryType processorFactoryType = ProcessorFactoryType.TSingletonProcessorFactory;
Jens Geyerd5436f52014-10-03 19:50:38 +0200414 int port = 9090;
415 string pipe = null;
416 for (int i = 0; i < args.Length; i++)
417 {
418 if (args[i] == "-pipe") // -pipe name
419 {
420 pipe = args[++i];
421 }
422 else if (args[i].Contains("--port="))
423 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000424 port = int.Parse(args[i].Substring(args[i].IndexOf("=") + 1));
Jens Geyerd5436f52014-10-03 19:50:38 +0200425 }
426 else if (args[i] == "-b" || args[i] == "--buffered" || args[i] == "--transport=buffered")
427 {
428 useBufferedSockets = true;
429 }
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000430 else if (args[i] == "-f" || args[i] == "--framed" || args[i] == "--transport=framed")
Jens Geyerd5436f52014-10-03 19:50:38 +0200431 {
432 useFramed = true;
433 }
434 else if (args[i] == "--compact" || args[i] == "--protocol=compact")
435 {
436 compact = true;
437 }
438 else if (args[i] == "--json" || args[i] == "--protocol=json")
439 {
440 json = true;
441 }
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000442 else if (args[i] == "--threaded" || args[i] == "--server-type=threaded")
443 {
444 serverType = ServerType.TThreadedServer;
445 }
446 else if (args[i] == "--threadpool" || args[i] == "--server-type=threadpool")
447 {
448 serverType = ServerType.TThreadPoolServer;
449 }
450 else if (args[i] == "--prototype" || args[i] == "--processor=prototype")
451 {
452 processorFactoryType = ProcessorFactoryType.TPrototypeProcessorFactory;
453 }
Jens Geyerd5436f52014-10-03 19:50:38 +0200454 else if (args[i] == "--ssl")
455 {
456 useEncryption = true;
457 }
458 }
David Reiss63191332009-01-06 19:49:22 +0000459
Jens Geyerd5436f52014-10-03 19:50:38 +0200460 // Transport
461 TServerTransport trans;
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000462 if (pipe != null)
Jens Geyerd5436f52014-10-03 19:50:38 +0200463 {
464 trans = new TNamedPipeServerTransport(pipe);
465 }
466 else
467 {
468 if (useEncryption)
469 {
Jens Geyer178b8132015-09-30 23:16:45 +0200470 string certPath = "../../../../test/keys/server.p12";
471 trans = new TTLSServerSocket(port, 0, useBufferedSockets, new X509Certificate2(certPath, "thrift"));
Jens Geyerd5436f52014-10-03 19:50:38 +0200472 }
473 else
474 {
475 trans = new TServerSocket(port, 0, useBufferedSockets);
476 }
477 }
David Reiss63191332009-01-06 19:49:22 +0000478
Jens Geyerd5436f52014-10-03 19:50:38 +0200479 TProtocolFactory proto;
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000480 if (compact)
Jens Geyerd5436f52014-10-03 19:50:38 +0200481 proto = new TCompactProtocol.Factory();
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000482 else if (json)
Jens Geyerd5436f52014-10-03 19:50:38 +0200483 proto = new TJSONProtocol.Factory();
484 else
485 proto = new TBinaryProtocol.Factory();
David Reiss63191332009-01-06 19:49:22 +0000486
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000487 TProcessorFactory processorFactory;
488 if (processorFactoryType == ProcessorFactoryType.TPrototypeProcessorFactory)
489 {
490 processorFactory = new TPrototypeProcessorFactory<ThriftTest.Processor, TestHandler>();
491 }
Jens Geyerd5436f52014-10-03 19:50:38 +0200492 else
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000493 {
494 // Processor
495 TestHandler testHandler = new TestHandler();
496 ThriftTest.Processor testProcessor = new ThriftTest.Processor(testHandler);
497 processorFactory = new TSingletonProcessorFactory(testProcessor);
498 }
David Reissd831a212009-02-13 03:09:52 +0000499
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000500 TTransportFactory transFactory;
501 if (useFramed)
502 transFactory = new TFramedTransport.Factory();
503 else
504 transFactory = new TTransportFactory();
Jens Geyerd5436f52014-10-03 19:50:38 +0200505
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000506 TServer serverEngine;
507 switch (serverType)
508 {
509 case ServerType.TThreadPoolServer:
510 serverEngine = new TThreadPoolServer(processorFactory, trans, transFactory, proto);
511 break;
512 case ServerType.TThreadedServer:
513 serverEngine = new TThreadedServer(processorFactory, trans, transFactory, proto);
514 break;
515 default:
516 serverEngine = new TSimpleServer(processorFactory, trans, transFactory, proto);
517 break;
518 }
David Reiss63191332009-01-06 19:49:22 +0000519
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000520 //Server event handler
521 TradeServerEventHandler serverEvents = new TradeServerEventHandler();
522 serverEngine.setEventHandler(serverEvents);
David Reiss63191332009-01-06 19:49:22 +0000523
Jens Geyerd5436f52014-10-03 19:50:38 +0200524 // Run it
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000525 string where = (pipe != null ? "on pipe " + pipe : "on port " + port);
526 Console.WriteLine("Starting the " + serverType.ToString() + " " + where +
527 (processorFactoryType == ProcessorFactoryType.TPrototypeProcessorFactory ? " with processor prototype factory " : "") +
Jens Geyerd5436f52014-10-03 19:50:38 +0200528 (useBufferedSockets ? " with buffered socket" : "") +
529 (useFramed ? " with framed transport" : "") +
530 (useEncryption ? " with encryption" : "") +
531 (compact ? " with compact protocol" : "") +
532 (json ? " with json protocol" : "") +
533 "...");
534 serverEngine.Serve();
David Reiss63191332009-01-06 19:49:22 +0000535
Jens Geyerd5436f52014-10-03 19:50:38 +0200536 }
537 catch (Exception x)
538 {
539 Console.Error.Write(x);
Roger Meier41ad4342015-03-24 22:30:40 +0100540 return false;
Jens Geyerd5436f52014-10-03 19:50:38 +0200541 }
542 Console.WriteLine("done.");
Roger Meier41ad4342015-03-24 22:30:40 +0100543 return true;
Jens Geyerd5436f52014-10-03 19:50:38 +0200544 }
545 }
David Reiss63191332009-01-06 19:49:22 +0000546}