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; |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 11 | using namespace facebook::thrift; |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 12 | using namespace facebook::thrift::concurrency; |
| 13 | using namespace facebook::thrift::transport; |
| 14 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 15 | class TThreadPoolServer::Task: public Runnable { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 16 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 17 | public: |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 18 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 19 | Task(shared_ptr<TProcessor> processor, |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 20 | shared_ptr<TProtocol> input, |
| 21 | shared_ptr<TProtocol> output) : |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 22 | processor_(processor), |
| 23 | input_(input), |
| 24 | output_(output) { |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 25 | } |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 26 | |
| 27 | ~Task() {} |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 28 | |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 29 | void run() { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 30 | try { |
| 31 | while (processor_->process(input_, output_)) { |
| 32 | if (!input_->getInputTransport()->peek()) { |
| 33 | break; |
| 34 | } |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 35 | } |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 36 | } catch (TTransportException& ttx) { |
| 37 | cerr << "TThreadPoolServer client died: " << ttx.what() << endl; |
| 38 | } catch (TException& x) { |
| 39 | cerr << "TThreadPoolServer exception: " << x.what() << endl; |
| 40 | } catch (...) { |
| 41 | cerr << "TThreadPoolServer uncaught exception." << endl; |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 42 | } |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 43 | input_->getInputTransport()->close(); |
| 44 | output_->getOutputTransport()->close(); |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 45 | } |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 46 | |
| 47 | private: |
| 48 | shared_ptr<TProcessor> processor_; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 49 | shared_ptr<TProtocol> input_; |
| 50 | shared_ptr<TProtocol> output_; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 51 | |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 52 | }; |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 53 | |
| 54 | TThreadPoolServer::TThreadPoolServer(shared_ptr<TProcessor> processor, |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 55 | shared_ptr<TServerTransport> serverTransport, |
| 56 | shared_ptr<TTransportFactory> transportFactory, |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 57 | shared_ptr<TProtocolFactory> protocolFactory, |
| 58 | |
| 59 | shared_ptr<ThreadManager> threadManager) : |
| 60 | TServer(processor, serverTransport, transportFactory, protocolFactory), |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 61 | threadManager_(threadManager) { |
| 62 | } |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 63 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 64 | TThreadPoolServer::~TThreadPoolServer() {} |
| 65 | |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 66 | void TThreadPoolServer::serve() { |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 67 | |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 68 | shared_ptr<TTransport> client; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 69 | pair<shared_ptr<TTransport>,shared_ptr<TTransport> > iot; |
| 70 | pair<shared_ptr<TProtocol>,shared_ptr<TProtocol> > iop; |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 71 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 72 | try { |
| 73 | // Start the server listening |
| 74 | serverTransport_->listen(); |
| 75 | } catch (TTransportException& ttx) { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 76 | cerr << "TThreadPoolServer::run() listen(): " << ttx.what() << endl; |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 77 | return; |
| 78 | } |
| 79 | |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 80 | while (true) { |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 81 | try { |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 82 | // Fetch client from server |
| 83 | client = serverTransport_->accept(); |
| 84 | // Make IO transports |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 85 | iot = transportFactory_->getIOTransports(client); |
| 86 | iop = protocolFactory_->getIOProtocols(iot.first, iot.second); |
| 87 | |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 88 | // Add to threadmanager pool |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 89 | threadManager_->add(shared_ptr<TThreadPoolServer::Task>(new TThreadPoolServer::Task(processor_, iop.first, iop.second))); |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 90 | } catch (TTransportException& ttx) { |
| 91 | break; |
| 92 | } |
| 93 | } |
| 94 | } |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 95 | |
| 96 | }}} // facebook::thrift::server |