blob: c6fd99ea72df153f9d92755908ae944cf75cc0d9 [file] [log] [blame]
Kevin Clarkab4460d2009-03-20 02:28:41 +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.
Todd Lipcon53ae9f32009-12-07 00:42:38 +000018 *
19 * Contains some contributions under the Thrift Software License.
20 * Please see doc/old-thrift-license.txt in the Thrift distribution for
21 * details.
Kevin Clarkab4460d2009-03-20 02:28:41 +000022 */
23
David Reiss7f42bcf2008-01-11 20:59:12 +000024using System;
David Reiss7f42bcf2008-01-11 20:59:12 +000025using Thrift.Transport;
26using Thrift.Protocol;
27
28namespace Thrift.Server
29{
30 /// <summary>
31 /// Simple single-threaded server for testing
32 /// </summary>
David Reiss437c03b2008-04-02 22:10:06 +000033 public class TSimpleServer : TServer
David Reiss7f42bcf2008-01-11 20:59:12 +000034 {
David Reissdc815f52008-03-02 00:58:04 +000035 private bool stop = false;
David Reiss63191332009-01-06 19:49:22 +000036
David Reiss7f42bcf2008-01-11 20:59:12 +000037 public TSimpleServer(TProcessor processor,
38 TServerTransport serverTransport)
David Reiss63191332009-01-06 19:49:22 +000039 :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)
David Reiss7f42bcf2008-01-11 20:59:12 +000047 {
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(),
David Reiss63191332009-01-06 19:49:22 +000058 new TBinaryProtocol.Factory(),
59 DefaultLogDelegate)
David Reiss7f42bcf2008-01-11 20:59:12 +000060 {
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,
David Reiss63191332009-01-06 19:49:22 +000072 protocolFactory,
73 DefaultLogDelegate)
David Reiss7f42bcf2008-01-11 20:59:12 +000074 {
75 }
76
77 public override void Serve()
78 {
79 try
80 {
81 serverTransport.Listen();
82 }
83 catch (TTransportException ttx)
84 {
David Reiss63191332009-01-06 19:49:22 +000085 logDelegate(ttx.ToString());
David Reiss7f42bcf2008-01-11 20:59:12 +000086 return;
87 }
88
David Reissdc815f52008-03-02 00:58:04 +000089 while (!stop)
David Reiss7f42bcf2008-01-11 20:59:12 +000090 {
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 }
David Reiss63191332009-01-06 19:49:22 +0000108 catch (TTransportException ttx)
David Reiss7f42bcf2008-01-11 20:59:12 +0000109 {
110 // Client died, just move on
David Reiss63191332009-01-06 19:49:22 +0000111 if (stop)
112 {
113 logDelegate("TSimpleServer was shutting down, caught " + ttx.GetType().Name);
114 }
David Reiss7f42bcf2008-01-11 20:59:12 +0000115 }
116 catch (Exception x)
117 {
David Reiss63191332009-01-06 19:49:22 +0000118 logDelegate(x.ToString());
David Reiss7f42bcf2008-01-11 20:59:12 +0000119 }
120
121 if (inputTransport != null)
122 {
123 inputTransport.Close();
124 }
125
126 if (outputTransport != null)
127 {
128 outputTransport.Close();
129 }
130 }
David Reissdc815f52008-03-02 00:58:04 +0000131
132 if (stop)
133 {
134 try
135 {
136 serverTransport.Close();
137 }
138 catch (TTransportException ttx)
139 {
David Reiss63191332009-01-06 19:49:22 +0000140 logDelegate("TServerTranport failed on close: " + ttx.Message);
David Reissdc815f52008-03-02 00:58:04 +0000141 }
142 stop = false;
143 }
144 }
145
146 public override void Stop()
147 {
148 stop = true;
David Reiss63191332009-01-06 19:49:22 +0000149 serverTransport.Close();
David Reiss7f42bcf2008-01-11 20:59:12 +0000150 }
151 }
152}