blob: 3eb035eed4e8569b0df898eb1cccae14497676a1 [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 Slee4af6ed72006-10-25 19:02:49 +000017 pair<shared_ptr<TTransport>,shared_ptr<TTransport> > iot;
18 pair<shared_ptr<TProtocol>,shared_ptr<TProtocol> > iop;
Mark Sleee8540632006-05-30 09:24:40 +000019
Mark Slee8d7e1f62006-06-07 06:48:56 +000020 try {
21 // Start the server listening
22 serverTransport_->listen();
23 } catch (TTransportException& ttx) {
24 cerr << "TSimpleServer::run() listen(): " << ttx.getMessage() << endl;
Mark Sleee8540632006-05-30 09:24:40 +000025 return;
26 }
27
28 // Fetch client from server
Mark Sleed788b2e2006-09-07 01:26:35 +000029 try {
30 while (true) {
Mark Slee8d7e1f62006-06-07 06:48:56 +000031 client = serverTransport_->accept();
Mark Slee4af6ed72006-10-25 19:02:49 +000032 iot = transportFactory_->getIOTransports(client);
33 iop = protocolFactory_->getIOProtocols(iot.first, iot.second);
Mark Sleed788b2e2006-09-07 01:26:35 +000034 try {
Mark Slee4af6ed72006-10-25 19:02:49 +000035 while (processor_->process(iop.first, iop.second)) {}
Mark Sleed788b2e2006-09-07 01:26:35 +000036 } catch (TTransportException& ttx) {
Mark Slee8d7e1f62006-06-07 06:48:56 +000037 cerr << "TSimpleServer client died: " << ttx.getMessage() << endl;
Mark Sleee8540632006-05-30 09:24:40 +000038 }
Mark Slee4af6ed72006-10-25 19:02:49 +000039 iot.first->close();
40 iot.second->close();
Mark Slee8d7e1f62006-06-07 06:48:56 +000041 client->close();
Mark Sleed788b2e2006-09-07 01:26:35 +000042 }
43 } catch (TTransportException& ttx) {
44 cerr << "TServerTransport died on accept: " << ttx.getMessage() << endl;
Mark Sleee8540632006-05-30 09:24:40 +000045 }
46
Mark Slee8d7e1f62006-06-07 06:48:56 +000047 // TODO(mcslee): Could this be a timeout case? Or always the real thing?
Mark Sleee8540632006-05-30 09:24:40 +000048}
Marc Slemko6f038a72006-08-03 18:58:09 +000049
50}}} // facebook::thrift::server