| David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 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 | */ | 
| Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 19 |  | 
| Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 20 | #ifndef _THRIFT_TPROCESSOR_H_ | 
|  | 21 | #define _THRIFT_TPROCESSOR_H_ 1 | 
| Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 22 |  | 
|  | 23 | #include <string> | 
| Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 24 | #include <thrift/protocol/TProtocol.h> | 
| Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 25 |  | 
| Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 26 | namespace apache { | 
|  | 27 | namespace thrift { | 
| Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 28 |  | 
| Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 29 | /** | 
| David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 30 | * Virtual interface class that can handle events from the processor. To | 
|  | 31 | * use this you should subclass it and implement the methods that you care | 
|  | 32 | * about. Your subclass can also store local data that you may care about, | 
|  | 33 | * such as additional "arguments" to these methods (stored in the object | 
|  | 34 | * instance's state). | 
|  | 35 | */ | 
|  | 36 | class TProcessorEventHandler { | 
| Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 37 | public: | 
| Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 38 | virtual ~TProcessorEventHandler() = default; | 
| David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 39 |  | 
|  | 40 | /** | 
|  | 41 | * Called before calling other callback methods. | 
|  | 42 | * Expected to return some sort of context object. | 
|  | 43 | * The return value is passed to all other callbacks | 
|  | 44 | * for that function invocation. | 
|  | 45 | */ | 
| Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 46 | virtual void* getContext(const char* fn_name, void* serverContext) { | 
| Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 47 | (void)fn_name; | 
|  | 48 | (void)serverContext; | 
| Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 49 | return nullptr; | 
| Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 50 | } | 
| David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 51 |  | 
|  | 52 | /** | 
|  | 53 | * Expected to free resources associated with a context. | 
|  | 54 | */ | 
| Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 55 | virtual void freeContext(void* ctx, const char* fn_name) { | 
| Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 56 | (void)ctx; | 
|  | 57 | (void)fn_name; | 
| Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 58 | } | 
| David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 59 |  | 
|  | 60 | /** | 
|  | 61 | * Called before reading arguments. | 
|  | 62 | */ | 
| Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 63 | virtual void preRead(void* ctx, const char* fn_name) { | 
| Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 64 | (void)ctx; | 
|  | 65 | (void)fn_name; | 
| Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 66 | } | 
| David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 67 |  | 
|  | 68 | /** | 
|  | 69 | * Called between reading arguments and calling the handler. | 
|  | 70 | */ | 
| Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 71 | virtual void postRead(void* ctx, const char* fn_name, uint32_t bytes) { | 
| Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 72 | (void)ctx; | 
|  | 73 | (void)fn_name; | 
|  | 74 | (void)bytes; | 
| Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 75 | } | 
| David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 76 |  | 
|  | 77 | /** | 
|  | 78 | * Called between calling the handler and writing the response. | 
|  | 79 | */ | 
| Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 80 | virtual void preWrite(void* ctx, const char* fn_name) { | 
| Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 81 | (void)ctx; | 
|  | 82 | (void)fn_name; | 
| Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 83 | } | 
| David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 84 |  | 
|  | 85 | /** | 
|  | 86 | * Called after writing the response. | 
|  | 87 | */ | 
| Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 88 | virtual void postWrite(void* ctx, const char* fn_name, uint32_t bytes) { | 
| Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 89 | (void)ctx; | 
|  | 90 | (void)fn_name; | 
|  | 91 | (void)bytes; | 
| Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 92 | } | 
| David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 93 |  | 
|  | 94 | /** | 
|  | 95 | * Called when an async function call completes successfully. | 
|  | 96 | */ | 
| Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 97 | virtual void asyncComplete(void* ctx, const char* fn_name) { | 
| Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 98 | (void)ctx; | 
|  | 99 | (void)fn_name; | 
| Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 100 | } | 
| David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 101 |  | 
|  | 102 | /** | 
|  | 103 | * Called if the handler throws an undeclared exception. | 
|  | 104 | */ | 
| Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 105 | virtual void handlerError(void* ctx, const char* fn_name) { | 
| Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 106 | (void)ctx; | 
|  | 107 | (void)fn_name; | 
| Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 108 | } | 
| David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 109 |  | 
| Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 110 | protected: | 
| Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 111 | TProcessorEventHandler() = default; | 
| David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 112 | }; | 
|  | 113 |  | 
|  | 114 | /** | 
| David Reiss | 18cd0f0 | 2010-10-06 17:09:39 +0000 | [diff] [blame] | 115 | * A helper class used by the generated code to free each context. | 
|  | 116 | */ | 
|  | 117 | class TProcessorContextFreer { | 
| Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 118 | public: | 
|  | 119 | TProcessorContextFreer(TProcessorEventHandler* handler, void* context, const char* method) | 
|  | 120 | : handler_(handler), context_(context), method_(method) {} | 
|  | 121 | ~TProcessorContextFreer() { | 
| Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 122 | if (handler_ != nullptr) | 
| Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 123 | handler_->freeContext(context_, method_); | 
|  | 124 | } | 
| Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 125 | void unregister() { handler_ = nullptr; } | 
| Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 126 |  | 
|  | 127 | private: | 
| David Reiss | 18cd0f0 | 2010-10-06 17:09:39 +0000 | [diff] [blame] | 128 | apache::thrift::TProcessorEventHandler* handler_; | 
|  | 129 | void* context_; | 
|  | 130 | const char* method_; | 
|  | 131 | }; | 
|  | 132 |  | 
|  | 133 | /** | 
| Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 134 | * A processor is a generic object that acts upon two streams of data, one | 
|  | 135 | * an input and the other an output. The definition of this object is loose, | 
|  | 136 | * though the typical case is for some sort of server that either generates | 
|  | 137 | * responses to an input stream or forwards data from one pipe onto another. | 
|  | 138 | * | 
| Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 139 | */ | 
|  | 140 | class TProcessor { | 
| Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 141 | public: | 
| Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 142 | virtual ~TProcessor() = default; | 
| Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 143 |  | 
| cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 144 | virtual bool process(std::shared_ptr<protocol::TProtocol> in, | 
|  | 145 | std::shared_ptr<protocol::TProtocol> out, | 
| David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 146 | void* connectionContext) = 0; | 
| Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 147 |  | 
| cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 148 | bool process(std::shared_ptr<apache::thrift::protocol::TProtocol> io, void* connectionContext) { | 
| David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 149 | return process(io, io, connectionContext); | 
| Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 150 | } | 
| Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 151 |  | 
| cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 152 | std::shared_ptr<TProcessorEventHandler> getEventHandler() const { return eventHandler_; } | 
| David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 153 |  | 
| cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 154 | void setEventHandler(std::shared_ptr<TProcessorEventHandler> eventHandler) { | 
| David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 155 | eventHandler_ = eventHandler; | 
|  | 156 | } | 
|  | 157 |  | 
| Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 158 | protected: | 
| Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 159 | TProcessor() = default; | 
| David Reiss | d719206 | 2010-10-06 17:09:33 +0000 | [diff] [blame] | 160 |  | 
| cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 161 | std::shared_ptr<TProcessorEventHandler> eventHandler_; | 
| Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 162 | }; | 
|  | 163 |  | 
| Bryan Duxbury | 2173ce0 | 2011-09-01 18:00:37 +0000 | [diff] [blame] | 164 | /** | 
| cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 165 | * This is a helper class to allow std::shared_ptr to be used with handler | 
| Bryan Duxbury | 2173ce0 | 2011-09-01 18:00:37 +0000 | [diff] [blame] | 166 | * pointers returned by the generated handler factories. | 
|  | 167 | * | 
|  | 168 | * The handler factory classes generated by the thrift compiler return raw | 
|  | 169 | * pointers, and factory->releaseHandler() must be called when the handler is | 
|  | 170 | * no longer needed. | 
|  | 171 | * | 
|  | 172 | * A ReleaseHandler object can be instantiated and passed as the second | 
|  | 173 | * parameter to a shared_ptr, so that factory->releaseHandler() will be called | 
|  | 174 | * when the object is no longer needed, instead of deleting the pointer. | 
|  | 175 | */ | 
| Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 176 | template <typename HandlerFactory_> | 
| Bryan Duxbury | 2173ce0 | 2011-09-01 18:00:37 +0000 | [diff] [blame] | 177 | class ReleaseHandler { | 
| Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 178 | public: | 
| cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 179 | ReleaseHandler(const std::shared_ptr<HandlerFactory_>& handlerFactory) | 
| Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 180 | : handlerFactory_(handlerFactory) {} | 
| Bryan Duxbury | 2173ce0 | 2011-09-01 18:00:37 +0000 | [diff] [blame] | 181 |  | 
| Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 182 | void operator()(typename HandlerFactory_::Handler* handler) { | 
|  | 183 | if (handler) { | 
|  | 184 | handlerFactory_->releaseHandler(handler); | 
|  | 185 | } | 
|  | 186 | } | 
| Bryan Duxbury | 2173ce0 | 2011-09-01 18:00:37 +0000 | [diff] [blame] | 187 |  | 
| Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 188 | private: | 
| cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 189 | std::shared_ptr<HandlerFactory_> handlerFactory_; | 
| Bryan Duxbury | 2173ce0 | 2011-09-01 18:00:37 +0000 | [diff] [blame] | 190 | }; | 
|  | 191 |  | 
| Bryan Duxbury | 6dd9cd0 | 2011-09-01 18:06:20 +0000 | [diff] [blame] | 192 | struct TConnectionInfo { | 
|  | 193 | // The input and output protocols | 
| cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 194 | std::shared_ptr<protocol::TProtocol> input; | 
|  | 195 | std::shared_ptr<protocol::TProtocol> output; | 
| Bryan Duxbury | 6dd9cd0 | 2011-09-01 18:06:20 +0000 | [diff] [blame] | 196 | // The underlying transport used for the connection | 
|  | 197 | // This is the transport that was returned by TServerTransport::accept(), | 
|  | 198 | // and it may be different than the transport pointed to by the input and | 
|  | 199 | // output protocols. | 
| cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 200 | std::shared_ptr<transport::TTransport> transport; | 
| Bryan Duxbury | 6dd9cd0 | 2011-09-01 18:06:20 +0000 | [diff] [blame] | 201 | }; | 
|  | 202 |  | 
|  | 203 | class TProcessorFactory { | 
| Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 204 | public: | 
| Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 205 | virtual ~TProcessorFactory() = default; | 
| Bryan Duxbury | 6dd9cd0 | 2011-09-01 18:06:20 +0000 | [diff] [blame] | 206 |  | 
|  | 207 | /** | 
|  | 208 | * Get the TProcessor to use for a particular connection. | 
|  | 209 | * | 
|  | 210 | * This method is always invoked in the same thread that the connection was | 
|  | 211 | * accepted on.  This generally means that this call does not need to be | 
|  | 212 | * thread safe, as it will always be invoked from a single thread. | 
|  | 213 | */ | 
| cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 214 | virtual std::shared_ptr<TProcessor> getProcessor(const TConnectionInfo& connInfo) = 0; | 
| Bryan Duxbury | 6dd9cd0 | 2011-09-01 18:06:20 +0000 | [diff] [blame] | 215 | }; | 
|  | 216 |  | 
|  | 217 | class TSingletonProcessorFactory : public TProcessorFactory { | 
| Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 218 | public: | 
| cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 219 | TSingletonProcessorFactory(std::shared_ptr<TProcessor> processor) : processor_(processor) {} | 
| Bryan Duxbury | 6dd9cd0 | 2011-09-01 18:06:20 +0000 | [diff] [blame] | 220 |  | 
| Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 221 | std::shared_ptr<TProcessor> getProcessor(const TConnectionInfo&) override { return processor_; } | 
| Bryan Duxbury | 6dd9cd0 | 2011-09-01 18:06:20 +0000 | [diff] [blame] | 222 |  | 
| Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 223 | private: | 
| cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 224 | std::shared_ptr<TProcessor> processor_; | 
| Bryan Duxbury | 6dd9cd0 | 2011-09-01 18:06:20 +0000 | [diff] [blame] | 225 | }; | 
| Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 226 | } | 
|  | 227 | } // apache::thrift | 
| Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 228 |  | 
| David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 229 | #endif // #ifndef _THRIFT_TPROCESSOR_H_ |