blob: 63b6c6be3c25a6acc679b61a347d5cb93a89f81d [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;
Mark Sleed788b2e2006-09-07 01:26:35 +000017 pair<shared_ptr<TTransport>,shared_ptr<TTransport> > io;
Mark Sleee8540632006-05-30 09:24:40 +000018
Mark Slee8d7e1f62006-06-07 06:48:56 +000019 try {
20 // Start the server listening
21 serverTransport_->listen();
22 } catch (TTransportException& ttx) {
23 cerr << "TSimpleServer::run() listen(): " << ttx.getMessage() << endl;
Mark Sleee8540632006-05-30 09:24:40 +000024 return;
25 }
26
27 // Fetch client from server
Mark Sleed788b2e2006-09-07 01:26:35 +000028 try {
29 while (true) {
Mark Slee8d7e1f62006-06-07 06:48:56 +000030 client = serverTransport_->accept();
Mark Sleed788b2e2006-09-07 01:26:35 +000031 io = transportFactory_->getIOTransports(client);
32 try {
33 while (processor_->process(io.first, io.second)) {}
34 } catch (TTransportException& ttx) {
Mark Slee8d7e1f62006-06-07 06:48:56 +000035 cerr << "TSimpleServer client died: " << ttx.getMessage() << endl;
Mark Sleee8540632006-05-30 09:24:40 +000036 }
Mark Sleed788b2e2006-09-07 01:26:35 +000037 io.first->close();
38 io.second->close();
Mark Slee8d7e1f62006-06-07 06:48:56 +000039 client->close();
Mark Sleed788b2e2006-09-07 01:26:35 +000040 }
41 } catch (TTransportException& ttx) {
42 cerr << "TServerTransport died on accept: " << ttx.getMessage() << endl;
Mark Sleee8540632006-05-30 09:24:40 +000043 }
44
Mark Slee8d7e1f62006-06-07 06:48:56 +000045 // TODO(mcslee): Could this be a timeout case? Or always the real thing?
Mark Sleee8540632006-05-30 09:24:40 +000046}
Marc Slemko6f038a72006-08-03 18:58:09 +000047
48}}} // facebook::thrift::server