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 #include <thrift/server/TThreadPoolServer.h>
21 
22 namespace apache {
23 namespace thrift {
24 namespace server {
25 
26 using apache::thrift::concurrency::ThreadManager;
27 using apache::thrift::protocol::TProtocol;
28 using apache::thrift::protocol::TProtocolFactory;
29 using apache::thrift::transport::TServerTransport;
30 using apache::thrift::transport::TTransport;
31 using apache::thrift::transport::TTransportException;
32 using apache::thrift::transport::TTransportFactory;
33 using std::shared_ptr;
34 using std::string;
35 
TThreadPoolServer(const shared_ptr<TProcessorFactory> & processorFactory,const shared_ptr<TServerTransport> & serverTransport,const shared_ptr<TTransportFactory> & transportFactory,const shared_ptr<TProtocolFactory> & protocolFactory,const shared_ptr<ThreadManager> & threadManager)36 TThreadPoolServer::TThreadPoolServer(const shared_ptr<TProcessorFactory>& processorFactory,
37                                      const shared_ptr<TServerTransport>& serverTransport,
38                                      const shared_ptr<TTransportFactory>& transportFactory,
39                                      const shared_ptr<TProtocolFactory>& protocolFactory,
40                                      const shared_ptr<ThreadManager>& threadManager)
41   : TServerFramework(processorFactory, serverTransport, transportFactory, protocolFactory),
42     threadManager_(threadManager),
43     timeout_(0),
44     taskExpiration_(0) {
45 }
46 
TThreadPoolServer(const shared_ptr<TProcessor> & processor,const shared_ptr<TServerTransport> & serverTransport,const shared_ptr<TTransportFactory> & transportFactory,const shared_ptr<TProtocolFactory> & protocolFactory,const shared_ptr<ThreadManager> & threadManager)47 TThreadPoolServer::TThreadPoolServer(const shared_ptr<TProcessor>& processor,
48                                      const shared_ptr<TServerTransport>& serverTransport,
49                                      const shared_ptr<TTransportFactory>& transportFactory,
50                                      const shared_ptr<TProtocolFactory>& protocolFactory,
51                                      const shared_ptr<ThreadManager>& threadManager)
52   : TServerFramework(processor, serverTransport, transportFactory, protocolFactory),
53     threadManager_(threadManager),
54     timeout_(0),
55     taskExpiration_(0) {
56 }
57 
TThreadPoolServer(const shared_ptr<TProcessorFactory> & processorFactory,const shared_ptr<TServerTransport> & serverTransport,const shared_ptr<TTransportFactory> & inputTransportFactory,const shared_ptr<TTransportFactory> & outputTransportFactory,const shared_ptr<TProtocolFactory> & inputProtocolFactory,const shared_ptr<TProtocolFactory> & outputProtocolFactory,const shared_ptr<ThreadManager> & threadManager)58 TThreadPoolServer::TThreadPoolServer(const shared_ptr<TProcessorFactory>& processorFactory,
59                                      const shared_ptr<TServerTransport>& serverTransport,
60                                      const shared_ptr<TTransportFactory>& inputTransportFactory,
61                                      const shared_ptr<TTransportFactory>& outputTransportFactory,
62                                      const shared_ptr<TProtocolFactory>& inputProtocolFactory,
63                                      const shared_ptr<TProtocolFactory>& outputProtocolFactory,
64                                      const shared_ptr<ThreadManager>& threadManager)
65   : TServerFramework(processorFactory,
66                      serverTransport,
67                      inputTransportFactory,
68                      outputTransportFactory,
69                      inputProtocolFactory,
70                      outputProtocolFactory),
71     threadManager_(threadManager),
72     timeout_(0),
73     taskExpiration_(0) {
74 }
75 
TThreadPoolServer(const shared_ptr<TProcessor> & processor,const shared_ptr<TServerTransport> & serverTransport,const shared_ptr<TTransportFactory> & inputTransportFactory,const shared_ptr<TTransportFactory> & outputTransportFactory,const shared_ptr<TProtocolFactory> & inputProtocolFactory,const shared_ptr<TProtocolFactory> & outputProtocolFactory,const shared_ptr<ThreadManager> & threadManager)76 TThreadPoolServer::TThreadPoolServer(const shared_ptr<TProcessor>& processor,
77                                      const shared_ptr<TServerTransport>& serverTransport,
78                                      const shared_ptr<TTransportFactory>& inputTransportFactory,
79                                      const shared_ptr<TTransportFactory>& outputTransportFactory,
80                                      const shared_ptr<TProtocolFactory>& inputProtocolFactory,
81                                      const shared_ptr<TProtocolFactory>& outputProtocolFactory,
82                                      const shared_ptr<ThreadManager>& threadManager)
83   : TServerFramework(processor,
84                      serverTransport,
85                      inputTransportFactory,
86                      outputTransportFactory,
87                      inputProtocolFactory,
88                      outputProtocolFactory),
89     threadManager_(threadManager),
90     timeout_(0),
91     taskExpiration_(0) {
92 }
93 
94 TThreadPoolServer::~TThreadPoolServer() = default;
95 
serve()96 void TThreadPoolServer::serve() {
97   TServerFramework::serve();
98   threadManager_->stop();
99 }
100 
getTimeout() const101 int64_t TThreadPoolServer::getTimeout() const {
102   return timeout_;
103 }
104 
setTimeout(int64_t value)105 void TThreadPoolServer::setTimeout(int64_t value) {
106   timeout_ = value;
107 }
108 
getTaskExpiration() const109 int64_t TThreadPoolServer::getTaskExpiration() const {
110   return taskExpiration_;
111 }
112 
setTaskExpiration(int64_t value)113 void TThreadPoolServer::setTaskExpiration(int64_t value) {
114   taskExpiration_ = value;
115 }
116 
117 std::shared_ptr<apache::thrift::concurrency::ThreadManager>
getThreadManager() const118 TThreadPoolServer::getThreadManager() const {
119   return threadManager_;
120 }
121 
onClientConnected(const shared_ptr<TConnectedClient> & pClient)122 void TThreadPoolServer::onClientConnected(const shared_ptr<TConnectedClient>& pClient) {
123   threadManager_->add(pClient, getTimeout(), getTaskExpiration());
124 }
125 
onClientDisconnected(TConnectedClient *)126 void TThreadPoolServer::onClientDisconnected(TConnectedClient*) {
127 }
128 
129 }
130 }
131 } // apache::thrift::server
132