blob: d2eabde121162cc1ba4dd4458ff6fc043fe72b7c [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +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.
18 */
Mark Slee9f0c6512007-02-28 23:58:26 +000019
Mark Sleed788b2e2006-09-07 01:26:35 +000020#ifndef _THRIFT_SERVER_TSERVER_H_
21#define _THRIFT_SERVER_TSERVER_H_ 1
Mark Sleee8540632006-05-30 09:24:40 +000022
Roger Meier49ff8b12012-04-13 09:12:31 +000023#include <thrift/TProcessor.h>
24#include <thrift/transport/TServerTransport.h>
25#include <thrift/protocol/TBinaryProtocol.h>
26#include <thrift/concurrency/Thread.h>
Marc Slemko16698852006-08-04 03:16:10 +000027
cyy316723a2019-01-05 16:35:14 +080028#include <memory>
Mark Sleee8540632006-05-30 09:24:40 +000029
Konrad Grochowski16a23a62014-11-13 15:33:38 +010030namespace apache {
31namespace thrift {
32namespace server {
Marc Slemko6f038a72006-08-03 18:58:09 +000033
T Jake Lucianib5e62212009-01-31 22:36:20 +000034using apache::thrift::TProcessor;
35using apache::thrift::protocol::TBinaryProtocolFactory;
36using apache::thrift::protocol::TProtocol;
37using apache::thrift::protocol::TProtocolFactory;
38using apache::thrift::transport::TServerTransport;
39using apache::thrift::transport::TTransport;
40using apache::thrift::transport::TTransportFactory;
Marc Slemko6f038a72006-08-03 18:58:09 +000041
Mark Sleee8540632006-05-30 09:24:40 +000042/**
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000043 * Virtual interface class that can handle events from the server core. To
44 * use this you should subclass it and implement the methods that you care
45 * about. Your subclass can also store local data that you may care about,
46 * such as additional "arguments" to these methods (stored in the object
47 * instance's state).
48 */
49class TServerEventHandler {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010050public:
Sebastian Zenker042580f2019-01-29 15:48:12 +010051 virtual ~TServerEventHandler() = default;
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000052
53 /**
54 * Called before the server begins.
55 */
56 virtual void preServe() {}
57
58 /**
59 * Called when a new client has connected and is about to being processing.
60 */
cyy316723a2019-01-05 16:35:14 +080061 virtual void* createContext(std::shared_ptr<TProtocol> input,
62 std::shared_ptr<TProtocol> output) {
David Reiss23248712010-10-06 17:10:08 +000063 (void)input;
64 (void)output;
Sebastian Zenker042580f2019-01-29 15:48:12 +010065 return nullptr;
David Reiss23248712010-10-06 17:10:08 +000066 }
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000067
68 /**
David Reiss23248712010-10-06 17:10:08 +000069 * Called when a client has finished request-handling to delete server
70 * context.
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000071 */
David Reiss23248712010-10-06 17:10:08 +000072 virtual void deleteContext(void* serverContext,
cyy316723a2019-01-05 16:35:14 +080073 std::shared_ptr<TProtocol> input,
74 std::shared_ptr<TProtocol> output) {
David Reiss23248712010-10-06 17:10:08 +000075 (void)serverContext;
76 (void)input;
77 (void)output;
78 }
79
80 /**
81 * Called when a client is about to call the processor.
82 */
cyy316723a2019-01-05 16:35:14 +080083 virtual void processContext(void* serverContext, std::shared_ptr<TTransport> transport) {
David Reiss23248712010-10-06 17:10:08 +000084 (void)serverContext;
85 (void)transport;
Konrad Grochowski16a23a62014-11-13 15:33:38 +010086 }
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000087
Konrad Grochowski16a23a62014-11-13 15:33:38 +010088protected:
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000089 /**
90 * Prevent direct instantiation.
91 */
Sebastian Zenker042580f2019-01-29 15:48:12 +010092 TServerEventHandler() = default;
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000093};
94
95/**
Mark Sleee8540632006-05-30 09:24:40 +000096 * Thrift server.
97 *
Mark Sleee8540632006-05-30 09:24:40 +000098 */
Aditya Agarwald622e962006-10-11 02:42:49 +000099class TServer : public concurrency::Runnable {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100100public:
Sebastian Zenker042580f2019-01-29 15:48:12 +0100101 ~TServer() override = default;
Mark Slee4af6ed72006-10-25 19:02:49 +0000102
Mark Slee794993d2006-09-20 01:56:10 +0000103 virtual void serve() = 0;
Aditya Agarwald622e962006-10-11 02:42:49 +0000104
Mark Slee6e3f6372007-03-01 22:05:46 +0000105 virtual void stop() {}
106
Aditya Agarwald622e962006-10-11 02:42:49 +0000107 // Allows running the server as a Runnable thread
Sebastian Zenker042580f2019-01-29 15:48:12 +0100108 void run() override { serve(); }
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000109
cyy316723a2019-01-05 16:35:14 +0800110 std::shared_ptr<TProcessorFactory> getProcessorFactory() { return processorFactory_; }
Aditya Agarwal1ea90522007-01-19 02:02:12 +0000111
cyy316723a2019-01-05 16:35:14 +0800112 std::shared_ptr<TServerTransport> getServerTransport() { return serverTransport_; }
Aditya Agarwal1ea90522007-01-19 02:02:12 +0000113
cyy316723a2019-01-05 16:35:14 +0800114 std::shared_ptr<TTransportFactory> getInputTransportFactory() { return inputTransportFactory_; }
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000115
cyy316723a2019-01-05 16:35:14 +0800116 std::shared_ptr<TTransportFactory> getOutputTransportFactory() {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000117 return outputTransportFactory_;
Aditya Agarwal1ea90522007-01-19 02:02:12 +0000118 }
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000119
cyy316723a2019-01-05 16:35:14 +0800120 std::shared_ptr<TProtocolFactory> getInputProtocolFactory() { return inputProtocolFactory_; }
Mark Slee4af6ed72006-10-25 19:02:49 +0000121
cyy316723a2019-01-05 16:35:14 +0800122 std::shared_ptr<TProtocolFactory> getOutputProtocolFactory() { return outputProtocolFactory_; }
Aditya Agarwal1ea90522007-01-19 02:02:12 +0000123
cyy316723a2019-01-05 16:35:14 +0800124 std::shared_ptr<TServerEventHandler> getEventHandler() { return eventHandler_; }
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000125
Marc Slemko16698852006-08-04 03:16:10 +0000126protected:
cyy316723a2019-01-05 16:35:14 +0800127 TServer(const std::shared_ptr<TProcessorFactory>& processorFactory)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100128 : processorFactory_(processorFactory) {
cyy316723a2019-01-05 16:35:14 +0800129 setInputTransportFactory(std::shared_ptr<TTransportFactory>(new TTransportFactory()));
130 setOutputTransportFactory(std::shared_ptr<TTransportFactory>(new TTransportFactory()));
131 setInputProtocolFactory(std::shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory()));
132 setOutputProtocolFactory(std::shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory()));
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000133 }
134
cyy316723a2019-01-05 16:35:14 +0800135 TServer(const std::shared_ptr<TProcessor>& processor)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100136 : processorFactory_(new TSingletonProcessorFactory(processor)) {
cyy316723a2019-01-05 16:35:14 +0800137 setInputTransportFactory(std::shared_ptr<TTransportFactory>(new TTransportFactory()));
138 setOutputTransportFactory(std::shared_ptr<TTransportFactory>(new TTransportFactory()));
139 setInputProtocolFactory(std::shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory()));
140 setOutputProtocolFactory(std::shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory()));
Konrad Grochowski74260aa2014-11-13 15:33:38 +0100141 }
142
cyy316723a2019-01-05 16:35:14 +0800143 TServer(const std::shared_ptr<TProcessorFactory>& processorFactory,
144 const std::shared_ptr<TServerTransport>& serverTransport)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100145 : processorFactory_(processorFactory), serverTransport_(serverTransport) {
cyy316723a2019-01-05 16:35:14 +0800146 setInputTransportFactory(std::shared_ptr<TTransportFactory>(new TTransportFactory()));
147 setOutputTransportFactory(std::shared_ptr<TTransportFactory>(new TTransportFactory()));
148 setInputProtocolFactory(std::shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory()));
149 setOutputProtocolFactory(std::shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory()));
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100150 }
151
cyy316723a2019-01-05 16:35:14 +0800152 TServer(const std::shared_ptr<TProcessor>& processor,
153 const std::shared_ptr<TServerTransport>& serverTransport)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100154 : processorFactory_(new TSingletonProcessorFactory(processor)),
155 serverTransport_(serverTransport) {
cyy316723a2019-01-05 16:35:14 +0800156 setInputTransportFactory(std::shared_ptr<TTransportFactory>(new TTransportFactory()));
157 setOutputTransportFactory(std::shared_ptr<TTransportFactory>(new TTransportFactory()));
158 setInputProtocolFactory(std::shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory()));
159 setOutputProtocolFactory(std::shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory()));
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100160 }
161
cyy316723a2019-01-05 16:35:14 +0800162 TServer(const std::shared_ptr<TProcessorFactory>& processorFactory,
163 const std::shared_ptr<TServerTransport>& serverTransport,
164 const std::shared_ptr<TTransportFactory>& transportFactory,
165 const std::shared_ptr<TProtocolFactory>& protocolFactory)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100166 : processorFactory_(processorFactory),
167 serverTransport_(serverTransport),
168 inputTransportFactory_(transportFactory),
169 outputTransportFactory_(transportFactory),
170 inputProtocolFactory_(protocolFactory),
171 outputProtocolFactory_(protocolFactory) {}
Bryan Duxbury7a9fb812011-09-01 18:31:53 +0000172
cyy316723a2019-01-05 16:35:14 +0800173 TServer(const std::shared_ptr<TProcessor>& processor,
174 const std::shared_ptr<TServerTransport>& serverTransport,
175 const std::shared_ptr<TTransportFactory>& transportFactory,
176 const std::shared_ptr<TProtocolFactory>& protocolFactory)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100177 : processorFactory_(new TSingletonProcessorFactory(processor)),
178 serverTransport_(serverTransport),
179 inputTransportFactory_(transportFactory),
180 outputTransportFactory_(transportFactory),
181 inputProtocolFactory_(protocolFactory),
182 outputProtocolFactory_(protocolFactory) {}
Mark Sleed788b2e2006-09-07 01:26:35 +0000183
cyy316723a2019-01-05 16:35:14 +0800184 TServer(const std::shared_ptr<TProcessorFactory>& processorFactory,
185 const std::shared_ptr<TServerTransport>& serverTransport,
186 const std::shared_ptr<TTransportFactory>& inputTransportFactory,
187 const std::shared_ptr<TTransportFactory>& outputTransportFactory,
188 const std::shared_ptr<TProtocolFactory>& inputProtocolFactory,
189 const std::shared_ptr<TProtocolFactory>& outputProtocolFactory)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100190 : processorFactory_(processorFactory),
191 serverTransport_(serverTransport),
192 inputTransportFactory_(inputTransportFactory),
193 outputTransportFactory_(outputTransportFactory),
194 inputProtocolFactory_(inputProtocolFactory),
195 outputProtocolFactory_(outputProtocolFactory) {}
Bryan Duxbury7a9fb812011-09-01 18:31:53 +0000196
cyy316723a2019-01-05 16:35:14 +0800197 TServer(const std::shared_ptr<TProcessor>& processor,
198 const std::shared_ptr<TServerTransport>& serverTransport,
199 const std::shared_ptr<TTransportFactory>& inputTransportFactory,
200 const std::shared_ptr<TTransportFactory>& outputTransportFactory,
201 const std::shared_ptr<TProtocolFactory>& inputProtocolFactory,
202 const std::shared_ptr<TProtocolFactory>& outputProtocolFactory)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100203 : processorFactory_(new TSingletonProcessorFactory(processor)),
204 serverTransport_(serverTransport),
205 inputTransportFactory_(inputTransportFactory),
206 outputTransportFactory_(outputTransportFactory),
207 inputProtocolFactory_(inputProtocolFactory),
208 outputProtocolFactory_(outputProtocolFactory) {}
Mark Slee4af6ed72006-10-25 19:02:49 +0000209
Bryan Duxbury6dd9cd02011-09-01 18:06:20 +0000210 /**
211 * Get a TProcessor to handle calls on a particular connection.
212 *
213 * This method should only be called once per connection (never once per
214 * call). This allows the TProcessorFactory to return a different processor
215 * for each connection if it desires.
216 */
cyy316723a2019-01-05 16:35:14 +0800217 std::shared_ptr<TProcessor> getProcessor(std::shared_ptr<TProtocol> inputProtocol,
218 std::shared_ptr<TProtocol> outputProtocol,
219 std::shared_ptr<TTransport> transport) {
Bryan Duxbury6dd9cd02011-09-01 18:06:20 +0000220 TConnectionInfo connInfo;
221 connInfo.input = inputProtocol;
222 connInfo.output = outputProtocol;
223 connInfo.transport = transport;
224 return processorFactory_->getProcessor(connInfo);
225 }
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000226
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000227 // Class variables
cyy316723a2019-01-05 16:35:14 +0800228 std::shared_ptr<TProcessorFactory> processorFactory_;
229 std::shared_ptr<TServerTransport> serverTransport_;
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000230
cyy316723a2019-01-05 16:35:14 +0800231 std::shared_ptr<TTransportFactory> inputTransportFactory_;
232 std::shared_ptr<TTransportFactory> outputTransportFactory_;
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000233
cyy316723a2019-01-05 16:35:14 +0800234 std::shared_ptr<TProtocolFactory> inputProtocolFactory_;
235 std::shared_ptr<TProtocolFactory> outputProtocolFactory_;
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000236
cyy316723a2019-01-05 16:35:14 +0800237 std::shared_ptr<TServerEventHandler> eventHandler_;
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000238
David Reissef22dc62007-11-30 20:38:49 +0000239public:
cyy316723a2019-01-05 16:35:14 +0800240 void setInputTransportFactory(std::shared_ptr<TTransportFactory> inputTransportFactory) {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000241 inputTransportFactory_ = inputTransportFactory;
242 }
243
cyy316723a2019-01-05 16:35:14 +0800244 void setOutputTransportFactory(std::shared_ptr<TTransportFactory> outputTransportFactory) {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000245 outputTransportFactory_ = outputTransportFactory;
246 }
247
cyy316723a2019-01-05 16:35:14 +0800248 void setInputProtocolFactory(std::shared_ptr<TProtocolFactory> inputProtocolFactory) {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000249 inputProtocolFactory_ = inputProtocolFactory;
250 }
251
cyy316723a2019-01-05 16:35:14 +0800252 void setOutputProtocolFactory(std::shared_ptr<TProtocolFactory> outputProtocolFactory) {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000253 outputProtocolFactory_ = outputProtocolFactory;
254 }
255
cyy316723a2019-01-05 16:35:14 +0800256 void setServerEventHandler(std::shared_ptr<TServerEventHandler> eventHandler) {
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000257 eventHandler_ = eventHandler;
258 }
Mark Sleee8540632006-05-30 09:24:40 +0000259};
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000260
veeve01d187c2008-02-26 05:12:08 +0000261/**
262 * Helper function to increase the max file descriptors limit
263 * for the current process and all of its children.
264 * By default, tries to increase it to as much as 2^24.
265 */
Ben Craig96ea9da2013-10-09 15:23:49 -0500266#ifdef HAVE_SYS_RESOURCE_H
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100267int increase_max_fds(int max_fds = (1 << 24));
Ben Craig96ea9da2013-10-09 15:23:49 -0500268#endif
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100269}
270}
271} // apache::thrift::server
Marc Slemko6f038a72006-08-03 18:58:09 +0000272
Mark Sleed788b2e2006-09-07 01:26:35 +0000273#endif // #ifndef _THRIFT_SERVER_TSERVER_H_