blob: 34a51de4aa2e0bcaee03a891ba6f740d8a979a39 [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.
18 */
19
David Reiss7f42bcf2008-01-11 20:59:12 +000020using System;
David Reiss7f42bcf2008-01-11 20:59:12 +000021using Thrift.Transport;
22using Thrift.Protocol;
23
24namespace Thrift.Server
25{
26 /// <summary>
27 /// Simple single-threaded server for testing
28 /// </summary>
David Reiss437c03b2008-04-02 22:10:06 +000029 public class TSimpleServer : TServer
David Reiss7f42bcf2008-01-11 20:59:12 +000030 {
David Reissdc815f52008-03-02 00:58:04 +000031 private bool stop = false;
David Reiss63191332009-01-06 19:49:22 +000032
David Reiss7f42bcf2008-01-11 20:59:12 +000033 public TSimpleServer(TProcessor processor,
34 TServerTransport serverTransport)
David Reiss63191332009-01-06 19:49:22 +000035 :base(processor, serverTransport, new TTransportFactory(), new TTransportFactory(), new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), DefaultLogDelegate)
36 {
37 }
38
39 public TSimpleServer(TProcessor processor,
40 TServerTransport serverTransport,
41 LogDelegate logDel)
42 : base(processor, serverTransport, new TTransportFactory(), new TTransportFactory(), new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), logDel)
David Reiss7f42bcf2008-01-11 20:59:12 +000043 {
44 }
45
46 public TSimpleServer(TProcessor processor,
47 TServerTransport serverTransport,
48 TTransportFactory transportFactory)
49 :base(processor,
50 serverTransport,
51 transportFactory,
52 transportFactory,
53 new TBinaryProtocol.Factory(),
David Reiss63191332009-01-06 19:49:22 +000054 new TBinaryProtocol.Factory(),
55 DefaultLogDelegate)
David Reiss7f42bcf2008-01-11 20:59:12 +000056 {
57 }
58
59 public TSimpleServer(TProcessor processor,
60 TServerTransport serverTransport,
61 TTransportFactory transportFactory,
62 TProtocolFactory protocolFactory)
63 :base(processor,
64 serverTransport,
65 transportFactory,
66 transportFactory,
67 protocolFactory,
David Reiss63191332009-01-06 19:49:22 +000068 protocolFactory,
69 DefaultLogDelegate)
David Reiss7f42bcf2008-01-11 20:59:12 +000070 {
71 }
72
73 public override void Serve()
74 {
75 try
76 {
77 serverTransport.Listen();
78 }
79 catch (TTransportException ttx)
80 {
David Reiss63191332009-01-06 19:49:22 +000081 logDelegate(ttx.ToString());
David Reiss7f42bcf2008-01-11 20:59:12 +000082 return;
83 }
84
David Reissdc815f52008-03-02 00:58:04 +000085 while (!stop)
David Reiss7f42bcf2008-01-11 20:59:12 +000086 {
87 TTransport client = null;
88 TTransport inputTransport = null;
89 TTransport outputTransport = null;
90 TProtocol inputProtocol = null;
91 TProtocol outputProtocol = null;
92 try
93 {
94 client = serverTransport.Accept();
95 if (client != null)
96 {
97 inputTransport = inputTransportFactory.GetTransport(client);
98 outputTransport = outputTransportFactory.GetTransport(client);
99 inputProtocol = inputProtocolFactory.GetProtocol(inputTransport);
100 outputProtocol = outputProtocolFactory.GetProtocol(outputTransport);
101 while (processor.Process(inputProtocol, outputProtocol)) { }
102 }
103 }
David Reiss63191332009-01-06 19:49:22 +0000104 catch (TTransportException ttx)
David Reiss7f42bcf2008-01-11 20:59:12 +0000105 {
106 // Client died, just move on
David Reiss63191332009-01-06 19:49:22 +0000107 if (stop)
108 {
109 logDelegate("TSimpleServer was shutting down, caught " + ttx.GetType().Name);
110 }
David Reiss7f42bcf2008-01-11 20:59:12 +0000111 }
112 catch (Exception x)
113 {
David Reiss63191332009-01-06 19:49:22 +0000114 logDelegate(x.ToString());
David Reiss7f42bcf2008-01-11 20:59:12 +0000115 }
116
117 if (inputTransport != null)
118 {
119 inputTransport.Close();
120 }
121
122 if (outputTransport != null)
123 {
124 outputTransport.Close();
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}