blob: eb936f5e32e956913100bac059ae0722d0f9832b [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 +000011using namespace std;
12
Marc Slemko6f038a72006-08-03 18:58:09 +000013namespace facebook { namespace thrift { namespace server {
14
Mark Slee8d7e1f62006-06-07 06:48:56 +000015/**
16 * A simple single-threaded application server. Perfect for unit tests!
17 *
18 * @author Mark Slee <mcslee@facebook.com>
19 */
Mark Slee794993d2006-09-20 01:56:10 +000020void TSimpleServer::serve() {
Marc Slemko16698852006-08-04 03:16:10 +000021
22 shared_ptr<TTransport> client;
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000023 shared_ptr<TTransport> inputTransport;
24 shared_ptr<TTransport> outputTransport;
25 shared_ptr<TProtocol> inputProtocol;
26 shared_ptr<TProtocol> outputProtocol;
Mark Sleee8540632006-05-30 09:24:40 +000027
Mark Slee8d7e1f62006-06-07 06:48:56 +000028 try {
29 // Start the server listening
30 serverTransport_->listen();
31 } catch (TTransportException& ttx) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000032 cerr << "TSimpleServer::run() listen(): " << ttx.what() << endl;
Mark Sleee8540632006-05-30 09:24:40 +000033 return;
34 }
35
36 // Fetch client from server
Aditya Agarwalfdef47e2007-02-07 03:54:18 +000037 while (true) {
38 try {
Mark Slee8d7e1f62006-06-07 06:48:56 +000039 client = serverTransport_->accept();
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000040 inputTransport = inputTransportFactory_->getTransport(client);
41 outputTransport = outputTransportFactory_->getTransport(client);
42 inputProtocol = inputProtocolFactory_->getProtocol(inputTransport);
43 outputProtocol = outputProtocolFactory_->getProtocol(outputTransport);
Mark Sleed788b2e2006-09-07 01:26:35 +000044 try {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000045 while (processor_->process(inputProtocol, outputProtocol)) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000046 // Peek ahead, is the remote side closed?
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000047 if (!inputTransport->peek()) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000048 break;
49 }
50 }
Mark Sleed788b2e2006-09-07 01:26:35 +000051 } catch (TTransportException& ttx) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000052 cerr << "TSimpleServer client died: " << ttx.what() << endl;
Mark Slee3860c9a2006-12-06 00:13:42 +000053 } catch (TException& tx) {
54 cerr << "TSimpleServer exception: " << tx.what() << endl;
Mark Sleee8540632006-05-30 09:24:40 +000055 }
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000056 inputTransport->close();
57 outputTransport->close();
Aditya Agarwalfdef47e2007-02-07 03:54:18 +000058 client->close();
59 } catch (TTransportException& ttx) {
60 inputTransport->close();
61 outputTransport->close();
Mark Slee8d7e1f62006-06-07 06:48:56 +000062 client->close();
Aditya Agarwalfdef47e2007-02-07 03:54:18 +000063 cerr << "TServerTransport died on accept: " << ttx.what() << endl;
64 continue;
65 } catch (TException& tx) {
66 inputTransport->close();
67 outputTransport->close();
68 client->close();
69 cerr << "Some kind of accept exception: " << tx.what() << endl;
70 continue;
71 } catch (string s) {
72 inputTransport->close();
73 outputTransport->close();
74 client->close();
75 cerr << "TThreadPoolServer: Unknown exception: " << s << endl;
76 break;
Mark Sleed788b2e2006-09-07 01:26:35 +000077 }
Mark Sleee8540632006-05-30 09:24:40 +000078 }
79
Mark Slee8d7e1f62006-06-07 06:48:56 +000080 // TODO(mcslee): Could this be a timeout case? Or always the real thing?
Mark Sleee8540632006-05-30 09:24:40 +000081}
Marc Slemko6f038a72006-08-03 18:58:09 +000082
83}}} // facebook::thrift::server