| 30 |
ashish |
1 |
/*
|
|
|
2 |
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
3 |
* or more contributor license agreements. See the NOTICE file
|
|
|
4 |
* distributed with this work for additional information
|
|
|
5 |
* regarding copyright ownership. The ASF licenses this file
|
|
|
6 |
* to you under the Apache License, Version 2.0 (the
|
|
|
7 |
* "License"); you may not use this file except in compliance
|
|
|
8 |
* with the License. You may obtain a copy of the License at
|
|
|
9 |
*
|
|
|
10 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
11 |
*
|
|
|
12 |
* Unless required by applicable law or agreed to in writing,
|
|
|
13 |
* software distributed under the License is distributed on an
|
|
|
14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
15 |
* KIND, either express or implied. See the License for the
|
|
|
16 |
* specific language governing permissions and limitations
|
|
|
17 |
* under the License.
|
|
|
18 |
*/
|
|
|
19 |
|
|
|
20 |
#ifndef _THRIFT_SERVER_TSERVER_H_
|
|
|
21 |
#define _THRIFT_SERVER_TSERVER_H_ 1
|
|
|
22 |
|
|
|
23 |
#include <TProcessor.h>
|
|
|
24 |
#include <transport/TServerTransport.h>
|
|
|
25 |
#include <protocol/TBinaryProtocol.h>
|
|
|
26 |
#include <concurrency/Thread.h>
|
|
|
27 |
|
|
|
28 |
#include <boost/shared_ptr.hpp>
|
|
|
29 |
|
|
|
30 |
namespace apache { namespace thrift { namespace server {
|
|
|
31 |
|
|
|
32 |
using apache::thrift::TProcessor;
|
|
|
33 |
using apache::thrift::protocol::TBinaryProtocolFactory;
|
|
|
34 |
using apache::thrift::protocol::TProtocol;
|
|
|
35 |
using apache::thrift::protocol::TProtocolFactory;
|
|
|
36 |
using apache::thrift::transport::TServerTransport;
|
|
|
37 |
using apache::thrift::transport::TTransport;
|
|
|
38 |
using apache::thrift::transport::TTransportFactory;
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Virtual interface class that can handle events from the server core. To
|
|
|
42 |
* use this you should subclass it and implement the methods that you care
|
|
|
43 |
* about. Your subclass can also store local data that you may care about,
|
|
|
44 |
* such as additional "arguments" to these methods (stored in the object
|
|
|
45 |
* instance's state).
|
|
|
46 |
*/
|
|
|
47 |
class TServerEventHandler {
|
|
|
48 |
public:
|
|
|
49 |
|
|
|
50 |
virtual ~TServerEventHandler() {}
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* Called before the server begins.
|
|
|
54 |
*/
|
|
|
55 |
virtual void preServe() {}
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* Called when a new client has connected and is about to being processing.
|
|
|
59 |
*/
|
|
|
60 |
virtual void clientBegin(boost::shared_ptr<TProtocol> /* input */,
|
|
|
61 |
boost::shared_ptr<TProtocol> /* output */) {}
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
* Called when a client has finished making requests.
|
|
|
65 |
*/
|
|
|
66 |
virtual void clientEnd(boost::shared_ptr<TProtocol> /* input */,
|
|
|
67 |
boost::shared_ptr<TProtocol> /* output */) {}
|
|
|
68 |
|
|
|
69 |
protected:
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* Prevent direct instantiation.
|
|
|
73 |
*/
|
|
|
74 |
TServerEventHandler() {}
|
|
|
75 |
|
|
|
76 |
};
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* Thrift server.
|
|
|
80 |
*
|
|
|
81 |
*/
|
|
|
82 |
class TServer : public concurrency::Runnable {
|
|
|
83 |
public:
|
|
|
84 |
|
|
|
85 |
virtual ~TServer() {}
|
|
|
86 |
|
|
|
87 |
virtual void serve() = 0;
|
|
|
88 |
|
|
|
89 |
virtual void stop() {}
|
|
|
90 |
|
|
|
91 |
// Allows running the server as a Runnable thread
|
|
|
92 |
virtual void run() {
|
|
|
93 |
serve();
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
boost::shared_ptr<TProcessor> getProcessor() {
|
|
|
97 |
return processor_;
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
boost::shared_ptr<TServerTransport> getServerTransport() {
|
|
|
101 |
return serverTransport_;
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
boost::shared_ptr<TTransportFactory> getInputTransportFactory() {
|
|
|
105 |
return inputTransportFactory_;
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
boost::shared_ptr<TTransportFactory> getOutputTransportFactory() {
|
|
|
109 |
return outputTransportFactory_;
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
boost::shared_ptr<TProtocolFactory> getInputProtocolFactory() {
|
|
|
113 |
return inputProtocolFactory_;
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
boost::shared_ptr<TProtocolFactory> getOutputProtocolFactory() {
|
|
|
117 |
return outputProtocolFactory_;
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
boost::shared_ptr<TServerEventHandler> getEventHandler() {
|
|
|
121 |
return eventHandler_;
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
protected:
|
|
|
125 |
TServer(boost::shared_ptr<TProcessor> processor):
|
|
|
126 |
processor_(processor) {
|
|
|
127 |
setInputTransportFactory(boost::shared_ptr<TTransportFactory>(new TTransportFactory()));
|
|
|
128 |
setOutputTransportFactory(boost::shared_ptr<TTransportFactory>(new TTransportFactory()));
|
|
|
129 |
setInputProtocolFactory(boost::shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory()));
|
|
|
130 |
setOutputProtocolFactory(boost::shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory()));
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
TServer(boost::shared_ptr<TProcessor> processor,
|
|
|
134 |
boost::shared_ptr<TServerTransport> serverTransport):
|
|
|
135 |
processor_(processor),
|
|
|
136 |
serverTransport_(serverTransport) {
|
|
|
137 |
setInputTransportFactory(boost::shared_ptr<TTransportFactory>(new TTransportFactory()));
|
|
|
138 |
setOutputTransportFactory(boost::shared_ptr<TTransportFactory>(new TTransportFactory()));
|
|
|
139 |
setInputProtocolFactory(boost::shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory()));
|
|
|
140 |
setOutputProtocolFactory(boost::shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory()));
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
TServer(boost::shared_ptr<TProcessor> processor,
|
|
|
144 |
boost::shared_ptr<TServerTransport> serverTransport,
|
|
|
145 |
boost::shared_ptr<TTransportFactory> transportFactory,
|
|
|
146 |
boost::shared_ptr<TProtocolFactory> protocolFactory):
|
|
|
147 |
processor_(processor),
|
|
|
148 |
serverTransport_(serverTransport),
|
|
|
149 |
inputTransportFactory_(transportFactory),
|
|
|
150 |
outputTransportFactory_(transportFactory),
|
|
|
151 |
inputProtocolFactory_(protocolFactory),
|
|
|
152 |
outputProtocolFactory_(protocolFactory) {}
|
|
|
153 |
|
|
|
154 |
TServer(boost::shared_ptr<TProcessor> processor,
|
|
|
155 |
boost::shared_ptr<TServerTransport> serverTransport,
|
|
|
156 |
boost::shared_ptr<TTransportFactory> inputTransportFactory,
|
|
|
157 |
boost::shared_ptr<TTransportFactory> outputTransportFactory,
|
|
|
158 |
boost::shared_ptr<TProtocolFactory> inputProtocolFactory,
|
|
|
159 |
boost::shared_ptr<TProtocolFactory> outputProtocolFactory):
|
|
|
160 |
processor_(processor),
|
|
|
161 |
serverTransport_(serverTransport),
|
|
|
162 |
inputTransportFactory_(inputTransportFactory),
|
|
|
163 |
outputTransportFactory_(outputTransportFactory),
|
|
|
164 |
inputProtocolFactory_(inputProtocolFactory),
|
|
|
165 |
outputProtocolFactory_(outputProtocolFactory) {}
|
|
|
166 |
|
|
|
167 |
|
|
|
168 |
// Class variables
|
|
|
169 |
boost::shared_ptr<TProcessor> processor_;
|
|
|
170 |
boost::shared_ptr<TServerTransport> serverTransport_;
|
|
|
171 |
|
|
|
172 |
boost::shared_ptr<TTransportFactory> inputTransportFactory_;
|
|
|
173 |
boost::shared_ptr<TTransportFactory> outputTransportFactory_;
|
|
|
174 |
|
|
|
175 |
boost::shared_ptr<TProtocolFactory> inputProtocolFactory_;
|
|
|
176 |
boost::shared_ptr<TProtocolFactory> outputProtocolFactory_;
|
|
|
177 |
|
|
|
178 |
boost::shared_ptr<TServerEventHandler> eventHandler_;
|
|
|
179 |
|
|
|
180 |
public:
|
|
|
181 |
void setInputTransportFactory(boost::shared_ptr<TTransportFactory> inputTransportFactory) {
|
|
|
182 |
inputTransportFactory_ = inputTransportFactory;
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
void setOutputTransportFactory(boost::shared_ptr<TTransportFactory> outputTransportFactory) {
|
|
|
186 |
outputTransportFactory_ = outputTransportFactory;
|
|
|
187 |
}
|
|
|
188 |
|
|
|
189 |
void setInputProtocolFactory(boost::shared_ptr<TProtocolFactory> inputProtocolFactory) {
|
|
|
190 |
inputProtocolFactory_ = inputProtocolFactory;
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
void setOutputProtocolFactory(boost::shared_ptr<TProtocolFactory> outputProtocolFactory) {
|
|
|
194 |
outputProtocolFactory_ = outputProtocolFactory;
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
void setServerEventHandler(boost::shared_ptr<TServerEventHandler> eventHandler) {
|
|
|
198 |
eventHandler_ = eventHandler;
|
|
|
199 |
}
|
|
|
200 |
|
|
|
201 |
};
|
|
|
202 |
|
|
|
203 |
/**
|
|
|
204 |
* Helper function to increase the max file descriptors limit
|
|
|
205 |
* for the current process and all of its children.
|
|
|
206 |
* By default, tries to increase it to as much as 2^24.
|
|
|
207 |
*/
|
|
|
208 |
int increase_max_fds(int max_fds=(1<<24));
|
|
|
209 |
|
|
|
210 |
|
|
|
211 |
}}} // apache::thrift::server
|
|
|
212 |
|
|
|
213 |
#endif // #ifndef _THRIFT_SERVER_TSERVER_H_
|