Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 1 | // Copyright (c) 2006- Facebook |
| 2 | // Distributed under the Thrift Software License |
| 3 | // |
| 4 | // See accompanying file LICENSE or visit the Thrift site at: |
| 5 | // http://developers.facebook.com/thrift/ |
| 6 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 7 | #include "server/TSimpleServer.h" |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 8 | #include "transport/TTransportException.h" |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 9 | #include <string> |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 10 | #include <iostream> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 11 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame^] | 12 | namespace apache { namespace thrift { namespace server { |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 13 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 14 | using namespace std; |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame^] | 15 | using namespace apache::thrift; |
| 16 | using namespace apache::thrift::protocol; |
| 17 | using namespace apache::thrift::transport; |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 18 | using boost::shared_ptr; |
| 19 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 20 | /** |
| 21 | * A simple single-threaded application server. Perfect for unit tests! |
| 22 | * |
| 23 | * @author Mark Slee <mcslee@facebook.com> |
| 24 | */ |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 25 | void TSimpleServer::serve() { |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 26 | |
| 27 | shared_ptr<TTransport> client; |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 28 | shared_ptr<TTransport> inputTransport; |
| 29 | shared_ptr<TTransport> outputTransport; |
| 30 | shared_ptr<TProtocol> inputProtocol; |
| 31 | shared_ptr<TProtocol> outputProtocol; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 32 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 33 | try { |
| 34 | // Start the server listening |
| 35 | serverTransport_->listen(); |
| 36 | } catch (TTransportException& ttx) { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 37 | cerr << "TSimpleServer::run() listen(): " << ttx.what() << endl; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 38 | return; |
| 39 | } |
| 40 | |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 41 | // Run the preServe event |
| 42 | if (eventHandler_ != NULL) { |
| 43 | eventHandler_->preServe(); |
| 44 | } |
| 45 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 46 | // Fetch client from server |
Mark Slee | 6e3f637 | 2007-03-01 22:05:46 +0000 | [diff] [blame] | 47 | while (!stop_) { |
Aditya Agarwal | fdef47e | 2007-02-07 03:54:18 +0000 | [diff] [blame] | 48 | try { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 49 | client = serverTransport_->accept(); |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 50 | inputTransport = inputTransportFactory_->getTransport(client); |
| 51 | outputTransport = outputTransportFactory_->getTransport(client); |
| 52 | inputProtocol = inputProtocolFactory_->getProtocol(inputTransport); |
| 53 | outputProtocol = outputProtocolFactory_->getProtocol(outputTransport); |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 54 | if (eventHandler_ != NULL) { |
| 55 | eventHandler_->clientBegin(inputProtocol, outputProtocol); |
| 56 | } |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 57 | try { |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 58 | while (processor_->process(inputProtocol, outputProtocol)) { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 59 | // Peek ahead, is the remote side closed? |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 60 | if (!inputTransport->peek()) { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 61 | break; |
| 62 | } |
| 63 | } |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 64 | } catch (TTransportException& ttx) { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 65 | cerr << "TSimpleServer client died: " << ttx.what() << endl; |
Mark Slee | 3860c9a | 2006-12-06 00:13:42 +0000 | [diff] [blame] | 66 | } catch (TException& tx) { |
| 67 | cerr << "TSimpleServer exception: " << tx.what() << endl; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 68 | } |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 69 | if (eventHandler_ != NULL) { |
| 70 | eventHandler_->clientEnd(inputProtocol, outputProtocol); |
| 71 | } |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 72 | inputTransport->close(); |
| 73 | outputTransport->close(); |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 74 | client->close(); |
Aditya Agarwal | fdef47e | 2007-02-07 03:54:18 +0000 | [diff] [blame] | 75 | } catch (TTransportException& ttx) { |
Mark Slee | 3303f36 | 2007-03-05 20:09:37 +0000 | [diff] [blame] | 76 | if (inputTransport != NULL) { inputTransport->close(); } |
| 77 | if (outputTransport != NULL) { outputTransport->close(); } |
| 78 | if (client != NULL) { client->close(); } |
Aditya Agarwal | fdef47e | 2007-02-07 03:54:18 +0000 | [diff] [blame] | 79 | cerr << "TServerTransport died on accept: " << ttx.what() << endl; |
| 80 | continue; |
| 81 | } catch (TException& tx) { |
Mark Slee | 3303f36 | 2007-03-05 20:09:37 +0000 | [diff] [blame] | 82 | if (inputTransport != NULL) { inputTransport->close(); } |
| 83 | if (outputTransport != NULL) { outputTransport->close(); } |
| 84 | if (client != NULL) { client->close(); } |
Aditya Agarwal | fdef47e | 2007-02-07 03:54:18 +0000 | [diff] [blame] | 85 | cerr << "Some kind of accept exception: " << tx.what() << endl; |
| 86 | continue; |
| 87 | } catch (string s) { |
Mark Slee | 3303f36 | 2007-03-05 20:09:37 +0000 | [diff] [blame] | 88 | if (inputTransport != NULL) { inputTransport->close(); } |
| 89 | if (outputTransport != NULL) { outputTransport->close(); } |
| 90 | if (client != NULL) { client->close(); } |
Aditya Agarwal | fdef47e | 2007-02-07 03:54:18 +0000 | [diff] [blame] | 91 | cerr << "TThreadPoolServer: Unknown exception: " << s << endl; |
| 92 | break; |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 93 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Mark Slee | 6e3f637 | 2007-03-01 22:05:46 +0000 | [diff] [blame] | 96 | if (stop_) { |
| 97 | try { |
| 98 | serverTransport_->close(); |
| 99 | } catch (TTransportException &ttx) { |
| 100 | cerr << "TServerTransport failed on close: " << ttx.what() << endl; |
| 101 | } |
| 102 | stop_ = false; |
| 103 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 104 | } |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 105 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame^] | 106 | }}} // apache::thrift::server |