Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 1 | #include "server/TThreadPoolServer.h" |
| 2 | #include "transport/TBufferedTransport.h" |
| 3 | #include "transport/TTransportException.h" |
| 4 | #include "concurrency/Thread.h" |
| 5 | #include "concurrency/ThreadManager.h" |
| 6 | #include <string> |
| 7 | #include <iostream> |
| 8 | |
| 9 | namespace facebook { namespace thrift { namespace server { |
| 10 | |
| 11 | using namespace std; |
| 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 { |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 16 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 17 | shared_ptr<TProcessor> _processor; |
| 18 | shared_ptr<TTransport> _transport; |
| 19 | shared_ptr<TBufferedTransport> _bufferedTransport; |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 20 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 21 | public: |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 22 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 23 | Task(shared_ptr<TProcessor> processor, |
| 24 | shared_ptr<TTransport> transport) : |
| 25 | _processor(processor), |
| 26 | _transport(transport), |
| 27 | _bufferedTransport(new TBufferedTransport(transport)) { |
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 | |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 32 | void run() { |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 33 | |
| 34 | while(true) { |
| 35 | |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 36 | try { |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 37 | _processor->process(_bufferedTransport); |
| 38 | |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 39 | } catch (TTransportException& ttx) { |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 40 | |
| 41 | break; |
| 42 | |
| 43 | } catch(...) { |
| 44 | |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 45 | break; |
| 46 | } |
| 47 | } |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 48 | |
| 49 | _bufferedTransport->close(); |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 50 | } |
| 51 | }; |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 52 | |
| 53 | TThreadPoolServer::TThreadPoolServer(shared_ptr<TProcessor> processor, |
| 54 | shared_ptr<TServerOptions> options, |
| 55 | shared_ptr<TServerTransport> serverTransport, |
| 56 | shared_ptr<ThreadManager> threadManager) : |
| 57 | TServer(processor, options), |
| 58 | serverTransport_(serverTransport), |
| 59 | threadManager_(threadManager) { |
| 60 | } |
| 61 | |
| 62 | TThreadPoolServer::~TThreadPoolServer() {} |
| 63 | |
| 64 | void TThreadPoolServer::run() { |
| 65 | |
| 66 | try { |
| 67 | // Start the server listening |
| 68 | serverTransport_->listen(); |
| 69 | } catch (TTransportException& ttx) { |
| 70 | cerr << "TThreadPoolServer::run() listen(): " << ttx.getMessage() << endl; |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | // Fetch client from server |
| 75 | |
| 76 | while (true) { |
| 77 | |
| 78 | try { |
| 79 | |
| 80 | threadManager_->add(shared_ptr<TThreadPoolServer::Task>(new TThreadPoolServer::Task(processor_, |
| 81 | shared_ptr<TTransport>(serverTransport_->accept())))); |
| 82 | |
| 83 | } catch (TTransportException& ttx) { |
| 84 | break; |
| 85 | } |
| 86 | } |
| 87 | } |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 88 | |
| 89 | }}} // facebook::thrift::server |