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_)) { |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 32 | if (!input_->getTransport()->peek()) { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 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 | } |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 43 | input_->getTransport()->close(); |
| 44 | output_->getTransport()->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, |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 58 | shared_ptr<ThreadManager> threadManager) : |
| 59 | TServer(processor, serverTransport, transportFactory, protocolFactory), |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 60 | threadManager_(threadManager) {} |
| 61 | |
| 62 | TThreadPoolServer::TThreadPoolServer(shared_ptr<TProcessor> processor, |
| 63 | shared_ptr<TServerTransport> serverTransport, |
| 64 | shared_ptr<TTransportFactory> inputTransportFactory, |
| 65 | shared_ptr<TTransportFactory> outputTransportFactory, |
| 66 | shared_ptr<TProtocolFactory> inputProtocolFactory, |
| 67 | shared_ptr<TProtocolFactory> outputProtocolFactory, |
| 68 | shared_ptr<ThreadManager> threadManager) : |
| 69 | TServer(processor, serverTransport, inputTransportFactory, outputTransportFactory, |
| 70 | inputProtocolFactory, outputProtocolFactory), |
| 71 | threadManager_(threadManager) {} |
| 72 | |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 73 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 74 | TThreadPoolServer::~TThreadPoolServer() {} |
| 75 | |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 76 | void TThreadPoolServer::serve() { |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 77 | |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 78 | shared_ptr<TTransport> client; |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 79 | shared_ptr<TTransport> inputTransport; |
| 80 | shared_ptr<TTransport> outputTransport; |
| 81 | shared_ptr<TProtocol> inputProtocol; |
| 82 | shared_ptr<TProtocol> outputProtocol; |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 83 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 84 | try { |
| 85 | // Start the server listening |
| 86 | serverTransport_->listen(); |
| 87 | } catch (TTransportException& ttx) { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 88 | cerr << "TThreadPoolServer::run() listen(): " << ttx.what() << endl; |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 89 | return; |
| 90 | } |
| 91 | |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 92 | while (true) { |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 93 | try { |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 94 | // Fetch client from server |
| 95 | client = serverTransport_->accept(); |
| 96 | // Make IO transports |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 97 | inputTransport = inputTransportFactory_->getTransport(client); |
| 98 | outputTransport = outputTransportFactory_->getTransport(client); |
| 99 | inputProtocol = inputProtocolFactory_->getProtocol(inputTransport); |
| 100 | outputProtocol = outputProtocolFactory_->getProtocol(outputTransport); |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 101 | |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 102 | // Add to threadmanager pool |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 103 | threadManager_->add(shared_ptr<TThreadPoolServer::Task>(new TThreadPoolServer::Task(processor_, inputProtocol, outputProtocol))); |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 104 | } catch (TTransportException& ttx) { |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | } |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 109 | |
| 110 | }}} // facebook::thrift::server |