blob: d5d3797b9e79ada8e312d6f9e299570c8ccad144 [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
Marc Slemko6f038a72006-08-03 18:58:09 +000012namespace facebook { namespace thrift { namespace server {
13
Mark Slee5ea15f92007-03-05 22:55:59 +000014using namespace std;
15using namespace facebook::thrift;
16using namespace facebook::thrift::protocol;
17using namespace facebook::thrift::transport;
18using 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
41 // Fetch client from server
Mark Slee6e3f6372007-03-01 22:05:46 +000042 while (!stop_) {
Aditya Agarwalfdef47e2007-02-07 03:54:18 +000043 try {
Mark Slee8d7e1f62006-06-07 06:48:56 +000044 client = serverTransport_->accept();
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000045 inputTransport = inputTransportFactory_->getTransport(client);
46 outputTransport = outputTransportFactory_->getTransport(client);
47 inputProtocol = inputProtocolFactory_->getProtocol(inputTransport);
48 outputProtocol = outputProtocolFactory_->getProtocol(outputTransport);
Mark Sleed788b2e2006-09-07 01:26:35 +000049 try {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000050 while (processor_->process(inputProtocol, outputProtocol)) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000051 // Peek ahead, is the remote side closed?
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000052 if (!inputTransport->peek()) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000053 break;
54 }
55 }
Mark Sleed788b2e2006-09-07 01:26:35 +000056 } catch (TTransportException& ttx) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000057 cerr << "TSimpleServer client died: " << ttx.what() << endl;
Mark Slee3860c9a2006-12-06 00:13:42 +000058 } catch (TException& tx) {
59 cerr << "TSimpleServer exception: " << tx.what() << endl;
Mark Sleee8540632006-05-30 09:24:40 +000060 }
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000061 inputTransport->close();
62 outputTransport->close();
Aditya Agarwalfdef47e2007-02-07 03:54:18 +000063 client->close();
64 } catch (TTransportException& ttx) {
Mark Slee3303f362007-03-05 20:09:37 +000065 if (inputTransport != NULL) { inputTransport->close(); }
66 if (outputTransport != NULL) { outputTransport->close(); }
67 if (client != NULL) { client->close(); }
Aditya Agarwalfdef47e2007-02-07 03:54:18 +000068 cerr << "TServerTransport died on accept: " << ttx.what() << endl;
69 continue;
70 } catch (TException& tx) {
Mark Slee3303f362007-03-05 20:09:37 +000071 if (inputTransport != NULL) { inputTransport->close(); }
72 if (outputTransport != NULL) { outputTransport->close(); }
73 if (client != NULL) { client->close(); }
Aditya Agarwalfdef47e2007-02-07 03:54:18 +000074 cerr << "Some kind of accept exception: " << tx.what() << endl;
75 continue;
76 } catch (string s) {
Mark Slee3303f362007-03-05 20:09:37 +000077 if (inputTransport != NULL) { inputTransport->close(); }
78 if (outputTransport != NULL) { outputTransport->close(); }
79 if (client != NULL) { client->close(); }
Aditya Agarwalfdef47e2007-02-07 03:54:18 +000080 cerr << "TThreadPoolServer: Unknown exception: " << s << endl;
81 break;
Mark Sleed788b2e2006-09-07 01:26:35 +000082 }
Mark Sleee8540632006-05-30 09:24:40 +000083 }
84
Mark Slee6e3f6372007-03-01 22:05:46 +000085 if (stop_) {
86 try {
87 serverTransport_->close();
88 } catch (TTransportException &ttx) {
89 cerr << "TServerTransport failed on close: " << ttx.what() << endl;
90 }
91 stop_ = false;
92 }
Mark Sleee8540632006-05-30 09:24:40 +000093}
Marc Slemko6f038a72006-08-03 18:58:09 +000094
95}}} // facebook::thrift::server