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 | */ |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 23 | |
| 24 | using System; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 25 | using Thrift.Protocol; |
| 26 | using Thrift.Transport; |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 27 | using System.IO; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 28 | |
| 29 | namespace Thrift.Server |
| 30 | { |
| 31 | public abstract class TServer |
| 32 | { |
| 33 | /** |
| 34 | * Core processor |
| 35 | */ |
| 36 | protected TProcessor processor; |
| 37 | |
| 38 | /** |
| 39 | * Server transport |
| 40 | */ |
| 41 | protected TServerTransport serverTransport; |
| 42 | |
| 43 | /** |
| 44 | * Input Transport Factory |
| 45 | */ |
| 46 | protected TTransportFactory inputTransportFactory; |
| 47 | |
| 48 | /** |
| 49 | * Output Transport Factory |
| 50 | */ |
| 51 | protected TTransportFactory outputTransportFactory; |
| 52 | |
| 53 | /** |
| 54 | * Input Protocol Factory |
| 55 | */ |
| 56 | protected TProtocolFactory inputProtocolFactory; |
| 57 | |
| 58 | /** |
| 59 | * Output Protocol Factory |
| 60 | */ |
| 61 | protected TProtocolFactory outputProtocolFactory; |
Jens Geyer | d335acd | 2013-11-11 21:33:54 +0100 | [diff] [blame] | 62 | |
Jens Geyer | 1c99e70 | 2014-03-17 22:50:39 +0200 | [diff] [blame] | 63 | public delegate void LogDelegate(string str); |
| 64 | private LogDelegate _logDelegate; |
| 65 | protected LogDelegate logDelegate |
| 66 | { |
| 67 | get { return _logDelegate; } |
Jens Geyer | d335acd | 2013-11-11 21:33:54 +0100 | [diff] [blame] | 68 | set { _logDelegate = (value != null) ? value : DefaultLogDelegate; } |
| 69 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * Default constructors. |
| 73 | */ |
| 74 | |
| 75 | public TServer(TProcessor processor, |
| 76 | TServerTransport serverTransport) |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 77 | :this(processor, serverTransport, new TTransportFactory(), new TTransportFactory(), new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), DefaultLogDelegate) |
| 78 | { |
| 79 | } |
| 80 | |
| 81 | public TServer(TProcessor processor, |
| 82 | TServerTransport serverTransport, |
| 83 | LogDelegate logDelegate) |
| 84 | : this(processor, serverTransport, new TTransportFactory(), new TTransportFactory(), new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), DefaultLogDelegate) |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 85 | { |
| 86 | } |
| 87 | |
| 88 | public TServer(TProcessor processor, |
| 89 | TServerTransport serverTransport, |
| 90 | TTransportFactory transportFactory) |
| 91 | :this(processor, |
| 92 | serverTransport, |
| 93 | transportFactory, |
| 94 | transportFactory, |
| 95 | new TBinaryProtocol.Factory(), |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 96 | new TBinaryProtocol.Factory(), |
| 97 | DefaultLogDelegate) |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 98 | { |
| 99 | } |
| 100 | |
| 101 | public TServer(TProcessor processor, |
| 102 | TServerTransport serverTransport, |
| 103 | TTransportFactory transportFactory, |
| 104 | TProtocolFactory protocolFactory) |
| 105 | :this(processor, |
| 106 | serverTransport, |
| 107 | transportFactory, |
| 108 | transportFactory, |
| 109 | protocolFactory, |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 110 | protocolFactory, |
| 111 | DefaultLogDelegate) |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 112 | { |
| 113 | } |
| 114 | |
| 115 | public TServer(TProcessor processor, |
| 116 | TServerTransport serverTransport, |
| 117 | TTransportFactory inputTransportFactory, |
| 118 | TTransportFactory outputTransportFactory, |
| 119 | TProtocolFactory inputProtocolFactory, |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 120 | TProtocolFactory outputProtocolFactory, |
| 121 | LogDelegate logDelegate) |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 122 | { |
| 123 | this.processor = processor; |
| 124 | this.serverTransport = serverTransport; |
| 125 | this.inputTransportFactory = inputTransportFactory; |
| 126 | this.outputTransportFactory = outputTransportFactory; |
| 127 | this.inputProtocolFactory = inputProtocolFactory; |
| 128 | this.outputProtocolFactory = outputProtocolFactory; |
Jens Geyer | d335acd | 2013-11-11 21:33:54 +0100 | [diff] [blame] | 129 | this.logDelegate = (logDelegate != null) ? logDelegate : DefaultLogDelegate; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /** |
| 133 | * The run method fires up the server and gets things going. |
| 134 | */ |
| 135 | public abstract void Serve(); |
David Reiss | dc815f5 | 2008-03-02 00:58:04 +0000 | [diff] [blame] | 136 | |
| 137 | public abstract void Stop(); |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 138 | |
| 139 | protected static void DefaultLogDelegate(string s) |
| 140 | { |
| 141 | Console.Error.WriteLine(s); |
| 142 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |