blob: 2936e4210dc27f591df3b0140ff33dba86afe42f [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
19using namespace facebook::thrift;
Mark Sleed788b2e2006-09-07 01:26:35 +000020using namespace facebook::thrift::transport;
Marc Slemko16698852006-08-04 03:16:10 +000021using namespace boost;
Marc Slemko6f038a72006-08-03 18:58:09 +000022
Mark Sleee8540632006-05-30 09:24:40 +000023/**
24 * Thrift server.
25 *
26 * @author Mark Slee <mcslee@facebook.com>
27 */
Aditya Agarwald622e962006-10-11 02:42:49 +000028class TServer : public concurrency::Runnable {
Marc Slemko16698852006-08-04 03:16:10 +000029public:
Mark Sleee8540632006-05-30 09:24:40 +000030 virtual ~TServer() {}
Mark Slee4af6ed72006-10-25 19:02:49 +000031
Mark Slee794993d2006-09-20 01:56:10 +000032 virtual void serve() = 0;
Aditya Agarwald622e962006-10-11 02:42:49 +000033
Mark Slee6e3f6372007-03-01 22:05:46 +000034 virtual void stop() {}
35
Aditya Agarwald622e962006-10-11 02:42:49 +000036 // Allows running the server as a Runnable thread
Mark Slee4af6ed72006-10-25 19:02:49 +000037 virtual void run() {
38 serve();
39 }
Marc Slemko16698852006-08-04 03:16:10 +000040
Mark Slee2f6404d2006-10-10 01:37:40 +000041 shared_ptr<TProcessor> getProcessor() {
42 return processor_;
43 }
Aditya Agarwal1ea90522007-01-19 02:02:12 +000044
45 shared_ptr<TServerTransport> getServerTransport() {
46 return serverTransport_;
47 }
48
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000049 shared_ptr<TTransportFactory> getInputTransportFactory() {
50 return inputTransportFactory_;
51 }
52
53 shared_ptr<TTransportFactory> getOutputTransportFactory() {
54 return outputTransportFactory_;
Aditya Agarwal1ea90522007-01-19 02:02:12 +000055 }
Mark Slee2f6404d2006-10-10 01:37:40 +000056
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000057 shared_ptr<TProtocolFactory> getInputProtocolFactory() {
58 return inputProtocolFactory_;
Mark Slee4af6ed72006-10-25 19:02:49 +000059 }
60
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000061 shared_ptr<TProtocolFactory> getOutputProtocolFactory() {
62 return outputProtocolFactory_;
63 }
Aditya Agarwal1ea90522007-01-19 02:02:12 +000064
Marc Slemko16698852006-08-04 03:16:10 +000065protected:
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000066 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 Sleed788b2e2006-09-07 01:26:35 +000084 TServer(shared_ptr<TProcessor> processor,
85 shared_ptr<TServerTransport> serverTransport,
86 shared_ptr<TTransportFactory> transportFactory,
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000087 shared_ptr<TProtocolFactory> protocolFactory):
Mark Sleed788b2e2006-09-07 01:26:35 +000088 processor_(processor),
89 serverTransport_(serverTransport),
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000090 inputTransportFactory_(transportFactory),
91 outputTransportFactory_(transportFactory),
92 inputProtocolFactory_(protocolFactory),
93 outputProtocolFactory_(protocolFactory) {}
Mark Sleed788b2e2006-09-07 01:26:35 +000094
95 TServer(shared_ptr<TProcessor> processor,
Mark Slee4af6ed72006-10-25 19:02:49 +000096 shared_ptr<TServerTransport> serverTransport,
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000097 shared_ptr<TTransportFactory> inputTransportFactory,
98 shared_ptr<TTransportFactory> outputTransportFactory,
99 shared_ptr<TProtocolFactory> inputProtocolFactory,
100 shared_ptr<TProtocolFactory> outputProtocolFactory):
Mark Slee4af6ed72006-10-25 19:02:49 +0000101 processor_(processor),
102 serverTransport_(serverTransport),
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000103 inputTransportFactory_(inputTransportFactory),
104 outputTransportFactory_(outputTransportFactory),
105 inputProtocolFactory_(inputProtocolFactory),
106 outputProtocolFactory_(outputProtocolFactory) {}
Mark Slee4af6ed72006-10-25 19:02:49 +0000107
Mark Sleed788b2e2006-09-07 01:26:35 +0000108
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000109 // Class variables
Marc Slemko16698852006-08-04 03:16:10 +0000110 shared_ptr<TProcessor> processor_;
Mark Sleed788b2e2006-09-07 01:26:35 +0000111 shared_ptr<TServerTransport> serverTransport_;
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000112
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 Sleee8540632006-05-30 09:24:40 +0000135};
Marc Slemko16698852006-08-04 03:16:10 +0000136
Marc Slemko6f038a72006-08-03 18:58:09 +0000137}}} // facebook::thrift::server
138
Mark Sleed788b2e2006-09-07 01:26:35 +0000139#endif // #ifndef _THRIFT_SERVER_TSERVER_H_