blob: f05b2ebbf291412bb63400399a9508434fc54458 [file] [log] [blame]
Mark Slee9f0c6512007-02-28 23:58:26 +00001// Copyright (c) 2006- Facebook
2// Distributed under the Thrift Software License
3//
4// See accompanying file LICENSE or visit the Thrift site at:
5// http://developers.facebook.com/thrift/
6
Mark Sleed788b2e2006-09-07 01:26:35 +00007#ifndef _THRIFT_SERVER_TSERVER_H_
8#define _THRIFT_SERVER_TSERVER_H_ 1
Mark Sleee8540632006-05-30 09:24:40 +00009
Marc Slemko16698852006-08-04 03:16:10 +000010#include <TProcessor.h>
Mark Sleed788b2e2006-09-07 01:26:35 +000011#include <transport/TServerTransport.h>
Mark Slee4af6ed72006-10-25 19:02:49 +000012#include <protocol/TBinaryProtocol.h>
Marc Slemko3ea00332006-08-17 01:11:13 +000013#include <concurrency/Thread.h>
Marc Slemko16698852006-08-04 03:16:10 +000014
15#include <boost/shared_ptr.hpp>
Mark Sleee8540632006-05-30 09:24:40 +000016
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000017namespace facebook { namespace thrift { namespace server {
Marc Slemko6f038a72006-08-03 18:58:09 +000018
Mark Slee5ea15f92007-03-05 22:55:59 +000019using facebook::thrift::TProcessor;
20using facebook::thrift::protocol::TBinaryProtocolFactory;
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000021using facebook::thrift::protocol::TProtocol;
Mark Slee5ea15f92007-03-05 22:55:59 +000022using facebook::thrift::protocol::TProtocolFactory;
23using facebook::thrift::transport::TServerTransport;
24using facebook::thrift::transport::TTransport;
25using facebook::thrift::transport::TTransportFactory;
Marc Slemko6f038a72006-08-03 18:58:09 +000026
Mark Sleee8540632006-05-30 09:24:40 +000027/**
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000028 * Virtual interface class that can handle events from the server core. To
29 * use this you should subclass it and implement the methods that you care
30 * about. Your subclass can also store local data that you may care about,
31 * such as additional "arguments" to these methods (stored in the object
32 * instance's state).
33 */
34class TServerEventHandler {
35 public:
36
37 virtual ~TServerEventHandler() {}
38
39 /**
40 * Called before the server begins.
41 */
42 virtual void preServe() {}
43
44 /**
45 * Called when a new client has connected and is about to being processing.
46 */
Mark Sleea8de4892008-02-09 00:02:26 +000047 virtual void clientBegin(boost::shared_ptr<TProtocol> /* input */,
48 boost::shared_ptr<TProtocol> /* output */) {}
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000049
50 /**
51 * Called when a client has finished making requests.
52 */
Mark Sleea8de4892008-02-09 00:02:26 +000053 virtual void clientEnd(boost::shared_ptr<TProtocol> /* input */,
54 boost::shared_ptr<TProtocol> /* output */) {}
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000055
56 protected:
57
58 /**
59 * Prevent direct instantiation.
60 */
61 TServerEventHandler() {}
62
63};
64
65/**
Mark Sleee8540632006-05-30 09:24:40 +000066 * Thrift server.
67 *
68 * @author Mark Slee <mcslee@facebook.com>
69 */
Aditya Agarwald622e962006-10-11 02:42:49 +000070class TServer : public concurrency::Runnable {
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000071 public:
72
Mark Sleee8540632006-05-30 09:24:40 +000073 virtual ~TServer() {}
Mark Slee4af6ed72006-10-25 19:02:49 +000074
Mark Slee794993d2006-09-20 01:56:10 +000075 virtual void serve() = 0;
Aditya Agarwald622e962006-10-11 02:42:49 +000076
Mark Slee6e3f6372007-03-01 22:05:46 +000077 virtual void stop() {}
78
Aditya Agarwald622e962006-10-11 02:42:49 +000079 // Allows running the server as a Runnable thread
Mark Slee4af6ed72006-10-25 19:02:49 +000080 virtual void run() {
81 serve();
82 }
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000083
Mark Slee5ea15f92007-03-05 22:55:59 +000084 boost::shared_ptr<TProcessor> getProcessor() {
Mark Slee2f6404d2006-10-10 01:37:40 +000085 return processor_;
86 }
Aditya Agarwal1ea90522007-01-19 02:02:12 +000087
Mark Slee5ea15f92007-03-05 22:55:59 +000088 boost::shared_ptr<TServerTransport> getServerTransport() {
Aditya Agarwal1ea90522007-01-19 02:02:12 +000089 return serverTransport_;
90 }
91
Mark Slee5ea15f92007-03-05 22:55:59 +000092 boost::shared_ptr<TTransportFactory> getInputTransportFactory() {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000093 return inputTransportFactory_;
94 }
95
Mark Slee5ea15f92007-03-05 22:55:59 +000096 boost::shared_ptr<TTransportFactory> getOutputTransportFactory() {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000097 return outputTransportFactory_;
Aditya Agarwal1ea90522007-01-19 02:02:12 +000098 }
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000099
Mark Slee5ea15f92007-03-05 22:55:59 +0000100 boost::shared_ptr<TProtocolFactory> getInputProtocolFactory() {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000101 return inputProtocolFactory_;
Mark Slee4af6ed72006-10-25 19:02:49 +0000102 }
103
Mark Slee5ea15f92007-03-05 22:55:59 +0000104 boost::shared_ptr<TProtocolFactory> getOutputProtocolFactory() {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000105 return outputProtocolFactory_;
106 }
Aditya Agarwal1ea90522007-01-19 02:02:12 +0000107
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000108 boost::shared_ptr<TServerEventHandler> getEventHandler() {
109 return eventHandler_;
110 }
111
Marc Slemko16698852006-08-04 03:16:10 +0000112protected:
Mark Slee5ea15f92007-03-05 22:55:59 +0000113 TServer(boost::shared_ptr<TProcessor> processor):
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000114 processor_(processor) {
Mark Slee5ea15f92007-03-05 22:55:59 +0000115 setInputTransportFactory(boost::shared_ptr<TTransportFactory>(new TTransportFactory()));
116 setOutputTransportFactory(boost::shared_ptr<TTransportFactory>(new TTransportFactory()));
117 setInputProtocolFactory(boost::shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory()));
118 setOutputProtocolFactory(boost::shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory()));
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000119 }
120
Mark Slee5ea15f92007-03-05 22:55:59 +0000121 TServer(boost::shared_ptr<TProcessor> processor,
122 boost::shared_ptr<TServerTransport> serverTransport):
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000123 processor_(processor),
124 serverTransport_(serverTransport) {
Mark Slee5ea15f92007-03-05 22:55:59 +0000125 setInputTransportFactory(boost::shared_ptr<TTransportFactory>(new TTransportFactory()));
126 setOutputTransportFactory(boost::shared_ptr<TTransportFactory>(new TTransportFactory()));
127 setInputProtocolFactory(boost::shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory()));
128 setOutputProtocolFactory(boost::shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory()));
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000129 }
130
Mark Slee5ea15f92007-03-05 22:55:59 +0000131 TServer(boost::shared_ptr<TProcessor> processor,
132 boost::shared_ptr<TServerTransport> serverTransport,
133 boost::shared_ptr<TTransportFactory> transportFactory,
134 boost::shared_ptr<TProtocolFactory> protocolFactory):
Mark Sleed788b2e2006-09-07 01:26:35 +0000135 processor_(processor),
136 serverTransport_(serverTransport),
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000137 inputTransportFactory_(transportFactory),
138 outputTransportFactory_(transportFactory),
139 inputProtocolFactory_(protocolFactory),
140 outputProtocolFactory_(protocolFactory) {}
Mark Sleed788b2e2006-09-07 01:26:35 +0000141
Mark Slee5ea15f92007-03-05 22:55:59 +0000142 TServer(boost::shared_ptr<TProcessor> processor,
143 boost::shared_ptr<TServerTransport> serverTransport,
144 boost::shared_ptr<TTransportFactory> inputTransportFactory,
145 boost::shared_ptr<TTransportFactory> outputTransportFactory,
146 boost::shared_ptr<TProtocolFactory> inputProtocolFactory,
147 boost::shared_ptr<TProtocolFactory> outputProtocolFactory):
Mark Slee4af6ed72006-10-25 19:02:49 +0000148 processor_(processor),
149 serverTransport_(serverTransport),
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000150 inputTransportFactory_(inputTransportFactory),
151 outputTransportFactory_(outputTransportFactory),
152 inputProtocolFactory_(inputProtocolFactory),
153 outputProtocolFactory_(outputProtocolFactory) {}
Mark Slee4af6ed72006-10-25 19:02:49 +0000154
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000155
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000156 // Class variables
Mark Slee5ea15f92007-03-05 22:55:59 +0000157 boost::shared_ptr<TProcessor> processor_;
158 boost::shared_ptr<TServerTransport> serverTransport_;
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000159
Mark Slee5ea15f92007-03-05 22:55:59 +0000160 boost::shared_ptr<TTransportFactory> inputTransportFactory_;
161 boost::shared_ptr<TTransportFactory> outputTransportFactory_;
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000162
Mark Slee5ea15f92007-03-05 22:55:59 +0000163 boost::shared_ptr<TProtocolFactory> inputProtocolFactory_;
164 boost::shared_ptr<TProtocolFactory> outputProtocolFactory_;
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000165
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000166 boost::shared_ptr<TServerEventHandler> eventHandler_;
167
David Reissef22dc62007-11-30 20:38:49 +0000168public:
Mark Slee5ea15f92007-03-05 22:55:59 +0000169 void setInputTransportFactory(boost::shared_ptr<TTransportFactory> inputTransportFactory) {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000170 inputTransportFactory_ = inputTransportFactory;
171 }
172
Mark Slee5ea15f92007-03-05 22:55:59 +0000173 void setOutputTransportFactory(boost::shared_ptr<TTransportFactory> outputTransportFactory) {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000174 outputTransportFactory_ = outputTransportFactory;
175 }
176
Mark Slee5ea15f92007-03-05 22:55:59 +0000177 void setInputProtocolFactory(boost::shared_ptr<TProtocolFactory> inputProtocolFactory) {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000178 inputProtocolFactory_ = inputProtocolFactory;
179 }
180
Mark Slee5ea15f92007-03-05 22:55:59 +0000181 void setOutputProtocolFactory(boost::shared_ptr<TProtocolFactory> outputProtocolFactory) {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000182 outputProtocolFactory_ = outputProtocolFactory;
183 }
184
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000185 void setServerEventHandler(boost::shared_ptr<TServerEventHandler> eventHandler) {
186 eventHandler_ = eventHandler;
187 }
188
Mark Sleee8540632006-05-30 09:24:40 +0000189};
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000190
veeve01d187c2008-02-26 05:12:08 +0000191/**
192 * Helper function to increase the max file descriptors limit
193 * for the current process and all of its children.
194 * By default, tries to increase it to as much as 2^24.
195 */
196 int increase_max_fds(int max_fds=(1<<24));
197
198
Marc Slemko6f038a72006-08-03 18:58:09 +0000199}}} // facebook::thrift::server
200
Mark Sleed788b2e2006-09-07 01:26:35 +0000201#endif // #ifndef _THRIFT_SERVER_TSERVER_H_