Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 1 | #include "server/TSimpleServer.h" |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 2 | #include "transport/TTransportException.h" |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 3 | #include <string> |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 4 | #include <iostream> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 5 | using namespace std; |
| 6 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 7 | namespace facebook { namespace thrift { namespace server { |
| 8 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 9 | /** |
| 10 | * A simple single-threaded application server. Perfect for unit tests! |
| 11 | * |
| 12 | * @author Mark Slee <mcslee@facebook.com> |
| 13 | */ |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 14 | void TSimpleServer::serve() { |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 15 | |
| 16 | shared_ptr<TTransport> client; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame^] | 17 | pair<shared_ptr<TTransport>,shared_ptr<TTransport> > iot; |
| 18 | pair<shared_ptr<TProtocol>,shared_ptr<TProtocol> > iop; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 19 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 20 | try { |
| 21 | // Start the server listening |
| 22 | serverTransport_->listen(); |
| 23 | } catch (TTransportException& ttx) { |
| 24 | cerr << "TSimpleServer::run() listen(): " << ttx.getMessage() << endl; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 25 | return; |
| 26 | } |
| 27 | |
| 28 | // Fetch client from server |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 29 | try { |
| 30 | while (true) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 31 | client = serverTransport_->accept(); |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame^] | 32 | iot = transportFactory_->getIOTransports(client); |
| 33 | iop = protocolFactory_->getIOProtocols(iot.first, iot.second); |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 34 | try { |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame^] | 35 | while (processor_->process(iop.first, iop.second)) {} |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 36 | } catch (TTransportException& ttx) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 37 | cerr << "TSimpleServer client died: " << ttx.getMessage() << endl; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 38 | } |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame^] | 39 | iot.first->close(); |
| 40 | iot.second->close(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 41 | client->close(); |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 42 | } |
| 43 | } catch (TTransportException& ttx) { |
| 44 | cerr << "TServerTransport died on accept: " << ttx.getMessage() << endl; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 47 | // TODO(mcslee): Could this be a timeout case? Or always the real thing? |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 48 | } |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 49 | |
| 50 | }}} // facebook::thrift::server |