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 | |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 20 | #ifndef _THRIFT_SERVER_TTHREADEDSERVER_H_ |
| 21 | #define _THRIFT_SERVER_TTHREADEDSERVER_H_ 1 |
| 22 | |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 23 | #include <thrift/concurrency/Monitor.h> |
Jim King | 5ec805b | 2015-04-26 07:52:40 -0400 | [diff] [blame] | 24 | #include <thrift/concurrency/PlatformThreadFactory.h> |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 25 | #include <thrift/concurrency/Thread.h> |
Jim King | 21b6852 | 2015-04-26 18:30:26 -0400 | [diff] [blame^] | 26 | #include <thrift/server/TServerFramework.h> |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 27 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 28 | namespace apache { |
| 29 | namespace thrift { |
| 30 | namespace server { |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 31 | |
Jim King | 21b6852 | 2015-04-26 18:30:26 -0400 | [diff] [blame^] | 32 | #define THRIFT_DEFAULT_THREAD_FACTORY |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 33 | |
Jim King | 21b6852 | 2015-04-26 18:30:26 -0400 | [diff] [blame^] | 34 | /** |
| 35 | * Manage clients using a thread pool. |
| 36 | */ |
| 37 | class TThreadedServer : public TServerFramework { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 38 | public: |
Jim King | 21b6852 | 2015-04-26 18:30:26 -0400 | [diff] [blame^] | 39 | TThreadedServer(const boost::shared_ptr<apache::thrift::TProcessorFactory>& processorFactory, |
| 40 | const boost::shared_ptr<apache::thrift::transport::TServerTransport>& serverTransport, |
| 41 | const boost::shared_ptr<apache::thrift::transport::TTransportFactory>& transportFactory, |
| 42 | const boost::shared_ptr<apache::thrift::protocol::TProtocolFactory>& protocolFactory, |
| 43 | const boost::shared_ptr<apache::thrift::concurrency::ThreadFactory>& threadFactory = |
| 44 | boost::shared_ptr<apache::thrift::concurrency::ThreadFactory>( |
| 45 | new apache::thrift::concurrency::PlatformThreadFactory)); |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 46 | |
Jim King | 21b6852 | 2015-04-26 18:30:26 -0400 | [diff] [blame^] | 47 | TThreadedServer(const boost::shared_ptr<apache::thrift::TProcessor>& processor, |
| 48 | const boost::shared_ptr<apache::thrift::transport::TServerTransport>& serverTransport, |
| 49 | const boost::shared_ptr<apache::thrift::transport::TTransportFactory>& transportFactory, |
| 50 | const boost::shared_ptr<apache::thrift::protocol::TProtocolFactory>& protocolFactory, |
| 51 | const boost::shared_ptr<apache::thrift::concurrency::ThreadFactory>& threadFactory = |
| 52 | boost::shared_ptr<apache::thrift::concurrency::ThreadFactory>( |
| 53 | new apache::thrift::concurrency::PlatformThreadFactory)); |
Bryan Duxbury | 7a9fb81 | 2011-09-01 18:31:53 +0000 | [diff] [blame] | 54 | |
Jim King | 21b6852 | 2015-04-26 18:30:26 -0400 | [diff] [blame^] | 55 | TThreadedServer(const boost::shared_ptr<apache::thrift::TProcessorFactory>& processorFactory, |
| 56 | const boost::shared_ptr<apache::thrift::transport::TServerTransport>& serverTransport, |
| 57 | const boost::shared_ptr<apache::thrift::transport::TTransportFactory>& inputTransportFactory, |
| 58 | const boost::shared_ptr<apache::thrift::transport::TTransportFactory>& outputTransportFactory, |
| 59 | const boost::shared_ptr<apache::thrift::protocol::TProtocolFactory>& inputProtocolFactory, |
| 60 | const boost::shared_ptr<apache::thrift::protocol::TProtocolFactory>& outputProtocolFactory, |
| 61 | const boost::shared_ptr<apache::thrift::concurrency::ThreadFactory>& threadFactory = |
| 62 | boost::shared_ptr<apache::thrift::concurrency::ThreadFactory>( |
| 63 | new apache::thrift::concurrency::PlatformThreadFactory)); |
Bryan Duxbury | 7a9fb81 | 2011-09-01 18:31:53 +0000 | [diff] [blame] | 64 | |
Jim King | 21b6852 | 2015-04-26 18:30:26 -0400 | [diff] [blame^] | 65 | TThreadedServer(const boost::shared_ptr<apache::thrift::TProcessor>& processor, |
| 66 | const boost::shared_ptr<apache::thrift::transport::TServerTransport>& serverTransport, |
| 67 | const boost::shared_ptr<apache::thrift::transport::TTransportFactory>& inputTransportFactory, |
| 68 | const boost::shared_ptr<apache::thrift::transport::TTransportFactory>& outputTransportFactory, |
| 69 | const boost::shared_ptr<apache::thrift::protocol::TProtocolFactory>& inputProtocolFactory, |
| 70 | const boost::shared_ptr<apache::thrift::protocol::TProtocolFactory>& outputProtocolFactory, |
| 71 | const boost::shared_ptr<apache::thrift::concurrency::ThreadFactory>& threadFactory = |
| 72 | boost::shared_ptr<apache::thrift::concurrency::ThreadFactory>( |
| 73 | new apache::thrift::concurrency::PlatformThreadFactory)); |
David Reiss | 45d5696 | 2009-03-14 23:35:16 +0000 | [diff] [blame] | 74 | |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 75 | virtual ~TThreadedServer(); |
| 76 | |
Jim King | 5ec805b | 2015-04-26 07:52:40 -0400 | [diff] [blame] | 77 | /** |
Jim King | 5ec805b | 2015-04-26 07:52:40 -0400 | [diff] [blame] | 78 | * Post-conditions (return guarantees): |
Jim King | 21b6852 | 2015-04-26 18:30:26 -0400 | [diff] [blame^] | 79 | * There will be no clients connected. |
Jim King | 5ec805b | 2015-04-26 07:52:40 -0400 | [diff] [blame] | 80 | */ |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 81 | virtual void serve(); |
Jim King | 5ec805b | 2015-04-26 07:52:40 -0400 | [diff] [blame] | 82 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 83 | protected: |
Jim King | 21b6852 | 2015-04-26 18:30:26 -0400 | [diff] [blame^] | 84 | virtual void onClientConnected(const boost::shared_ptr<TConnectedClient>& pClient) /* override */; |
| 85 | virtual void onClientDisconnected(TConnectedClient *pClient) /* override */; |
Bryan Duxbury | 7a9fb81 | 2011-09-01 18:31:53 +0000 | [diff] [blame] | 86 | |
Jim King | 21b6852 | 2015-04-26 18:30:26 -0400 | [diff] [blame^] | 87 | boost::shared_ptr<apache::thrift::concurrency::ThreadFactory> threadFactory_; |
| 88 | apache::thrift::concurrency::Monitor clientsMonitor_; |
Jim King | 5ec805b | 2015-04-26 07:52:40 -0400 | [diff] [blame] | 89 | std::set<TConnectedClient*> clients_; |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 90 | }; |
| 91 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | } // apache::thrift::server |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 95 | |
| 96 | #endif // #ifndef _THRIFT_SERVER_TTHREADEDSERVER_H_ |