David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 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 Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 19 | |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 20 | #ifndef _THRIFT_SERVER_TSERVER_H_ |
| 21 | #define _THRIFT_SERVER_TSERVER_H_ 1 |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 22 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 23 | #include <TProcessor.h> |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 24 | #include <transport/TServerTransport.h> |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 25 | #include <protocol/TBinaryProtocol.h> |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 26 | #include <concurrency/Thread.h> |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 27 | |
| 28 | #include <boost/shared_ptr.hpp> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 29 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 30 | namespace apache { namespace thrift { namespace server { |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 31 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 32 | using apache::thrift::TProcessor; |
| 33 | using apache::thrift::protocol::TBinaryProtocolFactory; |
| 34 | using apache::thrift::protocol::TProtocol; |
| 35 | using apache::thrift::protocol::TProtocolFactory; |
| 36 | using apache::thrift::transport::TServerTransport; |
| 37 | using apache::thrift::transport::TTransport; |
| 38 | using apache::thrift::transport::TTransportFactory; |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 39 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 40 | /** |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 41 | * 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 | */ |
| 47 | class 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 | */ |
Mark Slee | a8de489 | 2008-02-09 00:02:26 +0000 | [diff] [blame] | 60 | virtual void clientBegin(boost::shared_ptr<TProtocol> /* input */, |
| 61 | boost::shared_ptr<TProtocol> /* output */) {} |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * Called when a client has finished making requests. |
| 65 | */ |
Mark Slee | a8de489 | 2008-02-09 00:02:26 +0000 | [diff] [blame] | 66 | virtual void clientEnd(boost::shared_ptr<TProtocol> /* input */, |
| 67 | boost::shared_ptr<TProtocol> /* output */) {} |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 68 | |
| 69 | protected: |
| 70 | |
| 71 | /** |
| 72 | * Prevent direct instantiation. |
| 73 | */ |
| 74 | TServerEventHandler() {} |
| 75 | |
| 76 | }; |
| 77 | |
| 78 | /** |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 79 | * Thrift server. |
| 80 | * |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 81 | */ |
Aditya Agarwal | d622e96 | 2006-10-11 02:42:49 +0000 | [diff] [blame] | 82 | class TServer : public concurrency::Runnable { |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 83 | public: |
| 84 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 85 | virtual ~TServer() {} |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 86 | |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 87 | virtual void serve() = 0; |
Aditya Agarwal | d622e96 | 2006-10-11 02:42:49 +0000 | [diff] [blame] | 88 | |
Mark Slee | 6e3f637 | 2007-03-01 22:05:46 +0000 | [diff] [blame] | 89 | virtual void stop() {} |
| 90 | |
Aditya Agarwal | d622e96 | 2006-10-11 02:42:49 +0000 | [diff] [blame] | 91 | // Allows running the server as a Runnable thread |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 92 | virtual void run() { |
| 93 | serve(); |
| 94 | } |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 95 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 96 | boost::shared_ptr<TProcessor> getProcessor() { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 97 | return processor_; |
| 98 | } |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 99 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 100 | boost::shared_ptr<TServerTransport> getServerTransport() { |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 101 | return serverTransport_; |
| 102 | } |
| 103 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 104 | boost::shared_ptr<TTransportFactory> getInputTransportFactory() { |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 105 | return inputTransportFactory_; |
| 106 | } |
| 107 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 108 | boost::shared_ptr<TTransportFactory> getOutputTransportFactory() { |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 109 | return outputTransportFactory_; |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 110 | } |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 111 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 112 | boost::shared_ptr<TProtocolFactory> getInputProtocolFactory() { |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 113 | return inputProtocolFactory_; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 116 | boost::shared_ptr<TProtocolFactory> getOutputProtocolFactory() { |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 117 | return outputProtocolFactory_; |
| 118 | } |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 119 | |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 120 | boost::shared_ptr<TServerEventHandler> getEventHandler() { |
| 121 | return eventHandler_; |
| 122 | } |
| 123 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 124 | protected: |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 125 | TServer(boost::shared_ptr<TProcessor> processor): |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 126 | processor_(processor) { |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 127 | setInputTransportFactory(boost::shared_ptr<TTransportFactory>(new TTransportFactory())); |
| 128 | setOutputTransportFactory(boost::shared_ptr<TTransportFactory>(new TTransportFactory())); |
| 129 | setInputProtocolFactory(boost::shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory())); |
| 130 | setOutputProtocolFactory(boost::shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory())); |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 133 | TServer(boost::shared_ptr<TProcessor> processor, |
| 134 | boost::shared_ptr<TServerTransport> serverTransport): |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 135 | processor_(processor), |
| 136 | serverTransport_(serverTransport) { |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 137 | setInputTransportFactory(boost::shared_ptr<TTransportFactory>(new TTransportFactory())); |
| 138 | setOutputTransportFactory(boost::shared_ptr<TTransportFactory>(new TTransportFactory())); |
| 139 | setInputProtocolFactory(boost::shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory())); |
| 140 | setOutputProtocolFactory(boost::shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory())); |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 143 | TServer(boost::shared_ptr<TProcessor> processor, |
| 144 | boost::shared_ptr<TServerTransport> serverTransport, |
| 145 | boost::shared_ptr<TTransportFactory> transportFactory, |
| 146 | boost::shared_ptr<TProtocolFactory> protocolFactory): |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 147 | processor_(processor), |
| 148 | serverTransport_(serverTransport), |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 149 | inputTransportFactory_(transportFactory), |
| 150 | outputTransportFactory_(transportFactory), |
| 151 | inputProtocolFactory_(protocolFactory), |
| 152 | outputProtocolFactory_(protocolFactory) {} |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 153 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 154 | TServer(boost::shared_ptr<TProcessor> processor, |
| 155 | boost::shared_ptr<TServerTransport> serverTransport, |
| 156 | boost::shared_ptr<TTransportFactory> inputTransportFactory, |
| 157 | boost::shared_ptr<TTransportFactory> outputTransportFactory, |
| 158 | boost::shared_ptr<TProtocolFactory> inputProtocolFactory, |
| 159 | boost::shared_ptr<TProtocolFactory> outputProtocolFactory): |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 160 | processor_(processor), |
| 161 | serverTransport_(serverTransport), |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 162 | inputTransportFactory_(inputTransportFactory), |
| 163 | outputTransportFactory_(outputTransportFactory), |
| 164 | inputProtocolFactory_(inputProtocolFactory), |
| 165 | outputProtocolFactory_(outputProtocolFactory) {} |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 166 | |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 167 | |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 168 | // Class variables |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 169 | boost::shared_ptr<TProcessor> processor_; |
| 170 | boost::shared_ptr<TServerTransport> serverTransport_; |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 171 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 172 | boost::shared_ptr<TTransportFactory> inputTransportFactory_; |
| 173 | boost::shared_ptr<TTransportFactory> outputTransportFactory_; |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 174 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 175 | boost::shared_ptr<TProtocolFactory> inputProtocolFactory_; |
| 176 | boost::shared_ptr<TProtocolFactory> outputProtocolFactory_; |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 177 | |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 178 | boost::shared_ptr<TServerEventHandler> eventHandler_; |
| 179 | |
David Reiss | ef22dc6 | 2007-11-30 20:38:49 +0000 | [diff] [blame] | 180 | public: |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 181 | void setInputTransportFactory(boost::shared_ptr<TTransportFactory> inputTransportFactory) { |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 182 | inputTransportFactory_ = inputTransportFactory; |
| 183 | } |
| 184 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 185 | void setOutputTransportFactory(boost::shared_ptr<TTransportFactory> outputTransportFactory) { |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 186 | outputTransportFactory_ = outputTransportFactory; |
| 187 | } |
| 188 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 189 | void setInputProtocolFactory(boost::shared_ptr<TProtocolFactory> inputProtocolFactory) { |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 190 | inputProtocolFactory_ = inputProtocolFactory; |
| 191 | } |
| 192 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 193 | void setOutputProtocolFactory(boost::shared_ptr<TProtocolFactory> outputProtocolFactory) { |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 194 | outputProtocolFactory_ = outputProtocolFactory; |
| 195 | } |
| 196 | |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 197 | void setServerEventHandler(boost::shared_ptr<TServerEventHandler> eventHandler) { |
| 198 | eventHandler_ = eventHandler; |
| 199 | } |
| 200 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 201 | }; |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 202 | |
veeve | 01d187c | 2008-02-26 05:12:08 +0000 | [diff] [blame] | 203 | /** |
| 204 | * Helper function to increase the max file descriptors limit |
| 205 | * for the current process and all of its children. |
| 206 | * By default, tries to increase it to as much as 2^24. |
| 207 | */ |
| 208 | int increase_max_fds(int max_fds=(1<<24)); |
| 209 | |
| 210 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 211 | }}} // apache::thrift::server |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 212 | |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 213 | #endif // #ifndef _THRIFT_SERVER_TSERVER_H_ |