blob: 7199ab9c58780d9ffcf8a88d3e6ef4ad934db5d8 [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() {
Mark Slee8d7e1f62006-06-07 06:48:56 +000016 TTransport* client = NULL;
Mark Sleee8540632006-05-30 09:24:40 +000017
Mark Slee8d7e1f62006-06-07 06:48:56 +000018 try {
19 // Start the server listening
20 serverTransport_->listen();
21 } catch (TTransportException& ttx) {
22 cerr << "TSimpleServer::run() listen(): " << ttx.getMessage() << endl;
Mark Sleee8540632006-05-30 09:24:40 +000023 return;
24 }
25
26 // Fetch client from server
27 while (true) {
Mark Slee8d7e1f62006-06-07 06:48:56 +000028 try {
29 client = serverTransport_->accept();
30 if (client != NULL) {
31 // Process for as long as we can keep the processor happy!
32 TBufferedTransport bufferedClient(client);
33 while (processor_->process(&bufferedClient)) {}
Mark Sleee8540632006-05-30 09:24:40 +000034 }
Mark Slee8d7e1f62006-06-07 06:48:56 +000035 } catch (TTransportException& ttx) {
36 if (client != NULL) {
37 cerr << "TSimpleServer client died: " << ttx.getMessage() << endl;
Mark Sleee8540632006-05-30 09:24:40 +000038 }
Mark Sleee8540632006-05-30 09:24:40 +000039 }
40
Mark Slee8d7e1f62006-06-07 06:48:56 +000041 // Clean up the client
42 if (client != NULL) {
43
44 // Ensure no resource leaks
45 client->close();
46
47 // Ensure no memory leaks
48 delete client;
49
50 // Ensure we don't try to double-free on the next pass
51 client = NULL;
52 }
Mark Sleee8540632006-05-30 09:24:40 +000053 }
54
Mark Slee8d7e1f62006-06-07 06:48:56 +000055 // TODO(mcslee): Could this be a timeout case? Or always the real thing?
Mark Sleee8540632006-05-30 09:24:40 +000056}
Marc Slemko6f038a72006-08-03 18:58:09 +000057
58}}} // facebook::thrift::server