Thrift TTransportFactory model for servers
Summary: Servers need to create bufferedtransports etc. around the transports they get in a user-definable way. So use a factory pattern to allow the user to supply an object to the server that defines this behavior.
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664792 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/cpp/src/server/TSimpleServer.cc b/lib/cpp/src/server/TSimpleServer.cc
index 2ad5145..041a52f 100644
--- a/lib/cpp/src/server/TSimpleServer.cc
+++ b/lib/cpp/src/server/TSimpleServer.cc
@@ -1,5 +1,4 @@
#include "server/TSimpleServer.h"
-#include "transport/TBufferedTransport.h"
#include "transport/TTransportException.h"
#include <string>
#include <iostream>
@@ -15,6 +14,7 @@
void TSimpleServer::run() {
shared_ptr<TTransport> client;
+ pair<shared_ptr<TTransport>,shared_ptr<TTransport> > io;
try {
// Start the server listening
@@ -25,26 +25,21 @@
}
// Fetch client from server
- while (true) {
- try {
+ try {
+ while (true) {
client = serverTransport_->accept();
- if (client != NULL) {
- // Process for as long as we can keep the processor happy!
- shared_ptr<TBufferedTransport> bufferedClient(new TBufferedTransport(client));
- while (processor_->process(bufferedClient)) {}
- }
- } catch (TTransportException& ttx) {
- if (client != NULL) {
+ io = transportFactory_->getIOTransports(client);
+ try {
+ while (processor_->process(io.first, io.second)) {}
+ } catch (TTransportException& ttx) {
cerr << "TSimpleServer client died: " << ttx.getMessage() << endl;
}
- }
-
- // Clean up the client
- if (client != NULL) {
-
- // Ensure no resource leaks
+ io.first->close();
+ io.second->close();
client->close();
- }
+ }
+ } catch (TTransportException& ttx) {
+ cerr << "TServerTransport died on accept: " << ttx.getMessage() << endl;
}
// TODO(mcslee): Could this be a timeout case? Or always the real thing?