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 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 17 | namespace facebook { namespace thrift { namespace server { |
| 18 | |
| 19 | using namespace facebook::thrift; |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 20 | using namespace facebook::thrift::transport; |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 21 | using namespace boost; |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 22 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 23 | /** |
| 24 | * Thrift server. |
| 25 | * |
| 26 | * @author Mark Slee <mcslee@facebook.com> |
| 27 | */ |
Aditya Agarwal | d622e96 | 2006-10-11 02:42:49 +0000 | [diff] [blame] | 28 | class TServer : public concurrency::Runnable { |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 29 | public: |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 30 | virtual ~TServer() {} |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 31 | |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 32 | virtual void serve() = 0; |
Aditya Agarwal | d622e96 | 2006-10-11 02:42:49 +0000 | [diff] [blame] | 33 | |
Mark Slee | 6e3f637 | 2007-03-01 22:05:46 +0000 | [diff] [blame] | 34 | virtual void stop() {} |
| 35 | |
Aditya Agarwal | d622e96 | 2006-10-11 02:42:49 +0000 | [diff] [blame] | 36 | // Allows running the server as a Runnable thread |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 37 | virtual void run() { |
| 38 | serve(); |
| 39 | } |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 40 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 41 | shared_ptr<TProcessor> getProcessor() { |
| 42 | return processor_; |
| 43 | } |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 44 | |
| 45 | shared_ptr<TServerTransport> getServerTransport() { |
| 46 | return serverTransport_; |
| 47 | } |
| 48 | |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 49 | shared_ptr<TTransportFactory> getInputTransportFactory() { |
| 50 | return inputTransportFactory_; |
| 51 | } |
| 52 | |
| 53 | shared_ptr<TTransportFactory> getOutputTransportFactory() { |
| 54 | return outputTransportFactory_; |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 55 | } |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 56 | |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 57 | shared_ptr<TProtocolFactory> getInputProtocolFactory() { |
| 58 | return inputProtocolFactory_; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 61 | shared_ptr<TProtocolFactory> getOutputProtocolFactory() { |
| 62 | return outputProtocolFactory_; |
| 63 | } |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 64 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 65 | protected: |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 66 | TServer(shared_ptr<TProcessor> processor): |
| 67 | processor_(processor) { |
| 68 | setInputTransportFactory(shared_ptr<TTransportFactory>(new TTransportFactory())); |
| 69 | setOutputTransportFactory(shared_ptr<TTransportFactory>(new TTransportFactory())); |
| 70 | setInputProtocolFactory(shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory())); |
| 71 | setOutputProtocolFactory(shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory())); |
| 72 | } |
| 73 | |
| 74 | TServer(shared_ptr<TProcessor> processor, |
| 75 | shared_ptr<TServerTransport> serverTransport): |
| 76 | processor_(processor), |
| 77 | serverTransport_(serverTransport) { |
| 78 | setInputTransportFactory(shared_ptr<TTransportFactory>(new TTransportFactory())); |
| 79 | setOutputTransportFactory(shared_ptr<TTransportFactory>(new TTransportFactory())); |
| 80 | setInputProtocolFactory(shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory())); |
| 81 | setOutputProtocolFactory(shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory())); |
| 82 | } |
| 83 | |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 84 | TServer(shared_ptr<TProcessor> processor, |
| 85 | shared_ptr<TServerTransport> serverTransport, |
| 86 | shared_ptr<TTransportFactory> transportFactory, |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 87 | shared_ptr<TProtocolFactory> protocolFactory): |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 88 | processor_(processor), |
| 89 | serverTransport_(serverTransport), |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 90 | inputTransportFactory_(transportFactory), |
| 91 | outputTransportFactory_(transportFactory), |
| 92 | inputProtocolFactory_(protocolFactory), |
| 93 | outputProtocolFactory_(protocolFactory) {} |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 94 | |
| 95 | TServer(shared_ptr<TProcessor> processor, |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 96 | shared_ptr<TServerTransport> serverTransport, |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 97 | shared_ptr<TTransportFactory> inputTransportFactory, |
| 98 | shared_ptr<TTransportFactory> outputTransportFactory, |
| 99 | shared_ptr<TProtocolFactory> inputProtocolFactory, |
| 100 | shared_ptr<TProtocolFactory> outputProtocolFactory): |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 101 | processor_(processor), |
| 102 | serverTransport_(serverTransport), |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 103 | inputTransportFactory_(inputTransportFactory), |
| 104 | outputTransportFactory_(outputTransportFactory), |
| 105 | inputProtocolFactory_(inputProtocolFactory), |
| 106 | outputProtocolFactory_(outputProtocolFactory) {} |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 107 | |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 108 | |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 109 | // Class variables |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 110 | shared_ptr<TProcessor> processor_; |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 111 | shared_ptr<TServerTransport> serverTransport_; |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 112 | |
| 113 | shared_ptr<TTransportFactory> inputTransportFactory_; |
| 114 | shared_ptr<TTransportFactory> outputTransportFactory_; |
| 115 | |
| 116 | shared_ptr<TProtocolFactory> inputProtocolFactory_; |
| 117 | shared_ptr<TProtocolFactory> outputProtocolFactory_; |
| 118 | |
| 119 | void setInputTransportFactory(shared_ptr<TTransportFactory> inputTransportFactory) { |
| 120 | inputTransportFactory_ = inputTransportFactory; |
| 121 | } |
| 122 | |
| 123 | void setOutputTransportFactory(shared_ptr<TTransportFactory> outputTransportFactory) { |
| 124 | outputTransportFactory_ = outputTransportFactory; |
| 125 | } |
| 126 | |
| 127 | void setInputProtocolFactory(shared_ptr<TProtocolFactory> inputProtocolFactory) { |
| 128 | inputProtocolFactory_ = inputProtocolFactory; |
| 129 | } |
| 130 | |
| 131 | void setOutputProtocolFactory(shared_ptr<TProtocolFactory> outputProtocolFactory) { |
| 132 | outputProtocolFactory_ = outputProtocolFactory; |
| 133 | } |
| 134 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 135 | }; |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 136 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 137 | }}} // facebook::thrift::server |
| 138 | |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 139 | #endif // #ifndef _THRIFT_SERVER_TSERVER_H_ |