summaryrefslogtreecommitdiff
path: root/test/csharp/ThriftTest/TestServer.cs
blob: e3706404046e54153daf02ec57bd0ee249fc5019 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

// Distributed under the Thrift Software License
//
// See accompanying file LICENSE or visit the Thrift site at:
// http://developers.facebook.com/thrift/
using System;
using System.Collections.Generic;
using Thrift.Collections;
using Thrift.Test; //generated code
using Thrift.Transport;
using Thrift.Protocol;
using Thrift.Server;

namespace Test
{
	public class TestServer
	{
		public class TestHandler : ThriftTest.Iface
		{
			public TServer server;

			public TestHandler() { }

			public void testVoid()
			{
				Console.WriteLine("testVoid()");
			}

			public string testString(string thing)
			{
				Console.WriteLine("teststring(\"" + thing + "\")");
				return thing;
			}

			public byte testByte(byte thing)
			{
				Console.WriteLine("testByte(" + thing + ")");
				return thing;
			}

			public int testI32(int thing)
			{
				Console.WriteLine("testI32(" + thing + ")");
				return thing;
			}

			public long testI64(long thing)
			{
				Console.WriteLine("testI64(" + thing + ")");
				return thing;
			}

			public double testDouble(double thing)
			{
				Console.WriteLine("testDouble(" + thing + ")");
				return thing;
			}

			public Xtruct testStruct(Xtruct thing)
			{
				Console.WriteLine("testStruct({" +
								 "\"" + thing.String_thing + "\", " +
								 thing.Byte_thing + ", " +
								 thing.I32_thing + ", " +
								 thing.I64_thing + "})");
				return thing;
			}

			public Xtruct2 testNest(Xtruct2 nest)
			{
				Xtruct thing = nest.Struct_thing;
				Console.WriteLine("testNest({" +
								 nest.Byte_thing + ", {" +
								 "\"" + thing.String_thing + "\", " +
								 thing.Byte_thing + ", " +
								 thing.I32_thing + ", " +
								 thing.I64_thing + "}, " +
								 nest.I32_thing + "})");
				return nest;
			}

			public Dictionary<int, int> testMap(Dictionary<int, int> thing)
			{
				Console.WriteLine("testMap({");
				bool first = true;
				foreach (int key in thing.Keys)
				{
					if (first)
					{
						first = false;
					}
					else
					{
						Console.WriteLine(", ");
					}
					Console.WriteLine(key + " => " + thing[key]);
				}
				Console.WriteLine("})");
				return thing;
			}

			public THashSet<int> testSet(THashSet<int> thing)
			{
				Console.WriteLine("testSet({");
				bool first = true;
				foreach (int elem in thing)
				{
					if (first)
					{
						first = false;
					}
					else
					{
						Console.WriteLine(", ");
					}
					Console.WriteLine(elem);
				}
				Console.WriteLine("})");
				return thing;
			}

			public List<int> testList(List<int> thing)
			{
				Console.WriteLine("testList({");
				bool first = true;
				foreach (int elem in thing)
				{
					if (first)
					{
						first = false;
					}
					else
					{
						Console.WriteLine(", ");
					}
					Console.WriteLine(elem);
				}
				Console.WriteLine("})");
				return thing;
			}

			public Numberz testEnum(Numberz thing)
			{
				Console.WriteLine("testEnum(" + thing + ")");
				return thing;
			}

			public long testTypedef(long thing)
			{
				Console.WriteLine("testTypedef(" + thing + ")");
				return thing;
			}

			public Dictionary<int, Dictionary<int, int>> testMapMap(int hello)
			{
				Console.WriteLine("testMapMap(" + hello + ")");
				Dictionary<int, Dictionary<int, int>> mapmap =
				  new Dictionary<int, Dictionary<int, int>>();

				Dictionary<int, int> pos = new Dictionary<int, int>();
				Dictionary<int, int> neg = new Dictionary<int, int>();
				for (int i = 1; i < 5; i++)
				{
					pos[i] = i;
					neg[-i] = -i;
				}

				mapmap[4] = pos;
				mapmap[-4] = neg;

				return mapmap;
			}

