blob: cee2ae3e6a5615befd4c0819b7eb39356285eac1 [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 */
David Reiss7f42bcf2008-01-11 20:59:12 +000023
24using System;
David Reiss7f42bcf2008-01-11 20:59:12 +000025using Thrift.Protocol;
26using Thrift.Transport;
David Reiss63191332009-01-06 19:49:22 +000027using System.IO;
David Reiss7f42bcf2008-01-11 20:59:12 +000028
29namespace Thrift.Server
30{
Randy Abernethy0e86f1f2014-07-13 09:50:19 -070031 public abstract class TServer
32 {
33 //Attributes
Jonathan Heard2bfd7df2015-10-28 17:34:27 +000034 protected TProcessorFactory processorFactory;
Randy Abernethy0e86f1f2014-07-13 09:50:19 -070035 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 Reiss7f42bcf2008-01-11 20:59:12 +000041
Randy Abernethy0e86f1f2014-07-13 09:50:19 -070042 //Methods
43 public void setEventHandler(TServerEventHandler seh)
44 {
45 serverEventHandler = seh;
46 }
47 public TServerEventHandler getEventHandler()
48 {
49 return serverEventHandler;
50 }
David Reiss7f42bcf2008-01-11 20:59:12 +000051
Randy Abernethy0e86f1f2014-07-13 09:50:19 -070052 //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 Reiss7f42bcf2008-01-11 20:59:12 +000064
Randy Abernethy0e86f1f2014-07-13 09:50:19 -070065 //Construction
66 public TServer(TProcessor processor,
67 TServerTransport serverTransport)
Jonathan Heard2bfd7df2015-10-28 17:34:27 +000068 : this(processor, serverTransport,
69 new TTransportFactory(),
70 new TTransportFactory(),
71 new TBinaryProtocol.Factory(),
72 new TBinaryProtocol.Factory(),
73 DefaultLogDelegate)
Randy Abernethy0e86f1f2014-07-13 09:50:19 -070074 {
75 }
David Reiss7f42bcf2008-01-11 20:59:12 +000076
Randy Abernethy0e86f1f2014-07-13 09:50:19 -070077 public TServer(TProcessor processor,
78 TServerTransport serverTransport,
79 LogDelegate logDelegate)
Jonathan Heard2bfd7df2015-10-28 17:34:27 +000080 : this(processor,
81 serverTransport,
82 new TTransportFactory(),
83 new TTransportFactory(),
84 new TBinaryProtocol.Factory(),
85 new TBinaryProtocol.Factory(),
86 logDelegate)
Randy Abernethy0e86f1f2014-07-13 09:50:19 -070087 {
88 }
David Reiss7f42bcf2008-01-11 20:59:12 +000089
Randy Abernethy0e86f1f2014-07-13 09:50:19 -070090 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 Geyerd335acd2013-11-11 21:33:54 +0100102
Randy Abernethy0e86f1f2014-07-13 09:50:19 -0700103 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 Reiss7f42bcf2008-01-11 20:59:12 +0000116
Randy Abernethy0e86f1f2014-07-13 09:50:19 -0700117 public TServer(TProcessor processor,
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000118 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 Abernethy0e86f1f2014-07-13 09:50:19 -0700135 TServerTransport serverTransport,
136 TTransportFactory inputTransportFactory,
137 TTransportFactory outputTransportFactory,
138 TProtocolFactory inputProtocolFactory,
139 TProtocolFactory outputProtocolFactory,
140 LogDelegate logDelegate)
141 {
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000142 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 Abernethy0e86f1f2014-07-13 09:50:19 -0700149 }
David Reiss7f42bcf2008-01-11 20:59:12 +0000150
Randy Abernethy0e86f1f2014-07-13 09:50:19 -0700151 //Abstract Interface
152 public abstract void Serve();
153 public abstract void Stop();
154 }
David Reiss7f42bcf2008-01-11 20:59:12 +0000155}