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