blob: 92f5cf81945fe9d46106c0377e0f47806257ee77 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
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 */
Mark Slee9f0c6512007-02-28 23:58:26 +000019
Roger Meier49ff8b12012-04-13 09:12:31 +000020#include <thrift/concurrency/PlatformThreadFactory.h>
Jim King21b68522015-04-26 18:30:26 -040021#include <thrift/server/TThreadedServer.h>
Mark Sleeb3cb6292007-02-01 22:55:00 +000022
Konrad Grochowski16a23a62014-11-13 15:33:38 +010023namespace apache {
24namespace thrift {
25namespace server {
Mark Sleeb3cb6292007-02-01 22:55:00 +000026
Jim King21b68522015-04-26 18:30:26 -040027using apache::thrift::concurrency::Synchronized;
28using apache::thrift::concurrency::Thread;
29using apache::thrift::concurrency::ThreadFactory;
30using apache::thrift::protocol::TProtocol;
31using apache::thrift::protocol::TProtocolFactory;
32using apache::thrift::transport::TServerTransport;
33using apache::thrift::transport::TTransport;
34using apache::thrift::transport::TTransportException;
35using apache::thrift::transport::TTransportFactory;
Mark Slee5ea15f92007-03-05 22:55:59 +000036using boost::shared_ptr;
Jim King21b68522015-04-26 18:30:26 -040037using std::string;
38
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020039TThreadedServer::TThreadedServer(const shared_ptr<TProcessorFactory>& processorFactory,
40 const shared_ptr<TServerTransport>& serverTransport,
41 const shared_ptr<TTransportFactory>& transportFactory,
42 const shared_ptr<TProtocolFactory>& protocolFactory,
43 const shared_ptr<ThreadFactory>& threadFactory)
Jim King21b68522015-04-26 18:30:26 -040044 : TServerFramework(processorFactory, serverTransport, transportFactory, protocolFactory),
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020045 threadFactory_(threadFactory) {
46}
Jim King21b68522015-04-26 18:30:26 -040047
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020048TThreadedServer::TThreadedServer(const shared_ptr<TProcessor>& processor,
49 const shared_ptr<TServerTransport>& serverTransport,
50 const shared_ptr<TTransportFactory>& transportFactory,
51 const shared_ptr<TProtocolFactory>& protocolFactory,
52 const shared_ptr<ThreadFactory>& threadFactory)
Jim King21b68522015-04-26 18:30:26 -040053 : TServerFramework(processor, serverTransport, transportFactory, protocolFactory),
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020054 threadFactory_(threadFactory) {
55}
Jim King21b68522015-04-26 18:30:26 -040056
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020057TThreadedServer::TThreadedServer(const shared_ptr<TProcessorFactory>& processorFactory,
58 const shared_ptr<TServerTransport>& serverTransport,
59 const shared_ptr<TTransportFactory>& inputTransportFactory,
60 const shared_ptr<TTransportFactory>& outputTransportFactory,
61 const shared_ptr<TProtocolFactory>& inputProtocolFactory,
62 const shared_ptr<TProtocolFactory>& outputProtocolFactory,
63 const shared_ptr<ThreadFactory>& threadFactory)
64 : TServerFramework(processorFactory,
65 serverTransport,
66 inputTransportFactory,
67 outputTransportFactory,
68 inputProtocolFactory,
69 outputProtocolFactory),
70 threadFactory_(threadFactory) {
71}
Jim King21b68522015-04-26 18:30:26 -040072
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020073TThreadedServer::TThreadedServer(const shared_ptr<TProcessor>& processor,
74 const shared_ptr<TServerTransport>& serverTransport,
75 const shared_ptr<TTransportFactory>& inputTransportFactory,
76 const shared_ptr<TTransportFactory>& outputTransportFactory,
77 const shared_ptr<TProtocolFactory>& inputProtocolFactory,
78 const shared_ptr<TProtocolFactory>& outputProtocolFactory,
79 const shared_ptr<ThreadFactory>& threadFactory)
80 : TServerFramework(processor,
81 serverTransport,
82 inputTransportFactory,
83 outputTransportFactory,
84 inputProtocolFactory,
85 outputProtocolFactory),
86 threadFactory_(threadFactory) {
87}
Mark Sleeb3cb6292007-02-01 22:55:00 +000088
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020089TThreadedServer::~TThreadedServer() {
90}
Mark Sleeb3cb6292007-02-01 22:55:00 +000091
92void TThreadedServer::serve() {
Jim King21b68522015-04-26 18:30:26 -040093 TServerFramework::serve();
Mark Sleeb3cb6292007-02-01 22:55:00 +000094
Jim King21b68522015-04-26 18:30:26 -040095 // Drain all clients - no more will arrive
96 try {
97 Synchronized s(clientsMonitor_);
Jim King79c99112015-04-30 07:10:08 -040098 while (getConcurrentClientCount() > 0) {
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020099 clientsMonitor_.wait();
Mark Sleeb3cb6292007-02-01 22:55:00 +0000100 }
Jim King21b68522015-04-26 18:30:26 -0400101 } catch (TException& tx) {
102 string errStr = string("TThreadedServer: Exception joining workers: ") + tx.what();
103 GlobalOutput(errStr.c_str());
Mark Slee1d4ce802007-03-07 05:16:16 +0000104 }
Mark Sleeb3cb6292007-02-01 22:55:00 +0000105}
Ben Craig1684c422015-04-24 08:52:44 -0500106
Jim King79c99112015-04-30 07:10:08 -0400107void TThreadedServer::onClientConnected(const shared_ptr<TConnectedClient>& pClient) {
108 threadFactory_->newThread(pClient)->start();
Jim King21b68522015-04-26 18:30:26 -0400109}
110
Konrad Grochowski24ea0bf2015-05-07 14:59:29 +0200111void TThreadedServer::onClientDisconnected(TConnectedClient* pClient) {
112 THRIFT_UNUSED_VARIABLE(pClient);
Jim King21b68522015-04-26 18:30:26 -0400113 Synchronized s(clientsMonitor_);
Jim King79c99112015-04-30 07:10:08 -0400114 if (getConcurrentClientCount() == 0) {
115 clientsMonitor_.notify();
Jim King21b68522015-04-26 18:30:26 -0400116 }
Jim King5ec805b2015-04-26 07:52:40 -0400117}
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100118}
119}
120} // apache::thrift::server