blob: d944e7280d9f5c55af78a62b28d5ef043df36239 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
David Reiss63191332009-01-06 19:49:22 +000020// Distributed under the Thrift Software License
21//
22// See accompanying file LICENSE or visit the Thrift site at:
23// http://developers.facebook.com/thrift/
24using System;
25using System.Collections.Generic;
Jens Geyerc1d79432014-04-22 22:52:43 +020026using System.Security.Cryptography.X509Certificates;
David Reissd831a212009-02-13 03:09:52 +000027using Thrift.Collections;
David Reiss63191332009-01-06 19:49:22 +000028using Thrift.Test; //generated code
David Reiss63191332009-01-06 19:49:22 +000029using Thrift.Transport;
30using Thrift.Protocol;
31using Thrift.Server;
32
33namespace Test
34{
Jens Geyerd5436f52014-10-03 19:50:38 +020035 public class TestServer
36 {
Randy Abernethy0e86f1f2014-07-13 09:50:19 -070037 public class TradeServerEventHandler : TServerEventHandler
38 {
39 public int callCount = 0;
40 public void preServe()
41 {
42 callCount++;
43 }
44 public Object createContext(Thrift.Protocol.TProtocol input, Thrift.Protocol.TProtocol output)
45 {
46 callCount++;
47 return null;
48 }
49 public void deleteContext(Object serverContext, Thrift.Protocol.TProtocol input, Thrift.Protocol.TProtocol output)
50 {
51 callCount++;
52 }
53 public void processContext(Object serverContext, Thrift.Transport.TTransport transport)
54 {
55 callCount++;
56 }
57 };
58
Jens Geyerd5436f52014-10-03 19:50:38 +020059 public class TestHandler : ThriftTest.Iface
60 {
61 public TServer server;
David Reiss63191332009-01-06 19:49:22 +000062
Jens Geyerd5436f52014-10-03 19:50:38 +020063 public TestHandler() { }
David Reiss63191332009-01-06 19:49:22 +000064
Jens Geyerd5436f52014-10-03 19:50:38 +020065 public void testVoid()
66 {
67 Console.WriteLine("testVoid()");
68 }
David Reiss63191332009-01-06 19:49:22 +000069
Jens Geyerd5436f52014-10-03 19:50:38 +020070 public string testString(string thing)
71 {
72 Console.WriteLine("teststring(\"" + thing + "\")");
73 return thing;
74 }
David Reiss63191332009-01-06 19:49:22 +000075
Jens Geyerd5436f52014-10-03 19:50:38 +020076 public sbyte testByte(sbyte thing)
77 {
78 Console.WriteLine("testByte(" + thing + ")");
79 return thing;
80 }
David Reiss63191332009-01-06 19:49:22 +000081
Jens Geyerd5436f52014-10-03 19:50:38 +020082 public int testI32(int thing)
83 {
84 Console.WriteLine("testI32(" + thing + ")");
85 return thing;
86 }
David Reiss63191332009-01-06 19:49:22 +000087
Jens Geyerd5436f52014-10-03 19:50:38 +020088 public long testI64(long thing)
89 {
90 Console.WriteLine("testI64(" + thing + ")");
91 return thing;
92 }
David Reiss63191332009-01-06 19:49:22 +000093
Jens Geyerd5436f52014-10-03 19:50:38 +020094 public double testDouble(double thing)
95 {
96 Console.WriteLine("testDouble(" + thing + ")");
97 return thing;
98 }
David Reiss63191332009-01-06 19:49:22 +000099
Jens Geyerd5436f52014-10-03 19:50:38 +0200100 public Xtruct testStruct(Xtruct thing)
101 {
102 Console.WriteLine("testStruct({" +
103 "\"" + thing.String_thing + "\", " +
104 thing.Byte_thing + ", " +
105 thing.I32_thing + ", " +
106 thing.I64_thing + "})");
107 return thing;
108 }
David Reiss63191332009-01-06 19:49:22 +0000109
Jens Geyerd5436f52014-10-03 19:50:38 +0200110 public Xtruct2 testNest(Xtruct2 nest)
111 {
112 Xtruct thing = nest.Struct_thing;
113 Console.WriteLine("testNest({" +
114 nest.Byte_thing + ", {" +
115 "\"" + thing.String_thing + "\", " +
116 thing.Byte_thing + ", " +
117 thing.I32_thing + ", " +
118 thing.I64_thing + "}, " +
119 nest.I32_thing + "})");
120 return nest;
121 }
David Reiss63191332009-01-06 19:49:22 +0000122
Jens Geyerd5436f52014-10-03 19:50:38 +0200123 public Dictionary<int, int> testMap(Dictionary<int, int> thing)
124 {
125 Console.WriteLine("testMap({");
126 bool first = true;
127 foreach (int key in thing.Keys)
128 {
129 if (first)
130 {
131 first = false;
132 }
133 else
134 {
135 Console.WriteLine(", ");
136 }
137 Console.WriteLine(key + " => " + thing[key]);
138 }
139 Console.WriteLine("})");
140 return thing;
141 }
Jens Geyer1c99e702014-03-17 22:50:39 +0200142
Jens Geyerd5436f52014-10-03 19:50:38 +0200143 public Dictionary<string, string> testStringMap(Dictionary<string, string> thing)
144 {
145 Console.WriteLine("testStringMap({");
146 bool first = true;
147 foreach (string key in thing.Keys)
148 {
149 if (first)
150 {
151 first = false;
152 }
153 else
154 {
155 Console.WriteLine(", ");
156 }
157 Console.WriteLine(key + " => " + thing[key]);
158 }
159 Console.WriteLine("})");
160 return thing;
161 }
David Reiss63191332009-01-06 19:49:22 +0000162
Jens Geyerd5436f52014-10-03 19:50:38 +0200163 public THashSet<int> testSet(THashSet<int> thing)
164 {
165 Console.WriteLine("testSet({");
166 bool first = true;
167 foreach (int elem in thing)
168 {
169 if (first)
170 {
171 first = false;
172 }
173 else
174 {
175 Console.WriteLine(", ");
176 }
177 Console.WriteLine(elem);
178 }
179 Console.WriteLine("})");
180 return thing;
181 }
David Reiss63191332009-01-06 19:49:22 +0000182
Jens Geyerd5436f52014-10-03 19:50:38 +0200183 public List<int> testList(List<int> thing)
184 {
185 Console.WriteLine("testList({");
186 bool first = true;
187 foreach (int elem in thing)
188 {
189 if (first)
190 {
191 first = false;
192 }
193 else
194 {
195 Console.WriteLine(", ");
196 }
197 Console.WriteLine(elem);
198 }
199 Console.WriteLine("})");
200 return thing;
201 }
David Reiss63191332009-01-06 19:49:22 +0000202
Jens Geyerd5436f52014-10-03 19:50:38 +0200203 public Numberz testEnum(Numberz thing)
204 {
205 Console.WriteLine("testEnum(" + thing + ")");
206 return thing;
207 }
David Reiss63191332009-01-06 19:49:22 +0000208
Jens Geyerd5436f52014-10-03 19:50:38 +0200209 public long testTypedef(long thing)
210 {
211 Console.WriteLine("testTypedef(" + thing + ")");
212 return thing;
213 }
David Reiss63191332009-01-06 19:49:22 +0000214
Jens Geyerd5436f52014-10-03 19:50:38 +0200215 public Dictionary<int, Dictionary<int, int>> testMapMap(int hello)
216 {
217 Console.WriteLine("testMapMap(" + hello + ")");
218 Dictionary<int, Dictionary<int, int>> mapmap =
219 new Dictionary<int, Dictionary<int, int>>();
David Reiss63191332009-01-06 19:49:22 +0000220
Jens Geyerd5436f52014-10-03 19:50:38 +0200221 Dictionary<int, int> pos = new Dictionary<int, int>();
222 Dictionary<int, int> neg = new Dictionary<int, int>();
223 for (int i = 1; i < 5; i++)
224 {
225 pos[i] = i;
226 neg[-i] = -i;
227 }
David Reiss63191332009-01-06 19:49:22 +0000228
Jens Geyerd5436f52014-10-03 19:50:38 +0200229 mapmap[4] = pos;
230 mapmap[-4] = neg;
David Reiss63191332009-01-06 19:49:22 +0000231
Jens Geyerd5436f52014-10-03 19:50:38 +0200232 return mapmap;
233 }
David Reiss63191332009-01-06 19:49:22 +0000234
Jens Geyerd5436f52014-10-03 19:50:38 +0200235 public Dictionary<long, Dictionary<Numberz, Insanity>> testInsanity(Insanity argument)
236 {
237 Console.WriteLine("testInsanity()");
David Reiss63191332009-01-06 19:49:22 +0000238
Jens Geyerd5436f52014-10-03 19:50:38 +0200239 Xtruct hello = new Xtruct();
240 hello.String_thing = "Hello2";
241 hello.Byte_thing = 2;
242 hello.I32_thing = 2;
243 hello.I64_thing = 2;
David Reiss63191332009-01-06 19:49:22 +0000244
Jens Geyerd5436f52014-10-03 19:50:38 +0200245 Xtruct goodbye = new Xtruct();
246 goodbye.String_thing = "Goodbye4";
247 goodbye.Byte_thing = (sbyte)4;
248 goodbye.I32_thing = 4;
249 goodbye.I64_thing = (long)4;
David Reiss63191332009-01-06 19:49:22 +0000250
Jens Geyerd5436f52014-10-03 19:50:38 +0200251 Insanity crazy = new Insanity();
252 crazy.UserMap = new Dictionary<Numberz, long>();
253 crazy.UserMap[Numberz.EIGHT] = (long)8;
254 crazy.Xtructs = new List<Xtruct>();
255 crazy.Xtructs.Add(goodbye);
David Reiss63191332009-01-06 19:49:22 +0000256
Jens Geyerd5436f52014-10-03 19:50:38 +0200257 Insanity looney = new Insanity();
258 crazy.UserMap[Numberz.FIVE] = (long)5;
259 crazy.Xtructs.Add(hello);
David Reiss63191332009-01-06 19:49:22 +0000260
Jens Geyerd5436f52014-10-03 19:50:38 +0200261 Dictionary<Numberz, Insanity> first_map = new Dictionary<Numberz, Insanity>();
262 Dictionary<Numberz, Insanity> second_map = new Dictionary<Numberz, Insanity>(); ;
David Reiss63191332009-01-06 19:49:22 +0000263
Jens Geyerd5436f52014-10-03 19:50:38 +0200264 first_map[Numberz.TWO] = crazy;
265 first_map[Numberz.THREE] = crazy;
David Reiss63191332009-01-06 19:49:22 +0000266
Jens Geyerd5436f52014-10-03 19:50:38 +0200267 second_map[Numberz.SIX] = looney;
David Reiss63191332009-01-06 19:49:22 +0000268
Jens Geyerd5436f52014-10-03 19:50:38 +0200269 Dictionary<long, Dictionary<Numberz, Insanity>> insane =
270 new Dictionary<long, Dictionary<Numberz, Insanity>>();
271 insane[(long)1] = first_map;
272 insane[(long)2] = second_map;
David Reiss63191332009-01-06 19:49:22 +0000273
Jens Geyerd5436f52014-10-03 19:50:38 +0200274 return insane;
275 }
David Reiss63191332009-01-06 19:49:22 +0000276
Jens Geyerd5436f52014-10-03 19:50:38 +0200277 public Xtruct testMulti(sbyte arg0, int arg1, long arg2, Dictionary<short, string> arg3, Numberz arg4, long arg5)
278 {
279 Console.WriteLine("testMulti()");
David Reiss63191332009-01-06 19:49:22 +0000280
Jens Geyerd5436f52014-10-03 19:50:38 +0200281 Xtruct hello = new Xtruct(); ;
282 hello.String_thing = "Hello2";
283 hello.Byte_thing = arg0;
284 hello.I32_thing = arg1;
285 hello.I64_thing = arg2;
286 return hello;
287 }
David Reiss63191332009-01-06 19:49:22 +0000288
Jens Geyerd5436f52014-10-03 19:50:38 +0200289 public void testException(string arg)
290 {
291 Console.WriteLine("testException(" + arg + ")");
292 if (arg == "Xception")
293 {
294 Xception x = new Xception();
295 x.ErrorCode = 1001;
296 x.Message = "This is an Xception";
297 throw x;
298 }
299 return;
300 }
David Reiss63191332009-01-06 19:49:22 +0000301
Jens Geyerd5436f52014-10-03 19:50:38 +0200302 public Xtruct testMultiException(string arg0, string arg1)
303 {
304 Console.WriteLine("testMultiException(" + arg0 + ", " + arg1 + ")");
305 if (arg0 == "Xception")
306 {
307 Xception x = new Xception();
308 x.ErrorCode = 1001;
309 x.Message = "This is an Xception";
310 throw x;
311 }
312 else if (arg0 == "Xception2")
313 {
314 Xception2 x = new Xception2();
315 x.ErrorCode = 2002;
316 x.Struct_thing = new Xtruct();
317 x.Struct_thing.String_thing = "This is an Xception2";
318 throw x;
319 }
David Reiss63191332009-01-06 19:49:22 +0000320
Jens Geyerd5436f52014-10-03 19:50:38 +0200321 Xtruct result = new Xtruct();
322 result.String_thing = arg1;
323 return result;
324 }
David Reiss63191332009-01-06 19:49:22 +0000325
Jens Geyerd5436f52014-10-03 19:50:38 +0200326 public void testStop()
327 {
328 if (server != null)
329 {
330 server.Stop();
331 }
332 }
David Reiss63191332009-01-06 19:49:22 +0000333
Jens Geyerd5436f52014-10-03 19:50:38 +0200334 public void testOneway(int arg)
335 {
336 Console.WriteLine("testOneway(" + arg + "), sleeping...");
337 System.Threading.Thread.Sleep(arg * 1000);
338 Console.WriteLine("testOneway finished");
339 }
David Reiss63191332009-01-06 19:49:22 +0000340
Jens Geyerd5436f52014-10-03 19:50:38 +0200341 } // class TestHandler
David Reiss63191332009-01-06 19:49:22 +0000342
Jens Geyerd5436f52014-10-03 19:50:38 +0200343 public static void Execute(string[] args)
344 {
345 try
346 {
347 bool useBufferedSockets = false, useFramed = false, useEncryption = false, compact = false, json = false;
348 int port = 9090;
349 string pipe = null;
350 for (int i = 0; i < args.Length; i++)
351 {
352 if (args[i] == "-pipe") // -pipe name
353 {
354 pipe = args[++i];
355 }
356 else if (args[i].Contains("--port="))
357 {
358 port = int.Parse(args[i].Substring(args[i].IndexOf("=")+1));
359 }
360 else if (args[i] == "-b" || args[i] == "--buffered" || args[i] == "--transport=buffered")
361 {
362 useBufferedSockets = true;
363 }
364 else if (args[i] == "-f" || args[i] == "--framed" || args[i] == "--transport=framed")
365 {
366 useFramed = true;
367 }
368 else if (args[i] == "--compact" || args[i] == "--protocol=compact")
369 {
370 compact = true;
371 }
372 else if (args[i] == "--json" || args[i] == "--protocol=json")
373 {
374 json = true;
375 }
376 else if (args[i] == "--ssl")
377 {
378 useEncryption = true;
379 }
380 }
David Reiss63191332009-01-06 19:49:22 +0000381
Jens Geyerd5436f52014-10-03 19:50:38 +0200382 // Processor
383 TestHandler testHandler = new TestHandler();
384 ThriftTest.Processor testProcessor = new ThriftTest.Processor(testHandler);
David Reiss63191332009-01-06 19:49:22 +0000385
Jens Geyerd5436f52014-10-03 19:50:38 +0200386 // Transport
387 TServerTransport trans;
388 if( pipe != null)
389 {
390 trans = new TNamedPipeServerTransport(pipe);
391 }
392 else
393 {
394 if (useEncryption)
395 {
396 trans = new TTLSServerSocket(port, 0, useBufferedSockets, new X509Certificate2("../../../../../keys/server.pem"));
397 }
398 else
399 {
400 trans = new TServerSocket(port, 0, useBufferedSockets);
401 }
402 }
David Reiss63191332009-01-06 19:49:22 +0000403
Jens Geyerd5436f52014-10-03 19:50:38 +0200404 TProtocolFactory proto;
405 if ( compact )
406 proto = new TCompactProtocol.Factory();
407 else if ( json )
408 proto = new TJSONProtocol.Factory();
409 else
410 proto = new TBinaryProtocol.Factory();
David Reiss63191332009-01-06 19:49:22 +0000411
Jens Geyerd5436f52014-10-03 19:50:38 +0200412 // Simple Server
413 TServer serverEngine;
414 if ( useFramed )
415 serverEngine = new TSimpleServer(testProcessor, trans, new TFramedTransport.Factory(), proto);
416 else
417 serverEngine = new TSimpleServer(testProcessor, trans, new TTransportFactory(), proto);
David Reissd831a212009-02-13 03:09:52 +0000418
Jens Geyerd5436f52014-10-03 19:50:38 +0200419 // ThreadPool Server
420 // serverEngine = new TThreadPoolServer(testProcessor, tServerSocket);
421
422 // Threaded Server
423 // serverEngine = new TThreadedServer(testProcessor, tServerSocket);
David Reiss63191332009-01-06 19:49:22 +0000424
Randy Abernethy0e86f1f2014-07-13 09:50:19 -0700425 //Server event handler
426 TradeServerEventHandler serverEvents = new TradeServerEventHandler();
427 serverEngine.setEventHandler(serverEvents);
428
Jens Geyerd5436f52014-10-03 19:50:38 +0200429 testHandler.server = serverEngine;
David Reiss63191332009-01-06 19:49:22 +0000430
Jens Geyerd5436f52014-10-03 19:50:38 +0200431 // Run it
432 string where = ( pipe != null ? "on pipe "+pipe : "on port " + port);
433 Console.WriteLine("Starting the server " + where +
434 (useBufferedSockets ? " with buffered socket" : "") +
435 (useFramed ? " with framed transport" : "") +
436 (useEncryption ? " with encryption" : "") +
437 (compact ? " with compact protocol" : "") +
438 (json ? " with json protocol" : "") +
439 "...");
440 serverEngine.Serve();
David Reiss63191332009-01-06 19:49:22 +0000441
Jens Geyerd5436f52014-10-03 19:50:38 +0200442 }
443 catch (Exception x)
444 {
445 Console.Error.Write(x);
446 }
447 Console.WriteLine("done.");
448 }
449 }
David Reiss63191332009-01-06 19:49:22 +0000450}