blob: 2ad5145e4e91d6e18dbc50029f47b984ebc156c8 [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/TBufferedTransport.h"
3#include "transport/TTransportException.h"
Mark Sleee8540632006-05-30 09:24:40 +00004#include <string>
Mark Slee8d7e1f62006-06-07 06:48:56 +00005#include <iostream>
Mark Sleee8540632006-05-30 09:24:40 +00006using namespace std;
7
Marc Slemko6f038a72006-08-03 18:58:09 +00008namespace facebook { namespace thrift { namespace server {
9
Mark Slee8d7e1f62006-06-07 06:48:56 +000010/**
11 * A simple single-threaded application server. Perfect for unit tests!
12 *
13 * @author Mark Slee <mcslee@facebook.com>
14 */
Mark Sleee8540632006-05-30 09:24:40 +000015void TSimpleServer::run() {
Marc Slemko16698852006-08-04 03:16:10 +000016
17 shared_ptr<TTransport> client;
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
28 while (true) {
Mark Slee8d7e1f62006-06-07 06:48:56 +000029 try {
30 client = serverTransport_->accept();
31 if (client != NULL) {
32 // Process for as long as we can keep the processor happy!
Marc Slemko16698852006-08-04 03:16:10 +000033 shared_ptr<TBufferedTransport> bufferedClient(new TBufferedTransport(client));
34 while (processor_->process(bufferedClient)) {}
Mark Sleee8540632006-05-30 09:24:40 +000035 }
Mark Slee8d7e1f62006-06-07 06:48:56 +000036 } catch (TTransportException& ttx) {
37 if (client != NULL) {
38 cerr << "TSimpleServer client died: " << ttx.getMessage() << endl;
Mark Sleee8540632006-05-30 09:24:40 +000039 }
Mark Sleee8540632006-05-30 09:24:40 +000040 }
41
Mark Slee8d7e1f62006-06-07 06:48:56 +000042 // Clean up the client
43 if (client != NULL) {
44
45 // Ensure no resource leaks
46 client->close();
Marc Slemko16698852006-08-04 03:16:10 +000047 }
Mark Sleee8540632006-05-30 09:24:40 +000048 }
49
Mark Slee8d7e1f62006-06-07 06:48:56 +000050 // TODO(mcslee): Could this be a timeout case? Or always the real thing?
Mark Sleee8540632006-05-30 09:24:40 +000051}
Marc Slemko6f038a72006-08-03 18:58:09 +000052
53}}} // facebook::thrift::server