Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 1 | #include "server/TThreadPoolServer.h" |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 2 | #include "transport/TTransportException.h" |
| 3 | #include "concurrency/Thread.h" |
| 4 | #include "concurrency/ThreadManager.h" |
| 5 | #include <string> |
| 6 | #include <iostream> |
| 7 | |
| 8 | namespace facebook { namespace thrift { namespace server { |
| 9 | |
| 10 | using namespace std; |
| 11 | using namespace facebook::thrift::concurrency; |
| 12 | using namespace facebook::thrift::transport; |
| 13 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 14 | class TThreadPoolServer::Task: public Runnable { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 15 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 16 | public: |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 17 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 18 | Task(shared_ptr<TProcessor> processor, |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 19 | shared_ptr<TTransport> input, |
| 20 | shared_ptr<TTransport> output) : |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 21 | processor_(processor), |
| 22 | input_(input), |
| 23 | output_(output) { |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 24 | } |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 25 | |
| 26 | ~Task() {} |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 27 | |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 28 | void run() { |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 29 | while(true) { |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 30 | try { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 31 | processor_->process(input_, output_); |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 32 | } catch (TTransportException& ttx) { |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 33 | break; |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 34 | } catch(...) { |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 35 | break; |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 36 | } |
| 37 | } |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 38 | input_->close(); |
| 39 | output_->close(); |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 40 | } |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 41 | |
| 42 | private: |
| 43 | shared_ptr<TProcessor> processor_; |
| 44 | shared_ptr<TTransport> input_; |
| 45 | shared_ptr<TTransport> output_; |
| 46 | |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 47 | }; |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 48 | |
| 49 | TThreadPoolServer::TThreadPoolServer(shared_ptr<TProcessor> processor, |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 50 | shared_ptr<TServerTransport> serverTransport, |
| 51 | shared_ptr<TTransportFactory> transportFactory, |
| 52 | shared_ptr<ThreadManager> threadManager, |
| 53 | shared_ptr<TServerOptions> options) : |
| 54 | TServer(processor, serverTransport, transportFactory, options), |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 55 | threadManager_(threadManager) { |
| 56 | } |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 57 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 58 | TThreadPoolServer::~TThreadPoolServer() {} |
| 59 | |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 60 | void TThreadPoolServer::serve() { |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 61 | |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 62 | shared_ptr<TTransport> client; |
| 63 | pair<shared_ptr<TTransport>,shared_ptr<TTransport> > io; |
| 64 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 65 | try { |
| 66 | // Start the server listening |
| 67 | serverTransport_->listen(); |
| 68 | } catch (TTransportException& ttx) { |
| 69 | cerr << "TThreadPoolServer::run() listen(): " << ttx.getMessage() << endl; |
| 70 | return; |
| 71 | } |
| 72 | |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 73 | while (true) { |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 74 | try { |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 75 | // Fetch client from server |
| 76 | client = serverTransport_->accept(); |
| 77 | // Make IO transports |
| 78 | io = transportFactory_->getIOTransports(client); |
| 79 | // Add to threadmanager pool |
| 80 | threadManager_->add(shared_ptr<TThreadPoolServer::Task>(new TThreadPoolServer::Task(processor_, io.first, io.second))); |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 81 | } catch (TTransportException& ttx) { |
| 82 | break; |
| 83 | } |
| 84 | } |
| 85 | } |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 86 | |
| 87 | }}} // facebook::thrift::server |