More boosification of thrift driver, server, transport and protocol code
Modified TestServer to use thread-pool manager
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664737 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/cpp/src/server/TServer.h b/lib/cpp/src/server/TServer.h
index f34944b..68728f2 100644
--- a/lib/cpp/src/server/TServer.h
+++ b/lib/cpp/src/server/TServer.h
@@ -1,11 +1,14 @@
#ifndef T_SERVER_H
#define T_SERVER_H
-#include "TProcessor.h"
+#include <TProcessor.h>
+
+#include <boost/shared_ptr.hpp>
namespace facebook { namespace thrift { namespace server {
using namespace facebook::thrift;
+using namespace boost;
class TServerOptions;
@@ -15,25 +18,25 @@
* @author Mark Slee <mcslee@facebook.com>
*/
class TServer {
- public:
+public:
virtual ~TServer() {}
virtual void run() = 0;
-
- protected:
- TServer(TProcessor* processor, TServerOptions* options) :
+
+protected:
+ TServer(shared_ptr<TProcessor> processor, shared_ptr<TServerOptions> options) :
processor_(processor), options_(options) {}
-
- TProcessor* processor_;
- TServerOptions* options_;
+
+ shared_ptr<TProcessor> processor_;
+ shared_ptr<TServerOptions> options_;
};
-
+
/**
* Class to encapsulate all generic server options.
*/
class TServerOptions {
- public:
+public:
// TODO(mcslee): Fill in getters/setters here
- protected:
+protected:
// TODO(mcslee): Fill data members in here
};
diff --git a/lib/cpp/src/server/TSimpleServer.cc b/lib/cpp/src/server/TSimpleServer.cc
index 7199ab9..2ad5145 100644
--- a/lib/cpp/src/server/TSimpleServer.cc
+++ b/lib/cpp/src/server/TSimpleServer.cc
@@ -13,7 +13,8 @@
* @author Mark Slee <mcslee@facebook.com>
*/
void TSimpleServer::run() {
- TTransport* client = NULL;
+
+ shared_ptr<TTransport> client;
try {
// Start the server listening
@@ -29,8 +30,8 @@
client = serverTransport_->accept();
if (client != NULL) {
// Process for as long as we can keep the processor happy!
- TBufferedTransport bufferedClient(client);
- while (processor_->process(&bufferedClient)) {}
+ shared_ptr<TBufferedTransport> bufferedClient(new TBufferedTransport(client));
+ while (processor_->process(bufferedClient)) {}
}
} catch (TTransportException& ttx) {
if (client != NULL) {
@@ -43,13 +44,7 @@
// Ensure no resource leaks
client->close();
-
- // Ensure no memory leaks
- delete client;
-
- // Ensure we don't try to double-free on the next pass
- client = NULL;
- }
+ }
}
// TODO(mcslee): Could this be a timeout case? Or always the real thing?
diff --git a/lib/cpp/src/server/TSimpleServer.h b/lib/cpp/src/server/TSimpleServer.h
index a4a2d98..a8242d4 100644
--- a/lib/cpp/src/server/TSimpleServer.h
+++ b/lib/cpp/src/server/TSimpleServer.h
@@ -16,9 +16,9 @@
*/
class TSimpleServer : public TServer {
public:
- TSimpleServer(TProcessor* processor,
- TServerOptions* options,
- TServerTransport* serverTransport) :
+ TSimpleServer(shared_ptr<TProcessor> processor,
+ shared_ptr<TServerOptions> options,
+ shared_ptr<TServerTransport> serverTransport) :
TServer(processor, options), serverTransport_(serverTransport) {}
~TSimpleServer() {}
@@ -26,7 +26,7 @@
void run();
protected:
- TServerTransport* serverTransport_;
+ shared_ptr<TServerTransport> serverTransport_;
};
}}} // facebook::thrift::server
diff --git a/lib/cpp/src/server/TThreadPoolServer.cc b/lib/cpp/src/server/TThreadPoolServer.cc
index 2d6290c..d53d174 100644
--- a/lib/cpp/src/server/TThreadPoolServer.cc
+++ b/lib/cpp/src/server/TThreadPoolServer.cc
@@ -12,87 +12,78 @@
using namespace facebook::thrift::concurrency;
using namespace facebook::thrift::transport;
-class TThreadPoolServer : public TServer {
-
- class Task: public Runnable {
+class TThreadPoolServer::Task: public Runnable {
- TProcessor* _processor;
- TTransport* _transport;
- TBufferedTransport _bufferedTransport;
+ shared_ptr<TProcessor> _processor;
+ shared_ptr<TTransport> _transport;
+ shared_ptr<TBufferedTransport> _bufferedTransport;
- public:
+public:
- Task(TProcessor* processor,
- TTransport* transport) :
- _processor(processor),
- _transport(transport),
- _bufferedTransport(_transport) {
- }
-
- ~Task() {
- delete _transport;
- }
-
- void run() {
-
- while(true) {
-
- try {
- _processor->process(&_bufferedTransport);
-
- } catch (TTransportException& ttx) {
-
- break;
-
- } catch(...) {
-
- break;
- }
- }
-
- _bufferedTransport.close();
- }
- };
-
- TThreadPoolServer(TProcessor* processor,
- TServerOptions* options,
- TServerTransport* serverTransport,
- ThreadManager* threadManager) :
- TServer(processor, options),
- serverTransport_(serverTransport),
- threadManager_(threadManager) {
+ Task(shared_ptr<TProcessor> processor,
+ shared_ptr<TTransport> transport) :
+ _processor(processor),
+ _transport(transport),
+ _bufferedTransport(new TBufferedTransport(transport)) {
}
+
+ ~Task() {}
- ~TThreadPoolServer() {}
-
- protected:
-
- TServerTransport* serverTransport_;
- ThreadManager* threadManager_;
-
void run() {
-
- try {
- // Start the server listening
- serverTransport_->listen();
- } catch (TTransportException& ttx) {
- cerr << "TThreadPoolServer::run() listen(): " << ttx.getMessage() << endl;
- return;
- }
-
- // Fetch client from server
-
- while (true) {
-
+
+ while(true) {
+
try {
-
- threadManager_->add(shared_ptr<Task>(new TThreadPoolServer::Task(processor_, serverTransport_->accept())));;
-
+ _processor->process(_bufferedTransport);
+
} catch (TTransportException& ttx) {
+
+ break;
+
+ } catch(...) {
+
break;
}
}
+
+ _bufferedTransport->close();
}
};
+
+TThreadPoolServer::TThreadPoolServer(shared_ptr<TProcessor> processor,
+ shared_ptr<TServerOptions> options,
+ shared_ptr<TServerTransport> serverTransport,
+ shared_ptr<ThreadManager> threadManager) :
+ TServer(processor, options),
+ serverTransport_(serverTransport),
+ threadManager_(threadManager) {
+}
+
+TThreadPoolServer::~TThreadPoolServer() {}
+
+void TThreadPoolServer::run() {
+
+ try {
+ // Start the server listening
+ serverTransport_->listen();
+ } catch (TTransportException& ttx) {
+ cerr << "TThreadPoolServer::run() listen(): " << ttx.getMessage() << endl;
+ return;
+ }
+
+ // Fetch client from server
+
+ while (true) {
+
+ try {
+
+ threadManager_->add(shared_ptr<TThreadPoolServer::Task>(new TThreadPoolServer::Task(processor_,
+ shared_ptr<TTransport>(serverTransport_->accept()))));
+
+ } catch (TTransportException& ttx) {
+ break;
+ }
+ }
+}
}}} // facebook::thrift::server
diff --git a/lib/cpp/src/server/TThreadPoolServer.h b/lib/cpp/src/server/TThreadPoolServer.h
index ef63a37..827491d 100644
--- a/lib/cpp/src/server/TThreadPoolServer.h
+++ b/lib/cpp/src/server/TThreadPoolServer.h
@@ -1,10 +1,39 @@
#ifndef T_THREADPOOL_SERVER_H
#define T_THREADPOOL_SERVER_H
-#include "server/TServer.h"
-#include "transport/TServerTransport.h"
+#include <concurrency/ThreadManager.h>
+#include <server/TServer.h>
+#include <transport/TServerTransport.h>
+
+#include <boost/shared_ptr.hpp>
namespace facebook { namespace thrift { namespace server {
+
+using namespace facebook::thrift::concurrency;
+using namespace facebook::thrift::transport;
+using namespace boost;
+
+class TThreadPoolServer : public TServer {
+public:
+
+ class Task;
+
+ TThreadPoolServer(shared_ptr<TProcessor> processor,
+ shared_ptr<TServerOptions> options,
+ shared_ptr<TServerTransport> serverTransport,
+ shared_ptr<ThreadManager> threadManager);
+
+ virtual ~TThreadPoolServer();
+
+ virtual void run();
+
+protected:
+
+ shared_ptr<TServerTransport> serverTransport_;
+ shared_ptr<ThreadManager> threadManager_;
+
+};
+
}}} // facebook::thrift::server
#endif