TProcessor.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. #ifndef _THRIFT_TPROCESSOR_H_
  20. #define _THRIFT_TPROCESSOR_H_ 1
  21. #include <string>
  22. #include <thrift/protocol/TProtocol.h>
  23. #include <thrift/stdcxx.h>
  24. namespace apache {
  25. namespace thrift {
  26. /**
  27. * Virtual interface class that can handle events from the processor. To
  28. * use this you should subclass it and implement the methods that you care
  29. * about. Your subclass can also store local data that you may care about,
  30. * such as additional "arguments" to these methods (stored in the object
  31. * instance's state).
  32. */
  33. class TProcessorEventHandler {
  34. public:
  35. virtual ~TProcessorEventHandler() {}
  36. /**
  37. * Called before calling other callback methods.
  38. * Expected to return some sort of context object.
  39. * The return value is passed to all other callbacks
  40. * for that function invocation.
  41. */
  42. virtual void* getContext(const char* fn_name, void* serverContext) {
  43. (void)fn_name;
  44. (void)serverContext;
  45. return NULL;
  46. }
  47. /**
  48. * Expected to free resources associated with a context.
  49. */
  50. virtual void freeContext(void* ctx, const char* fn_name) {
  51. (void)ctx;
  52. (void)fn_name;
  53. }
  54. /**
  55. * Called before reading arguments.
  56. */
  57. virtual void preRead(void* ctx, const char* fn_name) {
  58. (void)ctx;
  59. (void)fn_name;
  60. }
  61. /**
  62. * Called between reading arguments and calling the handler.
  63. */
  64. virtual void postRead(void* ctx, const char* fn_name, uint32_t bytes) {
  65. (void)ctx;
  66. (void)fn_name;
  67. (void)bytes;
  68. }
  69. /**
  70. * Called between calling the handler and writing the response.
  71. */
  72. virtual void preWrite(void* ctx, const char* fn_name) {
  73. (void)ctx;
  74. (void)fn_name;
  75. }
  76. /**
  77. * Called after writing the response.
  78. */
  79. virtual void postWrite(void* ctx, const char* fn_name, uint32_t bytes) {
  80. (void)ctx;
  81. (void)fn_name;
  82. (void)bytes;
  83. }
  84. /**
  85. * Called when an async function call completes successfully.
  86. */
  87. virtual void asyncComplete(void* ctx, const char* fn_name) {
  88. (void)ctx;
  89. (void)fn_name;
  90. }
  91. /**
  92. * Called if the handler throws an undeclared exception.
  93. */
  94. virtual void handlerError(void* ctx, const char* fn_name) {
  95. (void)ctx;
  96. (void)fn_name;
  97. }
  98. protected:
  99. TProcessorEventHandler() {}
  100. };
  101. /**
  102. * A helper class used by the generated code to free each context.
  103. */
  104. class TProcessorContextFreer {
  105. public:
  106. TProcessorContextFreer(TProcessorEventHandler* handler, void* context, const char* method)
  107. : handler_(handler), context_(context), method_(method) {}
  108. ~TProcessorContextFreer() {
  109. if (handler_ != NULL)
  110. handler_->freeContext(context_, method_);
  111. }
  112. void unregister() { handler_ = NULL; }
  113. private:
  114. apache::thrift::TProcessorEventHandler* handler_;
  115. void* context_;
  116. const char* method_;
  117. };
  118. /**
  119. * A processor is a generic object that acts upon two streams of data, one
  120. * an input and the other an output. The definition of this object is loose,
  121. * though the typical case is for some sort of server that either generates
  122. * responses to an input stream or forwards data from one pipe onto another.
  123. *
  124. */
  125. class TProcessor {
  126. public:
  127. virtual ~TProcessor() {}
  128. virtual bool process(stdcxx::shared_ptr<protocol::TProtocol> in,
  129. stdcxx::shared_ptr<protocol::TProtocol> out,
  130. void* connectionContext) = 0;
  131. bool process(stdcxx::shared_ptr<apache::thrift::protocol::TProtocol> io, void* connectionContext) {
  132. return process(io, io, connectionContext);
  133. }
  134. stdcxx::shared_ptr<TProcessorEventHandler> getEventHandler() const { return eventHandler_; }
  135. void setEventHandler(stdcxx::shared_ptr<TProcessorEventHandler> eventHandler) {
  136. eventHandler_ = eventHandler;
  137. }
  138. protected:
  139. TProcessor() {}
  140. stdcxx::shared_ptr<TProcessorEventHandler> eventHandler_;
  141. };
  142. /**
  143. * This is a helper class to allow stdcxx::shared_ptr to be used with handler
  144. * pointers returned by the generated handler factories.
  145. *
  146. * The handler factory classes generated by the thrift compiler return raw
  147. * pointers, and factory->releaseHandler() must be called when the handler is
  148. * no longer needed.
  149. *
  150. * A ReleaseHandler object can be instantiated and passed as the second
  151. * parameter to a shared_ptr, so that factory->releaseHandler() will be called
  152. * when the object is no longer needed, instead of deleting the pointer.
  153. */
  154. template <typename HandlerFactory_>
  155. class ReleaseHandler {
  156. public:
  157. ReleaseHandler(const stdcxx::shared_ptr<HandlerFactory_>& handlerFactory)
  158. : handlerFactory_(handlerFactory) {}
  159. void operator()(typename HandlerFactory_::Handler* handler) {
  160. if (handler) {
  161. handlerFactory_->releaseHandler(handler);
  162. }
  163. }
  164. private:
  165. stdcxx::shared_ptr<HandlerFactory_> handlerFactory_;
  166. };
  167. struct TConnectionInfo {
  168. // The input and output protocols
  169. stdcxx::shared_ptr<protocol::TProtocol> input;
  170. stdcxx::shared_ptr<protocol::TProtocol> output;
  171. // The underlying transport used for the connection
  172. // This is the transport that was returned by TServerTransport::accept(),
  173. // and it may be different than the transport pointed to by the input and
  174. // output protocols.
  175. stdcxx::shared_ptr<transport::TTransport> transport;
  176. };
  177. class TProcessorFactory {
  178. public:
  179. virtual ~TProcessorFactory() {}
  180. /**
  181. * Get the TProcessor to use for a particular connection.
  182. *
  183. * This method is always invoked in the same thread that the connection was
  184. * accepted on. This generally means that this call does not need to be
  185. * thread safe, as it will always be invoked from a single thread.
  186. */
  187. virtual stdcxx::shared_ptr<TProcessor> getProcessor(const TConnectionInfo& connInfo) = 0;
  188. };
  189. class TSingletonProcessorFactory : public TProcessorFactory {
  190. public:
  191. TSingletonProcessorFactory(stdcxx::shared_ptr<TProcessor> processor) : processor_(processor) {}
  192. stdcxx::shared_ptr<TProcessor> getProcessor(const TConnectionInfo&) { return processor_; }
  193. private:
  194. stdcxx::shared_ptr<TProcessor> processor_;
  195. };
  196. }
  197. } // apache::thrift
  198. #endif // #ifndef _THRIFT_TPROCESSOR_H_