blob: a9bcb26e240271660eca36a51c1f64da2383ea0d [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
34 // Allows running the server as a Runnable thread
Mark Slee4af6ed72006-10-25 19:02:49 +000035 virtual void run() {
36 serve();
37 }
Marc Slemko16698852006-08-04 03:16:10 +000038
Mark Slee2f6404d2006-10-10 01:37:40 +000039 shared_ptr<TProcessor> getProcessor() {
40 return processor_;
41 }
Aditya Agarwal1ea90522007-01-19 02:02:12 +000042
43 shared_ptr<TServerTransport> getServerTransport() {
44 return serverTransport_;
45 }
46
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000047 shared_ptr<TTransportFactory> getInputTransportFactory() {
48 return inputTransportFactory_;
49 }
50
51 shared_ptr<TTransportFactory> getOutputTransportFactory() {
52 return outputTransportFactory_;
Aditya Agarwal1ea90522007-01-19 02:02:12 +000053 }
Mark Slee2f6404d2006-10-10 01:37:40 +000054
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000055 shared_ptr<TProtocolFactory> getInputProtocolFactory() {
56 return inputProtocolFactory_;
Mark Slee4af6ed72006-10-25 19:02:49 +000057 }
58
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000059 shared_ptr<TProtocolFactory> getOutputProtocolFactory() {
60 return outputProtocolFactory_;
61 }
Aditya Agarwal1ea90522007-01-19 02:02:12 +000062
Marc Slemko16698852006-08-04 03:16:10 +000063protected:
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000064 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 Sleed788b2e2006-09-07 01:26:35 +000082 TServer(shared_ptr<TProcessor> processor,
83 shared_ptr<TServerTransport> serverTransport,
84 shared_ptr<TTransportFactory> transportFactory,
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000085 shared_ptr<TProtocolFactory> protocolFactory):
Mark Sleed788b2e2006-09-07 01:26:35 +000086 processor_(processor),
87 serverTransport_(serverTransport),
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000088 inputTransportFactory_(transportFactory),
89 outputTransportFactory_(transportFactory),
90 inputProtocolFactory_(protocolFactory),
91 outputProtocolFactory_(protocolFactory) {}
Mark Sleed788b2e2006-09-07 01:26:35 +000092
93 TServer(shared_ptr<TProcessor> processor,
Mark Slee4af6ed72006-10-25 19:02:49 +000094 shared_ptr<TServerTransport> serverTransport,
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000095 shared_ptr<TTransportFactory> inputTransportFactory,
96 shared_ptr<TTransportFactory> outputTransportFactory,
97 shared_ptr<TProtocolFactory> inputProtocolFactory,
98 shared_ptr<TProtocolFactory> outputProtocolFactory):
Mark Slee4af6ed72006-10-25 19:02:49 +000099 processor_(processor),
100 serverTransport_(serverTransport),
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000101 inputTransportFactory_(inputTransportFactory),
102 outputTransportFactory_(outputTransportFactory),
103 inputProtocolFactory_(inputProtocolFactory),
104 outputProtocolFactory_(outputProtocolFactory) {}
Mark Slee4af6ed72006-10-25 19:02:49 +0000105
Mark Sleed788b2e2006-09-07 01:26:35 +0000106
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000107 // Class variables
Marc Slemko16698852006-08-04 03:16:10 +0000108 shared_ptr<TProcessor> processor_;
Mark Sleed788b2e2006-09-07 01:26:35 +0000109 shared_ptr<TServerTransport> serverTransport_;
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000110
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 Sleee8540632006-05-30 09:24:40 +0000133};
Marc Slemko16698852006-08-04 03:16:10 +0000134
Marc Slemko6f038a72006-08-03 18:58:09 +0000135}}} // facebook::thrift::server
136
Mark Sleed788b2e2006-09-07 01:26:35 +0000137#endif // #ifndef _THRIFT_SERVER_TSERVER_H_