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/TBufferedTransport.h" |
| 3 | #include "transport/TTransportException.h" |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 4 | #include <string> |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 5 | #include <iostream> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 6 | using namespace std; |
| 7 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 8 | namespace facebook { namespace thrift { namespace server { |
| 9 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 10 | /** |
| 11 | * A simple single-threaded application server. Perfect for unit tests! |
| 12 | * |
| 13 | * @author Mark Slee <mcslee@facebook.com> |
| 14 | */ |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 15 | void TSimpleServer::run() { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 16 | TTransport* client = NULL; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 17 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 18 | try { |
| 19 | // Start the server listening |
| 20 | serverTransport_->listen(); |
| 21 | } catch (TTransportException& ttx) { |
| 22 | cerr << "TSimpleServer::run() listen(): " << ttx.getMessage() << endl; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 23 | return; |
| 24 | } |
| 25 | |
| 26 | // Fetch client from server |
| 27 | while (true) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 28 | try { |
| 29 | client = serverTransport_->accept(); |
| 30 | if (client != NULL) { |
| 31 | // Process for as long as we can keep the processor happy! |
| 32 | TBufferedTransport bufferedClient(client); |
| 33 | while (processor_->process(&bufferedClient)) {} |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 34 | } |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 35 | } catch (TTransportException& ttx) { |
| 36 | if (client != NULL) { |
| 37 | cerr << "TSimpleServer client died: " << ttx.getMessage() << endl; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 38 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 41 | // Clean up the client |
| 42 | if (client != NULL) { |
| 43 | |
| 44 | // Ensure no resource leaks |
| 45 | client->close(); |
| 46 | |
| 47 | // Ensure no memory leaks |
| 48 | delete client; |
| 49 | |
| 50 | // Ensure we don't try to double-free on the next pass |
| 51 | client = NULL; |
| 52 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 55 | // 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] | 56 | } |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 57 | |
| 58 | }}} // facebook::thrift::server |