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. |
| 18 | */ |
| 19 | |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 20 | using System; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 21 | using Thrift.Transport; |
| 22 | using Thrift.Protocol; |
| 23 | |
| 24 | namespace Thrift.Server |
| 25 | { |
| 26 | /// <summary> |
| 27 | /// Simple single-threaded server for testing |
| 28 | /// </summary> |
David Reiss | 437c03b | 2008-04-02 22:10:06 +0000 | [diff] [blame] | 29 | public class TSimpleServer : TServer |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 30 | { |
David Reiss | dc815f5 | 2008-03-02 00:58:04 +0000 | [diff] [blame] | 31 | private bool stop = false; |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 32 | |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 33 | public TSimpleServer(TProcessor processor, |
| 34 | TServerTransport serverTransport) |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 35 | :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 Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 43 | { |
| 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 Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 54 | new TBinaryProtocol.Factory(), |
| 55 | DefaultLogDelegate) |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 56 | { |
| 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 Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 68 | protocolFactory, |
| 69 | DefaultLogDelegate) |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 70 | { |
| 71 | } |
| 72 | |
| 73 | public override void Serve() |
| 74 | { |
| 75 | try |
| 76 | { |
| 77 | serverTransport.Listen(); |
| 78 | } |
| 79 | catch (TTransportException ttx) |
| 80 | { |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 81 | logDelegate(ttx.ToString()); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 82 | return; |
| 83 | } |
| 84 | |
David Reiss | dc815f5 | 2008-03-02 00:58:04 +0000 | [diff] [blame] | 85 | while (!stop) |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 86 | { |
| 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 Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 104 | catch (TTransportException ttx) |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 105 | { |
| 106 | // Client died, just move on |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 107 | if (stop) |
| 108 | { |
| 109 | logDelegate("TSimpleServer was shutting down, caught " + ttx.GetType().Name); |
| 110 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 111 | } |
| 112 | catch (Exception x) |
| 113 | { |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 114 | logDelegate(x.ToString()); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | if (inputTransport != null) |
| 118 | { |
| 119 | inputTransport.Close(); |
| 120 | } |
| 121 | |
| 122 | if (outputTransport != null) |
| 123 | { |
| 124 | outputTransport.Close(); |
| 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 | } |