			public Dictionary<long, Dictionary<Numberz, Insanity>> testInsanity(Insanity argument)
			{
				Console.WriteLine("testInsanity()");

				Xtruct hello = new Xtruct();
				hello.String_thing = "Hello2";
				hello.Byte_thing = 2;
				hello.I32_thing = 2;
				hello.I64_thing = 2;

				Xtruct goodbye = new Xtruct();
				goodbye.String_thing = "Goodbye4";
				goodbye.Byte_thing = (byte)4;
				goodbye.I32_thing = 4;
				goodbye.I64_thing = (long)4;

				Insanity crazy = new Insanity();
				crazy.UserMap = new Dictionary<Numberz, long>();
				crazy.UserMap[Numberz.EIGHT] = (long)8;
				crazy.Xtructs = new List<Xtruct>();
				crazy.Xtructs.Add(goodbye);

				Insanity looney = new Insanity();
				crazy.UserMap[Numberz.FIVE] = (long)5;
				crazy.Xtructs.Add(hello);

				Dictionary<Numberz, Insanity> first_map = new Dictionary<Numberz, Insanity>();
				Dictionary<Numberz, Insanity> second_map = new Dictionary<Numberz, Insanity>(); ;

				first_map[Numberz.TWO] = crazy;
				first_map[Numberz.THREE] = crazy;

				second_map[Numberz.SIX] = looney;

				Dictionary<long, Dictionary<Numberz, Insanity>> insane =
				  new Dictionary<long, Dictionary<Numberz, Insanity>>();
				insane[(long)1] = first_map;
				insane[(long)2] = second_map;

				return insane;
			}

			public Xtruct testMulti(byte arg0, int arg1, long arg2, Dictionary<short, string> arg3, Numberz arg4, long arg5)
			{
				Console.WriteLine("testMulti()");

				Xtruct hello = new Xtruct(); ;
				hello.String_thing = "Hello2";
				hello.Byte_thing = arg0;
				hello.I32_thing = arg1;
				hello.I64_thing = arg2;
				return hello;
			}

			public void testException(string arg)
			{
				Console.WriteLine("testException(" + arg + ")");
				if (arg == "Xception")
				{
					Xception x = new Xception();
					x.ErrorCode = 1001;
					x.Message = "This is an Xception";
					throw x;
				}
				return;
			}

			public Xtruct testMultiException(string arg0, string arg1)
			{
				Console.WriteLine("testMultiException(" + arg0 + ", " + arg1 + ")");
				if (arg0 == "Xception")
				{
					Xception x = new Xception();
					x.ErrorCode = 1001;
					x.Message = "This is an Xception";
					throw x;
				}
				else if (arg0 == "Xception2")
				{
					Xception2 x = new Xception2();
					x.ErrorCode = 2002;
					x.Struct_thing = new Xtruct();
					x.Struct_thing.String_thing = "This is an Xception2";
					throw x;
				}

				Xtruct result = new Xtruct();
				result.String_thing = arg1;
				return result;
			}

			public void testStop()
			{
				if (server != null)
				{
					server.Stop();
				}
			}

			public void testOneway(int arg)
			{
				Console.WriteLine("testOneway(" + arg + "), sleeping...");
				System.Threading.Thread.Sleep(arg * 1000);
				Console.WriteLine("testOneway finished");
			}

		} // class TestHandler

		public static void Execute(string[] args)
		{
			try
			{
				bool useBufferedSockets = false;
				int port = 9090;
				if (args.Length > 0)
				{
					port = int.Parse(args[0]);

					if (args.Length > 1)
					{
						bool.TryParse(args[1], out useBufferedSockets);
					}
				}

				// Processor
				TestHandler testHandler = new TestHandler();
				ThriftTest.Processor testProcessor = new ThriftTest.Processor(testHandler);

				// Transport
				TServerSocket tServerSocket = new TServerSocket(port, 0, useBufferedSockets);

				TServer serverEngine;

				// Simple Server
				serverEngine = new TSimpleServer(testProcessor, tServerSocket);

				// ThreadPool Server
				// serverEngine = new TThreadPoolServer(testProcessor, tServerSocket);

				// Threaded Server
				// serverEngine = new TThreadedServer(testProcessor, tServerSocket);

				testHandler.server = serverEngine;

				// Run it
				Console.WriteLine("Starting the server on port " + port + (useBufferedSockets ? " with buffered socket" : "") + "...");
				serverEngine.Serve();

			}
			catch (Exception x)
			{
				Console.Error.Write(x);
			}
			Console.WriteLine("done.");
		}
	}
}