From: Bryan Duxbury Date: Thu, 1 Sep 2011 18:31:53 +0000 (+0000) Subject: THRIFT-1316. cpp: update server classes to accept X-Git-Tag: 0.8.0~104 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=7a9fb8179685ed7d82bf2f60ddd39157cdaf1d82;p=common%2Fthrift.git THRIFT-1316. cpp: update server classes to accept TProcessorFactory objects Patch: Adam Simpkins git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1164201 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/lib/cpp/src/Thrift.h b/lib/cpp/src/Thrift.h index c6b31a87..df52dabc 100644 --- a/lib/cpp/src/Thrift.h +++ b/lib/cpp/src/Thrift.h @@ -39,8 +39,33 @@ #include #include +#include +#include + #include "TLogging.h" +/** + * Helper macros to allow function overloading even when using + * boost::shared_ptr. + * + * shared_ptr makes overloading really annoying, since shared_ptr defines + * constructor methods to allow one shared_ptr type to be constructed from any + * other shared_ptr type. (Even if it would be a compile error to actually try + * to instantiate the constructor.) These macros add an extra argument to the + * function to cause it to only be instantiated if a pointer of type T is + * convertible to a pointer of type U. + * + * THRIFT_OVERLOAD_IF should be used in function declarations. + * THRIFT_OVERLOAD_IF_DEFN should be used in the function definition, if it is + * defined separately from where it is declared. + */ +#define THRIFT_OVERLOAD_IF_DEFN(T, Y) \ + typename ::boost::enable_if::type, \ + void*>::type + +#define THRIFT_OVERLOAD_IF(T, Y) \ + THRIFT_OVERLOAD_IF_DEFN(T, Y) = NULL + namespace apache { namespace thrift { class TEnumIterator : public std::iterator > { diff --git a/lib/cpp/src/server/TNonblockingServer.h b/lib/cpp/src/server/TNonblockingServer.h index 5cda2c53..a26fcc5e 100644 --- a/lib/cpp/src/server/TNonblockingServer.h +++ b/lib/cpp/src/server/TNonblockingServer.h @@ -252,18 +252,48 @@ class TNonblockingServer : public TServer { } public: - TNonblockingServer(const boost::shared_ptr& processor, - int port) : + template + TNonblockingServer( + const boost::shared_ptr& processorFactory, + int port, + THRIFT_OVERLOAD_IF(ProcessorFactory, TProcessorFactory)) : + TServer(processorFactory) { + init(port); + } + + template + TNonblockingServer(const boost::shared_ptr& processor, + int port, + THRIFT_OVERLOAD_IF(Processor, TProcessor)) : TServer(processor) { init(port); } + template + TNonblockingServer( + const boost::shared_ptr& processorFactory, + const boost::shared_ptr& protocolFactory, + int port, + const boost::shared_ptr& threadManager = + boost::shared_ptr(), + THRIFT_OVERLOAD_IF(ProcessorFactory, TProcessorFactory)) : + TServer(processorFactory) { + + init(port); + + setInputProtocolFactory(protocolFactory); + setOutputProtocolFactory(protocolFactory); + setThreadManager(threadManager); + } + + template TNonblockingServer( - const boost::shared_ptr& processor, + const boost::shared_ptr& processor, const boost::shared_ptr& protocolFactory, int port, const boost::shared_ptr& threadManager = - boost::shared_ptr()) : + boost::shared_ptr(), + THRIFT_OVERLOAD_IF(Processor, TProcessor)) : TServer(processor) { init(port); @@ -273,15 +303,39 @@ class TNonblockingServer : public TServer { setThreadManager(threadManager); } + template + TNonblockingServer( + const boost::shared_ptr& processorFactory, + const boost::shared_ptr& inputTransportFactory, + const boost::shared_ptr& outputTransportFactory, + const boost::shared_ptr& inputProtocolFactory, + const boost::shared_ptr& outputProtocolFactory, + int port, + const boost::shared_ptr& threadManager = + boost::shared_ptr(), + THRIFT_OVERLOAD_IF(ProcessorFactory, TProcessorFactory)) : + TServer(processorFactory) { + + init(port); + + setInputTransportFactory(inputTransportFactory); + setOutputTransportFactory(outputTransportFactory); + setInputProtocolFactory(inputProtocolFactory); + setOutputProtocolFactory(outputProtocolFactory); + setThreadManager(threadManager); + } + + template TNonblockingServer( - const boost::shared_ptr& processor, + const boost::shared_ptr& processor, const boost::shared_ptr& inputTransportFactory, const boost::shared_ptr& outputTransportFactory, const boost::shared_ptr& inputProtocolFactory, const boost::shared_ptr& outputProtocolFactory, int port, const boost::shared_ptr& threadManager = - boost::shared_ptr()) : + boost::shared_ptr(), + THRIFT_OVERLOAD_IF(Processor, TProcessor)) : TServer(processor) { init(port); diff --git a/lib/cpp/src/server/TServer.h b/lib/cpp/src/server/TServer.h index 6bd1398a..99e2205f 100644 --- a/lib/cpp/src/server/TServer.h +++ b/lib/cpp/src/server/TServer.h @@ -141,7 +141,23 @@ class TServer : public concurrency::Runnable { } protected: - TServer(boost::shared_ptr processor): + template + TServer(const boost::shared_ptr& processorFactory, + THRIFT_OVERLOAD_IF(ProcessorFactory, TProcessorFactory)): + processorFactory_(processorFactory_) { + setInputTransportFactory(boost::shared_ptr( + new TTransportFactory())); + setOutputTransportFactory(boost::shared_ptr( + new TTransportFactory())); + setInputProtocolFactory(boost::shared_ptr( + new TBinaryProtocolFactory())); + setOutputProtocolFactory(boost::shared_ptr( + new TBinaryProtocolFactory())); + } + + template + TServer(const boost::shared_ptr& processor, + THRIFT_OVERLOAD_IF(Processor, TProcessor)): processorFactory_(new TSingletonProcessorFactory(processor)) { setInputTransportFactory(boost::shared_ptr(new TTransportFactory())); setOutputTransportFactory(boost::shared_ptr(new TTransportFactory())); @@ -149,8 +165,26 @@ protected: setOutputProtocolFactory(boost::shared_ptr(new TBinaryProtocolFactory())); } - TServer(boost::shared_ptr processor, - boost::shared_ptr serverTransport): + template + TServer(const boost::shared_ptr& processorFactory, + const boost::shared_ptr& serverTransport, + THRIFT_OVERLOAD_IF(ProcessorFactory, TProcessorFactory)): + processorFactory_(processorFactory), + serverTransport_(serverTransport) { + setInputTransportFactory(boost::shared_ptr( + new TTransportFactory())); + setOutputTransportFactory(boost::shared_ptr( + new TTransportFactory())); + setInputProtocolFactory(boost::shared_ptr( + new TBinaryProtocolFactory())); + setOutputProtocolFactory(boost::shared_ptr( + new TBinaryProtocolFactory())); + } + + template + TServer(const boost::shared_ptr& processor, + const boost::shared_ptr& serverTransport, + THRIFT_OVERLOAD_IF(Processor, TProcessor)): processorFactory_(new TSingletonProcessorFactory(processor)), serverTransport_(serverTransport) { setInputTransportFactory(boost::shared_ptr(new TTransportFactory())); @@ -159,10 +193,25 @@ protected: setOutputProtocolFactory(boost::shared_ptr(new TBinaryProtocolFactory())); } - TServer(boost::shared_ptr processor, - boost::shared_ptr serverTransport, - boost::shared_ptr transportFactory, - boost::shared_ptr protocolFactory): + template + TServer(const boost::shared_ptr& processorFactory, + const boost::shared_ptr& serverTransport, + const boost::shared_ptr& transportFactory, + const boost::shared_ptr& protocolFactory, + THRIFT_OVERLOAD_IF(ProcessorFactory, TProcessorFactory)): + processorFactory_(processorFactory), + serverTransport_(serverTransport), + inputTransportFactory_(transportFactory), + outputTransportFactory_(transportFactory), + inputProtocolFactory_(protocolFactory), + outputProtocolFactory_(protocolFactory) {} + + template + TServer(const boost::shared_ptr& processor, + const boost::shared_ptr& serverTransport, + const boost::shared_ptr& transportFactory, + const boost::shared_ptr& protocolFactory, + THRIFT_OVERLOAD_IF(Processor, TProcessor)): processorFactory_(new TSingletonProcessorFactory(processor)), serverTransport_(serverTransport), inputTransportFactory_(transportFactory), @@ -170,12 +219,29 @@ protected: inputProtocolFactory_(protocolFactory), outputProtocolFactory_(protocolFactory) {} - TServer(boost::shared_ptr processor, - boost::shared_ptr serverTransport, - boost::shared_ptr inputTransportFactory, - boost::shared_ptr outputTransportFactory, - boost::shared_ptr inputProtocolFactory, - boost::shared_ptr outputProtocolFactory): + template + TServer(const boost::shared_ptr& processorFactory, + const boost::shared_ptr& serverTransport, + const boost::shared_ptr& inputTransportFactory, + const boost::shared_ptr& outputTransportFactory, + const boost::shared_ptr& inputProtocolFactory, + const boost::shared_ptr& outputProtocolFactory, + THRIFT_OVERLOAD_IF(ProcessorFactory, TProcessorFactory)): + processorFactory_(processorFactory_), + serverTransport_(serverTransport), + inputTransportFactory_(inputTransportFactory), + outputTransportFactory_(outputTransportFactory), + inputProtocolFactory_(inputProtocolFactory), + outputProtocolFactory_(outputProtocolFactory) {} + + template + TServer(const boost::shared_ptr& processor, + const boost::shared_ptr& serverTransport, + const boost::shared_ptr& inputTransportFactory, + const boost::shared_ptr& outputTransportFactory, + const boost::shared_ptr& inputProtocolFactory, + const boost::shared_ptr& outputProtocolFactory, + THRIFT_OVERLOAD_IF(Processor, TProcessor)): processorFactory_(new TSingletonProcessorFactory(processor)), serverTransport_(serverTransport), inputTransportFactory_(inputTransportFactory), diff --git a/lib/cpp/src/server/TSimpleServer.h b/lib/cpp/src/server/TSimpleServer.h index 29e120e1..ea40ab09 100644 --- a/lib/cpp/src/server/TSimpleServer.h +++ b/lib/cpp/src/server/TSimpleServer.h @@ -34,19 +34,50 @@ namespace apache { namespace thrift { namespace server { */ class TSimpleServer : public TServer { public: - TSimpleServer(boost::shared_ptr processor, - boost::shared_ptr serverTransport, - boost::shared_ptr transportFactory, - boost::shared_ptr protocolFactory) : + template + TSimpleServer( + const boost::shared_ptr& processorFactory, + const boost::shared_ptr& serverTransport, + const boost::shared_ptr& transportFactory, + const boost::shared_ptr& protocolFactory, + THRIFT_OVERLOAD_IF(ProcessorFactory, TProcessorFactory)) : + TServer(processorFactory, serverTransport, transportFactory, + protocolFactory), + stop_(false) {} + + template + TSimpleServer( + const boost::shared_ptr& processor, + const boost::shared_ptr& serverTransport, + const boost::shared_ptr& transportFactory, + const boost::shared_ptr& protocolFactory, + THRIFT_OVERLOAD_IF(Processor, TProcessor)) : TServer(processor, serverTransport, transportFactory, protocolFactory), stop_(false) {} - TSimpleServer(boost::shared_ptr processor, - boost::shared_ptr serverTransport, - boost::shared_ptr inputTransportFactory, - boost::shared_ptr outputTransportFactory, - boost::shared_ptr inputProtocolFactory, - boost::shared_ptr outputProtocolFactory): + template + TSimpleServer( + const boost::shared_ptr& processorFactory, + const boost::shared_ptr& serverTransport, + const boost::shared_ptr& inputTransportFactory, + const boost::shared_ptr& outputTransportFactory, + const boost::shared_ptr& inputProtocolFactory, + const boost::shared_ptr& outputProtocolFactory, + THRIFT_OVERLOAD_IF(ProcessorFactory, TProcessorFactory)) : + TServer(processorFactory, serverTransport, + inputTransportFactory, outputTransportFactory, + inputProtocolFactory, outputProtocolFactory), + stop_(false) {} + + template + TSimpleServer( + const boost::shared_ptr& processor, + const boost::shared_ptr& serverTransport, + const boost::shared_ptr& inputTransportFactory, + const boost::shared_ptr& outputTransportFactory, + const boost::shared_ptr& inputProtocolFactory, + const boost::shared_ptr& outputProtocolFactory, + THRIFT_OVERLOAD_IF(Processor, TProcessor)) : TServer(processor, serverTransport, inputTransportFactory, outputTransportFactory, inputProtocolFactory, outputProtocolFactory), diff --git a/lib/cpp/src/server/TThreadPoolServer.cpp b/lib/cpp/src/server/TThreadPoolServer.cpp index 20183f19..c16e32f6 100644 --- a/lib/cpp/src/server/TThreadPoolServer.cpp +++ b/lib/cpp/src/server/TThreadPoolServer.cpp @@ -108,28 +108,6 @@ public: shared_ptr transport_; }; -TThreadPoolServer::TThreadPoolServer(shared_ptr processor, - shared_ptr serverTransport, - shared_ptr transportFactory, - shared_ptr protocolFactory, - shared_ptr threadManager) : - TServer(processor, serverTransport, transportFactory, protocolFactory), - threadManager_(threadManager), - stop_(false), timeout_(0) {} - -TThreadPoolServer::TThreadPoolServer(shared_ptr processor, - shared_ptr serverTransport, - shared_ptr inputTransportFactory, - shared_ptr outputTransportFactory, - shared_ptr inputProtocolFactory, - shared_ptr outputProtocolFactory, - shared_ptr threadManager) : - TServer(processor, serverTransport, inputTransportFactory, outputTransportFactory, - inputProtocolFactory, outputProtocolFactory), - threadManager_(threadManager), - stop_(false), timeout_(0) {} - - TThreadPoolServer::~TThreadPoolServer() {} void TThreadPoolServer::serve() { diff --git a/lib/cpp/src/server/TThreadPoolServer.h b/lib/cpp/src/server/TThreadPoolServer.h index 7b7e9064..b860ae25 100644 --- a/lib/cpp/src/server/TThreadPoolServer.h +++ b/lib/cpp/src/server/TThreadPoolServer.h @@ -37,19 +37,66 @@ class TThreadPoolServer : public TServer { public: class Task; - TThreadPoolServer(boost::shared_ptr processor, - boost::shared_ptr serverTransport, - boost::shared_ptr transportFactory, - boost::shared_ptr protocolFactory, - boost::shared_ptr threadManager); - - TThreadPoolServer(boost::shared_ptr processor, - boost::shared_ptr serverTransport, - boost::shared_ptr inputTransportFactory, - boost::shared_ptr outputTransportFactory, - boost::shared_ptr inputProtocolFactory, - boost::shared_ptr outputProtocolFactory, - boost::shared_ptr threadManager); + template + TThreadPoolServer( + const boost::shared_ptr& processorFactory, + const boost::shared_ptr& serverTransport, + const boost::shared_ptr& transportFactory, + const boost::shared_ptr& protocolFactory, + const boost::shared_ptr& threadManager, + THRIFT_OVERLOAD_IF(ProcessorFactory, TProtocolFactory)) : + TServer(processorFactory, serverTransport, transportFactory, + protocolFactory), + threadManager_(threadManager), + stop_(false), + timeout_(0) {} + + template + TThreadPoolServer( + const boost::shared_ptr& processor, + const boost::shared_ptr& serverTransport, + const boost::shared_ptr& transportFactory, + const boost::shared_ptr& protocolFactory, + const boost::shared_ptr& threadManager, + THRIFT_OVERLOAD_IF(Processor, TProcessor)) : + TServer(processor, serverTransport, transportFactory, protocolFactory), + threadManager_(threadManager), + stop_(false), + timeout_(0) {} + + template + TThreadPoolServer( + const boost::shared_ptr& processorFactory, + const boost::shared_ptr& serverTransport, + const boost::shared_ptr& inputTransportFactory, + const boost::shared_ptr& outputTransportFactory, + const boost::shared_ptr& inputProtocolFactory, + const boost::shared_ptr& outputProtocolFactory, + const boost::shared_ptr& threadManager, + THRIFT_OVERLOAD_IF(ProcessorFactory, TProcessorFactory)) : + TServer(processorFactory, serverTransport, + inputTransportFactory, outputTransportFactory, + inputProtocolFactory, outputProtocolFactory), + threadManager_(threadManager), + stop_(false), + timeout_(0) {} + + template + TThreadPoolServer( + const boost::shared_ptr& processor, + const boost::shared_ptr& serverTransport, + const boost::shared_ptr& inputTransportFactory, + const boost::shared_ptr& outputTransportFactory, + const boost::shared_ptr& inputProtocolFactory, + const boost::shared_ptr& outputProtocolFactory, + const boost::shared_ptr& threadManager, + THRIFT_OVERLOAD_IF(Processor, TProcessor)) : + TServer(processor, serverTransport, + inputTransportFactory, outputTransportFactory, + inputProtocolFactory, outputProtocolFactory), + threadManager_(threadManager), + stop_(false), + timeout_(0) {} virtual ~TThreadPoolServer(); diff --git a/lib/cpp/src/server/TThreadedServer.cpp b/lib/cpp/src/server/TThreadedServer.cpp index 43d1df21..f40135c5 100644 --- a/lib/cpp/src/server/TThreadedServer.cpp +++ b/lib/cpp/src/server/TThreadedServer.cpp @@ -119,24 +119,12 @@ public: shared_ptr transport_; }; +void TThreadedServer::init() { + stop_ = false; -TThreadedServer::TThreadedServer(shared_ptr processor, - shared_ptr serverTransport, - shared_ptr transportFactory, - shared_ptr protocolFactory): - TServer(processor, serverTransport, transportFactory, protocolFactory), - stop_(false) { - threadFactory_ = shared_ptr(new PosixThreadFactory()); -} - -TThreadedServer::TThreadedServer(boost::shared_ptr processor, - boost::shared_ptr serverTransport, - boost::shared_ptr transportFactory, - boost::shared_ptr protocolFactory, - boost::shared_ptr threadFactory): - TServer(processor, serverTransport, transportFactory, protocolFactory), - threadFactory_(threadFactory), - stop_(false) { + if (!threadFactory_) { + threadFactory_.reset(new PosixThreadFactory); + } } TThreadedServer::~TThreadedServer() {} diff --git a/lib/cpp/src/server/TThreadedServer.h b/lib/cpp/src/server/TThreadedServer.h index 4d0811aa..2db3facf 100644 --- a/lib/cpp/src/server/TThreadedServer.h +++ b/lib/cpp/src/server/TThreadedServer.h @@ -40,16 +40,35 @@ class TThreadedServer : public TServer { public: class Task; - TThreadedServer(boost::shared_ptr processor, - boost::shared_ptr serverTransport, - boost::shared_ptr transportFactory, - boost::shared_ptr protocolFactory); - - TThreadedServer(boost::shared_ptr processor, - boost::shared_ptr serverTransport, - boost::shared_ptr transportFactory, - boost::shared_ptr protocolFactory, - boost::shared_ptr threadFactory); + template + TThreadedServer(const boost::shared_ptr& processorFactory, + const boost::shared_ptr& serverTransport, + const boost::shared_ptr& transportFactory, + const boost::shared_ptr& protocolFactory, + THRIFT_OVERLOAD_IF(ProcessorFactory, TProcessorFactory)); + + template + TThreadedServer(const boost::shared_ptr& processorFactory, + const boost::shared_ptr& serverTransport, + const boost::shared_ptr& transportFactory, + const boost::shared_ptr& protocolFactory, + const boost::shared_ptr& threadFactory, + THRIFT_OVERLOAD_IF(ProcessorFactory, TProtocolFactory)); + + template + TThreadedServer(const boost::shared_ptr& processor, + const boost::shared_ptr& serverTransport, + const boost::shared_ptr& transportFactory, + const boost::shared_ptr& protocolFactory, + THRIFT_OVERLOAD_IF(Processor, TProcessor)); + + template + TThreadedServer(const boost::shared_ptr& processor, + const boost::shared_ptr& serverTransport, + const boost::shared_ptr& transportFactory, + const boost::shared_ptr& protocolFactory, + const boost::shared_ptr& threadFactory, + THRIFT_OVERLOAD_IF(Processor, TProcessor)); virtual ~TThreadedServer(); @@ -61,6 +80,8 @@ class TThreadedServer : public TServer { } protected: + void init(); + boost::shared_ptr threadFactory_; volatile bool stop_; @@ -69,6 +90,56 @@ class TThreadedServer : public TServer { }; +template +TThreadedServer::TThreadedServer( + const boost::shared_ptr& processorFactory, + const boost::shared_ptr& serverTransport, + const boost::shared_ptr& transportFactory, + const boost::shared_ptr& protocolFactory, + THRIFT_OVERLOAD_IF_DEFN(ProcessorFactory, TProcessorFactory)) : + TServer(processorFactory, serverTransport, transportFactory, + protocolFactory) { + init(); +} + +template +TThreadedServer::TThreadedServer( + const boost::shared_ptr& processorFactory, + const boost::shared_ptr& serverTransport, + const boost::shared_ptr& transportFactory, + const boost::shared_ptr& protocolFactory, + const boost::shared_ptr& threadFactory, + THRIFT_OVERLOAD_IF_DEFN(ProcessorFactory, TProtocolFactory)) : + TServer(processorFactory, serverTransport, transportFactory, + protocolFactory), + threadFactory_(threadFactory) { + init(); +} + +template +TThreadedServer::TThreadedServer( + const boost::shared_ptr& processor, + const boost::shared_ptr& serverTransport, + const boost::shared_ptr& transportFactory, + const boost::shared_ptr& protocolFactory, + THRIFT_OVERLOAD_IF_DEFN(Processor, TProcessor)) : + TServer(processor, serverTransport, transportFactory, protocolFactory) { + init(); +} + +template +TThreadedServer::TThreadedServer( + const boost::shared_ptr& processor, + const boost::shared_ptr& serverTransport, + const boost::shared_ptr& transportFactory, + const boost::shared_ptr& protocolFactory, + const boost::shared_ptr& threadFactory, + THRIFT_OVERLOAD_IF_DEFN(Processor, TProcessor)) : + TServer(processor, serverTransport, transportFactory, protocolFactory), + threadFactory_(threadFactory) { + init(); +} + }}} // apache::thrift::server #endif // #ifndef _THRIFT_SERVER_TTHREADEDSERVER_H_