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