blob: 08309b2d49723fb6ed81a244039e22e642f11782 [file] [log] [blame]
Mark Slee9f0c6512007-02-28 23:58:26 +00001// 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 Sleee8540632006-05-30 09:24:40 +00007#include "server/TSimpleServer.h"
Mark Slee8d7e1f62006-06-07 06:48:56 +00008#include "transport/TTransportException.h"
Mark Sleee8540632006-05-30 09:24:40 +00009#include <string>
Mark Slee8d7e1f62006-06-07 06:48:56 +000010#include <iostream>
Mark Sleee8540632006-05-30 09:24:40 +000011
T Jake Lucianib5e62212009-01-31 22:36:20 +000012namespace apache { namespace thrift { namespace server {
Marc Slemko6f038a72006-08-03 18:58:09 +000013
Mark Slee5ea15f92007-03-05 22:55:59 +000014using namespace std;
T Jake Lucianib5e62212009-01-31 22:36:20 +000015using namespace apache::thrift;
16using namespace apache::thrift::protocol;
17using namespace apache::thrift::transport;
Mark Slee5ea15f92007-03-05 22:55:59 +000018using boost::shared_ptr;
19
Mark Slee8d7e1f62006-06-07 06:48:56 +000020/**
21 * A simple single-threaded application server. Perfect for unit tests!
22 *
23 * @author Mark Slee <mcslee@facebook.com>
24 */
Mark Slee794993d2006-09-20 01:56:10 +000025void TSimpleServer::serve() {
Marc Slemko16698852006-08-04 03:16:10 +000026
27 shared_ptr<TTransport> client;
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000028 shared_ptr<TTransport> inputTransport;
29 shared_ptr<TTransport> outputTransport;
30 shared_ptr<TProtocol> inputProtocol;
31 shared_ptr<TProtocol> outputProtocol;
Mark Sleee8540632006-05-30 09:24:40 +000032
Mark Slee8d7e1f62006-06-07 06:48:56 +000033 try {
34 // Start the server listening
35 serverTransport_->listen();
36 } catch (TTransportException& ttx) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000037 cerr << "TSimpleServer::run() listen(): " << ttx.what() << endl;
Mark Sleee8540632006-05-30 09:24:40 +000038 return;
39 }
40
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000041 // Run the preServe event
42 if (eventHandler_ != NULL) {
43 eventHandler_->preServe();
44 }
45
Mark Sleee8540632006-05-30 09:24:40 +000046 // Fetch client from server
Mark Slee6e3f6372007-03-01 22:05:46 +000047 while (!stop_) {
Aditya Agarwalfdef47e2007-02-07 03:54:18 +000048 try {
Mark Slee8d7e1f62006-06-07 06:48:56 +000049 client = serverTransport_->accept();
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000050 inputTransport = inputTransportFactory_->getTransport(client);
51 outputTransport = outputTransportFactory_->getTransport(client);
52 inputProtocol = inputProtocolFactory_->getProtocol(inputTransport);
53 outputProtocol = outputProtocolFactory_->getProtocol(outputTransport);
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000054 if (eventHandler_ != NULL) {
55 eventHandler_->clientBegin(inputProtocol, outputProtocol);
56 }
Mark Sleed788b2e2006-09-07 01:26:35 +000057 try {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000058 while (processor_->process(inputProtocol, outputProtocol)) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000059 // Peek ahead, is the remote side closed?
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000060 if (!inputTransport->peek()) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000061 break;
62 }
63 }
Mark Sleed788b2e2006-09-07 01:26:35 +000064 } catch (TTransportException& ttx) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000065 cerr << "TSimpleServer client died: " << ttx.what() << endl;
Mark Slee3860c9a2006-12-06 00:13:42 +000066 } catch (TException& tx) {
67 cerr << "TSimpleServer exception: " << tx.what() << endl;
Mark Sleee8540632006-05-30 09:24:40 +000068 }
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000069 if (eventHandler_ != NULL) {
70 eventHandler_->clientEnd(inputProtocol, outputProtocol);
71 }
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000072 inputTransport->close();
73 outputTransport->close();
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000074 client->close();
Aditya Agarwalfdef47e2007-02-07 03:54:18 +000075 } catch (TTransportException& ttx) {
Mark Slee3303f362007-03-05 20:09:37 +000076 if (inputTransport != NULL) { inputTransport->close(); }
77 if (outputTransport != NULL) { outputTransport->close(); }
78 if (client != NULL) { client->close(); }
Aditya Agarwalfdef47e2007-02-07 03:54:18 +000079 cerr << "TServerTransport died on accept: " << ttx.what() << endl;
80 continue;
81 } catch (TException& tx) {
Mark Slee3303f362007-03-05 20:09:37 +000082 if (inputTransport != NULL) { inputTransport->close(); }
83 if (outputTransport != NULL) { outputTransport->close(); }
84 if (client != NULL) { client->close(); }
Aditya Agarwalfdef47e2007-02-07 03:54:18 +000085 cerr << "Some kind of accept exception: " << tx.what() << endl;
86 continue;
87 } catch (string s) {
Mark Slee3303f362007-03-05 20:09:37 +000088 if (inputTransport != NULL) { inputTransport->close(); }
89 if (outputTransport != NULL) { outputTransport->close(); }
90 if (client != NULL) { client->close(); }
Aditya Agarwalfdef47e2007-02-07 03:54:18 +000091 cerr << "TThreadPoolServer: Unknown exception: " << s << endl;
92 break;
Mark Sleed788b2e2006-09-07 01:26:35 +000093 }
Mark Sleee8540632006-05-30 09:24:40 +000094 }
95
Mark Slee6e3f6372007-03-01 22:05:46 +000096 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 Sleee8540632006-05-30 09:24:40 +0000104}
Marc Slemko6f038a72006-08-03 18:58:09 +0000105
T Jake Lucianib5e62212009-01-31 22:36:20 +0000106}}} // apache::thrift::server