blob: 5f13255332ec5dd68cd60ebd30456b24bd6c6de7 [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
Marc Slemko6f038a72006-08-03 18:58:09 +000017namespace facebook { namespace thrift { namespace server {
18
Mark Slee5ea15f92007-03-05 22:55:59 +000019using facebook::thrift::TProcessor;
20using facebook::thrift::protocol::TBinaryProtocolFactory;
21using facebook::thrift::protocol::TProtocolFactory;
22using facebook::thrift::transport::TServerTransport;
23using facebook::thrift::transport::TTransport;
24using facebook::thrift::transport::TTransportFactory;
Marc Slemko6f038a72006-08-03 18:58:09 +000025
Mark Sleee8540632006-05-30 09:24:40 +000026/**
27 * Thrift server.
28 *
29 * @author Mark Slee <mcslee@facebook.com>
30 */
Aditya Agarwald622e962006-10-11 02:42:49 +000031class TServer : public concurrency::Runnable {
Marc Slemko16698852006-08-04 03:16:10 +000032public:
Mark Sleee8540632006-05-30 09:24:40 +000033 virtual ~TServer() {}
Mark Slee4af6ed72006-10-25 19:02:49 +000034
Mark Slee794993d2006-09-20 01:56:10 +000035 virtual void serve() = 0;
Aditya Agarwald622e962006-10-11 02:42:49 +000036
Mark Slee6e3f6372007-03-01 22:05:46 +000037 virtual void stop() {}
38
Aditya Agarwald622e962006-10-11 02:42:49 +000039 // Allows running the server as a Runnable thread
Mark Slee4af6ed72006-10-25 19:02:49 +000040 virtual void run() {
41 serve();
42 }
Marc Slemko16698852006-08-04 03:16:10 +000043
Mark Slee5ea15f92007-03-05 22:55:59 +000044 boost::shared_ptr<TProcessor> getProcessor() {
Mark Slee2f6404d2006-10-10 01:37:40 +000045 return processor_;
46 }
Aditya Agarwal1ea90522007-01-19 02:02:12 +000047
Mark Slee5ea15f92007-03-05 22:55:59 +000048 boost::shared_ptr<TServerTransport> getServerTransport() {
Aditya Agarwal1ea90522007-01-19 02:02:12 +000049 return serverTransport_;
50 }
51
Mark Slee5ea15f92007-03-05 22:55:59 +000052 boost::shared_ptr<TTransportFactory> getInputTransportFactory() {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000053 return inputTransportFactory_;
54 }
55
Mark Slee5ea15f92007-03-05 22:55:59 +000056 boost::shared_ptr<TTransportFactory> getOutputTransportFactory() {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000057 return outputTransportFactory_;
Aditya Agarwal1ea90522007-01-19 02:02:12 +000058 }
Mark Slee2f6404d2006-10-10 01:37:40 +000059
Mark Slee5ea15f92007-03-05 22:55:59 +000060 boost::shared_ptr<TProtocolFactory> getInputProtocolFactory() {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000061 return inputProtocolFactory_;
Mark Slee4af6ed72006-10-25 19:02:49 +000062 }
63
Mark Slee5ea15f92007-03-05 22:55:59 +000064 boost::shared_ptr<TProtocolFactory> getOutputProtocolFactory() {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000065 return outputProtocolFactory_;
66 }
Aditya Agarwal1ea90522007-01-19 02:02:12 +000067
Marc Slemko16698852006-08-04 03:16:10 +000068protected:
Mark Slee5ea15f92007-03-05 22:55:59 +000069 TServer(boost::shared_ptr<TProcessor> processor):
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000070 processor_(processor) {
Mark Slee5ea15f92007-03-05 22:55:59 +000071 setInputTransportFactory(boost::shared_ptr<TTransportFactory>(new TTransportFactory()));
72 setOutputTransportFactory(boost::shared_ptr<TTransportFactory>(new TTransportFactory()));
73 setInputProtocolFactory(boost::shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory()));
74 setOutputProtocolFactory(boost::shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory()));
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000075 }
76
Mark Slee5ea15f92007-03-05 22:55:59 +000077 TServer(boost::shared_ptr<TProcessor> processor,
78 boost::shared_ptr<TServerTransport> serverTransport):
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000079 processor_(processor),
80 serverTransport_(serverTransport) {
Mark Slee5ea15f92007-03-05 22:55:59 +000081 setInputTransportFactory(boost::shared_ptr<TTransportFactory>(new TTransportFactory()));
82 setOutputTransportFactory(boost::shared_ptr<TTransportFactory>(new TTransportFactory()));
83 setInputProtocolFactory(boost::shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory()));
84 setOutputProtocolFactory(boost::shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory()));
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000085 }
86
Mark Slee5ea15f92007-03-05 22:55:59 +000087 TServer(boost::shared_ptr<TProcessor> processor,
88 boost::shared_ptr<TServerTransport> serverTransport,
89 boost::shared_ptr<TTransportFactory> transportFactory,
90 boost::shared_ptr<TProtocolFactory> protocolFactory):
Mark Sleed788b2e2006-09-07 01:26:35 +000091 processor_(processor),
92 serverTransport_(serverTransport),
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000093 inputTransportFactory_(transportFactory),
94 outputTransportFactory_(transportFactory),
95 inputProtocolFactory_(protocolFactory),
96 outputProtocolFactory_(protocolFactory) {}
Mark Sleed788b2e2006-09-07 01:26:35 +000097
Mark Slee5ea15f92007-03-05 22:55:59 +000098 TServer(boost::shared_ptr<TProcessor> processor,
99 boost::shared_ptr<TServerTransport> serverTransport,
100 boost::shared_ptr<TTransportFactory> inputTransportFactory,
101 boost::shared_ptr<TTransportFactory> outputTransportFactory,
102 boost::shared_ptr<TProtocolFactory> inputProtocolFactory,
103 boost::shared_ptr<TProtocolFactory> outputProtocolFactory):
Mark Slee4af6ed72006-10-25 19:02:49 +0000104 processor_(processor),
105 serverTransport_(serverTransport),
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000106 inputTransportFactory_(inputTransportFactory),
107 outputTransportFactory_(outputTransportFactory),
108 inputProtocolFactory_(inputProtocolFactory),
109 outputProtocolFactory_(outputProtocolFactory) {}
Mark Slee4af6ed72006-10-25 19:02:49 +0000110
Mark Sleed788b2e2006-09-07 01:26:35 +0000111
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000112 // Class variables
Mark Slee5ea15f92007-03-05 22:55:59 +0000113 boost::shared_ptr<TProcessor> processor_;
114 boost::shared_ptr<TServerTransport> serverTransport_;
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000115
Mark Slee5ea15f92007-03-05 22:55:59 +0000116 boost::shared_ptr<TTransportFactory> inputTransportFactory_;
117 boost::shared_ptr<TTransportFactory> outputTransportFactory_;
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000118
Mark Slee5ea15f92007-03-05 22:55:59 +0000119 boost::shared_ptr<TProtocolFactory> inputProtocolFactory_;
120 boost::shared_ptr<TProtocolFactory> outputProtocolFactory_;
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000121
Mark Slee5ea15f92007-03-05 22:55:59 +0000122 void setInputTransportFactory(boost::shared_ptr<TTransportFactory> inputTransportFactory) {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000123 inputTransportFactory_ = inputTransportFactory;
124 }
125
Mark Slee5ea15f92007-03-05 22:55:59 +0000126 void setOutputTransportFactory(boost::shared_ptr<TTransportFactory> outputTransportFactory) {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000127 outputTransportFactory_ = outputTransportFactory;
128 }
129
Mark Slee5ea15f92007-03-05 22:55:59 +0000130 void setInputProtocolFactory(boost::shared_ptr<TProtocolFactory> inputProtocolFactory) {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000131 inputProtocolFactory_ = inputProtocolFactory;
132 }
133
Mark Slee5ea15f92007-03-05 22:55:59 +0000134 void setOutputProtocolFactory(boost::shared_ptr<TProtocolFactory> outputProtocolFactory) {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000135 outputProtocolFactory_ = outputProtocolFactory;
136 }
137
Mark Sleee8540632006-05-30 09:24:40 +0000138};
Marc Slemko16698852006-08-04 03:16:10 +0000139
Marc Slemko6f038a72006-08-03 18:58:09 +0000140}}} // facebook::thrift::server
141
Mark Sleed788b2e2006-09-07 01:26:35 +0000142#endif // #ifndef _THRIFT_SERVER_TSERVER_H_