blob: 6bd1398ac27bf85435fa083621532df7830d52c8 [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
Marc Slemko16698852006-08-04 03:16:10 +000023#include <TProcessor.h>
Mark Sleed788b2e2006-09-07 01:26:35 +000024#include <transport/TServerTransport.h>
Mark Slee4af6ed72006-10-25 19:02:49 +000025#include <protocol/TBinaryProtocol.h>
Marc Slemko3ea00332006-08-17 01:11:13 +000026#include <concurrency/Thread.h>
Marc Slemko16698852006-08-04 03:16:10 +000027
28#include <boost/shared_ptr.hpp>
Mark Sleee8540632006-05-30 09:24:40 +000029
T Jake Lucianib5e62212009-01-31 22:36:20 +000030namespace apache { namespace thrift { namespace server {
Marc Slemko6f038a72006-08-03 18:58:09 +000031
T Jake Lucianib5e62212009-01-31 22:36:20 +000032using apache::thrift::TProcessor;
33using apache::thrift::protocol::TBinaryProtocolFactory;
34using apache::thrift::protocol::TProtocol;
35using apache::thrift::protocol::TProtocolFactory;
36using apache::thrift::transport::TServerTransport;
37using apache::thrift::transport::TTransport;
38using apache::thrift::transport::TTransportFactory;
Marc Slemko6f038a72006-08-03 18:58:09 +000039
Mark Sleee8540632006-05-30 09:24:40 +000040/**
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000041 * Virtual interface class that can handle events from the server core. To
42 * use this you should subclass it and implement the methods that you care
43 * about. Your subclass can also store local data that you may care about,
44 * such as additional "arguments" to these methods (stored in the object
45 * instance's state).
46 */
47class TServerEventHandler {
48 public:
49
50 virtual ~TServerEventHandler() {}
51
52 /**
53 * Called before the server begins.
54 */
55 virtual void preServe() {}
56
57 /**
58 * Called when a new client has connected and is about to being processing.
59 */
David Reiss23248712010-10-06 17:10:08 +000060 virtual void* createContext(boost::shared_ptr<TProtocol> input,
61 boost::shared_ptr<TProtocol> output) {
62 (void)input;
63 (void)output;
64 return NULL;
65 }
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000066
67 /**
David Reiss23248712010-10-06 17:10:08 +000068 * Called when a client has finished request-handling to delete server
69 * context.
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000070 */
David Reiss23248712010-10-06 17:10:08 +000071 virtual void deleteContext(void* serverContext,
72 boost::shared_ptr<TProtocol>input,
73 boost::shared_ptr<TProtocol>output) {
74 (void)serverContext;
75 (void)input;
76 (void)output;
77 }
78
79 /**
80 * Called when a client is about to call the processor.
81 */
82 virtual void processContext(void* serverContext,
83 boost::shared_ptr<TTransport> transport) {
84 (void)serverContext;
85 (void)transport;
86}
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000087
88 protected:
89
90 /**
91 * Prevent direct instantiation.
92 */
93 TServerEventHandler() {}
94
95};
96
97/**
Mark Sleee8540632006-05-30 09:24:40 +000098 * Thrift server.
99 *
Mark Sleee8540632006-05-30 09:24:40 +0000100 */
Aditya Agarwald622e962006-10-11 02:42:49 +0000101class TServer : public concurrency::Runnable {
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000102 public:
103
Mark Sleee8540632006-05-30 09:24:40 +0000104 virtual ~TServer() {}
Mark Slee4af6ed72006-10-25 19:02:49 +0000105
Mark Slee794993d2006-09-20 01:56:10 +0000106 virtual void serve() = 0;
Aditya Agarwald622e962006-10-11 02:42:49 +0000107
Mark Slee6e3f6372007-03-01 22:05:46 +0000108 virtual void stop() {}
109
Aditya Agarwald622e962006-10-11 02:42:49 +0000110 // Allows running the server as a Runnable thread
Mark Slee4af6ed72006-10-25 19:02:49 +0000111 virtual void run() {
112 serve();
113 }
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000114
Bryan Duxbury6dd9cd02011-09-01 18:06:20 +0000115 boost::shared_ptr<TProcessorFactory> getProcessorFactory() {
116 return processorFactory_;
Mark Slee2f6404d2006-10-10 01:37:40 +0000117 }
Aditya Agarwal1ea90522007-01-19 02:02:12 +0000118
Mark Slee5ea15f92007-03-05 22:55:59 +0000119 boost::shared_ptr<TServerTransport> getServerTransport() {
Aditya Agarwal1ea90522007-01-19 02:02:12 +0000120 return serverTransport_;
121 }
122
Mark Slee5ea15f92007-03-05 22:55:59 +0000123 boost::shared_ptr<TTransportFactory> getInputTransportFactory() {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000124 return inputTransportFactory_;
125 }
126
Mark Slee5ea15f92007-03-05 22:55:59 +0000127 boost::shared_ptr<TTransportFactory> getOutputTransportFactory() {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000128 return outputTransportFactory_;
Aditya Agarwal1ea90522007-01-19 02:02:12 +0000129 }
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000130
Mark Slee5ea15f92007-03-05 22:55:59 +0000131 boost::shared_ptr<TProtocolFactory> getInputProtocolFactory() {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000132 return inputProtocolFactory_;
Mark Slee4af6ed72006-10-25 19:02:49 +0000133 }
134
Mark Slee5ea15f92007-03-05 22:55:59 +0000135 boost::shared_ptr<TProtocolFactory> getOutputProtocolFactory() {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000136 return outputProtocolFactory_;
137 }
Aditya Agarwal1ea90522007-01-19 02:02:12 +0000138
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000139 boost::shared_ptr<TServerEventHandler> getEventHandler() {
140 return eventHandler_;
141 }
142
Marc Slemko16698852006-08-04 03:16:10 +0000143protected:
Mark Slee5ea15f92007-03-05 22:55:59 +0000144 TServer(boost::shared_ptr<TProcessor> processor):
Bryan Duxbury6dd9cd02011-09-01 18:06:20 +0000145 processorFactory_(new TSingletonProcessorFactory(processor)) {
Mark Slee5ea15f92007-03-05 22:55:59 +0000146 setInputTransportFactory(boost::shared_ptr<TTransportFactory>(new TTransportFactory()));
147 setOutputTransportFactory(boost::shared_ptr<TTransportFactory>(new TTransportFactory()));
148 setInputProtocolFactory(boost::shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory()));
149 setOutputProtocolFactory(boost::shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory()));
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000150 }
151
Mark Slee5ea15f92007-03-05 22:55:59 +0000152 TServer(boost::shared_ptr<TProcessor> processor,
153 boost::shared_ptr<TServerTransport> serverTransport):
Bryan Duxbury6dd9cd02011-09-01 18:06:20 +0000154 processorFactory_(new TSingletonProcessorFactory(processor)),
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000155 serverTransport_(serverTransport) {
Mark Slee5ea15f92007-03-05 22:55:59 +0000156 setInputTransportFactory(boost::shared_ptr<TTransportFactory>(new TTransportFactory()));
157 setOutputTransportFactory(boost::shared_ptr<TTransportFactory>(new TTransportFactory()));
158 setInputProtocolFactory(boost::shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory()));
159 setOutputProtocolFactory(boost::shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory()));
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000160 }
161
Mark Slee5ea15f92007-03-05 22:55:59 +0000162 TServer(boost::shared_ptr<TProcessor> processor,
163 boost::shared_ptr<TServerTransport> serverTransport,
164 boost::shared_ptr<TTransportFactory> transportFactory,
165 boost::shared_ptr<TProtocolFactory> protocolFactory):
Bryan Duxbury6dd9cd02011-09-01 18:06:20 +0000166 processorFactory_(new TSingletonProcessorFactory(processor)),
Mark Sleed788b2e2006-09-07 01:26:35 +0000167 serverTransport_(serverTransport),
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000168 inputTransportFactory_(transportFactory),
169 outputTransportFactory_(transportFactory),
170 inputProtocolFactory_(protocolFactory),
171 outputProtocolFactory_(protocolFactory) {}
Mark Sleed788b2e2006-09-07 01:26:35 +0000172
Mark Slee5ea15f92007-03-05 22:55:59 +0000173 TServer(boost::shared_ptr<TProcessor> processor,
174 boost::shared_ptr<TServerTransport> serverTransport,
175 boost::shared_ptr<TTransportFactory> inputTransportFactory,
176 boost::shared_ptr<TTransportFactory> outputTransportFactory,
177 boost::shared_ptr<TProtocolFactory> inputProtocolFactory,
178 boost::shared_ptr<TProtocolFactory> outputProtocolFactory):
Bryan Duxbury6dd9cd02011-09-01 18:06:20 +0000179 processorFactory_(new TSingletonProcessorFactory(processor)),
Mark Slee4af6ed72006-10-25 19:02:49 +0000180 serverTransport_(serverTransport),
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000181 inputTransportFactory_(inputTransportFactory),
182 outputTransportFactory_(outputTransportFactory),
183 inputProtocolFactory_(inputProtocolFactory),
184 outputProtocolFactory_(outputProtocolFactory) {}
Mark Slee4af6ed72006-10-25 19:02:49 +0000185
Bryan Duxbury6dd9cd02011-09-01 18:06:20 +0000186 /**
187 * Get a TProcessor to handle calls on a particular connection.
188 *
189 * This method should only be called once per connection (never once per
190 * call). This allows the TProcessorFactory to return a different processor
191 * for each connection if it desires.
192 */
193 boost::shared_ptr<TProcessor> getProcessor(
194 boost::shared_ptr<TProtocol> inputProtocol,
195 boost::shared_ptr<TProtocol> outputProtocol,
196 boost::shared_ptr<TTransport> transport) {
197 TConnectionInfo connInfo;
198 connInfo.input = inputProtocol;
199 connInfo.output = outputProtocol;
200 connInfo.transport = transport;
201 return processorFactory_->getProcessor(connInfo);
202 }
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000203
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000204 // Class variables
Bryan Duxbury6dd9cd02011-09-01 18:06:20 +0000205 boost::shared_ptr<TProcessorFactory> processorFactory_;
Mark Slee5ea15f92007-03-05 22:55:59 +0000206 boost::shared_ptr<TServerTransport> serverTransport_;
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000207
Mark Slee5ea15f92007-03-05 22:55:59 +0000208 boost::shared_ptr<TTransportFactory> inputTransportFactory_;
209 boost::shared_ptr<TTransportFactory> outputTransportFactory_;
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000210
Mark Slee5ea15f92007-03-05 22:55:59 +0000211 boost::shared_ptr<TProtocolFactory> inputProtocolFactory_;
212 boost::shared_ptr<TProtocolFactory> outputProtocolFactory_;
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000213
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000214 boost::shared_ptr<TServerEventHandler> eventHandler_;
215
David Reissef22dc62007-11-30 20:38:49 +0000216public:
Mark Slee5ea15f92007-03-05 22:55:59 +0000217 void setInputTransportFactory(boost::shared_ptr<TTransportFactory> inputTransportFactory) {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000218 inputTransportFactory_ = inputTransportFactory;
219 }
220
Mark Slee5ea15f92007-03-05 22:55:59 +0000221 void setOutputTransportFactory(boost::shared_ptr<TTransportFactory> outputTransportFactory) {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000222 outputTransportFactory_ = outputTransportFactory;
223 }
224
Mark Slee5ea15f92007-03-05 22:55:59 +0000225 void setInputProtocolFactory(boost::shared_ptr<TProtocolFactory> inputProtocolFactory) {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000226 inputProtocolFactory_ = inputProtocolFactory;
227 }
228
Mark Slee5ea15f92007-03-05 22:55:59 +0000229 void setOutputProtocolFactory(boost::shared_ptr<TProtocolFactory> outputProtocolFactory) {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000230 outputProtocolFactory_ = outputProtocolFactory;
231 }
232
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000233 void setServerEventHandler(boost::shared_ptr<TServerEventHandler> eventHandler) {
234 eventHandler_ = eventHandler;
235 }
236
Mark Sleee8540632006-05-30 09:24:40 +0000237};
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000238
veeve01d187c2008-02-26 05:12:08 +0000239/**
240 * Helper function to increase the max file descriptors limit
241 * for the current process and all of its children.
242 * By default, tries to increase it to as much as 2^24.
243 */
244 int increase_max_fds(int max_fds=(1<<24));
245
246
T Jake Lucianib5e62212009-01-31 22:36:20 +0000247}}} // apache::thrift::server
Marc Slemko6f038a72006-08-03 18:58:09 +0000248
Mark Sleed788b2e2006-09-07 01:26:35 +0000249#endif // #ifndef _THRIFT_SERVER_TSERVER_H_