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 | { |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 31 | public abstract class TServer |
| 32 | { |
| 33 | //Attributes |
Jonathan Heard | 2bfd7df | 2015-10-28 17:34:27 +0000 | [diff] [blame^] | 34 | protected TProcessorFactory processorFactory; |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 35 | protected TServerTransport serverTransport; |
| 36 | protected TTransportFactory inputTransportFactory; |
| 37 | protected TTransportFactory outputTransportFactory; |
| 38 | protected TProtocolFactory inputProtocolFactory; |
| 39 | protected TProtocolFactory outputProtocolFactory; |
| 40 | protected TServerEventHandler serverEventHandler = null; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 41 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 42 | //Methods |
| 43 | public void setEventHandler(TServerEventHandler seh) |
| 44 | { |
| 45 | serverEventHandler = seh; |
| 46 | } |
| 47 | public TServerEventHandler getEventHandler() |
| 48 | { |
| 49 | return serverEventHandler; |
| 50 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 51 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 52 | //Log delegation |
| 53 | public delegate void LogDelegate(string str); |
| 54 | private LogDelegate _logDelegate; |
| 55 | protected LogDelegate logDelegate |
| 56 | { |
| 57 | get { return _logDelegate; } |
| 58 | set { _logDelegate = (value != null) ? value : DefaultLogDelegate; } |
| 59 | } |
| 60 | protected static void DefaultLogDelegate(string s) |
| 61 | { |
| 62 | Console.Error.WriteLine(s); |
| 63 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 64 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 65 | //Construction |
| 66 | public TServer(TProcessor processor, |
| 67 | TServerTransport serverTransport) |
Jonathan Heard | 2bfd7df | 2015-10-28 17:34:27 +0000 | [diff] [blame^] | 68 | : this(processor, serverTransport, |
| 69 | new TTransportFactory(), |
| 70 | new TTransportFactory(), |
| 71 | new TBinaryProtocol.Factory(), |
| 72 | new TBinaryProtocol.Factory(), |
| 73 | DefaultLogDelegate) |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 74 | { |
| 75 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 76 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 77 | public TServer(TProcessor processor, |
| 78 | TServerTransport serverTransport, |
| 79 | LogDelegate logDelegate) |
Jonathan Heard | 2bfd7df | 2015-10-28 17:34:27 +0000 | [diff] [blame^] | 80 | : this(processor, |
| 81 | serverTransport, |
| 82 | new TTransportFactory(), |
| 83 | new TTransportFactory(), |
| 84 | new TBinaryProtocol.Factory(), |
| 85 | new TBinaryProtocol.Factory(), |
| 86 | logDelegate) |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 87 | { |
| 88 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 89 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 90 | public TServer(TProcessor processor, |
| 91 | TServerTransport serverTransport, |
| 92 | TTransportFactory transportFactory) |
| 93 | : this(processor, |
| 94 | serverTransport, |
| 95 | transportFactory, |
| 96 | transportFactory, |
| 97 | new TBinaryProtocol.Factory(), |
| 98 | new TBinaryProtocol.Factory(), |
| 99 | DefaultLogDelegate) |
| 100 | { |
| 101 | } |
Jens Geyer | d335acd | 2013-11-11 21:33:54 +0100 | [diff] [blame] | 102 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 103 | public TServer(TProcessor processor, |
| 104 | TServerTransport serverTransport, |
| 105 | TTransportFactory transportFactory, |
| 106 | TProtocolFactory protocolFactory) |
| 107 | : this(processor, |
| 108 | serverTransport, |
| 109 | transportFactory, |
| 110 | transportFactory, |
| 111 | protocolFactory, |
| 112 | protocolFactory, |
| 113 | DefaultLogDelegate) |
| 114 | { |
| 115 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 116 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 117 | public TServer(TProcessor processor, |
Jonathan Heard | 2bfd7df | 2015-10-28 17:34:27 +0000 | [diff] [blame^] | 118 | TServerTransport serverTransport, |
| 119 | TTransportFactory inputTransportFactory, |
| 120 | TTransportFactory outputTransportFactory, |
| 121 | TProtocolFactory inputProtocolFactory, |
| 122 | TProtocolFactory outputProtocolFactory, |
| 123 | LogDelegate logDelegate) |
| 124 | { |
| 125 | this.processorFactory = new TSingletonProcessorFactory(processor); |
| 126 | this.serverTransport = serverTransport; |
| 127 | this.inputTransportFactory = inputTransportFactory; |
| 128 | this.outputTransportFactory = outputTransportFactory; |
| 129 | this.inputProtocolFactory = inputProtocolFactory; |
| 130 | this.outputProtocolFactory = outputProtocolFactory; |
| 131 | this.logDelegate = (logDelegate != null) ? logDelegate : DefaultLogDelegate; |
| 132 | } |
| 133 | |
| 134 | public TServer(TProcessorFactory processorFactory, |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 135 | TServerTransport serverTransport, |
| 136 | TTransportFactory inputTransportFactory, |
| 137 | TTransportFactory outputTransportFactory, |
| 138 | TProtocolFactory inputProtocolFactory, |
| 139 | TProtocolFactory outputProtocolFactory, |
| 140 | LogDelegate logDelegate) |
| 141 | { |
Jonathan Heard | 2bfd7df | 2015-10-28 17:34:27 +0000 | [diff] [blame^] | 142 | this.processorFactory = processorFactory; |
| 143 | this.serverTransport = serverTransport; |
| 144 | this.inputTransportFactory = inputTransportFactory; |
| 145 | this.outputTransportFactory = outputTransportFactory; |
| 146 | this.inputProtocolFactory = inputProtocolFactory; |
| 147 | this.outputProtocolFactory = outputProtocolFactory; |
| 148 | this.logDelegate = (logDelegate != null) ? logDelegate : DefaultLogDelegate; |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 149 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 150 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 151 | //Abstract Interface |
| 152 | public abstract void Serve(); |
| 153 | public abstract void Stop(); |
| 154 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 155 | } |