blob: f7e26733d8edb0b9b08f745fff5ef88db9416961 [file] [log] [blame]
Mark Sleee8540632006-05-30 09:24:40 +00001#include "server/TSimpleServer.h"
Mark Slee8d7e1f62006-06-07 06:48:56 +00002#include "transport/TTransportException.h"
Mark Sleee8540632006-05-30 09:24:40 +00003#include <string>
Mark Slee8d7e1f62006-06-07 06:48:56 +00004#include <iostream>
Mark Sleee8540632006-05-30 09:24:40 +00005using namespace std;
6
Marc Slemko6f038a72006-08-03 18:58:09 +00007namespace facebook { namespace thrift { namespace server {
8
Mark Slee8d7e1f62006-06-07 06:48:56 +00009/**
10 * A simple single-threaded application server. Perfect for unit tests!
11 *
12 * @author Mark Slee <mcslee@facebook.com>
13 */
Mark Slee794993d2006-09-20 01:56:10 +000014void TSimpleServer::serve() {
Marc Slemko16698852006-08-04 03:16:10 +000015
16 shared_ptr<TTransport> client;
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000017 shared_ptr<TTransport> inputTransport;
18 shared_ptr<TTransport> outputTransport;
19 shared_ptr<TProtocol> inputProtocol;
20 shared_ptr<TProtocol> outputProtocol;
Mark Sleee8540632006-05-30 09:24:40 +000021
Mark Slee8d7e1f62006-06-07 06:48:56 +000022 try {
23 // Start the server listening
24 serverTransport_->listen();
25 } catch (TTransportException& ttx) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000026 cerr << "TSimpleServer::run() listen(): " << ttx.what() << endl;
Mark Sleee8540632006-05-30 09:24:40 +000027 return;
28 }
29
30 // Fetch client from server
Aditya Agarwalfdef47e2007-02-07 03:54:18 +000031 while (true) {
32 try {
Mark Slee8d7e1f62006-06-07 06:48:56 +000033 client = serverTransport_->accept();
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000034 inputTransport = inputTransportFactory_->getTransport(client);
35 outputTransport = outputTransportFactory_->getTransport(client);
36 inputProtocol = inputProtocolFactory_->getProtocol(inputTransport);
37 outputProtocol = outputProtocolFactory_->getProtocol(outputTransport);
Mark Sleed788b2e2006-09-07 01:26:35 +000038 try {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000039 while (processor_->process(inputProtocol, outputProtocol)) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000040 // Peek ahead, is the remote side closed?
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000041 if (!inputTransport->peek()) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000042 break;
43 }
44 }
Mark Sleed788b2e2006-09-07 01:26:35 +000045 } catch (TTransportException& ttx) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000046 cerr << "TSimpleServer client died: " << ttx.what() << endl;
Mark Slee3860c9a2006-12-06 00:13:42 +000047 } catch (TException& tx) {
48 cerr << "TSimpleServer exception: " << tx.what() << endl;
Mark Sleee8540632006-05-30 09:24:40 +000049 }
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000050 inputTransport->close();
51 outputTransport->close();
Aditya Agarwalfdef47e2007-02-07 03:54:18 +000052 client->close();
53 } catch (TTransportException& ttx) {
54 inputTransport->close();
55 outputTransport->close();
Mark Slee8d7e1f62006-06-07 06:48:56 +000056 client->close();
Aditya Agarwalfdef47e2007-02-07 03:54:18 +000057 cerr << "TServerTransport died on accept: " << ttx.what() << endl;
58 continue;
59 } catch (TException& tx) {
60 inputTransport->close();
61 outputTransport->close();
62 client->close();
63 cerr << "Some kind of accept exception: " << tx.what() << endl;
64 continue;
65 } catch (string s) {
66 inputTransport->close();
67 outputTransport->close();
68 client->close();
69 cerr << "TThreadPoolServer: Unknown exception: " << s << endl;
70 break;
Mark Sleed788b2e2006-09-07 01:26:35 +000071 }
Mark Sleee8540632006-05-30 09:24:40 +000072 }
73
Mark Slee8d7e1f62006-06-07 06:48:56 +000074 // TODO(mcslee): Could this be a timeout case? Or always the real thing?
Mark Sleee8540632006-05-30 09:24:40 +000075}
Marc Slemko6f038a72006-08-03 18:58:09 +000076
77}}} // facebook::thrift::server