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; |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 17 | shared_ptr<TTransport> inputTransport; |
| 18 | shared_ptr<TTransport> outputTransport; |
| 19 | shared_ptr<TProtocol> inputProtocol; |
| 20 | shared_ptr<TProtocol> outputProtocol; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 21 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 22 | try { |
| 23 | // Start the server listening |
| 24 | serverTransport_->listen(); |
| 25 | } catch (TTransportException& ttx) { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 26 | cerr << "TSimpleServer::run() listen(): " << ttx.what() << endl; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 27 | return; |
| 28 | } |
| 29 | |
| 30 | // Fetch client from server |
Aditya Agarwal | fdef47e | 2007-02-07 03:54:18 +0000 | [diff] [blame] | 31 | while (true) { |
| 32 | try { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 33 | client = serverTransport_->accept(); |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 34 | inputTransport = inputTransportFactory_->getTransport(client); |
| 35 | outputTransport = outputTransportFactory_->getTransport(client); |
| 36 | inputProtocol = inputProtocolFactory_->getProtocol(inputTransport); |
| 37 | outputProtocol = outputProtocolFactory_->getProtocol(outputTransport); |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 38 | try { |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 39 | while (processor_->process(inputProtocol, outputProtocol)) { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 40 | // Peek ahead, is the remote side closed? |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 41 | if (!inputTransport->peek()) { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 42 | break; |
| 43 | } |
| 44 | } |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 45 | } catch (TTransportException& ttx) { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 46 | cerr << "TSimpleServer client died: " << ttx.what() << endl; |
Mark Slee | 3860c9a | 2006-12-06 00:13:42 +0000 | [diff] [blame] | 47 | } catch (TException& tx) { |
| 48 | cerr << "TSimpleServer exception: " << tx.what() << endl; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 49 | } |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 50 | inputTransport->close(); |
| 51 | outputTransport->close(); |
Aditya Agarwal | fdef47e | 2007-02-07 03:54:18 +0000 | [diff] [blame] | 52 | client->close(); |
| 53 | } catch (TTransportException& ttx) { |
| 54 | inputTransport->close(); |
| 55 | outputTransport->close(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 56 | client->close(); |
Aditya Agarwal | fdef47e | 2007-02-07 03:54:18 +0000 | [diff] [blame] | 57 | cerr << "TServerTransport died on accept: " << ttx.what() << endl; |
| 58 | continue; |
| 59 | } catch (TException& tx) { |
| 60 | inputTransport->close(); |
| 61 | outputTransport->close(); |
| 62 | client->close(); |
| 63 | cerr << "Some kind of accept exception: " << tx.what() << endl; |
| 64 | continue; |
| 65 | } catch (string s) { |
| 66 | inputTransport->close(); |
| 67 | outputTransport->close(); |
| 68 | client->close(); |
| 69 | cerr << "TThreadPoolServer: Unknown exception: " << s << endl; |
| 70 | break; |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 71 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 74 | // 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] | 75 | } |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 76 | |
| 77 | }}} // facebook::thrift::server |