blob: 1099fc1de15e9bb4706c507ba03c229db3e3b882 [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 {
Roger Meierb1ec4cc2012-04-11 21:21:41 +000098 using(client = serverTransport.Accept())
99 {
100 if (client != null)
101 {
102 using(inputTransport = inputTransportFactory.GetTransport(client))
103 {
104 using (outputTransport = outputTransportFactory.GetTransport(client))
105 {
106 inputProtocol = inputProtocolFactory.GetProtocol(inputTransport);
107 outputProtocol = outputProtocolFactory.GetProtocol(outputTransport);
108 while (processor.Process(inputProtocol, outputProtocol)) { }
109 }
110 }
111 }
112 }
113 }
114 catch (TTransportException ttx)
115 {
116 // Client died, just move on
117 if (stop)
118 {
119 logDelegate("TSimpleServer was shutting down, caught " + ttx.GetType().Name);
120 }
121 }
122 catch (Exception x)
123 {
124 logDelegate(x.ToString());
125 }
126 }
David Reissdc815f52008-03-02 00:58:04 +0000127
128 if (stop)
129 {
130 try
131 {
132 serverTransport.Close();
133 }
134 catch (TTransportException ttx)
135 {
David Reiss63191332009-01-06 19:49:22 +0000136 logDelegate("TServerTranport failed on close: " + ttx.Message);
David Reissdc815f52008-03-02 00:58:04 +0000137 }
138 stop = false;
139 }
140 }
141
142 public override void Stop()
143 {
144 stop = true;
David Reiss63191332009-01-06 19:49:22 +0000145 serverTransport.Close();
David Reiss7f42bcf2008-01-11 20:59:12 +0000146 }
147 }
148}