blob: 615ead8cc5d33c2bbddf0cfac35f2482bd0fe96e [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 Geyer71e814a2014-12-13 23:40:35 +0100100 public byte[] testBinary(byte[] thing)
101 {
102 string hex = BitConverter.ToString(thing).Replace("-", string.Empty);
103 Console.WriteLine("testBinary(" + hex + ")");
104 return thing;
105 }
106
Jens Geyerd5436f52014-10-03 19:50:38 +0200107 public Xtruct testStruct(Xtruct thing)
108 {
109 Console.WriteLine("testStruct({" +
110 "\"" + thing.String_thing + "\", " +
111 thing.Byte_thing + ", " +
112 thing.I32_thing + ", " +
113 thing.I64_thing + "})");
114 return thing;
115 }
David Reiss63191332009-01-06 19:49:22 +0000116
Jens Geyerd5436f52014-10-03 19:50:38 +0200117 public Xtruct2 testNest(Xtruct2 nest)
118 {
119 Xtruct thing = nest.Struct_thing;
120 Console.WriteLine("testNest({" +
121 nest.Byte_thing + ", {" +
122 "\"" + thing.String_thing + "\", " +
123 thing.Byte_thing + ", " +
124 thing.I32_thing + ", " +
125 thing.I64_thing + "}, " +
126 nest.I32_thing + "})");
127 return nest;
128 }
David Reiss63191332009-01-06 19:49:22 +0000129
Jens Geyerd5436f52014-10-03 19:50:38 +0200130 public Dictionary<int, int> testMap(Dictionary<int, int> thing)
131 {
132 Console.WriteLine("testMap({");
133 bool first = true;
134 foreach (int key in thing.Keys)
135 {
136 if (first)
137 {
138 first = false;
139 }
140 else
141 {
142 Console.WriteLine(", ");
143 }
144 Console.WriteLine(key + " => " + thing[key]);
145 }
146 Console.WriteLine("})");
147 return thing;
148 }
Jens Geyer1c99e702014-03-17 22:50:39 +0200149
Jens Geyerd5436f52014-10-03 19:50:38 +0200150 public Dictionary<string, string> testStringMap(Dictionary<string, string> thing)
151 {
152 Console.WriteLine("testStringMap({");
153 bool first = true;
154 foreach (string key in thing.Keys)
155 {
156 if (first)
157 {
158 first = false;
159 }
160 else
161 {
162 Console.WriteLine(", ");
163 }
164 Console.WriteLine(key + " => " + thing[key]);
165 }
166 Console.WriteLine("})");
167 return thing;
168 }
David Reiss63191332009-01-06 19:49:22 +0000169
Jens Geyerd5436f52014-10-03 19:50:38 +0200170 public THashSet<int> testSet(THashSet<int> thing)
171 {
172 Console.WriteLine("testSet({");
173 bool first = true;
174 foreach (int elem in thing)
175 {
176 if (first)
177 {
178 first = false;
179 }
180 else
181 {
182 Console.WriteLine(", ");
183 }
184 Console.WriteLine(elem);
185 }
186 Console.WriteLine("})");
187 return thing;
188 }
David Reiss63191332009-01-06 19:49:22 +0000189
Jens Geyerd5436f52014-10-03 19:50:38 +0200190 public List<int> testList(List<int> thing)
191 {
192 Console.WriteLine("testList({");
193 bool first = true;
194 foreach (int elem in thing)
195 {
196 if (first)
197 {
198 first = false;
199 }
200 else
201 {
202 Console.WriteLine(", ");
203 }
204 Console.WriteLine(elem);
205 }
206 Console.WriteLine("})");
207 return thing;
208 }
David Reiss63191332009-01-06 19:49:22 +0000209
Jens Geyerd5436f52014-10-03 19:50:38 +0200210 public Numberz testEnum(Numberz thing)
211 {
212 Console.WriteLine("testEnum(" + thing + ")");
213 return thing;
214 }
David Reiss63191332009-01-06 19:49:22 +0000215
Jens Geyerd5436f52014-10-03 19:50:38 +0200216 public long testTypedef(long thing)
217 {
218 Console.WriteLine("testTypedef(" + thing + ")");
219 return thing;
220 }
David Reiss63191332009-01-06 19:49:22 +0000221
Jens Geyerd5436f52014-10-03 19:50:38 +0200222 public Dictionary<int, Dictionary<int, int>> testMapMap(int hello)
223 {
224 Console.WriteLine("testMapMap(" + hello + ")");
225 Dictionary<int, Dictionary<int, int>> mapmap =
226 new Dictionary<int, Dictionary<int, int>>();
David Reiss63191332009-01-06 19:49:22 +0000227
Jens Geyerd5436f52014-10-03 19:50:38 +0200228 Dictionary<int, int> pos = new Dictionary<int, int>();
229 Dictionary<int, int> neg = new Dictionary<int, int>();
230 for (int i = 1; i < 5; i++)
231 {
232 pos[i] = i;
233 neg[-i] = -i;
234 }
David Reiss63191332009-01-06 19:49:22 +0000235
Jens Geyerd5436f52014-10-03 19:50:38 +0200236 mapmap[4] = pos;
237 mapmap[-4] = neg;
David Reiss63191332009-01-06 19:49:22 +0000238
Jens Geyerd5436f52014-10-03 19:50:38 +0200239 return mapmap;
240 }
David Reiss63191332009-01-06 19:49:22 +0000241
Jens Geyerd5436f52014-10-03 19:50:38 +0200242 public Dictionary<long, Dictionary<Numberz, Insanity>> testInsanity(Insanity argument)
243 {
244 Console.WriteLine("testInsanity()");
David Reiss63191332009-01-06 19:49:22 +0000245
Jens Geyerd5436f52014-10-03 19:50:38 +0200246 Xtruct hello = new Xtruct();
247 hello.String_thing = "Hello2";
248 hello.Byte_thing = 2;
249 hello.I32_thing = 2;
250 hello.I64_thing = 2;
David Reiss63191332009-01-06 19:49:22 +0000251
Jens Geyerd5436f52014-10-03 19:50:38 +0200252 Xtruct goodbye = new Xtruct();
253 goodbye.String_thing = "Goodbye4";
254 goodbye.Byte_thing = (sbyte)4;
255 goodbye.I32_thing = 4;
256 goodbye.I64_thing = (long)4;
David Reiss63191332009-01-06 19:49:22 +0000257
Jens Geyerd5436f52014-10-03 19:50:38 +0200258 Insanity crazy = new Insanity();
259 crazy.UserMap = new Dictionary<Numberz, long>();
260 crazy.UserMap[Numberz.EIGHT] = (long)8;
261 crazy.Xtructs = new List<Xtruct>();
262 crazy.Xtructs.Add(goodbye);
David Reiss63191332009-01-06 19:49:22 +0000263
Jens Geyerd5436f52014-10-03 19:50:38 +0200264 Insanity looney = new Insanity();
265 crazy.UserMap[Numberz.FIVE] = (long)5;
266 crazy.Xtructs.Add(hello);
David Reiss63191332009-01-06 19:49:22 +0000267
Jens Geyerd5436f52014-10-03 19:50:38 +0200268 Dictionary<Numberz, Insanity> first_map = new Dictionary<Numberz, Insanity>();
269 Dictionary<Numberz, Insanity> second_map = new Dictionary<Numberz, Insanity>(); ;
David Reiss63191332009-01-06 19:49:22 +0000270
Jens Geyerd5436f52014-10-03 19:50:38 +0200271 first_map[Numberz.TWO] = crazy;
272 first_map[Numberz.THREE] = crazy;
David Reiss63191332009-01-06 19:49:22 +0000273
Jens Geyerd5436f52014-10-03 19:50:38 +0200274 second_map[Numberz.SIX] = looney;
David Reiss63191332009-01-06 19:49:22 +0000275
Jens Geyerd5436f52014-10-03 19:50:38 +0200276 Dictionary<long, Dictionary<Numberz, Insanity>> insane =
277 new Dictionary<long, Dictionary<Numberz, Insanity>>();
278 insane[(long)1] = first_map;
279 insane[(long)2] = second_map;
David Reiss63191332009-01-06 19:49:22 +0000280
Jens Geyerd5436f52014-10-03 19:50:38 +0200281 return insane;
282 }
David Reiss63191332009-01-06 19:49:22 +0000283
Jens Geyerd5436f52014-10-03 19:50:38 +0200284 public Xtruct testMulti(sbyte arg0, int arg1, long arg2, Dictionary<short, string> arg3, Numberz arg4, long arg5)
285 {
286 Console.WriteLine("testMulti()");
David Reiss63191332009-01-06 19:49:22 +0000287
Jens Geyerd5436f52014-10-03 19:50:38 +0200288 Xtruct hello = new Xtruct(); ;
289 hello.String_thing = "Hello2";
290 hello.Byte_thing = arg0;
291 hello.I32_thing = arg1;
292 hello.I64_thing = arg2;
293 return hello;
294 }
David Reiss63191332009-01-06 19:49:22 +0000295
Jens Geyerd5436f52014-10-03 19:50:38 +0200296 public void testException(string arg)
297 {
298 Console.WriteLine("testException(" + arg + ")");
299 if (arg == "Xception")
300 {
301 Xception x = new Xception();
302 x.ErrorCode = 1001;
303 x.Message = "This is an Xception";
304 throw x;
305 }
306 return;
307 }
David Reiss63191332009-01-06 19:49:22 +0000308
Jens Geyerd5436f52014-10-03 19:50:38 +0200309 public Xtruct testMultiException(string arg0, string arg1)
310 {
311 Console.WriteLine("testMultiException(" + arg0 + ", " + arg1 + ")");
312 if (arg0 == "Xception")
313 {
314 Xception x = new Xception();
315 x.ErrorCode = 1001;
316 x.Message = "This is an Xception";
317 throw x;
318 }
319 else if (arg0 == "Xception2")
320 {
321 Xception2 x = new Xception2();
322 x.ErrorCode = 2002;
323 x.Struct_thing = new Xtruct();
324 x.Struct_thing.String_thing = "This is an Xception2";
325 throw x;
326 }
David Reiss63191332009-01-06 19:49:22 +0000327
Jens Geyerd5436f52014-10-03 19:50:38 +0200328 Xtruct result = new Xtruct();
329 result.String_thing = arg1;
330 return result;
331 }
David Reiss63191332009-01-06 19:49:22 +0000332
Jens Geyerd5436f52014-10-03 19:50:38 +0200333 public void testStop()
334 {
335 if (server != null)
336 {
337 server.Stop();
338 }
339 }
David Reiss63191332009-01-06 19:49:22 +0000340
Jens Geyerd5436f52014-10-03 19:50:38 +0200341 public void testOneway(int arg)
342 {
343 Console.WriteLine("testOneway(" + arg + "), sleeping...");
344 System.Threading.Thread.Sleep(arg * 1000);
345 Console.WriteLine("testOneway finished");
346 }
David Reiss63191332009-01-06 19:49:22 +0000347
Jens Geyerd5436f52014-10-03 19:50:38 +0200348 } // class TestHandler
David Reiss63191332009-01-06 19:49:22 +0000349
Jens Geyerd5436f52014-10-03 19:50:38 +0200350 public static void Execute(string[] args)
351 {
352 try
353 {
354 bool useBufferedSockets = false, useFramed = false, useEncryption = false, compact = false, json = false;
355 int port = 9090;
356 string pipe = null;
357 for (int i = 0; i < args.Length; i++)
358 {
359 if (args[i] == "-pipe") // -pipe name
360 {
361 pipe = args[++i];
362 }
363 else if (args[i].Contains("--port="))
364 {
365 port = int.Parse(args[i].Substring(args[i].IndexOf("=")+1));
366 }
367 else if (args[i] == "-b" || args[i] == "--buffered" || args[i] == "--transport=buffered")
368 {
369 useBufferedSockets = true;
370 }
371 else if (args[i] == "-f" || args[i] == "--framed" || args[i] == "--transport=framed")
372 {
373 useFramed = true;
374 }
375 else if (args[i] == "--compact" || args[i] == "--protocol=compact")
376 {
377 compact = true;
378 }
379 else if (args[i] == "--json" || args[i] == "--protocol=json")
380 {
381 json = true;
382 }
383 else if (args[i] == "--ssl")
384 {
385 useEncryption = true;
386 }
387 }
David Reiss63191332009-01-06 19:49:22 +0000388
Jens Geyerd5436f52014-10-03 19:50:38 +0200389 // Processor
390 TestHandler testHandler = new TestHandler();
391 ThriftTest.Processor testProcessor = new ThriftTest.Processor(testHandler);
David Reiss63191332009-01-06 19:49:22 +0000392
Jens Geyerd5436f52014-10-03 19:50:38 +0200393 // Transport
394 TServerTransport trans;
395 if( pipe != null)
396 {
397 trans = new TNamedPipeServerTransport(pipe);
398 }
399 else
400 {
401 if (useEncryption)
402 {
403 trans = new TTLSServerSocket(port, 0, useBufferedSockets, new X509Certificate2("../../../../../keys/server.pem"));
404 }
405 else
406 {
407 trans = new TServerSocket(port, 0, useBufferedSockets);
408 }
409 }
David Reiss63191332009-01-06 19:49:22 +0000410
Jens Geyerd5436f52014-10-03 19:50:38 +0200411 TProtocolFactory proto;
412 if ( compact )
413 proto = new TCompactProtocol.Factory();
414 else if ( json )
415 proto = new TJSONProtocol.Factory();
416 else
417 proto = new TBinaryProtocol.Factory();
David Reiss63191332009-01-06 19:49:22 +0000418
Jens Geyerd5436f52014-10-03 19:50:38 +0200419 // Simple Server
420 TServer serverEngine;
421 if ( useFramed )
422 serverEngine = new TSimpleServer(testProcessor, trans, new TFramedTransport.Factory(), proto);
423 else
424 serverEngine = new TSimpleServer(testProcessor, trans, new TTransportFactory(), proto);
David Reissd831a212009-02-13 03:09:52 +0000425
Jens Geyerd5436f52014-10-03 19:50:38 +0200426 // ThreadPool Server
427 // serverEngine = new TThreadPoolServer(testProcessor, tServerSocket);
428
429 // Threaded Server
430 // serverEngine = new TThreadedServer(testProcessor, tServerSocket);
David Reiss63191332009-01-06 19:49:22 +0000431
Randy Abernethy0e86f1f2014-07-13 09:50:19 -0700432 //Server event handler
433 TradeServerEventHandler serverEvents = new TradeServerEventHandler();
434 serverEngine.setEventHandler(serverEvents);
435
Jens Geyerd5436f52014-10-03 19:50:38 +0200436 testHandler.server = serverEngine;
David Reiss63191332009-01-06 19:49:22 +0000437
Jens Geyerd5436f52014-10-03 19:50:38 +0200438 // Run it
439 string where = ( pipe != null ? "on pipe "+pipe : "on port " + port);
440 Console.WriteLine("Starting the server " + where +
441 (useBufferedSockets ? " with buffered socket" : "") +
442 (useFramed ? " with framed transport" : "") +
443 (useEncryption ? " with encryption" : "") +
444 (compact ? " with compact protocol" : "") +
445 (json ? " with json protocol" : "") +
446 "...");
447 serverEngine.Serve();
David Reiss63191332009-01-06 19:49:22 +0000448
Jens Geyerd5436f52014-10-03 19:50:38 +0200449 }
450 catch (Exception x)
451 {
452 Console.Error.Write(x);
453 }
454 Console.WriteLine("done.");
455 }
456 }
David Reiss63191332009-01-06 19:49:22 +0000457}