David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 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 | */ |
Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 19 | |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 20 | #include <thrift/concurrency/PlatformThreadFactory.h> |
Jim King | 21b6852 | 2015-04-26 18:30:26 -0400 | [diff] [blame] | 21 | #include <thrift/server/TThreadedServer.h> |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 22 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 23 | namespace apache { |
| 24 | namespace thrift { |
| 25 | namespace server { |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 26 | |
Jim King | 21b6852 | 2015-04-26 18:30:26 -0400 | [diff] [blame] | 27 | using apache::thrift::concurrency::Synchronized; |
| 28 | using apache::thrift::concurrency::Thread; |
| 29 | using apache::thrift::concurrency::ThreadFactory; |
| 30 | using apache::thrift::protocol::TProtocol; |
| 31 | using apache::thrift::protocol::TProtocolFactory; |
| 32 | using apache::thrift::transport::TServerTransport; |
| 33 | using apache::thrift::transport::TTransport; |
| 34 | using apache::thrift::transport::TTransportException; |
| 35 | using apache::thrift::transport::TTransportFactory; |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 36 | using boost::shared_ptr; |
Jim King | 21b6852 | 2015-04-26 18:30:26 -0400 | [diff] [blame] | 37 | using std::string; |
| 38 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 39 | TThreadedServer::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 King | 21b6852 | 2015-04-26 18:30:26 -0400 | [diff] [blame] | 44 | : TServerFramework(processorFactory, serverTransport, transportFactory, protocolFactory), |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 45 | threadFactory_(threadFactory) { |
| 46 | } |
Jim King | 21b6852 | 2015-04-26 18:30:26 -0400 | [diff] [blame] | 47 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 48 | TThreadedServer::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 King | 21b6852 | 2015-04-26 18:30:26 -0400 | [diff] [blame] | 53 | : TServerFramework(processor, serverTransport, transportFactory, protocolFactory), |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 54 | threadFactory_(threadFactory) { |
| 55 | } |
Jim King | 21b6852 | 2015-04-26 18:30:26 -0400 | [diff] [blame] | 56 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 57 | TThreadedServer::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 King | 21b6852 | 2015-04-26 18:30:26 -0400 | [diff] [blame] | 72 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 73 | TThreadedServer::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 Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 88 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 89 | TThreadedServer::~TThreadedServer() { |
| 90 | } |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 91 | |
| 92 | void TThreadedServer::serve() { |
Jim King | 21b6852 | 2015-04-26 18:30:26 -0400 | [diff] [blame] | 93 | TServerFramework::serve(); |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 94 | |
Jim King | 21b6852 | 2015-04-26 18:30:26 -0400 | [diff] [blame] | 95 | // Drain all clients - no more will arrive |
| 96 | try { |
| 97 | Synchronized s(clientsMonitor_); |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 98 | while (getConcurrentClientCount() > 0) { |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 99 | clientsMonitor_.wait(); |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 100 | } |
Jim King | 21b6852 | 2015-04-26 18:30:26 -0400 | [diff] [blame] | 101 | } catch (TException& tx) { |
| 102 | string errStr = string("TThreadedServer: Exception joining workers: ") + tx.what(); |
| 103 | GlobalOutput(errStr.c_str()); |
Mark Slee | 1d4ce80 | 2007-03-07 05:16:16 +0000 | [diff] [blame] | 104 | } |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 105 | } |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 106 | |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 107 | void TThreadedServer::onClientConnected(const shared_ptr<TConnectedClient>& pClient) { |
| 108 | threadFactory_->newThread(pClient)->start(); |
Jim King | 21b6852 | 2015-04-26 18:30:26 -0400 | [diff] [blame] | 109 | } |
| 110 | |
Konrad Grochowski | 24ea0bf | 2015-05-07 14:59:29 +0200 | [diff] [blame] | 111 | void TThreadedServer::onClientDisconnected(TConnectedClient* pClient) { |
| 112 | THRIFT_UNUSED_VARIABLE(pClient); |
Jim King | 21b6852 | 2015-04-26 18:30:26 -0400 | [diff] [blame] | 113 | Synchronized s(clientsMonitor_); |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 114 | if (getConcurrentClientCount() == 0) { |
| 115 | clientsMonitor_.notify(); |
Jim King | 21b6852 | 2015-04-26 18:30:26 -0400 | [diff] [blame] | 116 | } |
Jim King | 5ec805b | 2015-04-26 07:52:40 -0400 | [diff] [blame] | 117 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | } // apache::thrift::server |