blob: 42e0cbeeb293829ab5b22ac4a4bc1ce26b899d38 [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{
Randy Abernethy0e86f1f2014-07-13 09:50:19 -070030 /// <summary>
31 /// Simple single-threaded server for testing
32 /// </summary>
33 public class TSimpleServer : TServer
34 {
35 private bool stop = false;
David Reiss63191332009-01-06 19:49:22 +000036
Randy Abernethy0e86f1f2014-07-13 09:50:19 -070037 public TSimpleServer(TProcessor processor,
38 TServerTransport serverTransport)
39 : base(processor, serverTransport, new TTransportFactory(), new TTransportFactory(), new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), DefaultLogDelegate)
40 {
41 }
David Reiss63191332009-01-06 19:49:22 +000042
Randy Abernethy0e86f1f2014-07-13 09:50:19 -070043 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 }
David Reiss7f42bcf2008-01-11 20:59:12 +000049
Randy Abernethy0e86f1f2014-07-13 09:50:19 -070050 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 }
David Reiss7f42bcf2008-01-11 20:59:12 +000062
Randy Abernethy0e86f1f2014-07-13 09:50:19 -070063 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 }
David Reiss7f42bcf2008-01-11 20:59:12 +000076
Randy Abernethy0e86f1f2014-07-13 09:50:19 -070077 public override void Serve()
78 {
79 try
80 {
81 serverTransport.Listen();
82 }
83 catch (TTransportException ttx)
84 {
85 logDelegate(ttx.ToString());
86 return;
87 }
David Reiss7f42bcf2008-01-11 20:59:12 +000088
Randy Abernethy0e86f1f2014-07-13 09:50:19 -070089 //Fire the preServe server event when server is up but before any client connections
90 if (serverEventHandler != null)
91 serverEventHandler.preServe();
92
93 while (!stop)
94 {
95 TTransport client = null;
96 TTransport inputTransport = null;
97 TTransport outputTransport = null;
98 TProtocol inputProtocol = null;
99 TProtocol outputProtocol = null;
100 Object connectionContext = null;
101 try
102 {
103 using (client = serverTransport.Accept())
Roger Meierb1ec4cc2012-04-11 21:21:41 +0000104 {
105 if (client != null)
106 {
Randy Abernethy0e86f1f2014-07-13 09:50:19 -0700107 using (inputTransport = inputTransportFactory.GetTransport(client))
Roger Meierb1ec4cc2012-04-11 21:21:41 +0000108 {
109 using (outputTransport = outputTransportFactory.GetTransport(client))
110 {
111 inputProtocol = inputProtocolFactory.GetProtocol(inputTransport);
112 outputProtocol = outputProtocolFactory.GetProtocol(outputTransport);
Randy Abernethy0e86f1f2014-07-13 09:50:19 -0700113
114 //Recover event handler (if any) and fire createContext server event when a client connects
115 if (serverEventHandler != null)
116 connectionContext = serverEventHandler.createContext(inputProtocol, outputProtocol);
117
118 //Process client requests until client disconnects
119 while (true)
120 {
Jens Geyereb8e5ad2014-09-29 21:50:15 +0200121 if (!inputTransport.Peek())
122 break;
123
Randy Abernethy0e86f1f2014-07-13 09:50:19 -0700124 //Fire processContext server event
125 //N.B. This is the pattern implemented in C++ and the event fires provisionally.
126 //That is to say it may be many minutes between the event firing and the client request
127 //actually arriving or the client may hang up without ever makeing a request.
128 if (serverEventHandler != null)
129 serverEventHandler.processContext(connectionContext, inputTransport);
130 //Process client request (blocks until transport is readable)
131 if (!processor.Process(inputProtocol, outputProtocol))
132 break;
133 }
Roger Meierb1ec4cc2012-04-11 21:21:41 +0000134 }
135 }
136 }
137 }
138 }
Randy Abernethy0e86f1f2014-07-13 09:50:19 -0700139 catch (TTransportException)
Roger Meierb1ec4cc2012-04-11 21:21:41 +0000140 {
Randy Abernethy0e86f1f2014-07-13 09:50:19 -0700141 //Usually a client disconnect, expected
Roger Meierb1ec4cc2012-04-11 21:21:41 +0000142 }
143 catch (Exception x)
144 {
Randy Abernethy0e86f1f2014-07-13 09:50:19 -0700145 //Unexpected
Roger Meierb1ec4cc2012-04-11 21:21:41 +0000146 logDelegate(x.ToString());
147 }
Randy Abernethy0e86f1f2014-07-13 09:50:19 -0700148
149 //Fire deleteContext server event after client disconnects
150 if (serverEventHandler != null)
151 serverEventHandler.deleteContext(connectionContext, inputProtocol, outputProtocol);
Roger Meierb1ec4cc2012-04-11 21:21:41 +0000152 }
Randy Abernethy0e86f1f2014-07-13 09:50:19 -0700153 }
David Reissdc815f52008-03-02 00:58:04 +0000154
Randy Abernethy0e86f1f2014-07-13 09:50:19 -0700155 public override void Stop()
156 {
157 stop = true;
158 serverTransport.Close();
159 }
160 }
David Reiss7f42bcf2008-01-11 20:59:12 +0000161}