Kevin Clark | ab4460d | 2009-03-20 02:28:41 +0000 | [diff] [blame] | 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. |
Todd Lipcon | 53ae9f3 | 2009-12-07 00:42:38 +0000 | [diff] [blame] | 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. |
Kevin Clark | ab4460d | 2009-03-20 02:28:41 +0000 | [diff] [blame] | 22 | */ |
| 23 | |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 24 | using System; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 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> |
David Reiss | 437c03b | 2008-04-02 22:10:06 +0000 | [diff] [blame] | 33 | public class TSimpleServer : TServer |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 34 | { |
David Reiss | dc815f5 | 2008-03-02 00:58:04 +0000 | [diff] [blame] | 35 | private bool stop = false; |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 36 | |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 37 | public TSimpleServer(TProcessor processor, |
| 38 | TServerTransport serverTransport) |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 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) |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 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(), |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 58 | new TBinaryProtocol.Factory(), |
| 59 | DefaultLogDelegate) |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 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, |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 72 | protocolFactory, |
| 73 | DefaultLogDelegate) |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 74 | { |
| 75 | } |
| 76 | |
| 77 | public override void Serve() |
| 78 | { |
| 79 | try |
| 80 | { |
| 81 | serverTransport.Listen(); |
| 82 | } |
| 83 | catch (TTransportException ttx) |
| 84 | { |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 85 | logDelegate(ttx.ToString()); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 86 | return; |
| 87 | } |
| 88 | |
David Reiss | dc815f5 | 2008-03-02 00:58:04 +0000 | [diff] [blame] | 89 | while (!stop) |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 90 | { |
| 91 | TTransport client = null; |
| 92 | TTransport inputTransport = null; |
| 93 | TTransport outputTransport = null; |
| 94 | TProtocol inputProtocol = null; |
| 95 | TProtocol outputProtocol = null; |
| 96 | try |
| 97 | { |
Roger Meier | b1ec4cc | 2012-04-11 21:21:41 +0000 | [diff] [blame] | 98 | 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 Reiss | dc815f5 | 2008-03-02 00:58:04 +0000 | [diff] [blame] | 127 | |
| 128 | if (stop) |
| 129 | { |
| 130 | try |
| 131 | { |
| 132 | serverTransport.Close(); |
| 133 | } |
| 134 | catch (TTransportException ttx) |
| 135 | { |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 136 | logDelegate("TServerTranport failed on close: " + ttx.Message); |
David Reiss | dc815f5 | 2008-03-02 00:58:04 +0000 | [diff] [blame] | 137 | } |
| 138 | stop = false; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | public override void Stop() |
| 143 | { |
| 144 | stop = true; |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 145 | serverTransport.Close(); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | } |