Subversion Repositories SmartDukaan

Rev

Rev 30 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
30 ashish 1
/*
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
 
20
// Distributed under the Thrift Software License
21
//
22
// See accompanying file LICENSE or visit the Thrift site at:
23
// http://developers.facebook.com/thrift/
24
using System;
25
using System.Collections.Generic;
26
using Thrift.Collections;
27
using Thrift.Test; //generated code
28
using Thrift.Transport;
29
using Thrift.Protocol;
30
using Thrift.Server;
31
 
32
namespace Test
33
{
34
	public class TestServer
35
	{
36
		public class TestHandler : ThriftTest.Iface
37
		{
38
			public TServer server;
39
 
40
			public TestHandler() { }
41
 
42
			public void testVoid()
43
			{
44
				Console.WriteLine("testVoid()");
45
			}
46
 
47
			public string testString(string thing)
48
			{
49
				Console.WriteLine("teststring(\"" + thing + "\")");
50
				return thing;
51
			}
52
 
53
			public byte testByte(byte thing)
54
			{
55
				Console.WriteLine("testByte(" + thing + ")");
56
				return thing;
57
			}
58
 
59
			public int testI32(int thing)
60
			{
61
				Console.WriteLine("testI32(" + thing + ")");
62
				return thing;
63
			}
64
 
65
			public long testI64(long thing)
66
			{
67
				Console.WriteLine("testI64(" + thing + ")");
68
				return thing;
69
			}
70
 
71
			public double testDouble(double thing)
72
			{
73
				Console.WriteLine("testDouble(" + thing + ")");
74
				return thing;
75
			}
76
 
77
			public Xtruct testStruct(Xtruct thing)
78
			{
79
				Console.WriteLine("testStruct({" +
80
								 "\"" + thing.String_thing + "\", " +
81
								 thing.Byte_thing + ", " +
82
								 thing.I32_thing + ", " +
83
								 thing.I64_thing + "})");
84
				return thing;
85
			}
86
 
87
			public Xtruct2 testNest(Xtruct2 nest)
88
			{
89
				Xtruct thing = nest.Struct_thing;
90
				Console.WriteLine("testNest({" +
91
								 nest.Byte_thing + ", {" +
92
								 "\"" + thing.String_thing + "\", " +
93
								 thing.Byte_thing + ", " +
94
								 thing.I32_thing + ", " +
95
								 thing.I64_thing + "}, " +
96
								 nest.I32_thing + "})");
97
				return nest;
98
			}
99
 
100
			public Dictionary<int, int> testMap(Dictionary<int, int> thing)
101
			{
102
				Console.WriteLine("testMap({");
103
				bool first = true;
104
				foreach (int key in thing.Keys)
105
				{
106
					if (first)
107
					{
108
						first = false;
109
					}
110
					else
111
					{
112
						Console.WriteLine(", ");
113
					}
114
					Console.WriteLine(key + " => " + thing[key]);
115
				}
116
				Console.WriteLine("})");
117
				return thing;
118
			}
119
 
120
			public THashSet<int> testSet(THashSet<int> thing)
121
			{
122
				Console.WriteLine("testSet({");
123
				bool first = true;
124
				foreach (int elem in thing)
125
				{
126
					if (first)
127
					{
128
						first = false;
129
					}
130
					else
131
					{
132
						Console.WriteLine(", ");
133
					}
134
					Console.WriteLine(elem);
135
				}
136
				Console.WriteLine("})");
137
				return thing;
138
			}
139
 
140
			public List<int> testList(List<int> thing)
141
			{
142
				Console.WriteLine("testList({");
143
				bool first = true;
144
				foreach (int elem in thing)
145
				{
146
					if (first)
147
					{
148
						first = false;
149
					}
150
					else
151
					{
152
						Console.WriteLine(", ");
153
					}
154
					Console.WriteLine(elem);
155
				}
156
				Console.WriteLine("})");
157
				return thing;
158
			}
159
 
160
			public Numberz testEnum(Numberz thing)
161
			{
162
				Console.WriteLine("testEnum(" + thing + ")");
163
				return thing;
164
			}
165
 
166
			public long testTypedef(long thing)
167
			{
168
				Console.WriteLine("testTypedef(" + thing + ")");
169
				return thing;
170
			}
171
 
172
			public Dictionary<int, Dictionary<int, int>> testMapMap(int hello)
173
			{
174
				Console.WriteLine("testMapMap(" + hello + ")");
175
				Dictionary<int, Dictionary<int, int>> mapmap =
176
				  new Dictionary<int, Dictionary<int, int>>();
177
 
178
				Dictionary<int, int> pos = new Dictionary<int, int>();
179
				Dictionary<int, int> neg = new Dictionary<int, int>();
180
				for (int i = 1; i < 5; i++)
181
				{
182
					pos[i] = i;
183
					neg[-i] = -i;
184
				}
185
 
186
				mapmap[4] = pos;
187
				mapmap[-4] = neg;
188
 
189
				return mapmap;
190
			}
191
 
192
			public Dictionary<long, Dictionary<Numberz, Insanity>> testInsanity(Insanity argument)
193
			{
194
				Console.WriteLine("testInsanity()");
195
 
196
				Xtruct hello = new Xtruct();
197
				hello.String_thing = "Hello2";
198
				hello.Byte_thing = 2;
199
				hello.I32_thing = 2;
200
				hello.I64_thing = 2;
201
 
202
				Xtruct goodbye = new Xtruct();
203
				goodbye.String_thing = "Goodbye4";
204
				goodbye.Byte_thing = (byte)4;
205
				goodbye.I32_thing = 4;
206
				goodbye.I64_thing = (long)4;
207
 
208
				Insanity crazy = new Insanity();
209
				crazy.UserMap = new Dictionary<Numberz, long>();
210
				crazy.UserMap[Numberz.EIGHT] = (long)8;
211
				crazy.Xtructs = new List<Xtruct>();
212
				crazy.Xtructs.Add(goodbye);
213
 
214
				Insanity looney = new Insanity();
215
				crazy.UserMap[Numberz.FIVE] = (long)5;
216
				crazy.Xtructs.Add(hello);
217
 
218
				Dictionary<Numberz, Insanity> first_map = new Dictionary<Numberz, Insanity>();
219
				Dictionary<Numberz, Insanity> second_map = new Dictionary<Numberz, Insanity>(); ;
220
 
221
				first_map[Numberz.TWO] = crazy;
222
				first_map[Numberz.THREE] = crazy;
223
 
224
				second_map[Numberz.SIX] = looney;
225
 
226
				Dictionary<long, Dictionary<Numberz, Insanity>> insane =
227
				  new Dictionary<long, Dictionary<Numberz, Insanity>>();
228
				insane[(long)1] = first_map;
229
				insane[(long)2] = second_map;
230
 
231
				return insane;
232
			}
233
 
234
			public Xtruct testMulti(byte arg0, int arg1, long arg2, Dictionary<short, string> arg3, Numberz arg4, long arg5)
235
			{
236
				Console.WriteLine("testMulti()");
237
 
238
				Xtruct hello = new Xtruct(); ;
239
				hello.String_thing = "Hello2";
240
				hello.Byte_thing = arg0;
241
				hello.I32_thing = arg1;
242
				hello.I64_thing = arg2;
243
				return hello;
244
			}
245
 
246
			public void testException(string arg)
247
			{
248
				Console.WriteLine("testException(" + arg + ")");
249
				if (arg == "Xception")
250
				{
251
					Xception x = new Xception();
252
					x.ErrorCode = 1001;
253
					x.Message = "This is an Xception";
254
					throw x;
255
				}
256
				return;
257
			}
258
 
259
			public Xtruct testMultiException(string arg0, string arg1)
260
			{
261
				Console.WriteLine("testMultiException(" + arg0 + ", " + arg1 + ")");
262
				if (arg0 == "Xception")
263
				{
264
					Xception x = new Xception();
265
					x.ErrorCode = 1001;
266
					x.Message = "This is an Xception";
267
					throw x;
268
				}
269
				else if (arg0 == "Xception2")
270
				{
271
					Xception2 x = new Xception2();
272
					x.ErrorCode = 2002;
273
					x.Struct_thing = new Xtruct();
274
					x.Struct_thing.String_thing = "This is an Xception2";
275
					throw x;
276
				}
277
 
278
				Xtruct result = new Xtruct();
279
				result.String_thing = arg1;
280
				return result;
281
			}
282
 
283
			public void testStop()
284
			{
285
				if (server != null)
286
				{
287
					server.Stop();
288
				}
289
			}
290
 
291
			public void testOneway(int arg)
292
			{
293
				Console.WriteLine("testOneway(" + arg + "), sleeping...");
294
				System.Threading.Thread.Sleep(arg * 1000);
295
				Console.WriteLine("testOneway finished");
296
			}
297
 
298
		} // class TestHandler
299
 
300
		public static void Execute(string[] args)
301
		{
302
			try
303
			{
304
				bool useBufferedSockets = false;
305
				int port = 9090;
306
				if (args.Length > 0)
307
				{
308
					port = int.Parse(args[0]);
309
 
310
					if (args.Length > 1)
311
					{
312
						bool.TryParse(args[1], out useBufferedSockets);
313
					}
314
				}
315
 
316
				// Processor
317
				TestHandler testHandler = new TestHandler();
318
				ThriftTest.Processor testProcessor = new ThriftTest.Processor(testHandler);
319
 
320
				// Transport
321
				TServerSocket tServerSocket = new TServerSocket(port, 0, useBufferedSockets);
322
 
323
				TServer serverEngine;
324
 
325
				// Simple Server
326
				serverEngine = new TSimpleServer(testProcessor, tServerSocket);
327
 
328
				// ThreadPool Server
329
				// serverEngine = new TThreadPoolServer(testProcessor, tServerSocket);
330
 
331
				// Threaded Server
332
				// serverEngine = new TThreadedServer(testProcessor, tServerSocket);
333
 
334
				testHandler.server = serverEngine;
335
 
336
				// Run it
337
				Console.WriteLine("Starting the server on port " + port + (useBufferedSockets ? " with buffered socket" : "") + "...");
338
				serverEngine.Serve();
339
 
340
			}
341
			catch (Exception x)
342
			{
343
				Console.Error.Write(x);
344
			}
345
			Console.WriteLine("done.");
346
		}
347
	}
348
}