Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 1 | // 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 Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 7 | #ifndef _THRIFT_SERVER_TSERVER_H_ |
| 8 | #define _THRIFT_SERVER_TSERVER_H_ 1 |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 9 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 10 | #include <TProcessor.h> |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 11 | #include <transport/TServerTransport.h> |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 12 | #include <protocol/TBinaryProtocol.h> |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 13 | #include <concurrency/Thread.h> |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 14 | |
| 15 | #include <boost/shared_ptr.hpp> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 16 | |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 17 | namespace facebook { namespace thrift { namespace server { |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 18 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 19 | using facebook::thrift::TProcessor; |
| 20 | using facebook::thrift::protocol::TBinaryProtocolFactory; |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 21 | using facebook::thrift::protocol::TProtocol; |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 22 | using facebook::thrift::protocol::TProtocolFactory; |
| 23 | using facebook::thrift::transport::TServerTransport; |
| 24 | using facebook::thrift::transport::TTransport; |
| 25 | using facebook::thrift::transport::TTransportFactory; |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 26 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 27 | /** |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 28 | * 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 | */ |
| 34 | class 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 Slee | a8de489 | 2008-02-09 00:02:26 +0000 | [diff] [blame] | 47 | virtual void clientBegin(boost::shared_ptr<TProtocol> /* input */, |
| 48 | boost::shared_ptr<TProtocol> /* output */) {} |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Called when a client has finished making requests. |
| 52 | */ |
Mark Slee | a8de489 | 2008-02-09 00:02:26 +0000 | [diff] [blame] | 53 | virtual void clientEnd(boost::shared_ptr<TProtocol> /* input */, |
| 54 | boost::shared_ptr<TProtocol> /* output */) {} |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 55 | |
| 56 | protected: |
| 57 | |
| 58 | /** |
| 59 | * Prevent direct instantiation. |
| 60 | */ |
| 61 | TServerEventHandler() {} |
| 62 | |
| 63 | }; |
| 64 | |
| 65 | /** |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 66 | * Thrift server. |
| 67 | * |
| 68 | * @author Mark Slee <mcslee@facebook.com> |
| 69 | */ |
Aditya Agarwal | d622e96 | 2006-10-11 02:42:49 +0000 | [diff] [blame] | 70 | class TServer : public concurrency::Runnable { |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 71 | public: |
| 72 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 73 | virtual ~TServer() {} |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 74 | |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 75 | virtual void serve() = 0; |
Aditya Agarwal | d622e96 | 2006-10-11 02:42:49 +0000 | [diff] [blame] | 76 | |
Mark Slee | 6e3f637 | 2007-03-01 22:05:46 +0000 | [diff] [blame] | 77 | virtual void stop() {} |
| 78 | |
Aditya Agarwal | d622e96 | 2006-10-11 02:42:49 +0000 | [diff] [blame] | 79 | // Allows running the server as a Runnable thread |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 80 | virtual void run() { |
| 81 | serve(); |
| 82 | } |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 83 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 84 | boost::shared_ptr<TProcessor> getProcessor() { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 85 | return processor_; |
| 86 | } |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 87 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 88 | boost::shared_ptr<TServerTransport> getServerTransport() { |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 89 | return serverTransport_; |
| 90 | } |
| 91 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 92 | boost::shared_ptr<TTransportFactory> getInputTransportFactory() { |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 93 | return inputTransportFactory_; |
| 94 | } |
| 95 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 96 | boost::shared_ptr<TTransportFactory> getOutputTransportFactory() { |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 97 | return outputTransportFactory_; |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 98 | } |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 99 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 100 | boost::shared_ptr<TProtocolFactory> getInputProtocolFactory() { |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 101 | return inputProtocolFactory_; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 104 | boost::shared_ptr<TProtocolFactory> getOutputProtocolFactory() { |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 105 | return outputProtocolFactory_; |
| 106 | } |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 107 | |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 108 | boost::shared_ptr<TServerEventHandler> getEventHandler() { |
| 109 | return eventHandler_; |
| 110 | } |
| 111 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 112 | protected: |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 113 | TServer(boost::shared_ptr<TProcessor> processor): |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 114 | processor_(processor) { |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 115 | 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 Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 121 | TServer(boost::shared_ptr<TProcessor> processor, |
| 122 | boost::shared_ptr<TServerTransport> serverTransport): |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 123 | processor_(processor), |
| 124 | serverTransport_(serverTransport) { |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 125 | 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 Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 131 | 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 Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 135 | processor_(processor), |
| 136 | serverTransport_(serverTransport), |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 137 | inputTransportFactory_(transportFactory), |
| 138 | outputTransportFactory_(transportFactory), |
| 139 | inputProtocolFactory_(protocolFactory), |
| 140 | outputProtocolFactory_(protocolFactory) {} |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 141 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 142 | 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 Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 148 | processor_(processor), |
| 149 | serverTransport_(serverTransport), |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 150 | inputTransportFactory_(inputTransportFactory), |
| 151 | outputTransportFactory_(outputTransportFactory), |
| 152 | inputProtocolFactory_(inputProtocolFactory), |
| 153 | outputProtocolFactory_(outputProtocolFactory) {} |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 154 | |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 155 | |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 156 | // Class variables |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 157 | boost::shared_ptr<TProcessor> processor_; |
| 158 | boost::shared_ptr<TServerTransport> serverTransport_; |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 159 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 160 | boost::shared_ptr<TTransportFactory> inputTransportFactory_; |
| 161 | boost::shared_ptr<TTransportFactory> outputTransportFactory_; |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 162 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 163 | boost::shared_ptr<TProtocolFactory> inputProtocolFactory_; |
| 164 | boost::shared_ptr<TProtocolFactory> outputProtocolFactory_; |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 165 | |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 166 | boost::shared_ptr<TServerEventHandler> eventHandler_; |
| 167 | |
David Reiss | ef22dc6 | 2007-11-30 20:38:49 +0000 | [diff] [blame] | 168 | public: |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 169 | void setInputTransportFactory(boost::shared_ptr<TTransportFactory> inputTransportFactory) { |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 170 | inputTransportFactory_ = inputTransportFactory; |
| 171 | } |
| 172 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 173 | void setOutputTransportFactory(boost::shared_ptr<TTransportFactory> outputTransportFactory) { |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 174 | outputTransportFactory_ = outputTransportFactory; |
| 175 | } |
| 176 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 177 | void setInputProtocolFactory(boost::shared_ptr<TProtocolFactory> inputProtocolFactory) { |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 178 | inputProtocolFactory_ = inputProtocolFactory; |
| 179 | } |
| 180 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 181 | void setOutputProtocolFactory(boost::shared_ptr<TProtocolFactory> outputProtocolFactory) { |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 182 | outputProtocolFactory_ = outputProtocolFactory; |
| 183 | } |
| 184 | |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 185 | void setServerEventHandler(boost::shared_ptr<TServerEventHandler> eventHandler) { |
| 186 | eventHandler_ = eventHandler; |
| 187 | } |
| 188 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 189 | }; |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 190 | |
veeve | 01d187c | 2008-02-26 05:12:08 +0000 | [diff] [blame] | 191 | /** |
| 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 Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 199 | }}} // facebook::thrift::server |
| 200 | |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 201 | #endif // #ifndef _THRIFT_SERVER_TSERVER_H_ |