blob: af9da1f5116f81eb8ca56633637e2c0413066cb2 [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
T Jake Lucianib5e62212009-01-31 22:36:20 +000017namespace apache { namespace thrift { namespace server {
Marc Slemko6f038a72006-08-03 18:58:09 +000018
T Jake Lucianib5e62212009-01-31 22:36:20 +000019using apache::thrift::TProcessor;
20using apache::thrift::protocol::TBinaryProtocolFactory;
21using apache::thrift::protocol::TProtocol;
22using apache::thrift::protocol::TProtocolFactory;
23using apache::thrift::transport::TServerTransport;
24using apache::thrift::transport::TTransport;
25using apache::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 *
Mark Sleee8540632006-05-30 09:24:40 +000068 */
Aditya Agarwald622e962006-10-11 02:42:49 +000069class TServer : public concurrency::Runnable {
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000070 public:
71
Mark Sleee8540632006-05-30 09:24:40 +000072 virtual ~TServer() {}
Mark Slee4af6ed72006-10-25 19:02:49 +000073
Mark Slee794993d2006-09-20 01:56:10 +000074 virtual void serve() = 0;
Aditya Agarwald622e962006-10-11 02:42:49 +000075
Mark Slee6e3f6372007-03-01 22:05:46 +000076 virtual void stop() {}
77
Aditya Agarwald622e962006-10-11 02:42:49 +000078 // Allows running the server as a Runnable thread
Mark Slee4af6ed72006-10-25 19:02:49 +000079 virtual void run() {
80 serve();
81 }
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000082
Mark Slee5ea15f92007-03-05 22:55:59 +000083 boost::shared_ptr<TProcessor> getProcessor() {
Mark Slee2f6404d2006-10-10 01:37:40 +000084 return processor_;
85 }
Aditya Agarwal1ea90522007-01-19 02:02:12 +000086
Mark Slee5ea15f92007-03-05 22:55:59 +000087 boost::shared_ptr<TServerTransport> getServerTransport() {
Aditya Agarwal1ea90522007-01-19 02:02:12 +000088 return serverTransport_;
89 }
90
Mark Slee5ea15f92007-03-05 22:55:59 +000091 boost::shared_ptr<TTransportFactory> getInputTransportFactory() {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000092 return inputTransportFactory_;
93 }
94
Mark Slee5ea15f92007-03-05 22:55:59 +000095 boost::shared_ptr<TTransportFactory> getOutputTransportFactory() {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000096 return outputTransportFactory_;
Aditya Agarwal1ea90522007-01-19 02:02:12 +000097 }
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000098
Mark Slee5ea15f92007-03-05 22:55:59 +000099 boost::shared_ptr<TProtocolFactory> getInputProtocolFactory() {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000100 return inputProtocolFactory_;
Mark Slee4af6ed72006-10-25 19:02:49 +0000101 }
102
Mark Slee5ea15f92007-03-05 22:55:59 +0000103 boost::shared_ptr<TProtocolFactory> getOutputProtocolFactory() {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000104 return outputProtocolFactory_;
105 }
Aditya Agarwal1ea90522007-01-19 02:02:12 +0000106
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000107 boost::shared_ptr<TServerEventHandler> getEventHandler() {
108 return eventHandler_;
109 }
110
Marc Slemko16698852006-08-04 03:16:10 +0000111protected:
Mark Slee5ea15f92007-03-05 22:55:59 +0000112 TServer(boost::shared_ptr<TProcessor> processor):
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000113 processor_(processor) {
Mark Slee5ea15f92007-03-05 22:55:59 +0000114 setInputTransportFactory(boost::shared_ptr<TTransportFactory>(new TTransportFactory()));
115 setOutputTransportFactory(boost::shared_ptr<TTransportFactory>(new TTransportFactory()));
116 setInputProtocolFactory(boost::shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory()));
117 setOutputProtocolFactory(boost::shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory()));
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000118 }
119
Mark Slee5ea15f92007-03-05 22:55:59 +0000120 TServer(boost::shared_ptr<TProcessor> processor,
121 boost::shared_ptr<TServerTransport> serverTransport):
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000122 processor_(processor),
123 serverTransport_(serverTransport) {
Mark Slee5ea15f92007-03-05 22:55:59 +0000124 setInputTransportFactory(boost::shared_ptr<TTransportFactory>(new TTransportFactory()));
125 setOutputTransportFactory(boost::shared_ptr<TTransportFactory>(new TTransportFactory()));
126 setInputProtocolFactory(boost::shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory()));
127 setOutputProtocolFactory(boost::shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory()));
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000128 }
129
Mark Slee5ea15f92007-03-05 22:55:59 +0000130 TServer(boost::shared_ptr<TProcessor> processor,
131 boost::shared_ptr<TServerTransport> serverTransport,
132 boost::shared_ptr<TTransportFactory> transportFactory,
133 boost::shared_ptr<TProtocolFactory> protocolFactory):
Mark Sleed788b2e2006-09-07 01:26:35 +0000134 processor_(processor),
135 serverTransport_(serverTransport),
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000136 inputTransportFactory_(transportFactory),
137 outputTransportFactory_(transportFactory),
138 inputProtocolFactory_(protocolFactory),
139 outputProtocolFactory_(protocolFactory) {}
Mark Sleed788b2e2006-09-07 01:26:35 +0000140
Mark Slee5ea15f92007-03-05 22:55:59 +0000141 TServer(boost::shared_ptr<TProcessor> processor,
142 boost::shared_ptr<TServerTransport> serverTransport,
143 boost::shared_ptr<TTransportFactory> inputTransportFactory,
144 boost::shared_ptr<TTransportFactory> outputTransportFactory,
145 boost::shared_ptr<TProtocolFactory> inputProtocolFactory,
146 boost::shared_ptr<TProtocolFactory> outputProtocolFactory):
Mark Slee4af6ed72006-10-25 19:02:49 +0000147 processor_(processor),
148 serverTransport_(serverTransport),
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000149 inputTransportFactory_(inputTransportFactory),
150 outputTransportFactory_(outputTransportFactory),
151 inputProtocolFactory_(inputProtocolFactory),
152 outputProtocolFactory_(outputProtocolFactory) {}
Mark Slee4af6ed72006-10-25 19:02:49 +0000153
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000154
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000155 // Class variables
Mark Slee5ea15f92007-03-05 22:55:59 +0000156 boost::shared_ptr<TProcessor> processor_;
157 boost::shared_ptr<TServerTransport> serverTransport_;
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000158
Mark Slee5ea15f92007-03-05 22:55:59 +0000159 boost::shared_ptr<TTransportFactory> inputTransportFactory_;
160 boost::shared_ptr<TTransportFactory> outputTransportFactory_;
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000161
Mark Slee5ea15f92007-03-05 22:55:59 +0000162 boost::shared_ptr<TProtocolFactory> inputProtocolFactory_;
163 boost::shared_ptr<TProtocolFactory> outputProtocolFactory_;
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000164
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000165 boost::shared_ptr<TServerEventHandler> eventHandler_;
166
David Reissef22dc62007-11-30 20:38:49 +0000167public:
Mark Slee5ea15f92007-03-05 22:55:59 +0000168 void setInputTransportFactory(boost::shared_ptr<TTransportFactory> inputTransportFactory) {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000169 inputTransportFactory_ = inputTransportFactory;
170 }
171
Mark Slee5ea15f92007-03-05 22:55:59 +0000172 void setOutputTransportFactory(boost::shared_ptr<TTransportFactory> outputTransportFactory) {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000173 outputTransportFactory_ = outputTransportFactory;
174 }
175
Mark Slee5ea15f92007-03-05 22:55:59 +0000176 void setInputProtocolFactory(boost::shared_ptr<TProtocolFactory> inputProtocolFactory) {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000177 inputProtocolFactory_ = inputProtocolFactory;
178 }
179
Mark Slee5ea15f92007-03-05 22:55:59 +0000180 void setOutputProtocolFactory(boost::shared_ptr<TProtocolFactory> outputProtocolFactory) {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000181 outputProtocolFactory_ = outputProtocolFactory;
182 }
183
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000184 void setServerEventHandler(boost::shared_ptr<TServerEventHandler> eventHandler) {
185 eventHandler_ = eventHandler;
186 }
187
Mark Sleee8540632006-05-30 09:24:40 +0000188};
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000189
veeve01d187c2008-02-26 05:12:08 +0000190/**
191 * Helper function to increase the max file descriptors limit
192 * for the current process and all of its children.
193 * By default, tries to increase it to as much as 2^24.
194 */
195 int increase_max_fds(int max_fds=(1<<24));
196
197
T Jake Lucianib5e62212009-01-31 22:36:20 +0000198}}} // apache::thrift::server
Marc Slemko6f038a72006-08-03 18:58:09 +0000199
Mark Sleed788b2e2006-09-07 01:26:35 +0000200#endif // #ifndef _THRIFT_SERVER_TSERVER_H_