Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | 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
 * Contains some contributions under the Thrift Software License.
20
 * Please see doc/old-thrift-license.txt in the Thrift distribution for
21
 * details.
22
 */
23
 
24
using System;
25
using Thrift.Transport;
26
using Thrift.Protocol;
27
 
28
namespace Thrift.Server
29
{
30
	/// <summary>
31
	/// Simple single-threaded server for testing
32
	/// </summary>
33
	public class TSimpleServer : TServer
34
	{
35
		private bool stop = false;
36
 
37
		public TSimpleServer(TProcessor processor,
38
						  TServerTransport serverTransport)
39
			:base(processor, serverTransport, new TTransportFactory(), new TTransportFactory(), new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), DefaultLogDelegate)
40
		{
41
		}
42
 
43
		public TSimpleServer(TProcessor processor,
44
							TServerTransport serverTransport,
45
							LogDelegate logDel)
46
			: base(processor, serverTransport, new TTransportFactory(), new TTransportFactory(), new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), logDel)
47
		{
48
		}
49
 
50
		public TSimpleServer(TProcessor processor,
51
						  TServerTransport serverTransport,
52
						  TTransportFactory transportFactory)
53
			:base(processor,
54
				 serverTransport,
55
				 transportFactory,
56
				 transportFactory,
57
				 new TBinaryProtocol.Factory(),
58
				 new TBinaryProtocol.Factory(),
59
			     DefaultLogDelegate)
60
		{
61
		}
62
 
63
		public TSimpleServer(TProcessor processor,
64
						  TServerTransport serverTransport,
65
						  TTransportFactory transportFactory,
66
						  TProtocolFactory protocolFactory)
67
			:base(processor,
68
				 serverTransport,
69
				 transportFactory,
70
				 transportFactory,
71
				 protocolFactory,
72
				 protocolFactory,
73
				 DefaultLogDelegate)
74
		{
75
		}
76
 
77
		public override void Serve()
78
		{
79
			try
80
			{
81
				serverTransport.Listen();
82
			}
83
			catch (TTransportException ttx)
84
			{
85
				logDelegate(ttx.ToString());
86
				return;
87
			}
88
 
89
			while (!stop)
90
			{
91
				TTransport client = null;
92
				TTransport inputTransport = null;
93
				TTransport outputTransport = null;
94
				TProtocol inputProtocol = null;
95
				TProtocol outputProtocol = null;
96
				try
97
				{
98
					client = serverTransport.Accept();
99
					if (client != null)
100
					{
101
						inputTransport = inputTransportFactory.GetTransport(client);
102
						outputTransport = outputTransportFactory.GetTransport(client);
103
						inputProtocol = inputProtocolFactory.GetProtocol(inputTransport);
104
						outputProtocol = outputProtocolFactory.GetProtocol(outputTransport);
105
						while (processor.Process(inputProtocol, outputProtocol)) { }
106
					}
107
				}
108
				catch (TTransportException ttx)
109
				{
110
					// Client died, just move on
111
					if (stop)
112
					{
113
						logDelegate("TSimpleServer was shutting down, caught " + ttx.GetType().Name);
114
					}
115
				}
116
				catch (Exception x)
117
				{
118
					logDelegate(x.ToString());
119
				}
120
 
121
				if (inputTransport != null)
122
				{
123
					inputTransport.Close();
124
				}
125
 
126
				if (outputTransport != null)
127
				{
128
					outputTransport.Close();
129
				}
130
			}
131
 
132
			if (stop)
133
			{
134
				try
135
				{
136
					serverTransport.Close();
137
				}
138
				catch (TTransportException ttx)
139
				{
140
					logDelegate("TServerTranport failed on close: " + ttx.Message);
141
				}
142
				stop = false;
143
			}
144
		}
145
 
146
		public override void Stop()
147
		{
148
			stop = true;
149
			serverTransport.Close();
150
		}
151
	}
152
}