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 | |
| 23 | #include <server/TServer.h> |
| 24 | #include <transport/TServerTransport.h> |
Mark Slee | 1d4ce80 | 2007-03-07 05:16:16 +0000 | [diff] [blame] | 25 | #include <concurrency/Monitor.h> |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 26 | #include <concurrency/Thread.h> |
| 27 | |
| 28 | #include <boost/shared_ptr.hpp> |
| 29 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 30 | namespace apache { namespace thrift { namespace server { |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 31 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 32 | using apache::thrift::TProcessor; |
| 33 | using apache::thrift::transport::TServerTransport; |
| 34 | using apache::thrift::transport::TTransportFactory; |
| 35 | using apache::thrift::concurrency::Monitor; |
| 36 | using apache::thrift::concurrency::ThreadFactory; |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 37 | |
| 38 | class TThreadedServer : public TServer { |
| 39 | |
| 40 | public: |
| 41 | class Task; |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 42 | |
Bryan Duxbury | 7a9fb81 | 2011-09-01 18:31:53 +0000 | [diff] [blame] | 43 | template<typename ProcessorFactory> |
| 44 | TThreadedServer(const boost::shared_ptr<ProcessorFactory>& processorFactory, |
| 45 | const boost::shared_ptr<TServerTransport>& serverTransport, |
| 46 | const boost::shared_ptr<TTransportFactory>& transportFactory, |
| 47 | const boost::shared_ptr<TProtocolFactory>& protocolFactory, |
| 48 | THRIFT_OVERLOAD_IF(ProcessorFactory, TProcessorFactory)); |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 49 | |
Bryan Duxbury | 7a9fb81 | 2011-09-01 18:31:53 +0000 | [diff] [blame] | 50 | template<typename ProcessorFactory> |
| 51 | TThreadedServer(const boost::shared_ptr<ProcessorFactory>& processorFactory, |
| 52 | const boost::shared_ptr<TServerTransport>& serverTransport, |
| 53 | const boost::shared_ptr<TTransportFactory>& transportFactory, |
| 54 | const boost::shared_ptr<TProtocolFactory>& protocolFactory, |
| 55 | const boost::shared_ptr<ThreadFactory>& threadFactory, |
| 56 | THRIFT_OVERLOAD_IF(ProcessorFactory, TProtocolFactory)); |
| 57 | |
| 58 | template<typename Processor> |
| 59 | TThreadedServer(const boost::shared_ptr<Processor>& processor, |
| 60 | const boost::shared_ptr<TServerTransport>& serverTransport, |
| 61 | const boost::shared_ptr<TTransportFactory>& transportFactory, |
| 62 | const boost::shared_ptr<TProtocolFactory>& protocolFactory, |
| 63 | THRIFT_OVERLOAD_IF(Processor, TProcessor)); |
| 64 | |
| 65 | template<typename Processor> |
| 66 | TThreadedServer(const boost::shared_ptr<Processor>& processor, |
| 67 | const boost::shared_ptr<TServerTransport>& serverTransport, |
| 68 | const boost::shared_ptr<TTransportFactory>& transportFactory, |
| 69 | const boost::shared_ptr<TProtocolFactory>& protocolFactory, |
| 70 | const boost::shared_ptr<ThreadFactory>& threadFactory, |
| 71 | THRIFT_OVERLOAD_IF(Processor, TProcessor)); |
David Reiss | 45d5696 | 2009-03-14 23:35:16 +0000 | [diff] [blame] | 72 | |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 73 | virtual ~TThreadedServer(); |
| 74 | |
| 75 | virtual void serve(); |
| 76 | |
Mark Slee | 1d4ce80 | 2007-03-07 05:16:16 +0000 | [diff] [blame] | 77 | void stop() { |
| 78 | stop_ = true; |
| 79 | serverTransport_->interrupt(); |
| 80 | } |
| 81 | |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 82 | protected: |
Bryan Duxbury | 7a9fb81 | 2011-09-01 18:31:53 +0000 | [diff] [blame] | 83 | void init(); |
| 84 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 85 | boost::shared_ptr<ThreadFactory> threadFactory_; |
Mark Slee | 1d4ce80 | 2007-03-07 05:16:16 +0000 | [diff] [blame] | 86 | volatile bool stop_; |
| 87 | |
| 88 | Monitor tasksMonitor_; |
| 89 | std::set<Task*> tasks_; |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 90 | |
| 91 | }; |
| 92 | |
Bryan Duxbury | 7a9fb81 | 2011-09-01 18:31:53 +0000 | [diff] [blame] | 93 | template<typename ProcessorFactory> |
| 94 | TThreadedServer::TThreadedServer( |
| 95 | const boost::shared_ptr<ProcessorFactory>& processorFactory, |
| 96 | const boost::shared_ptr<TServerTransport>& serverTransport, |
| 97 | const boost::shared_ptr<TTransportFactory>& transportFactory, |
| 98 | const boost::shared_ptr<TProtocolFactory>& protocolFactory, |
| 99 | THRIFT_OVERLOAD_IF_DEFN(ProcessorFactory, TProcessorFactory)) : |
| 100 | TServer(processorFactory, serverTransport, transportFactory, |
| 101 | protocolFactory) { |
| 102 | init(); |
| 103 | } |
| 104 | |
| 105 | template<typename ProcessorFactory> |
| 106 | TThreadedServer::TThreadedServer( |
| 107 | const boost::shared_ptr<ProcessorFactory>& processorFactory, |
| 108 | const boost::shared_ptr<TServerTransport>& serverTransport, |
| 109 | const boost::shared_ptr<TTransportFactory>& transportFactory, |
| 110 | const boost::shared_ptr<TProtocolFactory>& protocolFactory, |
| 111 | const boost::shared_ptr<ThreadFactory>& threadFactory, |
| 112 | THRIFT_OVERLOAD_IF_DEFN(ProcessorFactory, TProtocolFactory)) : |
| 113 | TServer(processorFactory, serverTransport, transportFactory, |
| 114 | protocolFactory), |
| 115 | threadFactory_(threadFactory) { |
| 116 | init(); |
| 117 | } |
| 118 | |
| 119 | template<typename Processor> |
| 120 | TThreadedServer::TThreadedServer( |
| 121 | const boost::shared_ptr<Processor>& processor, |
| 122 | const boost::shared_ptr<TServerTransport>& serverTransport, |
| 123 | const boost::shared_ptr<TTransportFactory>& transportFactory, |
| 124 | const boost::shared_ptr<TProtocolFactory>& protocolFactory, |
| 125 | THRIFT_OVERLOAD_IF_DEFN(Processor, TProcessor)) : |
| 126 | TServer(processor, serverTransport, transportFactory, protocolFactory) { |
| 127 | init(); |
| 128 | } |
| 129 | |
| 130 | template<typename Processor> |
| 131 | TThreadedServer::TThreadedServer( |
| 132 | const boost::shared_ptr<Processor>& processor, |
| 133 | const boost::shared_ptr<TServerTransport>& serverTransport, |
| 134 | const boost::shared_ptr<TTransportFactory>& transportFactory, |
| 135 | const boost::shared_ptr<TProtocolFactory>& protocolFactory, |
| 136 | const boost::shared_ptr<ThreadFactory>& threadFactory, |
| 137 | THRIFT_OVERLOAD_IF_DEFN(Processor, TProcessor)) : |
| 138 | TServer(processor, serverTransport, transportFactory, protocolFactory), |
| 139 | threadFactory_(threadFactory) { |
| 140 | init(); |
| 141 | } |
| 142 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 143 | }}} // apache::thrift::server |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 144 | |
| 145 | #endif // #ifndef _THRIFT_SERVER_TTHREADEDSERVER_H_ |